|
| 1 | +import each from '../../src/utils/each' |
| 2 | + |
| 3 | +describe('Utilities', () => { |
| 4 | + describe('each()', () => { |
| 5 | + function Fixture () { |
| 6 | + this.foo = 'bar' |
| 7 | + this.baz = 'bun' |
| 8 | + } |
| 9 | + |
| 10 | + describe('if passed an object literal...', () => { |
| 11 | + it('should invoke callback for each property', () => { |
| 12 | + const fixture = new Fixture() |
| 13 | + const spy = sinon.spy() |
| 14 | + each(fixture, spy) |
| 15 | + expect(spy).to.have.been.calledTwice |
| 16 | + }) |
| 17 | + |
| 18 | + it('should ignore properties on the prototype chain', () => { |
| 19 | + Fixture.prototype.biff = 'baff' |
| 20 | + const fixture = new Fixture() |
| 21 | + const spy = sinon.spy() |
| 22 | + each(fixture, spy) |
| 23 | + expect(spy).to.have.been.calledTwice |
| 24 | + }) |
| 25 | + |
| 26 | + it('should pass the value, key and collection to the callback', () => { |
| 27 | + const fixture = new Fixture() |
| 28 | + let _value, _key, _collection |
| 29 | + each(fixture, (value, key, collection) => { |
| 30 | + _value = value |
| 31 | + _key = key |
| 32 | + _collection = collection |
| 33 | + }) |
| 34 | + expect(_value).to.equal('bun') |
| 35 | + expect(_key).to.equal('baz') |
| 36 | + expect(_collection).to.deep.equal(fixture) |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + describe('if passed an array...', () => { |
| 41 | + const fixture = ['apple', 'orange', 'banana'] |
| 42 | + |
| 43 | + it('should invoke callback for each value', () => { |
| 44 | + const spy = sinon.spy() |
| 45 | + each(fixture, spy) |
| 46 | + expect(spy).to.have.been.calledThrice |
| 47 | + }) |
| 48 | + |
| 49 | + it('should pass the value, index and collection to the callback', () => { |
| 50 | + let _value, _index, _collection |
| 51 | + each(fixture, (value, index, collection) => { |
| 52 | + _value = value |
| 53 | + _index = index |
| 54 | + _collection = collection |
| 55 | + }) |
| 56 | + expect(_value).to.equal('banana') |
| 57 | + expect(_index).to.equal(2) |
| 58 | + expect(_collection).to.deep.equal(fixture) |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + describe('else', () => { |
| 63 | + it('should throw a type error when passed an invalid collection', () => { |
| 64 | + let caught |
| 65 | + try { |
| 66 | + each(null, () => {}) |
| 67 | + } catch (error) { |
| 68 | + caught = error |
| 69 | + } |
| 70 | + expect(caught).to.exist |
| 71 | + expect(caught).to.be.an.instanceof(TypeError) |
| 72 | + }) |
| 73 | + }) |
| 74 | + }) |
| 75 | +}) |
0 commit comments