forked from richardhopton/smartbed-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanJsonState.test.ts
More file actions
99 lines (88 loc) · 3.55 KB
/
cleanJsonState.test.ts
File metadata and controls
99 lines (88 loc) · 3.55 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
import { cleanJsonState } from './cleanJsonState';
import { getUnixEpoch } from './getUnixEpoch';
describe(cleanJsonState.name, () => {
it.each([
[{}, '', "''"],
[null, 'any string', ''],
])('should return %s if passed undefined with parent of %s%s', (expected, parentKey, _) => {
const actual = cleanJsonState(undefined, [], parentKey);
expect(actual).toEqual(expected);
});
it('should handle simple objects', () => {
const nowSecs = getUnixEpoch();
const expected = { value: 10, lastUpdate: new Date(nowSecs * 1000).toISOString(), type: 'degreesCelsius' };
const state = { value: 10, lastUpdateGMTSecs: nowSecs, type: 'degreesCelsius' };
const actual = cleanJsonState(state);
expect(actual).toEqual(expected);
});
it('should handle complex objects', () => {
const nowSecs = getUnixEpoch();
const expected = {
test: { value: 10, lastUpdate: new Date(nowSecs * 1000).toISOString(), type: 'degreesCelsius' },
tests: [{ lastUpdate: new Date(nowSecs * 1000).toISOString() }],
};
const state = {
test: { value: 10, lastUpdateGMTSecs: nowSecs, type: 'degreesCelsius' },
tests: [{ timeLastUpdate: nowSecs }],
};
const actual = cleanJsonState(state);
expect(actual).toEqual(expected);
});
it('properly cases with stripping gmt from beginning', () => {
const nowSecs = getUnixEpoch();
const expected = { lastUpdate: new Date(nowSecs * 1000).toISOString() };
const state = { gmtLastUpdate: nowSecs };
const actual = cleanJsonState(state);
expect(actual).toEqual(expected);
});
it('properly cases with stripping time from beginning', () => {
const nowSecs = getUnixEpoch();
const expected = { lastUpdate: new Date(nowSecs * 1000).toISOString() };
const state = { timeLastUpdate: nowSecs };
const actual = cleanJsonState(state);
expect(actual).toEqual(expected);
});
it('properly trims Gmt from end', () => {
const nowSecs = getUnixEpoch();
const expected = { lastUpdate: new Date(nowSecs * 1000).toISOString() };
const state = { lastUpdateGmt: nowSecs };
const actual = cleanJsonState(state);
expect(actual).toEqual(expected);
});
it('should strip GmtString fields', () => {
const expected = { value: 10 };
const state = { value: 10, someRandomGmtString: 'degreesCelsius' };
const actual = cleanJsonState(state);
expect(actual).toEqual(expected);
});
it('should convert localTimeNow field based on gmtOffset', () => {
const gmtOffset = -28800;
const localTimeNow = 1676751261;
const state = { gmtOffset, localTimeNow };
const actual = cleanJsonState(state);
expect(actual).toEqual({ localTimeNow: '2023-02-18T20:14:21.000-0800', gmtOffset });
});
it('should handle cableTime field', () => {
const nowSecs = getUnixEpoch();
const expected = { cableTime: new Date(nowSecs * 1000).toISOString() };
const state = { cableTime: nowSecs };
const actual = cleanJsonState(state);
expect(actual).toEqual(expected);
});
it('should strip unwanted fields', () => {
const expected = {
child: { value: 10 },
};
const state = { outerType: 'temperature', child: { value: 10, type: 'degreesCelsius' } };
const actual = cleanJsonState(state, ['outerType', 'child.type']);
expect(actual).toEqual(expected);
});
it('should handle simple arrays', () => {
const expected = {
productFeatures: ['api_flan_config_all', 'sleepz', 'motors', 'env_sensors'],
values: [1, 2, 3],
};
const actual = cleanJsonState(expected);
expect(actual).toEqual(expected);
});
});