-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompanion.test.js
79 lines (65 loc) · 2.66 KB
/
companion.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
import FsSettings from './src/companion';
import messaging from 'messaging';
import { settingsStorage } from 'settings';
import { peerSocket } from 'messaging';
const initialSettings = {
background: '#fff',
newProp: 'bar'
};
describe('fitbit-settings/companion', () => {
afterEach(() => {
peerSocket.registeredHandlers = [];
peerSocket.addEventListener.mockClear();
peerSocket.send.mockClear();
});
test('listen() syncs device with settings change events', () => {
const companionSettings = new FsSettings(initialSettings);
companionSettings.listen();
// Emit event to match live storage events
settingsStorage.emitMockEvent('change', {
key: 'background',
newValue: 'red'
});
expect(peerSocket.send).toHaveBeenCalledWith({ prop: 'FS_SETTINGS_UPDATE:background', value: 'red'});
});
test('listen() syncs device with any offline changes or mismatch states', () => {
const companionSettings = new FsSettings(initialSettings);
// Have to set the setting storage
settingsStorage.setItem('background', '#fff');
settingsStorage.setItem('newProp', 'bar');
companionSettings.listen();
const mockOldDeviceSettings = {
background: 'red',
newProp: 'yellow'
};
peerSocket.emitMockEvent('message', {
key: 'FS_SETTINGS_SYNC:INIT',
value: mockOldDeviceSettings
});
expect(peerSocket.send).toHaveBeenCalledWith({prop: 'FS_SETTINGS_UPDATE:background', value: '#fff'});
expect(peerSocket.send).toHaveBeenCalledWith({prop: 'FS_SETTINGS_UPDATE:newProp', value: 'bar'});
});
// The device should send over the settings and return
// the delta of differences over to app
test('listen() syncs settings with the device', () => {
settingsStorage.setItem = jest.fn();
const initialSettings = {
background: '#fff',
newProp: 'bar'
};
const companionSettings = new FsSettings(initialSettings);
companionSettings.listen();
// Emit event to match live storage events
messaging.peerSocket.emitMockEvent('message', ({
prop: 'FS_SETTINGS_UPDATE:background',
value: 'foo'
}));
expect(settingsStorage.setItem).toHaveBeenCalledWith('background', 'foo');
messaging.peerSocket.emitMockEvent('message', ({
prop: 'FS_SETTINGS_UPDATE:newProp',
value: 'bar'
}));
expect(settingsStorage.setItem).toHaveBeenCalledWith('newProp', 'bar');
expect(settingsStorage.setItem.mock.calls.length).toBe(2);
});
});