-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.test.js
254 lines (206 loc) · 7.52 KB
/
app.test.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import FsSettings from './src/app';
import fs from 'fs';
import messaging from 'messaging';
jest.mock('fs');
const mockStoredSettings = {
background: 'black',
name: 'henry',
border: 'red',
deprecatedProp: 'foo'
};
const mockWrite = fs.writeFileSync.mockReturnValue(true);
describe('fitbit-settings/app', () => {
afterEach(() => {
messaging.peerSocket.registeredHandlers = [];
messaging.peerSocket.addEventListener.mockClear();
fs.readFileSync.mockClear();
})
test('initialize properly', () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValue(mockStoredSettings);
const initialSettings = {
background: '#fff',
newProp: 'bar'
};
const settings = new FsSettings(initialSettings, { filePath: 'file-path.cbor'});
// Save to Disk Properly
expect(mockWrite.mock.calls[0][0]).toBe('file-path.cbor');
expect(mockWrite.mock.calls[0][1].name).toBe('henry'); // Check for Migration
expect(mockWrite.mock.calls[0][2]).toBe('cbor');
// Store initial settings
expect(settings.initial).toBe(initialSettings)
// Values Correct
expect(settings.getProp('newProp')).toBe('bar'); // Newly Added
expect(settings.getProp('name')).toBe('henry'); // Stored
expect(settings.getProp('deprecatedProp')).toBe('foo'); // Old Still Exists
});
test('update() reflects internal setting state', () => {
fs.existsSync.mockReturnValue(false);
const settings = new FsSettings({
background: 'white'
});
settings.update('background', 'yellow');
expect(settings.getProp('background')).toBe('yellow');
});
test('stored settings persist new initial settings in state', () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValue(mockStoredSettings);
const settings = new FsSettings({
background: 'black',
name: 'joe',
newProp: 'bar'
});
expect(settings.getProp('name')).toBe('henry');
expect(settings.getProp('border')).toBe('red');
expect(settings.getProp('background')).toBe('black');
expect(settings.getProp('deprecatedProp')).toBe('foo');
});
test('return value with getProp()', () => {
const settings = new FsSettings({
test: 1
});
expect(settings.getProp('test')).toBe(1);
});
test('reset() clears internal state', () => {
const settings = new FsSettings({
prop1: 1,
prop2: 2,
});
// Update
settings.update('prop1', 2);
settings.update('prop2', 3);
expect(settings.getProp('prop1', 2));
expect(settings.getProp('prop1', 3));
// Return to normal
settings.reset();
expect(settings.getProp('prop1')).toBe(1);
expect(settings.getProp('prop2')).toBe(2);
});
test('save() stores state to disk', () => {
fs.existsSync.mockReturnValue(false);
const settings = new FsSettings({
foo: 'bar',
choo: 'he'
}, {
filePath: 'testpath.cbor'
});
// Update
settings.update('choo', 'choo');
expect(mockWrite).toHaveBeenLastCalledWith('testpath.cbor', {
foo: 'bar',
choo: 'choo'
}, 'cbor');
});
test('main methods are chainable', () => {
const settings = new FsSettings({
foo: 'bar',
choo: 'bar2',
mary: 'little'
});
settings.update('choo', 'choo').update('mary', 'lamb').save();
expect(settings.getProp('mary', 'lamb'))
expect(mockWrite).toHaveBeenLastCalledWith('settings.cbor', {
foo: 'bar',
choo: 'choo',
mary: 'lamb'
}, 'cbor');
});
test('listen() calls stored listeners on init', () => {
const settings = new FsSettings({
prop1: 'bar', prop2: 1, prop3: true }, { callListenersOnInit: true });
const mockCb1 = jest.fn();
const mockCb2 = jest.fn();
const mockCb3 = jest.fn();
settings.onPropChange('prop1', mockCb1);
settings.onPropChange('prop2', mockCb2);
settings.onPropChange('prop3', mockCb3);
settings.listen();
expect(mockCb1).toHaveBeenCalledWith({
data: {
prop: 'prop1', value: 'bar'
}
});
expect(mockCb2).toHaveBeenCalledWith({
data: {
prop: 'prop2', value: 1
}
});
expect(mockCb3).toHaveBeenCalledWith({
data: {
prop: 'prop3', value: true
}
});
});
test('listen() provides the companion with its current state', () => {
const settings = new FsSettings({ 'foo': 'bar' }, { syncWithCompanion: true });
settings.listen();
messaging.peerSocket.emitMockEvent('open', {});
expect(messaging.peerSocket.send).toHaveBeenCalledWith({
key: 'FS_SETTINGS_SYNC:INIT',
value: {
foo: 'bar'
}
});
});
test('listen() register message event handler', () => {
const settings = new FsSettings({ 'foo': 'bar' });
settings.listen();
expect(messaging.peerSocket.addEventListener.mock.calls[0][0]).toBe('message');
// Trigger resolve
messaging.peerSocket.emitMockEvent('open');
});
test('listen() updates state on relevant events on new props and saves to disk', () => {
const settings = new FsSettings({ 'foo': 'bar' });
settings.listen();
messaging.peerSocket.emitMockEvent('message', {
prop: 'FS_SETTINGS_UPDATE:foo',
value: 'foo-bar!'
});
expect(settings.getProp('foo')).toBe('foo-bar!');
expect(mockWrite).toHaveBeenLastCalledWith('settings.cbor', {'foo': 'foo-bar!'}, 'cbor');
messaging.peerSocket.emitMockEvent('message', {
prop: 'irrelevant',
value: 'noise'
});
expect(settings.getProp('irrelevant')).toBeUndefined;
});
test('onPropChange() registers callbacks and are triggered', () => {
const settings = new FsSettings({ foo: 'bar', color: 'white' });
const callbackSpy = jest.fn();
const callbackSpy2 = jest.fn();
settings
.onPropChange('foo', callbackSpy)
.onPropChange('color', callbackSpy2);
settings.listen();
messaging.peerSocket.emitMockEvent('message', {
prop: 'FS_SETTINGS_UPDATE:foo',
value: 'test123!'
});
messaging.peerSocket.emitMockEvent('message', {
prop: 'FS_SETTINGS_UPDATE:color',
value: 'yaaay!'
});
expect(callbackSpy).toHaveBeenCalledWith({
data: {
prop: "foo",
value: "test123!"
}
});
expect(callbackSpy2).toHaveBeenCalledWith({
data: {
prop: "color",
value: "yaaay!"
}
});
});
test('updateSettingStorage() should send message to the companion', () => {
const settings = new FsSettings({
foo: 'value'
}, {
syncWithCompanion: true
});
settings
.update('foo', 'companion-sync-test');
expect(messaging.peerSocket.send).toHaveBeenLastCalledWith({prop: 'FS_SETTINGS_UPDATE:foo', value: 'companion-sync-test'});
});
});