forked from jestjs/jest
/
DOMCollection.test.ts
155 lines (132 loc) Β· 4.04 KB
/
DOMCollection.test.ts
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @jest-environment jsdom
*/
/* eslint-env browser*/
import prettyFormat from '../';
import setPrettyPrint from './setPrettyPrint';
const {DOMCollection, DOMElement} = prettyFormat.plugins;
setPrettyPrint([DOMCollection, DOMElement]);
describe('DOMCollection plugin for object properties', () => {
it('supports DOMStringMap', () => {
const el = document.createElement('div');
el.dataset.foo = 'bar';
expect(el.dataset).toPrettyPrintTo('DOMStringMap {\n "foo": "bar",\n}');
});
it('supports NamedNodeMap', () => {
const el = document.createElement('div');
el.setAttribute('foo', 'bar');
expect(el.attributes).toPrettyPrintTo('NamedNodeMap {\n "foo": "bar",\n}');
});
it('supports config.min option', () => {
const el = document.createElement('div');
el.setAttribute('name1', 'value1');
el.setAttribute('name2', 'value2');
expect(el.attributes).toPrettyPrintTo(
'{"name1": "value1", "name2": "value2"}',
{min: true},
);
});
});
describe('DOMCollection plugin for list items', () => {
const select = document.createElement('select');
select.innerHTML = [
'<option value="1">one</option>',
'<option value="2">two</option>',
'<option value="3">three</option>',
].join('');
const form = document.createElement('form');
form.appendChild(select);
const expectedOption1 = [
' <option',
' value="1"',
' >',
' one',
' </option>,', // comma because item
].join('\n');
const expectedOption2 = [
' <option',
' value="2"',
' >',
' two',
' </option>,', // comma because item
].join('\n');
const expectedOption3 = [
' <option',
' value="3"',
' >',
' three',
' </option>,', // comma because item
].join('\n');
const expectedHTMLCollection = [
'HTMLCollection [',
expectedOption1,
expectedOption2,
expectedOption3,
']',
].join('\n');
it('supports HTMLCollection for getElementsByTagName', () => {
const options = form.getElementsByTagName('option');
expect(options).toPrettyPrintTo(expectedHTMLCollection);
});
it('supports HTMLCollection for children', () => {
expect(select.children).toPrettyPrintTo(expectedHTMLCollection);
});
it('supports config.maxDepth option', () => {
expect(select.children).toPrettyPrintTo('[HTMLCollection]', {maxDepth: 0});
});
const expectedNodeList = [
'NodeList [',
expectedOption1,
expectedOption2,
expectedOption3,
']',
].join('\n');
it('supports NodeList for querySelectorAll', () => {
const options = form.querySelectorAll('option');
expect(options).toPrettyPrintTo(expectedNodeList);
});
it('supports NodeList for childNodes', () => {
expect(select.childNodes).toPrettyPrintTo(expectedNodeList);
});
const expectedHTMLOptionsCollection = [
'HTMLOptionsCollection [',
expectedOption1,
expectedOption2,
expectedOption3,
']',
].join('\n');
it('supports HTMLOptionsCollection for select options', () => {
expect(select.options).toPrettyPrintTo(expectedHTMLOptionsCollection);
});
// When Jest upgrades to a version of jsdom later than 12.2.0,
// the class name might become HTMLFormControlsCollection
const expectedHTMLFormControlsCollection = [
'HTMLCollection [',
' <select>',
' <option',
' value="1"',
' >',
' one',
' </option>', // no comma because element
' <option',
' value="2"',
' >',
' two',
' </option>', // no comma because element
' <option',
' value="3"',
' >',
' three',
' </option>', // no comma because element
' </select>,', // comma because item
']',
].join('\n');
it('supports HTMLCollection for form elements', () => {
expect(form.elements).toPrettyPrintTo(expectedHTMLFormControlsCollection);
});
});