-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path02-solution.test.js
More file actions
75 lines (59 loc) · 2.21 KB
/
Copy path02-solution.test.js
File metadata and controls
75 lines (59 loc) · 2.21 KB
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
import React from 'react'
import { render, unmountComponentAtNode } from 'react-dom'
import { act } from "react-dom/test-utils"
import Main, { validateColor } from './02-solution'
describe('validateColor', () => {
test('some colors', () => {
expect(validateColor('')).toBeFalsy()
expect(validateColor('orang')).toBeFalsy()
expect(validateColor('abc')).toBeFalsy()
expect(validateColor('orange')).toBeTruthy()
expect(validateColor('green')).toBeTruthy()
expect(validateColor('#abc')).toBeTruthy()
})
})
// input.value = "green" does not work in React 16, see https://stackoverflow.com/a/46012210/1176601
const inputValueSetter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value").set;
const modify = (input, value) => {
inputValueSetter.call(input, value);
input.dispatchEvent(new Event('input', { bubbles: true }))
}
describe('Main', () => {
let div
beforeEach(() => {
div = document.createElement('div')
document.body.appendChild(div)
})
afterEach(() => {
unmountComponentAtNode(div)
div.remove()
})
test('default color', () => {
act(() => { render(<Main />, div) })
const box = div.querySelector('.Main-box')
expect(box.style.backgroundColor).toBe('orange')
expect(box.textContent).toBe('orange')
});
test('modified color', () => {
act(() => { render(<Main />, div) })
const input = div.querySelector('input')
const box = div.querySelector('.Main-box')
expect(input).toHaveProperty('value', '')
act(() => { modify(input, 'gree') })
expect(input).toHaveProperty('value', 'gree')
expect(box.style.backgroundColor).toBe('orange')
expect(box.textContent).toBe('orange')
act(() => { modify(input, 'green') })
expect(input).toHaveProperty('value', 'green')
expect(box.style.backgroundColor).toBe('green')
expect(box.textContent).toBe('green')
act(() => { modify(input, 'g') })
expect(input).toHaveProperty('value', 'g')
expect(box.style.backgroundColor).toBe('green')
expect(box.textContent).toBe('green')
act(() => { modify(input, '') })
expect(input).toHaveProperty('value', '')
expect(box.style.backgroundColor).toBe('orange')
expect(box.textContent).toBe('orange')
})
})