|
| 1 | +import when from '../src/index'; |
| 2 | + |
| 3 | +describe('allOf', () => { |
| 4 | + let data = { |
| 5 | + brand: ['Apple', 'Samsung', 'Nokia'] |
| 6 | + } |
| 7 | + |
| 8 | + it('single rule', () => { |
| 9 | + expect(when(['allOf', 'brand', ['Apple', 'Samsung', 'Nokia']], data)).to.equal(true) |
| 10 | + expect(when(['allOf', 'brand', ['Nokia', 'Samsung', 'Apple']], data)).to.equal(true) |
| 11 | + expect(when(['allOf', 'brand', ['Nokia', 'Samsung']], data)).to.equal(true) |
| 12 | + expect(when(['allOf', 'brand', ['Nokia', 'Another']], data)).to.equal(false) |
| 13 | + expect(when(['allOf', 'brand', ['Apple', 'Samsung', 'Nokia', 'Another']], data)).to.equal(false) |
| 14 | + }) |
| 15 | + |
| 16 | + it('single rule using "and"', () => { |
| 17 | + expect(when(['and', ['allOf', 'brand', ['Apple', 'Samsung', 'Nokia']]], data)).to.equal(true) |
| 18 | + expect(when(['and', ['allOf', 'brand', ['Nokia', 'Samsung', 'Apple']]], data)).to.equal(true) |
| 19 | + expect(when(['and', ['allOf', 'brand', ['Nokia', 'Samsung']]], data)).to.equal(true) |
| 20 | + expect(when(['and', ['allOf', 'brand', ['Nokia', 'Another']]], data)).to.equal(false) |
| 21 | + expect(when(['and', ['allOf', 'brand', ['Apple', 'Samsung', 'Nokia', 'Another']]], data)).to.equal(false) |
| 22 | + }) |
| 23 | + |
| 24 | + it('single rule using "or"', () => { |
| 25 | + expect(when(['or', ['allOf', 'brand', ['Apple', 'Samsung', 'Nokia']]], data)).to.equal(true) |
| 26 | + expect(when(['or', ['allOf', 'brand', ['Nokia', 'Samsung', 'Apple']]], data)).to.equal(true) |
| 27 | + expect(when(['or', ['allOf', 'brand', ['Nokia', 'Samsung']]], data)).to.equal(true) |
| 28 | + expect(when(['or', ['allOf', 'brand', ['Nokia', 'Another']]], data)).to.equal(false) |
| 29 | + expect(when(['or', ['allOf', 'brand', ['Apple', 'Samsung', 'Nokia', 'Another']]], data)).to.equal(false) |
| 30 | + }) |
| 31 | + |
| 32 | + it('deep object key', () => { |
| 33 | + let data = { |
| 34 | + contact: { |
| 35 | + person: { |
| 36 | + brand: ['Apple', 'Samsung', 'Nokia'] |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + expect(when(['allOf', 'contact.person.brand', ['Apple', 'Samsung', 'Nokia']], data)).to.equal(true) |
| 42 | + expect(when(['allOf', 'contact.person.brand', ['Nokia', 'Samsung', 'Apple']], data)).to.equal(true) |
| 43 | + expect(when(['allOf', 'contact.person.brand', ['Nokia', 'Samsung']], data)).to.equal(true) |
| 44 | + expect(when(['allOf', 'contact.person.brand', ['Nokia', 'Another']], data)).to.equal(false) |
| 45 | + expect(when(['allOf', 'contact.person.brand', ['Apple', 'Samsung', 'Nokia', 'Another']], data)).to.equal(false) |
| 46 | + }) |
| 47 | + |
| 48 | + it('deep object array key', () => { |
| 49 | + let data = { |
| 50 | + contact: { |
| 51 | + person: [{ |
| 52 | + brand: ['Apple', 'Samsung', 'Nokia'] |
| 53 | + }] |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + expect(when(['allOf', 'contact.person.0.brand', ['Apple', 'Samsung', 'Nokia']], data)).to.equal(true) |
| 58 | + expect(when(['allOf', 'contact.person[0].brand', ['Nokia', 'Samsung', 'Apple']], data)).to.equal(true) |
| 59 | + expect(when(['allOf', 'contact.person[0].brand', ['Nokia', 'Samsung']], data)).to.equal(true) |
| 60 | + expect(when(['allOf', 'contact.person.0.brand', ['Nokia', 'Another']], data)).to.equal(false) |
| 61 | + expect(when(['allOf', 'contact.person[0].brand', ['Apple', 'Samsung', 'Nokia', 'Another']], data)).to.equal(false) |
| 62 | + }) |
| 63 | + |
| 64 | + it('throws error when values is not of type array', () => { |
| 65 | + (function() { |
| 66 | + when(['allOf', 'name', 'Apple'], data) |
| 67 | + }.should.throw(/"allOf" condition requires an array as #3 argument/)); |
| 68 | + }) |
| 69 | +}) |
0 commit comments