|
1 | 1 | import { assert, expect } from 'chai';
|
2 | 2 | import 'mocha';
|
3 |
| -import { hasNested } from './utility-functions'; |
| 3 | +import { ERRORS, hasNested, limitFn, range } from './utility-functions'; |
4 | 4 |
|
5 | 5 | describe('Utility Functions', () => {
|
6 | 6 | describe('hasNested', () => {
|
@@ -49,4 +49,104 @@ describe('Utility Functions', () => {
|
49 | 49 | expect(hasNested({}, '')).to.be.false;
|
50 | 50 | });
|
51 | 51 | });
|
| 52 | + |
| 53 | + |
| 54 | + describe('limitFn', () => { |
| 55 | + it('returns a function', () => { |
| 56 | + const fn = () => true; |
| 57 | + expect(limitFn(fn)).to.be.a('function'); |
| 58 | + }); |
| 59 | + |
| 60 | + |
| 61 | + it('returns original function\'s return value', () => { |
| 62 | + const fn = () => true; |
| 63 | + expect(limitFn(fn)()).to.be.true; |
| 64 | + }); |
| 65 | + |
| 66 | + |
| 67 | + it('limits the number of calls a function can take', () => { |
| 68 | + const fn = () => true; |
| 69 | + const limited = limitFn(fn); |
| 70 | + limited(); |
| 71 | + expect(limited()).to.be.undefined; |
| 72 | + }); |
| 73 | + |
| 74 | + |
| 75 | + it('takes custom limit argument', () => { |
| 76 | + const fn = () => true; |
| 77 | + const limited = limitFn(fn, 5); |
| 78 | + for (const i of range(1, 5)) { |
| 79 | + expect(limited()).to.be.true; |
| 80 | + } |
| 81 | + expect(limited()).to.be.undefined; |
| 82 | + }); |
| 83 | + |
| 84 | + |
| 85 | + it('passes the function arguments to the limited limited function', () => { |
| 86 | + const add = (x: number, y: number) => x + y; |
| 87 | + const limited = limitFn(add); |
| 88 | + expect(limited(1, 3)).to.equal(4); |
| 89 | + expect(limited(2, 2)).to.be.undefined; |
| 90 | + }); |
| 91 | + |
| 92 | + |
| 93 | + it('throws if passed limit argument is not a number', () => { |
| 94 | + const fn = () => true; |
| 95 | + assert.throws(() => limitFn(fn, '1'), ERRORS.LIMIT_NOT_A_NUMBER); |
| 96 | + }); |
| 97 | + |
| 98 | + |
| 99 | + it('throws if argument is not a function', () => { |
| 100 | + assert.throws(() => limitFn(1), ERRORS.NOT_A_FUNCTION); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + |
| 105 | + describe('range', () => { |
| 106 | + it('creates an array from 1 to 5', () => { |
| 107 | + const subject = range(1, 5); |
| 108 | + expect(subject).to.be.an('array'); |
| 109 | + expect(subject[0]).to.equal(1); |
| 110 | + expect(subject[subject.length - 1]).to.equal(5); |
| 111 | + }); |
| 112 | + |
| 113 | + |
| 114 | + it('creates an array from -1 to 5', () => { |
| 115 | + const subject = range(-1, 5); |
| 116 | + expect(subject).to.be.an('array'); |
| 117 | + expect(subject[0]).to.equal(-1); |
| 118 | + expect(subject[subject.length - 1]).to.equal(5); |
| 119 | + }); |
| 120 | + |
| 121 | + |
| 122 | + it('creates an array from 5 to 1', () => { |
| 123 | + const subject = range(5, 1); |
| 124 | + expect(subject).to.be.an('array'); |
| 125 | + expect(subject[0]).to.equal(5); |
| 126 | + expect(subject[subject.length - 1]).to.equal(1); |
| 127 | + }); |
| 128 | + |
| 129 | + |
| 130 | + it('creates an array from -5 to 1', () => { |
| 131 | + const subject = range(-5, 1); |
| 132 | + expect(subject).to.be.an('array'); |
| 133 | + expect(subject[0]).to.equal(-5); |
| 134 | + expect(subject[subject.length - 1]).to.equal(1); |
| 135 | + }); |
| 136 | + |
| 137 | + |
| 138 | + it('throws if arguments are missing', () => { |
| 139 | + assert.throws(() => range(1), ERRORS.RANGE_NEEDS_TWO_PARAMS); |
| 140 | + }); |
| 141 | + |
| 142 | + |
| 143 | + it('throws if arguments are not numbers', () => { |
| 144 | + assert.throws(() => range('1', '5'), ERRORS.RANGE_NEEDS_NUMBERS); |
| 145 | + }); |
| 146 | + |
| 147 | + |
| 148 | + it('throws if limit arguments are equal', () => { |
| 149 | + assert.throws(() => range(2, 2), ERRORS.RANGE_LIMITS_EQUAL); |
| 150 | + }); |
| 151 | + }); |
52 | 152 | });
|
0 commit comments