This repository has been archived by the owner on May 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
jsonToHtmlTable-test.js
100 lines (86 loc) · 3.48 KB
/
jsonToHtmlTable-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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import JsonToHtmlTableEsModule from './jsonToHtmlTable';
let tableDom;
let tempValue;
beforeEach(() => {
tableDom = document.createElement('table');
tempValue = 0;
});
describe('object test', () => {
it('should be a table', () => {
const json = { item1: 'A', item2: 1, item3: null, item4: undefined };
JsonToHtmlTableEsModule(tableDom, json);
const tbody = tableDom.childNodes[0];
expect(tbody.childNodes.length).toBe(4);
const tr0 = tbody.childNodes[0];
expect(tr0.childNodes.length).toBe(2);
expect(tr0.childNodes[0].textContent).toBe('item1');
expect(tr0.childNodes[1].textContent).toBe('A');
const tr1 = tbody.childNodes[1];
expect(tr1.childNodes.length).toBe(2);
expect(tr1.childNodes[0].textContent).toBe('item2');
expect(tr1.childNodes[1].textContent).toBe('1');
const tr2 = tbody.childNodes[2];
expect(tr2.childNodes.length).toBe(1);
expect(tr2.childNodes[0].textContent).toBe('item3');
const tr3 = tbody.childNodes[3];
expect(tr3.childNodes.length).toBe(2);
expect(tr3.childNodes[0].textContent).toBe('item4');
expect(tr3.childNodes[1].textContent).toBe('undefined');
});
it('should process string', () => {
const json = JSON.stringify({ item1: 'A', item2: 'B' });
JsonToHtmlTableEsModule(tableDom, json);
const tbody = tableDom.childNodes[0];
expect(tbody.childNodes.length).toBe(2);
const tr0 = tbody.childNodes[0];
const tr1 = tbody.childNodes[1];
expect(tr0.childNodes.length).toBe(2);
expect(tr0.childNodes[0].textContent).toBe('item1');
expect(tr0.childNodes[1].textContent).toBe('A');
expect(tr1.childNodes.length).toBe(2);
expect(tr1.childNodes[0].textContent).toBe('item2');
expect(tr1.childNodes[1].textContent).toBe('B');
});
it('should call callback', () => {
JsonToHtmlTableEsModule(tableDom, {}, () => (tempValue = 10));
expect(tempValue).toBe(10);
});
it('should throw error when using dom element which is not a table', () => {
let divDom = document.createElement('div');
expect(() => JsonToHtmlTableEsModule(divDom, {})).toThrow();
});
});
describe('array test', () => {
it('should a table', () => {
const json = ['A', 'B'];
JsonToHtmlTableEsModule(tableDom, json);
const td = tableDom.childNodes[0].childNodes[0].childNodes[0];
expect(td.childNodes.length).toBe(2);
const table0 = td.childNodes[0];
const text0 = table0.childNodes[0].childNodes[0].childNodes[0].textContent;
expect(text0).toBe('A');
const table1 = td.childNodes[1];
const text1 = table1.childNodes[0].childNodes[0].childNodes[0].textContent;
expect(text1).toBe('B');
});
it('should process string', () => {
const json = JSON.stringify(['A', 'B']);
JsonToHtmlTableEsModule(tableDom, json);
const td = tableDom.childNodes[0].childNodes[0].childNodes[0];
expect(td.childNodes.length).toBe(2);
const table0 = td.childNodes[0];
const text0 = table0.childNodes[0].childNodes[0].childNodes[0].textContent;
expect(text0).toBe('A');
const table1 = td.childNodes[1];
const text1 = table1.childNodes[0].childNodes[0].childNodes[0].textContent;
expect(text1).toBe('B');
});
it('should call callback', () => {
JsonToHtmlTableEsModule(tableDom, [], () => (tempValue = 10));
expect(tempValue).toBe(10);
});
it('should throw error when using dom element which is not a table', () => {
let divDom = document.createElement('div');
expect(() => JsonToHtmlTableEsModule(divDom, [])).toThrow();
});
});