|
1 | 1 | import Tree from '../models/tree';
|
2 |
| -import { serializeState, scrubUnserializableMembers } from '../models/tree'; |
3 |
| - |
4 |
| -xdescribe('Serialize state unit test', () => { |
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); |
| 2 | +import { serializeState } from '../models/tree'; |
20 | 3 |
|
| 4 | +describe('Serialize state unit test', () => { |
21 | 5 | it('should create a deep copy of state', () => {
|
| 6 | + const dummyState = { |
| 7 | + counter: 1, |
| 8 | + playerOne: 'X', |
| 9 | + board: [ |
| 10 | + ['', 'O', 'X'], |
| 11 | + ['', 'O', 'X'], |
| 12 | + ['O', 'X', ''], |
| 13 | + ], |
| 14 | + }; |
| 15 | + const serializedState = serializeState(dummyState); |
22 | 16 | expect(dummyState).toEqual(serializedState);
|
23 | 17 | expect(dummyState).not.toBe(serializedState);
|
24 | 18 | });
|
25 | 19 |
|
26 | 20 | it('should detect circular state', () => {
|
| 21 | + const circularState: { [key: string]: any } = {}; |
| 22 | + circularState.circ = circularState; |
| 23 | + const serializedCircularState = serializeState(circularState); |
27 | 24 | expect(serializedCircularState).toEqual('circularState');
|
28 | 25 | });
|
29 | 26 | });
|
30 | 27 |
|
31 |
| -xdescribe('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', () => { |
54 |
| - const newTree = new Tree({}); |
55 |
| - describe('Constructor', () => { |
| 28 | +describe('Tree unit test', () => { |
| 29 | + |
| 30 | + describe('Constructing a default tree', () => { |
| 31 | + const newTree: Tree = new Tree({}); |
56 | 32 | it('should be able to create a newTree', () => {
|
| 33 | + expect(newTree).toBeInstanceOf(Tree); |
57 | 34 | expect(newTree.state).toEqual({});
|
58 | 35 | });
|
59 | 36 |
|
60 |
| - it('should have 7 properties', () => { |
| 37 | + it('should have 6 properties', () => { |
61 | 38 | expect(newTree).toHaveProperty('state');
|
62 | 39 | expect(newTree).toHaveProperty('name');
|
63 | 40 | expect(newTree).toHaveProperty('componentData');
|
64 | 41 | expect(newTree).toHaveProperty('children');
|
65 |
| - expect(newTree).toHaveProperty('parent'); |
66 | 42 | expect(newTree).toHaveProperty('isExpanded');
|
67 | 43 | expect(newTree).toHaveProperty('rtid');
|
68 | 44 | });
|
69 | 45 |
|
70 |
| - it('has name default value as stateless', () => { |
| 46 | + it('should have default name, componentData, isExpanded and rtid', () => { |
71 | 47 | expect(newTree.name).toBe('nameless');
|
72 |
| - }); |
73 |
| - }); |
74 |
| - |
75 |
| - xdescribe('Adding children', () => { |
76 |
| - const returnChild: Tree = newTree.addChild('stateful', 'child', {}, null); |
77 |
| - |
78 |
| - it("should have the child be in the children's array property", () => { |
79 |
| - // check if returnChild is in the children array property of tree that invoked addChild |
80 |
| - expect(newTree.children).toContain(returnChild); |
| 48 | + expect(newTree.componentData).toEqual({}); |
| 49 | + expect(newTree.isExpanded).toBe(true); |
| 50 | + expect(newTree.rtid).toBe(null); |
81 | 51 | });
|
82 | 52 |
|
83 |
| - it("should have the object that invoked it be it's parent", () => { |
84 |
| - // checking parent to be the tree that invoked addChild |
85 |
| - expect(returnChild.parent).toEqual(newTree); |
| 53 | + it('should have no children', () => { |
| 54 | + expect(newTree.children).toHaveLength(0); |
86 | 55 | });
|
| 56 | + }); |
87 | 57 |
|
88 |
| - it('parent now contains an array of children and each children is a valid tree', () => { |
89 |
| - expect(newTree.children[0]).toHaveProperty('state'); |
90 |
| - expect(newTree.children[0]).toHaveProperty('name'); |
91 |
| - expect(newTree.children[0]).toHaveProperty('componentData'); |
| 58 | + describe('Constructing a tree root', () => { |
| 59 | + const root: Tree = new Tree('root', 'root'); |
| 60 | + it("should have name as 'root' and state as 'root'", () => { |
| 61 | + expect(root).toBeInstanceOf(Tree); |
| 62 | + expect(root.state).toBe('root'); |
| 63 | + expect(root.name).toBe('root'); |
| 64 | + expect(root.componentData).toEqual({}); |
| 65 | + expect(root.isExpanded).toBe(true); |
| 66 | + expect(root.rtid).toBe(null); |
| 67 | + expect(root.children).toHaveLength(0); |
92 | 68 | });
|
93 | 69 | });
|
94 | 70 |
|
95 |
| - xdescribe('Adding sibling', () => { |
96 |
| - const newTreeCopy = new Tree({}); |
97 |
| - const returnChild = newTreeCopy.addChild('stateful', 'child', {}, null); |
98 |
| - const returnSibling = returnChild.addSibling('stateful', 'child', {}, null); |
99 |
| - |
100 |
| - it('the tree now has 2 children', () => { |
101 |
| - expect(newTreeCopy.children.length).toBe(2); |
| 71 | + describe('Adding children', () => { |
| 72 | + const newTree: Tree = new Tree('root', 'root'); |
| 73 | + const child: Tree = newTree.addChild('stateful', 'child', {}, null); |
| 74 | + |
| 75 | + it('should return a new tree child', () => { |
| 76 | + expect(child).toBeInstanceOf(Tree); |
| 77 | + expect(child).toBeInstanceOf(Tree); |
| 78 | + expect(child.state).toBe('stateful'); |
| 79 | + expect(child.name).toBe('child'); |
| 80 | + expect(child.componentData).toEqual({}); |
| 81 | + expect(child.isExpanded).toBe(true); |
| 82 | + expect(child.rtid).toBe(null); |
| 83 | + expect(child.children).toHaveLength(0); |
102 | 84 | });
|
103 | 85 |
|
104 |
| - it('both of the children has the parent as this tree', () => { |
105 |
| - expect(returnChild.parent).toEqual(newTreeCopy); |
106 |
| - expect(returnSibling.parent).toEqual(newTreeCopy); |
| 86 | + it("should have the child be in the children's array property", () => { |
| 87 | + expect(newTree.children).toHaveLength(1); |
| 88 | + expect(newTree.children).toContain(child); |
| 89 | + expect(newTree.children[0]).toBe(child); |
107 | 90 | });
|
108 |
| - }); |
109 |
| - |
110 |
| - // TO DO- add serializing state tests |
111 |
| - xdescribe('Serializing state unit test', () => {}); |
112 |
| - // review this test |
113 |
| - // returnSibling not used? |
114 |
| - // Check Test |
115 | 91 |
|
116 |
| - xdescribe('Copy & clean tree', () => { |
117 |
| - const newTreeLastCopy = new Tree({}); |
118 |
| - const returnChild = newTreeLastCopy.addChild('stateful', 'child', {}, null); |
119 |
| - returnChild.addSibling('stateful', 'child', {}, null); |
120 |
| - const copy = newTreeLastCopy.cleanTreeCopy(); |
121 |
| - it('its copy has 2 children', () => { |
122 |
| - expect(copy.children.length).toEqual(2); |
| 92 | + it('should have unique name', () => { |
| 93 | + const nextChild1: Tree = child.addChild('stateful', 'child', {}, null); |
| 94 | + const nextChild2: Tree = child.addChild('stateful', 'child', {}, null); |
| 95 | + expect(child.children).toHaveLength(2); |
| 96 | + expect(child.children[0]).toBe(nextChild1); |
| 97 | + expect(child.children[1]).toBe(nextChild2); |
| 98 | + expect(nextChild1.name).toBe('child1'); |
| 99 | + expect(nextChild2.name).toBe('child2'); |
123 | 100 | });
|
124 | 101 | });
|
125 | 102 | });
|
0 commit comments