Skip to content

Commit 32dc2b3

Browse files
committed
Complete testing for masterState
1 parent 4dad1fc commit 32dc2b3

File tree

2 files changed

+86
-109
lines changed

2 files changed

+86
-109
lines changed

src/backend/__tests__/masterState.test.ts

Lines changed: 76 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,66 @@
1-
import { ComponentAction, ComponentActionRecord } from '../models/masterState';
21
import componentActionRecord from '../models/masterState';
32

43
describe('Master State unit tests', () => {
5-
describe('ComponentAction and ComponentActionRecord type tests', () => {
6-
it('ComponentAction should be an object with string keys and array values', () => {
7-
const componentAction: ComponentAction = {
8-
'http://test.com': ['action1', 'action2'],
9-
};
10-
expect(componentAction).toEqual(expect.any(Object));
11-
expect(Object.keys(componentAction)).toEqual(expect.arrayContaining(['http://test.com']));
12-
expect(Array.isArray(componentAction['http://test.com'])).toBe(true);
13-
});
14-
15-
it('ComponentActionRecord should be an array of ComponentAction', () => {
16-
const componentActionRecord: ComponentActionRecord = [
17-
{ 'http://test.com': ['action1', 'action2'] },
18-
{ 'http://test.com/2': ['action3', 'action4'] },
19-
];
20-
expect(componentActionRecord).toEqual(expect.any(Array));
21-
expect(componentActionRecord.length).toBe(2);
22-
expect(componentActionRecord[0]).toEqual(
23-
expect.objectContaining({ 'http://test.com': expect.any(Array) }),
24-
);
25-
expect(componentActionRecord[1]).toEqual(
26-
expect.objectContaining({ 'http://test.com/2': expect.any(Array) }),
27-
);
28-
});
29-
30-
it('ComponentAction should be an object with string keys and array values', () => {
31-
const componentAction: ComponentAction = {
32-
'http://test.com': ['action1', 'action2'],
33-
};
34-
expect(componentAction).toEqual(expect.any(Object));
35-
expect(Object.keys(componentAction)).toEqual(expect.arrayContaining(['http://test.com']));
36-
expect(Array.isArray(componentAction['http://test.com'])).toBe(true);
37-
});
38-
39-
it('ComponentActionRecord should be an array of ComponentActions', () => {
40-
const componentActionRecord: ComponentActionRecord = [
41-
{ 'http://test.com': ['action1', 'action2'] },
42-
{ 'http://test.com/2': ['action3', 'action4'] },
43-
];
44-
expect(componentActionRecord).toEqual(expect.any(Array));
45-
expect(componentActionRecord.length).toBe(2);
46-
expect(componentActionRecord[0]).toEqual(
47-
expect.objectContaining({ 'http://test.com': expect.any(Array) }),
48-
);
49-
expect(componentActionRecord[1]).toEqual(
50-
expect.objectContaining({ 'http://test.com/2': expect.any(Array) }),
51-
);
52-
});
53-
});
4+
// describe('ComponentAction and ComponentActionRecord type tests', () => {
5+
// it('ComponentAction should be an object with string keys and array values', () => {
6+
// const componentAction: ComponentAction = {
7+
// 'http://test.com': ['action1', 'action2'],
8+
// };
9+
// expect(componentAction).toEqual(expect.any(Object));
10+
// expect(Object.keys(componentAction)).toEqual(expect.arrayContaining(['http://test.com']));
11+
// expect(Array.isArray(componentAction['http://test.com'])).toBe(true);
12+
// });
13+
14+
// it('ComponentActionRecord should be an array of ComponentAction', () => {
15+
// const componentActionRecord: ComponentActionRecord = [
16+
// { 'http://test.com': ['action1', 'action2'] },
17+
// { 'http://test.com/2': ['action3', 'action4'] },
18+
// ];
19+
// expect(componentActionRecord).toEqual(expect.any(Array));
20+
// expect(componentActionRecord.length).toBe(2);
21+
// expect(componentActionRecord[0]).toEqual(
22+
// expect.objectContaining({ 'http://test.com': expect.any(Array) }),
23+
// );
24+
// expect(componentActionRecord[1]).toEqual(
25+
// expect.objectContaining({ 'http://test.com/2': expect.any(Array) }),
26+
// );
27+
// });
28+
29+
// it('ComponentAction should be an object with string keys and array values', () => {
30+
// const componentAction: ComponentAction = {
31+
// 'http://test.com': ['action1', 'action2'],
32+
// };
33+
// expect(componentAction).toEqual(expect.any(Object));
34+
// expect(Object.keys(componentAction)).toEqual(expect.arrayContaining(['http://test.com']));
35+
// expect(Array.isArray(componentAction['http://test.com'])).toBe(true);
36+
// });
37+
38+
// it('ComponentActionRecord should be an array of ComponentActions', () => {
39+
// const componentActionRecord: ComponentActionRecord = [
40+
// { 'http://test.com': ['action1', 'action2'] },
41+
// { 'http://test.com/2': ['action3', 'action4'] },
42+
// ];
43+
// expect(componentActionRecord).toEqual(expect.any(Array));
44+
// expect(componentActionRecord.length).toBe(2);
45+
// expect(componentActionRecord[0]).toEqual(
46+
// expect.objectContaining({ 'http://test.com': expect.any(Array) }),
47+
// );
48+
// expect(componentActionRecord[1]).toEqual(
49+
// expect.objectContaining({ 'http://test.com/2': expect.any(Array) }),
50+
// );
51+
// });
52+
// });
5453

5554
describe('componentActionRecord unit tests', () => {
5655
const component1 = { state: 'dummy state', props: {} };
5756
const component2 = { state: 'dummy state2', props: {} };
5857
const component3 = { state: 'dummy state3', props: {} };
59-
58+
let index1, index2, index3;
6059
beforeEach(() => {
6160
componentActionRecord.clear();
62-
});
63-
64-
describe('saveNew', () => {
65-
it('should add a new component to componentActionRecord and return its index', () => {
66-
const index1 = componentActionRecord.saveNew(component1);
67-
const index2 = componentActionRecord.saveNew(component2);
68-
const index3 = componentActionRecord.saveNew(component3);
69-
70-
expect(index1).toEqual(0);
71-
expect(index2).toEqual(1);
72-
expect(index3).toEqual(2);
73-
expect(componentActionRecord.getAllComponents()).toEqual([
74-
component1,
75-
component2,
76-
component3,
77-
]);
78-
});
61+
index1 = componentActionRecord.saveNew(component1);
62+
index2 = componentActionRecord.saveNew(component2);
63+
index3 = componentActionRecord.saveNew(component3);
7964
});
8065

8166
describe('clear', () => {
@@ -91,48 +76,53 @@ describe('Master State unit tests', () => {
9176
});
9277
});
9378

79+
describe('saveNew', () => {
80+
it('should add a new component to componentActionRecord and return its index', () => {
81+
expect(index1).toEqual(0);
82+
expect(index2).toEqual(1);
83+
expect(index3).toEqual(2);
84+
85+
expect(componentActionRecord.getAllComponents()).toHaveLength(3);
86+
87+
expect(componentActionRecord.getAllComponents()[index1]).toBe(component1);
88+
expect(componentActionRecord.getAllComponents()[index2]).toBe(component2);
89+
expect(componentActionRecord.getAllComponents()[index3]).toBe(component3);
90+
});
91+
});
92+
9493
describe('getComponentByIndex', () => {
9594
it('should return the component at the specified index', () => {
96-
componentActionRecord.saveNew(component1);
97-
componentActionRecord.saveNew(component2);
98-
99-
expect(componentActionRecord.getComponentByIndex(0)).toEqual(component1);
100-
expect(componentActionRecord.getComponentByIndex(1)).toEqual(component2);
95+
expect(componentActionRecord.getComponentByIndex(index1)).toBe(component1);
96+
expect(componentActionRecord.getComponentByIndex(index2)).toBe(component2);
97+
expect(componentActionRecord.getComponentByIndex(index3)).toBe(component3);
10198
});
10299

103100
it('should return undefined when passed an index that does not exist', () => {
104-
expect(componentActionRecord.getComponentByIndex(0)).toBeUndefined();
101+
expect(componentActionRecord.getComponentByIndex(3)).toBeUndefined();
105102
});
106103
});
107104

108105
describe('getComponentByIndexHooks', () => {
109106
it('should return the components at the specified indices', () => {
110-
componentActionRecord.saveNew(component1);
111-
componentActionRecord.saveNew(component2);
112-
componentActionRecord.saveNew(component3);
113-
114-
expect(componentActionRecord.getComponentByIndexHooks([0])).toEqual([component1]);
115-
expect(componentActionRecord.getComponentByIndexHooks([1, 2])).toEqual([
107+
expect(componentActionRecord.getComponentByIndexHooks([index1])).toContain(component1);
108+
expect(componentActionRecord.getComponentByIndexHooks([0, 1, 2])).toEqual([
109+
component1,
116110
component2,
117111
component3,
118112
]);
119113
});
120114

121115
it('should return undefined when passed an empty array', () => {
122-
expect(componentActionRecord.getComponentByIndexHooks()).toBeUndefined();
116+
expect(componentActionRecord.getComponentByIndexHooks([])).toEqual([]);
123117
});
124118

125119
it('should return undefined when passed an index that does not exist', () => {
126-
expect(componentActionRecord.getComponentByIndexHooks([0])).toBeUndefined();
120+
expect(componentActionRecord.getComponentByIndexHooks([3])).toEqual([]);
127121
});
128122
});
129123

130124
describe('getAllComponents', () => {
131125
it('should return all components in componentActionRecord', () => {
132-
componentActionRecord.saveNew(component1);
133-
componentActionRecord.saveNew(component2);
134-
componentActionRecord.saveNew(component3);
135-
136126
expect(componentActionRecord.getAllComponents()).toEqual([
137127
component1,
138128
component2,

src/backend/models/masterState.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
1-
/**
2-
* @type ComponentAction - an array of actions that can be performed on a component
3-
*/
4-
export type ComponentAction = {
5-
[url: string]: any[];
6-
};
7-
8-
export type ComponentActionRecord = ComponentAction[];
9-
101
// The HookState data structure is an array that holds the current value of a hook's state, as well as a dispatch function that is used to update that state.
112
// Information on these components include ComponentData as well as state
123
// For class components, there will be one "component" for each snapshot
134
// For functional components that utilize Hooks, there will be one "component"
5+
6+
import { index } from 'd3';
7+
148
// for each setter/getter every time we have a new snapshot
15-
let componentActionsRecord: ComponentActionRecord = [];
16-
// index keeps track of the current position in the array
17-
let index: number;
18-
index = 0;
9+
let componentActionsRecord = [];
1910

2011
export default {
2112
/**
2213
* @function clear - Clears componentActionsRecord
2314
*/
2415
clear: () => {
2516
componentActionsRecord = [];
26-
index = 0;
2717
},
2818

2919
/**
@@ -33,10 +23,6 @@ export default {
3323
*/
3424
saveNew: (component): number => {
3525
componentActionsRecord.push(component);
36-
// componentActionsRecord[index] = component;
37-
// index++;
38-
39-
// return index - 1;
4026
return componentActionsRecord.length - 1;
4127
},
4228
// ----------------------------CLASS COMPONENT--------------------------------
@@ -53,12 +39,13 @@ export default {
5339
* @param inputIndex - index of component inside `componentActionsRecord` coming from `timeJump.ts`
5440
* @returns - an array of objects containing the bound dispatch methods
5541
*/
56-
getComponentByIndexHooks: (inputIndex: Array<number> = []): any[] | undefined => {
57-
const validIndex = inputIndex.filter((index) => componentActionsRecord?.[index]);
58-
if (!validIndex.length) return undefined;
42+
getComponentByIndexHooks: (inputIndex: Array<number>): any[] =>
43+
inputIndex
44+
// filter for only index exists in componentActionsRecord
45+
.filter((index) => index in componentActionsRecord)
46+
// get the corresponding dispath method at position index
47+
.map((index) => componentActionsRecord[index]),
5948

60-
return validIndex.map((index) => componentActionsRecord[index]);
61-
},
6249
// ----------------------------------DEBUGGING--------------------------------
6350
/**
6451
* @function getAllComponents - This method is used for debugging purpose to access the array of setState/dispatch methods

0 commit comments

Comments
 (0)