-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
154 lines (131 loc) · 4.39 KB
/
test.js
File metadata and controls
154 lines (131 loc) · 4.39 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
import * as assert from 'uvu/assert';
import { suite } from 'uvu';
import { snoop } from 'snoop';
import { JSDOM } from 'jsdom';
import Store from './index.js';
const test = suite('store');
const { window } = new JSDOM();
global.NodeList = window.NodeList;
global.DOMTokenList = window.DOMTokenList;
global.HTMLCollection = window.HTMLCollection;
let callback;
test.before.each(() => callback = snoop(state => state));
test('create an instance with empty state', () => {
const store = new Store();
assert.instance(store, Store);
assert.type(store.get(), 'object');
});
test('get single key', () => {
const store = new Store({key: 'value'});
assert.is(store.get('key'), 'value');
assert.is(store.get().key, 'value');
});
test('get full state object', () => {
const state = {foo: 'bar', bool: false, count: 42};
const store = new Store(state);
assert.equal(store.get(), state);
});
test('state object returns a copy', () => {
const state = {hello: 'world'};
const store = new Store(state);
assert.is.not(store.state, state);
assert.is.not(store.get(), state);
});
test('state object only accessible via getter', () => {
const store = new Store({foo: 'bar'});
assert.throws(() => store.state = {});
assert.equal(store.state, store.get());
});
test('state is immutable', () => {
const store = new Store({name: 'Colorado'});
const state = store.get();
state.name = 'Minnesota';
assert.is(store.get('name'), 'Colorado');
});
test('set single property', () => {
const store = new Store({greeting: 'Hello'});
store.set('greeting', 'Howdy');
assert.is(store.get('greeting'), 'Howdy');
});
test('set via partial object', () => {
const store = new Store({name: 'Nik', age: 24});
store.set({age: 42});
assert.equal(store.get(), {name: 'Nik', age: 42});
});
test('set via function with current state', () => {
const store = new Store({count: 5})
store.set(state => ({count: state.count * 8 + 2}));
assert.equal(store.get(), {count: 42});
});
test('subscribe callback immediately invoked with current state', () => {
const store = new Store({a: 'a', b: 'b'});
store.subscribe(callback.fn);
store.subscribe(['a','b'], callback.fn);
assert.is(callback.callCount, 2);
assert.is(callback.calls[0].arguments[0].a, 'a');
assert.is(callback.calls[1].arguments[0].b, 'b');
});
test('subscribe without immediate callback', () => {
const store = new Store({a: 'a', b: 'b'});
store.subscribe(callback.fn, false);
store.subscribe(['a','b'], callback.fn, false);
assert.ok(callback.notCalled);
});
test('subscribe to state changes by key', () => {
const store = new Store({toggle: false});
store.subscribe('toggle', callback.fn);
store.set(state => ({toggle: !state.toggle}));
assert.ok(store.get('toggle'));
});
test('subscribe to all state changes', () => {
const store = new Store({a: null, b: null});
store.subscribe(callback.fn, false);
store.set({a: 'foo'});
store.set({b: 'bar'});
assert.is(callback.callCount, 2);
});
test('subscribe to multiple keys', () => {
const store = new Store({a: null, b: null});
store.subscribe(['a', 'b'], callback.fn, false);
store.set({a: 'a', b: 'b'});
assert.is(callback.callCount, 2);
});
test('subscribe receives latest state', () => {
const store = new Store({a: false});
store.subscribe('a', callback.fn, false);
store.set('a', true);
store.set('b', 'should not trigger callback');
assert.ok(callback.calledOnce);
assert.ok(callback.firstCall.result.a);
});
test('subscribe only fires for changed values', () => {
const store = new Store({count: 0});
store.subscribe('count', callback.fn, false);
store.set('count', 43);
store.set('count', 43);
assert.ok(callback.calledOnce);
});
test('unsubscribe via key', () => {
const store = new Store({status: 'idle'});
store.subscribe('status', callback.fn, false);
store.set('status', 'loading');
store.unsubscribe('status', callback.fn);
store.set('status', 'complete');
assert.ok(callback.calledOnce);
});
test('unsubscribe via returned function', () => {
const store = new Store({key: 'a'});
const unsubscribe = store.subscribe('key', callback.fn, false);
store.set('key', 'b');
unsubscribe();
store.set('key', 'c');
assert.ok(callback.calledOnce);
});
test('reset to initial state', () => {
const initial = {key: 'foo'};
const store = new Store(initial);
store.set('key', 'poo');
store.reset();
assert.equal(store.get(), initial);
});
test.run();