summaryrefslogtreecommitdiffstats
path: root/tests/memoize.js
blob: 98f367814de60134f67bb38cc037153725d2d464 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { memoize } from '..';
import { expect } from 'chai';

/*global describe,it*/

describe('memoize()', () => {
	it('should memoize when used as a simple decorator', next => {
		let c = {
			@memoize
			foo(key) {
				c[key] = (c[key] || 0) + 1;
			}
		};

		expect(c).not.to.have.property('a');
		c.foo('a');
		expect(c).to.have.property('a', 1);
		c.foo('a');
		c.foo('a');
		expect(c).to.have.property('a', 1);

		next();
	});

	it('should memoize when used as a function', next => {
		let c = memoize( key => {
				m[key] = (m[key] || 0) + 1;
			}),
			m = {};

		expect(m).not.to.have.property('a');
		c('a');
		expect(m).to.have.property('a', 1);
		c('a');
		c('a');
		expect(m).to.have.property('a', 1);

		next();
	});

	it('should memoize when called without arguments', next => {
		let c = memoize( key => {
				m[key] = (m[key] || 0) + 1;
			}),
			m = {};

		expect(m).not.to.have.property('undefined');
		c();
		expect(m).to.have.property('undefined', 1);
		c();
		c();
		expect(m).to.have.property('undefined', 1);

		next();
	});

	it('should memoize when called with an empty string', next => {
		let c = memoize( key => {
				m[key] = (m[key] || 0) + 1;
			}),
			m = {};

		expect(m).not.to.have.property('');
		c('');
		expect(m).to.have.property('', 1);
		c('');
		c('');
		expect(m).to.have.property('', 1);

		next();
	});
});