Repository navigation
Expand file tree
/
Copy pathref-holder.test.js
More file actions
170 lines (133 loc) · 4.33 KB
/
Copy pathref-holder.test.js
File metadata and controls
170 lines (133 loc) · 4.33 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import RefHolder from '../../lib/models/ref-holder';
describe('RefHolder', function() {
let sub;
afterEach(function() {
if (sub) { sub.dispose(); }
});
it('begins empty', function() {
const h = new RefHolder();
assert.isTrue(h.isEmpty());
assert.throws(() => h.get(), /empty/);
});
it('does not become populated when assigned null', function() {
const h = new RefHolder();
h.setter(null);
assert.isTrue(h.isEmpty());
});
it('provides synchronous access to its current value', function() {
const h = new RefHolder();
h.setter(1234);
assert.isFalse(h.isEmpty());
assert.strictEqual(h.get(), 1234);
});
describe('map', function() {
it('returns an empty RefHolder as-is', function() {
const h = new RefHolder();
assert.strictEqual(h.map(() => 14), h);
});
it('returns a new RefHolder wrapping the value returned from its present block', function() {
const h = new RefHolder();
h.setter(12);
assert.strictEqual(h.map(x => x + 1).get(), 13);
});
it('returns a RefHolder returned from its present block', function() {
const h0 = new RefHolder();
h0.setter(14);
const o = h0.map(() => {
const h1 = new RefHolder();
h1.setter(12);
return h1;
});
assert.notStrictEqual(0, h0);
assert.strictEqual(o.get(), 12);
});
it('returns a new RefHolder wrapping the value returned from its absent block', function() {
const h = new RefHolder();
const o = h.map(x => 1, () => 2);
assert.strictEqual(o.get(), 2);
});
it('returns a RefHolder returned from its absent block', function() {
const h0 = new RefHolder();
const o = h0.map(x => 1, () => {
const h1 = new RefHolder();
h1.setter(1);
return h1;
});
assert.strictEqual(o.get(), 1);
});
});
describe('getOr', function() {
it("returns the RefHolder's value if it is non-empty", function() {
const h = new RefHolder();
h.setter(1234);
assert.strictEqual(h.getOr(5678), 1234);
});
it('returns its argument if the RefHolder is empty', function() {
const h = new RefHolder();
assert.strictEqual(h.getOr(5678), 5678);
});
});
it('notifies subscribers when it becomes available', function() {
const h = new RefHolder();
const callback = sinon.spy();
sub = h.observe(callback);
h.setter(1);
assert.isTrue(callback.calledWith(1));
h.setter(2);
assert.isTrue(callback.calledWith(2));
sub.dispose();
h.setter(3);
assert.isFalse(callback.calledWith(3));
});
it('immediately notifies new subscribers if it is already available', function() {
const h = new RefHolder();
h.setter(12);
const callback = sinon.spy();
sub = h.observe(callback);
assert.isTrue(callback.calledWith(12));
});
it('does not notify subscribers when it is assigned the same value', function() {
const h = new RefHolder();
h.setter(12);
const callback = sinon.spy();
sub = h.observe(callback);
callback.resetHistory();
h.setter(12);
assert.isFalse(callback.called);
});
it('does not notify subscribers when it becomes empty', function() {
const h = new RefHolder();
h.setter(12);
assert.isFalse(h.isEmpty());
const callback = sinon.spy();
sub = h.observe(callback);
callback.resetHistory();
h.setter(null);
assert.isTrue(h.isEmpty());
assert.isFalse(callback.called);
callback.resetHistory();
h.setter(undefined);
assert.isTrue(h.isEmpty());
assert.isFalse(callback.called);
});
it('resolves a promise when it becomes available', async function() {
const thing = Symbol('Thing');
const h = new RefHolder();
const promise = h.getPromise();
h.setter(thing);
assert.strictEqual(await promise, thing);
assert.strictEqual(await h.getPromise(), thing);
});
describe('.on()', function() {
it('returns an existing RefHolder as-is', function() {
const original = new RefHolder();
const wrapped = RefHolder.on(original);
assert.strictEqual(original, wrapped);
});
it('wraps a non-RefHolder value with a RefHolder set to it', function() {
const wrapped = RefHolder.on(9000);
assert.isFalse(wrapped.isEmpty());
assert.strictEqual(wrapped.get(), 9000);
});
});
});