Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow arrow functions for ability factory arguments to AbilityBuilder #982

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading