Skip to content

Commit 832fa2f

Browse files
committed
add tests
1 parent fbce947 commit 832fa2f

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

src/engine/tw-monitor-state.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class MonitorState {
6262
}
6363

6464
/**
65+
* Removes monitors that do not satisfy callback.
6566
* @param {(record: MonitorRecord) => boolean} callback Returns true to keep.
6667
*/
6768
filter (callback) {

test/unit/tw_monitor_record.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const {test} = require('tap');
2+
const MonitorRecord = require('../../src/engine/monitor-record');
3+
4+
test('new and get', t => {
5+
const record = new MonitorRecord({
6+
id: 'a',
7+
x: 10,
8+
y: 20
9+
});
10+
11+
t.equal(record.id, 'a');
12+
t.equal(record.x, 10);
13+
t.equal(record.y, 20);
14+
15+
t.equal(record.get('id'), 'a');
16+
t.equal(record.get('x'), 10);
17+
t.equal(record.get('y'), 20);
18+
19+
t.end();
20+
});
21+
22+
test('merge', t => {
23+
const record = new MonitorRecord({
24+
id: 'a',
25+
x: 10,
26+
y: 20
27+
});
28+
29+
t.equal(record.merge({
30+
x: 40,
31+
y: null
32+
}), true);
33+
t.equal(record.x, 40);
34+
t.equal(record.y, 20);
35+
36+
t.equal(record.merge({
37+
x: 40,
38+
y: undefined
39+
}), false);
40+
t.equal(record.x, 40);
41+
t.equal(record.y, 20);
42+
43+
t.end();
44+
});
45+
46+
test('externalDeltaToJS', t => {
47+
t.same(MonitorRecord.externalDeltaToJS({
48+
whatever: 'a'
49+
}), {whatever: 'a'});
50+
51+
t.same(MonitorRecord.externalDeltaToJS({
52+
toJS: () => ({whatever: 'a'})
53+
}), {whatever: 'a'});
54+
55+
t.end();
56+
});

test/unit/tw_monitor_state.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
const {test} = require('tap');
2+
const MonitorState = require('../../src/engine/tw-monitor-state');
3+
const MonitorRecord = require('../../src/engine/monitor-record');
4+
5+
test('basic operation', t => {
6+
const state = new MonitorState();
7+
8+
t.equal(state.dirty, false);
9+
t.equal(state.has('id'), false);
10+
t.equal(state.size, 0);
11+
t.equal(state.empty(), true);
12+
13+
state.set('id', new MonitorRecord({
14+
id: 'id',
15+
value: 0
16+
}));
17+
t.equal(state.has('id'), true);
18+
t.equal(state.get('id').id, 'id');
19+
t.equal(state.get('id').value, 0);
20+
t.equal(state.size, 1);
21+
t.equal(state.empty(), false);
22+
t.equal(state.dirty, true);
23+
24+
state.dirty = false;
25+
state.set('id', new MonitorRecord({
26+
id: 'id',
27+
value: -0
28+
}));
29+
t.equal(state.get('id').value, -0);
30+
t.equal(state.dirty, true);
31+
32+
state.dirty = false;
33+
state.set('id', new MonitorRecord({
34+
id: 'id',
35+
value: -0
36+
}));
37+
t.equal(state.dirty, false);
38+
39+
state.dirty = false;
40+
state.delete('id');
41+
t.equal(state.has('id'), false);
42+
t.equal(state.get('id'), undefined);
43+
t.equal(state.size, 0);
44+
t.equal(state.empty(), true);
45+
t.equal(state.dirty, true);
46+
47+
state.dirty = false;
48+
state.delete('id');
49+
t.equal(state.dirty, false);
50+
51+
t.end();
52+
});
53+
54+
test('filter', t => {
55+
const state = new MonitorState();
56+
57+
state.set('id1', new MonitorRecord({
58+
id: 'id1',
59+
value: 5
60+
}));
61+
state.set('id2', new MonitorRecord({
62+
id: 'id2',
63+
value: 10
64+
}));
65+
state.set('id3', new MonitorRecord({
66+
id: 'id3',
67+
value: 15
68+
}));
69+
70+
state.dirty = false;
71+
state.filter(record => record.value !== 10);
72+
t.equal(state.dirty, true);
73+
t.equal(state.has('id1'), true);
74+
t.equal(state.has('id2'), false);
75+
t.equal(state.has('id3'), true);
76+
77+
state.dirty = false;
78+
state.filter(record => record.value !== 10);
79+
t.equal(state.dirty, false);
80+
81+
t.end();
82+
});
83+
84+
test('value', t => {
85+
const state = new MonitorState();
86+
87+
const a = new MonitorRecord({
88+
id: 'a'
89+
});
90+
const b = new MonitorRecord({
91+
id: 'b'
92+
});
93+
state.set('a', a);
94+
state.set('b', b);
95+
96+
t.same(state.values(), [a, b]);
97+
t.same(state.valueSeq(), [a, b]);
98+
99+
t.end();
100+
});
101+
102+
test('shallowClone', t => {
103+
const state = new MonitorState();
104+
105+
state.set('id', new MonitorRecord({
106+
id: 'id'
107+
}));
108+
109+
const clone = state.shallowClone();
110+
t.not(state, clone);
111+
t.equal(state.map, clone.map);
112+
t.equal(clone.dirty, false);
113+
114+
t.end();
115+
});

0 commit comments

Comments
 (0)