|
| 1 | +import React from 'react' |
| 2 | +import { render, unmountComponentAtNode } from 'react-dom' |
| 3 | +import { act } from "react-dom/test-utils" |
| 4 | +import Main, { validateColor, useColor } from './03-exercise' |
| 5 | + |
| 6 | +describe('useColor', () => { |
| 7 | + let div |
| 8 | + beforeEach(() => { |
| 9 | + div = document.createElement('div') |
| 10 | + document.body.appendChild(div) |
| 11 | + }) |
| 12 | + afterEach(() => { |
| 13 | + unmountComponentAtNode(div) |
| 14 | + div.remove() |
| 15 | + }) |
| 16 | + |
| 17 | + test('default value', () => { }) |
| 18 | + test('modify value', () => { }) |
| 19 | +}) |
| 20 | + |
| 21 | +describe('validateColor', () => { |
| 22 | + test('some colors', () => { |
| 23 | + expect(validateColor('')).toBeFalsy() |
| 24 | + expect(validateColor('orang')).toBeFalsy() |
| 25 | + expect(validateColor('abc')).toBeFalsy() |
| 26 | + |
| 27 | + expect(validateColor('orange')).toBeTruthy() |
| 28 | + expect(validateColor('green')).toBeTruthy() |
| 29 | + expect(validateColor('#abc')).toBeTruthy() |
| 30 | + }) |
| 31 | +}) |
| 32 | + |
| 33 | +// input.value = "green" does not work in React 16, see https://stackoverflow.com/a/46012210/1176601 |
| 34 | +const inputValueSetter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value").set; |
| 35 | +const modify = (input, value) => { |
| 36 | + inputValueSetter.call(input, value); |
| 37 | + input.dispatchEvent(new Event('input', { bubbles: true })) |
| 38 | +} |
| 39 | + |
| 40 | +describe('Main', () => { |
| 41 | + let div |
| 42 | + beforeEach(() => { |
| 43 | + div = document.createElement('div') |
| 44 | + document.body.appendChild(div) |
| 45 | + }) |
| 46 | + afterEach(() => { |
| 47 | + unmountComponentAtNode(div) |
| 48 | + div.remove() |
| 49 | + }) |
| 50 | + |
| 51 | + test('default color', () => { |
| 52 | + act(() => { render(<Main />, div) }) |
| 53 | + |
| 54 | + const box = div.querySelector('.Main-box') |
| 55 | + expect(box.style.backgroundColor).toBe('orange') |
| 56 | + expect(box.textContent).toBe('orange') |
| 57 | + }); |
| 58 | + |
| 59 | + test('modified color', () => { |
| 60 | + act(() => { render(<Main />, div) }) |
| 61 | + |
| 62 | + const input = div.querySelector('input') |
| 63 | + const box = div.querySelector('.Main-box') |
| 64 | + expect(input).toHaveProperty('value', '') |
| 65 | + |
| 66 | + act(() => { modify(input, 'gree') }) |
| 67 | + |
| 68 | + expect(input).toHaveProperty('value', 'gree') |
| 69 | + expect(box.style.backgroundColor).toBe('orange') |
| 70 | + expect(box.textContent).toBe('orange') |
| 71 | + |
| 72 | + act(() => { modify(input, 'green') }) |
| 73 | + |
| 74 | + expect(input).toHaveProperty('value', 'green') |
| 75 | + expect(box.style.backgroundColor).toBe('green') |
| 76 | + expect(box.textContent).toBe('green') |
| 77 | + |
| 78 | + act(() => { modify(input, 'g') }) |
| 79 | + |
| 80 | + expect(input).toHaveProperty('value', 'g') |
| 81 | + expect(box.style.backgroundColor).toBe('green') |
| 82 | + expect(box.textContent).toBe('green') |
| 83 | + |
| 84 | + act(() => { modify(input, '') }) |
| 85 | + |
| 86 | + expect(input).toHaveProperty('value', '') |
| 87 | + expect(box.style.backgroundColor).toBe('orange') |
| 88 | + expect(box.textContent).toBe('orange') |
| 89 | + }) |
| 90 | +}) |
0 commit comments