Skip to content

Commit

Permalink
fix(react): handles an prop as subject name
Browse files Browse the repository at this point in the history
Fixes #169
  • Loading branch information
stalniy committed Mar 25, 2019
1 parent 5310471 commit 608c99b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/casl-react/spec/Can.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ describe('`Can` component', () => {
expect(children).to.have.been.called.with.exactly(ability.can('read', 'Post'), ability)
})

it('requires to pass "a" or "this" or "of" as string or object', () => {
it('requires to pass "a", "an", "this" or "of" as string or object', () => {
const props = { ability, children, I: 'test' }

expect(() => validateProps(Can, props)).to.throw(/`a` is marked as required/)
expect(() => validateProps(Can, { a: 123, ...props })).to.throw(/Invalid prop `a`/)
expect(() => validateProps(Can, { a: {}, ...props })).not.to.throw(Error)
expect(() => validateProps(Can, { a: 'subject', ...props })).not.to.throw(Error)
expect(() => validateProps(Can, { an: 'subject', ...props })).not.to.throw(Error)
expect(() => validateProps(Can, { an: 123, ...props })).to.throw(/Invalid prop `an`/)
})

it('requires to pass "I" as string', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/casl-react/src/Can.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default class Can extends PureComponent {
isAllowed() {
const params = this.props;
const [action, field] = (params.I || params.do).split(/\s+/);
const subject = params.of || params.a || params.this || params.on;
const subject = params.of || params.a || params.an || params.this || params.on;
const can = params.not ? 'cannot' : 'can';

return params.ability[can](action, subject, field);
Expand Down

0 comments on commit 608c99b

Please sign in to comment.