|
1 | 1 | import Dropd from '../dist/index.cjs'
|
2 | 2 | import React, { Fragment } from 'react'
|
3 |
| -import { CLASSES, wait } from '../../helpers' |
| 3 | +import { CLASSES } from '../../helpers' |
4 | 4 | import { render, fireEvent, cleanup } from 'react-testing-library'
|
5 | 5 |
|
6 |
| -const list = ['January', 'February', 'March', 'April'] |
| 6 | +const monthList = ['January', 'February', 'March', 'April', 'May', 'June'] |
| 7 | + |
| 8 | +const mockDropd = props => { |
| 9 | + const { container, baseElement, getByTestId } = render( |
| 10 | + <Fragment> |
| 11 | + <input type="search" data-testid="input-mock" /> |
| 12 | + <Dropd list={monthList} {...props} /> |
| 13 | + </Fragment> |
| 14 | + ) |
| 15 | + |
| 16 | + const dropdElem = container.querySelector(CLASSES.container) |
| 17 | + const [list, button, placeholder, currentItem] = [ |
| 18 | + 'list', |
| 19 | + 'button', |
| 20 | + 'placeholder', |
| 21 | + 'currentItem', |
| 22 | + ].map(c => dropdElem.querySelector(CLASSES[c])) |
| 23 | + |
| 24 | + const inputMock = getByTestId('input-mock') |
| 25 | + const items = dropdElem.querySelectorAll(CLASSES.item) |
| 26 | + |
| 27 | + return { |
| 28 | + list, |
| 29 | + items, |
| 30 | + button, |
| 31 | + container, |
| 32 | + dropdElem, |
| 33 | + inputMock, |
| 34 | + placeholder, |
| 35 | + baseElement, |
| 36 | + currentItem, |
| 37 | + } |
| 38 | +} |
7 | 39 |
|
8 | 40 | describe('Dropd', () => {
|
9 | 41 | afterEach(cleanup)
|
10 | 42 |
|
11 | 43 | test('should not be open on mount', () => {
|
12 |
| - const { container } = render(<Dropd list={list} />) |
13 |
| - |
14 |
| - expect( |
15 |
| - container.querySelector(CLASSES.list).classList.contains('open') |
16 |
| - ).toBeFalsy() |
| 44 | + expect(mockDropd().list.classList.contains('open')).toBeFalsy() |
17 | 45 | })
|
18 | 46 |
|
19 | 47 | test('should contain the default placeholder', () => {
|
20 |
| - const { container } = render(<Dropd list={list} />) |
21 |
| - |
22 |
| - expect(container.querySelector(CLASSES.placeholder)).toBeTruthy() |
| 48 | + expect(mockDropd().placeholder).toBeTruthy() |
23 | 49 | })
|
24 | 50 |
|
25 | 51 | test('should render equal list items as the length of the list props', () => {
|
26 |
| - const { container } = render(<Dropd list={list} />) |
27 |
| - |
28 |
| - expect(container.querySelectorAll(CLASSES.item).length).toEqual(list.length) |
| 52 | + expect(mockDropd().items.length).toEqual(monthList.length) |
29 | 53 | })
|
30 | 54 |
|
31 | 55 | test('`currentItem` in state should be set to `value` prop on initial render', () => {
|
32 |
| - const { container } = render(<Dropd list={list} value={list[0]} />) |
33 |
| - |
34 |
| - expect(container.querySelector(CLASSES.currentItem).textContent).toEqual( |
35 |
| - list[0] |
| 56 | + expect(mockDropd({ value: monthList[0] }).currentItem.textContent).toEqual( |
| 57 | + monthList[0] |
36 | 58 | )
|
37 | 59 | })
|
38 | 60 |
|
39 | 61 | test('should be open on mount when the `defaultOpen` prop is set to true', () => {
|
40 |
| - const { container } = render(<Dropd list={list} defaultOpen={true} />) |
41 |
| - |
42 |
| - expect(container.querySelector(CLASSES.container).dataset.open).toBe('true') |
| 62 | + expect(mockDropd({ defaultOpen: true }).dropdElem.dataset.open).toBe('true') |
43 | 63 | })
|
44 | 64 |
|
45 | 65 | test('should close on click away when `closeOnBlur` prop is set to true', () => {
|
46 |
| - const { container, baseElement, getByTestId } = render( |
47 |
| - <Fragment> |
48 |
| - <input type="search" data-testid="input-mock" /> |
49 |
| - <Dropd placeholder="Choose" list={list} closeOnBlur={true} /> |
50 |
| - </Fragment> |
51 |
| - ) |
52 |
| - const dropdContainer = container.querySelector(CLASSES.container) |
53 |
| - const dropdList = dropdContainer.querySelector(CLASSES.list) |
54 |
| - |
55 |
| - fireEvent.mouseDown(dropdContainer.querySelector(CLASSES.button)) |
56 |
| - fireEvent.focus(getByTestId('input-mock')) |
57 |
| - wait().then(() => { |
58 |
| - expect(document.activeElement).toBe(getByTestId('input-mock')) |
59 |
| - expect(dropdList.classList.contains('open')).toBeFalsy() |
60 |
| - }) |
| 66 | + const { button, baseElement, list } = mockDropd({ closeOnBlur: true }) |
61 | 67 |
|
62 |
| - fireEvent.mouseDown(dropdContainer.querySelector(CLASSES.button)) |
| 68 | + fireEvent.mouseDown(button) |
63 | 69 | fireEvent.mouseDown(baseElement)
|
64 |
| - expect(dropdList.classList.contains('open')).toBeFalsy() |
| 70 | + expect(list.classList.contains('open')).toBeFalsy() |
65 | 71 | })
|
66 | 72 |
|
67 |
| - test('should not close on click away when `closeOnBlur` prop is set to false', () => { |
68 |
| - const { container, baseElement, getByTestId } = render( |
69 |
| - <Fragment> |
70 |
| - <input type="search" data-testid="input-mock" /> |
71 |
| - <Dropd placeholder="Choose" list={list} closeOnBlur={false} /> |
72 |
| - </Fragment> |
73 |
| - ) |
74 |
| - const dropdContainer = container.querySelector(CLASSES.container) |
75 |
| - const dropdList = dropdContainer.querySelector(CLASSES.list) |
76 |
| - |
77 |
| - fireEvent.mouseDown(dropdContainer.querySelector(CLASSES.button)) |
78 |
| - fireEvent.focus(getByTestId('input-mock')) |
79 |
| - wait().then(() => { |
80 |
| - expect(document.activeElement).toBe(getByTestId('input-mock')) |
81 |
| - expect(dropdList.classList.contains('open')).toBeTruthy() |
| 73 | + test('should close on input mousedown when `closeOnBlur` prop is set to true', () => { |
| 74 | + const { button, inputMock, baseElement, list } = mockDropd({ |
| 75 | + closeOnBlur: true, |
82 | 76 | })
|
83 | 77 |
|
| 78 | + fireEvent.mouseDown(button) |
| 79 | + fireEvent.mouseDown(inputMock) |
| 80 | + expect(document.activeElement).toBe(baseElement) |
| 81 | + expect(list.classList.contains('open')).toBeFalsy() |
| 82 | + }) |
| 83 | + |
| 84 | + test('should not close on click away when `closeOnBlur` prop is set to false', () => { |
| 85 | + const { button, baseElement, list } = mockDropd({ closeOnBlur: false }) |
| 86 | + |
| 87 | + fireEvent.mouseDown(button) |
84 | 88 | fireEvent.mouseDown(baseElement)
|
85 |
| - expect(dropdList.classList.contains('open')).toBeTruthy() |
| 89 | + expect(list.classList.contains('open')).toBeTruthy() |
| 90 | + }) |
| 91 | + |
| 92 | + test('should not close on input mousedown when `closeOnBlur` prop is set to false', () => { |
| 93 | + const { button, baseElement, list, inputMock } = mockDropd({ |
| 94 | + closeOnBlur: false, |
| 95 | + }) |
| 96 | + |
| 97 | + fireEvent.mouseDown(button) |
| 98 | + fireEvent.mouseDown(inputMock) |
| 99 | + expect(document.activeElement).toBe(baseElement) |
| 100 | + expect(list.classList.contains('open')).toBeTruthy() |
86 | 101 | })
|
87 | 102 |
|
88 | 103 | test('should call the `onOpen` function when it is passed', () => {
|
89 | 104 | const mockFn = jest.fn()
|
90 |
| - const { container } = render(<Dropd list={list} onOpen={mockFn} />) |
91 | 105 |
|
92 |
| - fireEvent.mouseDown(container.querySelector(CLASSES.button)) |
| 106 | + fireEvent.mouseDown(mockDropd({ onOpen: mockFn }).button) |
93 | 107 | expect(mockFn).toHaveBeenCalledTimes(1)
|
94 | 108 | })
|
95 | 109 |
|
96 | 110 | test('should call the `onItemChange` function when it is passed', () => {
|
97 | 111 | const mockFn = jest.fn()
|
98 |
| - const { container } = render(<Dropd list={list} onItemChange={mockFn} />) |
99 | 112 |
|
100 |
| - fireEvent.mouseDown(container.querySelector(CLASSES.item)) |
| 113 | + fireEvent.mouseDown(mockDropd({ onItemChange: mockFn }).items[0]) |
101 | 114 | expect(mockFn).toHaveBeenCalledTimes(1)
|
102 | 115 | })
|
103 | 116 | })
|
0 commit comments