Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 62dc35f

Browse files
author
george
committed
add dom api tests
1 parent f5ab70b commit 62dc35f

File tree

4 files changed

+214
-22
lines changed

4 files changed

+214
-22
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ module.exports = {
198198
"no-var": ERROR,
199199
"object-shorthand": [ERROR, ALWAYS],
200200
"prefer-const": ERROR,
201+
"prefer-spread": ERROR,
201202
"prefer-template": ERROR,
202203
"require-yield": ERROR,
203204

app/pages/Home/__tests__/markdownContent.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe("<StatusBadges />", () => {
1515
expect(wrapper).toMatchSnapshot()
1616
})
1717
})
18+
1819
describe("<InstallNpm />", () => {
1920
it("renders", () => {
2021
const wrapper = mount(<InstallNpm />)

src/js/__tests__/utils.spec.js

Lines changed: 208 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,222 @@ import ContextUtil, { dom, getFocusableElements } from "../utils"
22

33
const testDom = `<div data-tester="true" data-removable class="wrapper">
44
<p>Hello world! <a href="#">this link is focusable</a> </p>
5-
<p class="hello world test">Hello world again! <button type="button">I too, am focusable!</button></p>
5+
<p style="height: 32px;" class="hello world test">Hello world again! <button type="button">I too, am focusable!</button></p>
66
</div>\
77
`
88

99
describe("dom", () => {
10-
beforeEach(() => {
11-
document.body.innerHTML = testDom
10+
describe(".addClass(element, ...classes)", () => {
11+
it("can add a class", () => {
12+
// Given
13+
document.body.innerHTML = testDom
14+
const element = document.querySelector(".hello.world")
15+
const newClass = "test-class"
16+
// When
17+
dom.addClass(element, newClass)
18+
// Then
19+
const received = Object.values(element.classList)
20+
expect(received).toContainEqual(newClass)
21+
})
22+
23+
it("can add multiple classes", () => {
24+
// Given
25+
document.body.innerHTML = testDom
26+
const element = document.querySelector(".hello.world")
27+
const newClass1 = "test-class-1"
28+
const newClass2 = "test-class-2"
29+
// When
30+
dom.addClass(element, newClass1, newClass2)
31+
// Then
32+
const received = Object.values(element.classList)
33+
expect(received).toEqual(expect.arrayContaining([newClass1, newClass2]))
34+
})
1235
})
1336

14-
it("can get an attribute", () => {})
15-
it("can set an attribute", () => {})
16-
it("can remove an attribute", () => {})
17-
it("can detect an attribute", () => {})
18-
it("can retrieve an element", () => {})
19-
it("can retrieve a collection of elements", () => {})
20-
it("can retrieve css properties", () => {})
21-
it("can set css properties", () => {})
22-
it("can add a class", () => {})
23-
it("can add multiple classes", () => {})
24-
it("can remove a class from", () => {})
25-
it("can remove multiple classes from", () => {})
26-
it("can detect a class", () => {})
27-
it("can detect a class from multiple inputs", () => {})
37+
describe(".removeClass(element, ...classes)", () => {
38+
it("can remove a class from an element", () => {
39+
// Given
40+
document.body.innerHTML = testDom
41+
const element = document.querySelector(".hello.world")
42+
const removedClass = "test"
43+
// When
44+
dom.removeClass(element, removedClass)
45+
// Then
46+
const received = Object.values(element.classList)
47+
expect(received).not.toContainEqual(removedClass)
48+
})
49+
50+
it("can remove multiple classes from an element", () => {
51+
// Given
52+
document.body.innerHTML = testDom
53+
const element = document.querySelector(".hello.world")
54+
const newClass1 = "hello"
55+
const newClass2 = "world"
56+
// When
57+
dom.removeClass(element, newClass1, newClass2)
58+
// Then
59+
const received = Object.values(element.classList)
60+
expect(received).not.toEqual(expect.arrayContaining([newClass1, newClass2]))
61+
})
62+
})
63+
64+
describe(".hasClass(element, ...classes)", () => {
65+
it("returns false if class can't be found", () => {
66+
// Given
67+
document.body.innerHTML = testDom
68+
const element = document.querySelector(".hello.world")
69+
const classNameToDetect = "test-class"
70+
// When
71+
const received = dom.hasClass(element, classNameToDetect)
72+
// Then
73+
expect(received).toEqual(false)
74+
})
75+
76+
it("returns true if class can be found", () => {
77+
// Given
78+
document.body.innerHTML = testDom
79+
const element = document.querySelector(".hello.world")
80+
const classNameToDetect = "hello"
81+
// When
82+
const received = dom.hasClass(element, classNameToDetect)
83+
// Then
84+
expect(received).toEqual(true)
85+
})
86+
87+
it("returns false if class can't be found among given classes", () => {
88+
// Given
89+
document.body.innerHTML = testDom
90+
const element = document.querySelector(".hello.world")
91+
const classNameToDetect1 = "hello"
92+
const classNameToDetect2 = "not-in-class-list"
93+
// When
94+
const received = dom.hasClass(element, classNameToDetect1, classNameToDetect2)
95+
// Then
96+
expect(received).toEqual(true)
97+
})
98+
99+
it("returns true if class can be found among given classes", () => {
100+
// Given
101+
document.body.innerHTML = testDom
102+
const element = document.querySelector(".hello.world")
103+
const classNameToDetect1 = "hello"
104+
const classNameToDetect2 = "not-in-class-list"
105+
// When
106+
const received = dom.hasClass(element, classNameToDetect1, classNameToDetect2)
107+
// Then
108+
expect(received).toEqual(true)
109+
})
110+
})
111+
112+
describe(".getAttr(element, attr)", () => {
113+
it("returns an attribute", () => {
114+
// Given
115+
document.body.innerHTML = testDom
116+
const wrapper = document.querySelector(".wrapper")
117+
// When
118+
const received = dom.getAttr(wrapper, "data-tester")
119+
// Then
120+
expect(received).toEqual("true")
121+
})
122+
})
123+
124+
describe(".setAttr(element, attr, value)", () => {
125+
it("can set an attribute", () => {
126+
// Given
127+
document.body.innerHTML = testDom
128+
const wrapper = document.querySelector(".wrapper")
129+
// When
130+
dom.setAttr(wrapper, "data-tester", "false")
131+
// Then
132+
const received = wrapper.dataset.tester
133+
expect(received).toEqual("false")
134+
})
135+
})
136+
137+
describe(".removeAttr(element, attr)", () => {
138+
it("can remove an attribute", () => {
139+
// Given
140+
document.body.innerHTML = testDom
141+
const wrapper = document.querySelector(".wrapper")
142+
// When
143+
dom.removeAttr(wrapper, "data-tester")
144+
// Then
145+
const received = wrapper.dataset.tester
146+
expect(received).toEqual(undefined)
147+
})
148+
})
149+
150+
describe(".hasAttr(element, attr)", () => {
151+
it("can detect an attribute", () => {
152+
// Given
153+
document.body.innerHTML = testDom
154+
const wrapper = document.querySelector(".wrapper")
155+
// When
156+
const received = dom.hasAttr(wrapper, "data-tester")
157+
// Then
158+
expect(received).toEqual(true)
159+
})
160+
})
161+
162+
describe(".find(element, parent = document)", () => {
163+
it("returns an element", () => {
164+
// Given
165+
document.body.innerHTML = testDom
166+
const element = document.querySelector(".hello.world")
167+
const received = dom.find(".hello.world")
168+
// Then
169+
expect(received).toEqual(element)
170+
})
171+
})
172+
173+
describe(".findAll(element, parent = document)", () => {
174+
it("returns a collection of elements", () => {
175+
// Given
176+
document.body.innerHTML = testDom
177+
const received = dom.findAll("p")
178+
// Then
179+
expect(received).toHaveLength(2)
180+
})
181+
})
182+
183+
describe(".css(element, property, value = undefined)", () => {
184+
it("returns css property value", () => {
185+
// Given
186+
document.body.innerHTML = testDom
187+
const element = document.querySelector(".hello.world")
188+
// When
189+
const received = dom.css(element, "height")
190+
// Then
191+
expect(received).toEqual("32px")
192+
})
193+
194+
it("can set css property as a string", () => {
195+
// Given
196+
document.body.innerHTML = testDom
197+
const element = document.querySelector(".hello.world")
198+
const newHeight = "60px"
199+
// When
200+
dom.css(element, "height", newHeight)
201+
// Then
202+
const received = element.style.height
203+
expect(received).toEqual(newHeight)
204+
})
205+
206+
it("can set css property as null", () => {
207+
// Given
208+
document.body.innerHTML = testDom
209+
const element = document.querySelector(".hello.world")
210+
const newHeight = null
211+
// When
212+
dom.css(element, "height", newHeight)
213+
// Then
214+
expect(element.style.height).toEqual("")
215+
})
216+
})
28217
})
29218

30-
describe("getFocusableElements", () => {
31-
it("returns all focusable elements within a wrapper", () => {
219+
describe("getFocusableElements(container)", () => {
220+
it("returns all focusable elements within a given element", () => {
32221
document.body.innerHTML = testDom
33222
const elements = getFocusableElements(".wrapper")
34223
expect(elements).toHaveLength(2)

src/js/utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const dom = {
3131
hasAttr: (element, attr) => element.hasAttribute(attr),
3232

3333
find: (selector, parent = document) => parent.querySelector(selector),
34-
findAll: (selector, parent = document) => Array.apply(null, parent.querySelectorAll(selector)),
34+
findAll: (selector, parent = document) => [...parent.querySelectorAll(selector)],
3535

3636
css: (element, property, value) => {
3737
if (typeof value === "string" || value === null) {
@@ -45,10 +45,11 @@ export const dom = {
4545
removeClass: (element, ...classes) => element.classList.remove(...classes),
4646
hasClass: (element, ...classes) => {
4747
if (classes.length) {
48-
return classes.filter(cls => element.classList.contains(cls)).length
48+
return classes.filter(givenClassName => element.classList.contains(givenClassName)).length > 0
4949
}
5050

51-
return element.classList.contains(classes[0])
51+
const [givenClassName] = classes
52+
return element.classList.contains(givenClassName)
5253
},
5354
}
5455

0 commit comments

Comments
 (0)