Skip to content

Commit

Permalink
Allow arrow functions for ability factory arguments to AbilityBuilder (
Browse files Browse the repository at this point in the history
  • Loading branch information
forivall authored Oct 18, 2024
1 parent f8e594a commit ee01fce
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/casl-ability/spec/builder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ describe('AbilityBuilder', () => {
expect(factory).to.have.been.called.with(rules, options)
})

it('can create Ability instance from a factory arrow function', () => {
const factorySpy = spy()
const factory = (...args) => factorySpy(...args)
const { can, build, rules } = new AbilityBuilder(factory)
const options = {}
can('read', 'Post')
build(options)

expect(factorySpy).to.have.been.called.with(rules, options)
})

describe('defineAbility', () => {
it('defines `Ability` instance using DSL', () => {
const ability = defineAbility((can, cannot) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/casl-ability/src/AbilityBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from './types';

function isAbilityClass(factory: AbilityFactory<any>): factory is AnyClass {
return typeof factory.prototype.possibleRulesFor === 'function';
return factory.prototype !== undefined && typeof factory.prototype.possibleRulesFor === 'function';
}

class RuleBuilder<T extends AnyAbility> {
Expand Down

0 comments on commit ee01fce

Please sign in to comment.