Skip to content

Commit 6c5a996

Browse files
Zachary FreemanZachary Freeman
authored andcommitted
added unit tests for serialization on Tree class
1 parent 7c1d754 commit 6c5a996

File tree

3 files changed

+9397
-9374
lines changed

3 files changed

+9397
-9374
lines changed

src/backend/__tests__/tree.test.ts

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,56 @@
11
import Tree from '../tree';
22
import { serializeState, scrubUnserializableMembers } from '../tree';
33

4-
/**
5-
* Created new tree under sibling and copy and clean tree describe block --
6-
* Reason is because other tests are adding properties to tree and affecting the child block,
7-
* so this was a quick way to test the trees getting reset to initial state
8-
*
9-
* Possible fix if more time allowed: Making use of beforeEach or afterEach --
10-
*/
11-
124
describe('Serialize state unit test', () => {
13-
const dummyState = { counter: 1, playerOne: 'X' };
5+
const dummyState = {
6+
counter: 1,
7+
playerOne: 'X',
8+
board: [
9+
['', 'O', 'X'],
10+
['', 'O', 'X'],
11+
['O', 'X', ''],
12+
],
13+
};
14+
15+
const circularState: { [key: string]: any } = {};
16+
circularState.circ = circularState;
17+
18+
const serializedState = serializeState(dummyState);
19+
const serializedCircularState = serializeState(circularState);
20+
21+
it('should create a deep copy of state', () => {
22+
expect(dummyState).toEqual(serializedState);
23+
expect(dummyState).not.toBe(serializedState);
24+
});
25+
26+
it('should detect circular state', () => {
27+
expect(serializedCircularState).toEqual('circularState');
28+
});
1429
});
1530

16-
describe('Tree unit test', () => {
31+
describe('Scrub unserialized members unit test', () => {
32+
const dummyState = {
33+
counter: 1,
34+
playerOne: 'X',
35+
board: [
36+
['', 'O', 'X'],
37+
['', 'O', 'X'],
38+
['O', 'X', ''],
39+
],
40+
increment: function () {
41+
this.counter++;
42+
},
43+
};
44+
const newTree = new Tree(dummyState);
45+
const scrubbedTree = scrubUnserializableMembers(newTree);
46+
// make sure return type is tree
47+
it('should be instance of tree', () => {
48+
expect(newTree).toBeInstanceOf(Tree);
49+
});
50+
// make sure function is scrubbed
51+
});
52+
53+
xdescribe('Tree unit test', () => {
1754
const newTree = new Tree({});
1855
describe('Constructor', () => {
1956
it('should be able to create a newTree', () => {
@@ -35,20 +72,6 @@ describe('Tree unit test', () => {
3572
});
3673
});
3774

38-
/**
39-
*
40-
* making sure to adhere to ts practices when goign through tests
41-
*
42-
* ^^
43-
* the tree should have initial values of state,
44-
* name, etc to be default as per newly created tree
45-
* update the add child and add sibling tests
46-
*
47-
* update the clean tree copy test to make it test for deep equaltiy? (note:
48-
* this test may always fail if we make it so because there is no way to have deep equalituy
49-
* with some shit that isn't allowed)
50-
*/
51-
5275
describe('Adding children', () => {
5376
const returnChild = newTree.addChild('stateful', 'child', {}, null);
5477

src/backend/tree.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
/* eslint-disable no-param-reassign */
88

99
let copyInstances = 0; // Tells you if we have already made a copy of current tree??
10-
const circularComponentTable = new Set<Tree>(); // Keeps track of the nodes added to the tree and allows you make sure there isnt circular state
10+
const circularComponentTable = new Set<Tree>(); // Keeps track of the nodes added to the tree
1111
let componentNames = {}; // {componentName: frequency of use} => component name as a key and it's frequency of use as its value
1212

1313
// Functions dont serialize properly so we need to scrub for that
14-
function scrubUnserializableMembers(tree: Tree): Tree {
14+
export function scrubUnserializableMembers(tree: Tree): Tree {
1515
Object.entries(tree.state).forEach((keyValuePair) => {
1616
if (typeof keyValuePair[1] === 'function') tree.state[keyValuePair[0]] = 'function';
1717
});
1818
return tree;
1919
}
2020

2121
// Making a deep clone of state becuase we want to make a copy
22-
function serializeState(state) {
22+
export function serializeState(state) {
2323
try {
24-
// makes a deep clone, but this way can be very slow
24+
// makes a deep clone
2525
return JSON.parse(JSON.stringify(state));
2626
} catch (e) {
2727
// if there is an error, that means there is circular state i.e state that depends on itself

0 commit comments

Comments
 (0)