-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ability): adds pack/unpack rules methods
Relates to #44
- Loading branch information
Showing
6 changed files
with
203 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { packRules, unpackRules } from '../src/extra' | ||
|
||
fdescribe('Ability rules packing', () => { | ||
describe('`packRules` function', () => { | ||
it('converts array of rule objects to array of rule arrays', () => { | ||
const rules = packRules([ | ||
{ actions: 'read', subject: 'Post' }, | ||
{ actions: 'delete', subject: 'Post' } | ||
]) | ||
|
||
expect(rules.every(Array.isArray)).to.be.true | ||
}) | ||
|
||
it('puts `actions` as 1st element of rule array', () => { | ||
const rules = packRules([{ actions: 'read', subject: 'Post' }]) | ||
|
||
expect(rules[0][0]).to.equal('read') | ||
expect(rules[0]).to.have.length(2) | ||
}) | ||
|
||
it('joins `actions` with comma if its value is an array', () => { | ||
const rules = packRules([{ actions: ['read', 'update'], subject: 'Post' }]) | ||
|
||
expect(rules[0][0]).to.equal('read,update') | ||
expect(rules[0]).to.have.length(2) | ||
}) | ||
|
||
it('puts `subject` as 2nd element of rule array', () => { | ||
const rules = packRules([{ actions: 'read', subject: 'Post' }]) | ||
|
||
expect(rules[0][1]).to.equal('Post') | ||
expect(rules[0]).to.have.length(2) | ||
}) | ||
|
||
it('puts `subject` with comma if its value is an array', () => { | ||
const rules = packRules([{ actions: 'read', subject: ['Post', 'Comment'] }]) | ||
|
||
expect(rules[0][1]).to.equal('Post,Comment') | ||
expect(rules[0]).to.have.length(2) | ||
}) | ||
|
||
it('converts `inverted` to number and puts it as 3rd element of rule array', () => { | ||
const rules = packRules([{ actions: 'read', subject: 'Post', inverted: true }]) | ||
|
||
expect(rules[0][2]).to.equal(1) | ||
expect(rules[0]).to.have.length(3) | ||
}) | ||
|
||
it('puts `conditions` as 4th element of rule array', () => { | ||
const conditions = { private: true } | ||
const rules = packRules([{ actions: 'read', subject: 'Post', conditions }]) | ||
|
||
expect(rules[0][3]).to.equal(conditions) | ||
expect(rules[0]).to.have.length(4) | ||
}) | ||
|
||
it('puts `0` in place of `conditions` if they are not defined but `fields` are defined', () => { | ||
const rules = packRules([{ actions: 'read', subject: 'Post', fields: ['title'] }]) | ||
|
||
expect(rules[0][3]).to.equal(0) | ||
expect(rules[0]).to.have.length(5) | ||
}) | ||
|
||
it('joins `fields` and puts it as 5th element of rule array', () => { | ||
const fields = ['title', 'description'] | ||
const rules = packRules([{ actions: 'read', subject: 'Post', fields }]) | ||
|
||
expect(rules[0][4]).to.equal(fields.join(',')) | ||
expect(rules[0]).to.have.length(5) | ||
}) | ||
}) | ||
|
||
describe('`unpackRules` function', () => { | ||
it('converts array of rule arrays to array of rule objects', () => { | ||
const rules = unpackRules([ | ||
['read', 'Post'], | ||
['delete', 'Post'] | ||
]) | ||
|
||
expect(rules.every(rule => typeof rule === 'object')).to.be.true | ||
}) | ||
|
||
it('puts 1st element under `actions` field and converts it to an array', () => { | ||
const rules = unpackRules([['read,update', 'Post']]) | ||
|
||
expect(rules[0].actions).to.deep.equal(['read', 'update']) | ||
}) | ||
|
||
it('converts even a single `action` to an array', () => { | ||
const rules = unpackRules([['read', 'Post']]) | ||
|
||
expect(rules[0].actions).to.deep.equal(['read']) | ||
}) | ||
|
||
it('puts 2nd element under `subject` field and converts it to an array', () => { | ||
const rules = unpackRules([['read', 'Post,Comment']]) | ||
|
||
expect(rules[0].subject).to.deep.equal(['Post', 'Comment']) | ||
}) | ||
|
||
it('converts even a single `subject` to an array', () => { | ||
const rules = unpackRules([['read', 'Post']]) | ||
|
||
expect(rules[0].subject).to.deep.equal(['Post']) | ||
}) | ||
|
||
it('puts 3rd element under `inverted` field and converts it to boolean', () => { | ||
const rules = unpackRules([['read', 'Post,Comment', 1]]) | ||
|
||
expect(rules[0].inverted).to.be.true | ||
}) | ||
|
||
it('converts `inverted` to boolean, even it is not specified', () => { | ||
const rules = unpackRules([['read', 'Post,Comment']]) | ||
|
||
expect(rules[0].inverted).to.be.false | ||
}) | ||
|
||
it('puts 4th element under `conditions` field', () => { | ||
const conditions = { private: true } | ||
const rules = unpackRules([['read', 'Post,Comment', 0, conditions]]) | ||
|
||
expect(rules[0].conditions).to.equal(conditions) | ||
}) | ||
|
||
it('converts `conditions` to `null` if its value is `0`', () => { | ||
const rules = unpackRules([['read', 'Post,Comment', 1, 0]]) | ||
|
||
expect(rules[0].conditions).to.be.null | ||
}) | ||
|
||
it('puts 5th element under `fields` field and converts it to an array', () => { | ||
const fields = ['title', 'description'] | ||
const rules = unpackRules([['read', 'Post,Comment', 1, 0, fields.join(',')]]) | ||
|
||
expect(rules[0].fields).to.deep.equal(fields) | ||
}) | ||
|
||
|
||
it('converts `fields` to `null` if its value is `0`', () => { | ||
const rules = unpackRules([['read', 'Post,Comment', 1, 0, 0]]) | ||
|
||
expect(rules[0].fields).to.be.null | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function wrapArray(value) { | ||
return Array.isArray(value) ? value : [value]; | ||
} |