|
25 | 25 | */ |
26 | 26 |
|
27 | 27 | import '@testing-library/jest-dom/extend-expect' |
28 | | -// import { fireEvent } from '@testing-library/react' |
29 | | -import React from 'react' |
30 | | -import { Simulate } from 'react-dom/test-utils' |
| 28 | +import { fireEvent, screen } from '@testing-library/react' |
| 29 | +import React, { useState } from 'react' |
31 | 30 |
|
32 | 31 | import { renderWithTheme } from '@looker/components-test-utils' |
33 | | -import { theme } from '@looker/design-tokens' |
34 | 32 | import { ButtonGroup } from './ButtonGroup' |
35 | 33 | import { ButtonItem } from './ButtonItem' |
36 | 34 |
|
37 | | -const requiredProps = { |
38 | | - message: 'Foo', |
39 | | - onConfirm: jest.fn(), |
40 | | - title: 'Bar', |
41 | | -} |
42 | | - |
43 | | -const optionalProps = { |
44 | | - cancelLabel: 'Dont Delete', |
45 | | - confirmLabel: 'Delete', |
46 | | - message: 'This is permanent', |
47 | | - onCancel: jest.fn(), |
48 | | - title: 'Delete the thing?', |
49 | | -} |
50 | | - |
51 | | -afterEach(() => { |
52 | | - requiredProps.onConfirm.mockClear() |
53 | | - optionalProps.onCancel.mockClear() |
54 | | -}) |
55 | | - |
56 | | -test('<ButtonGroup/> uncontrolled', () => { |
57 | | - const onChangeMock = jest.fn() |
58 | | - const { container, getByLabelText, getByText } = renderWithTheme( |
59 | | - <form> |
60 | | - <ButtonGroup name="fruits" label="Fruits"> |
61 | | - <ButtonItem selected>Apples</ButtonItem> |
62 | | - <ButtonItem onChange={onChangeMock}>Oranges</ButtonItem> |
63 | | - <ButtonItem>Bananas</ButtonItem> |
| 35 | +describe('ButtonGroup', () => { |
| 36 | + test('controlled', () => { |
| 37 | + function TestComponent() { |
| 38 | + const [value, setValue] = useState(['Bananas']) |
| 39 | + return ( |
| 40 | + <ButtonGroup value={value} onChange={setValue}> |
| 41 | + <ButtonItem>Apples</ButtonItem> |
| 42 | + <ButtonItem>Oranges</ButtonItem> |
| 43 | + <ButtonItem>Bananas</ButtonItem> |
| 44 | + </ButtonGroup> |
| 45 | + ) |
| 46 | + } |
| 47 | + renderWithTheme(<TestComponent />) |
| 48 | + |
| 49 | + expect(screen.getByRole('group')).toMatchSnapshot() |
| 50 | + |
| 51 | + const apples = screen.getByText('Apples') |
| 52 | + const bananas = screen.getByText('Bananas') |
| 53 | + const oranges = screen.getByText('Oranges') |
| 54 | + |
| 55 | + expect(apples).toHaveAttribute('aria-pressed', 'false') |
| 56 | + expect(bananas).toHaveAttribute('aria-pressed', 'true') |
| 57 | + expect(oranges).toHaveAttribute('aria-pressed', 'false') |
| 58 | + |
| 59 | + fireEvent.click(apples) |
| 60 | + expect(apples).toHaveAttribute('aria-pressed', 'true') |
| 61 | + expect(bananas).toHaveAttribute('aria-pressed', 'true') |
| 62 | + expect(oranges).toHaveAttribute('aria-pressed', 'false') |
| 63 | + |
| 64 | + fireEvent.click(bananas) |
| 65 | + expect(apples).toHaveAttribute('aria-pressed', 'true') |
| 66 | + expect(bananas).toHaveAttribute('aria-pressed', 'false') |
| 67 | + expect(oranges).toHaveAttribute('aria-pressed', 'false') |
| 68 | + |
| 69 | + fireEvent.click(oranges) |
| 70 | + expect(apples).toHaveAttribute('aria-pressed', 'true') |
| 71 | + expect(bananas).toHaveAttribute('aria-pressed', 'false') |
| 72 | + expect(oranges).toHaveAttribute('aria-pressed', 'true') |
| 73 | + }) |
| 74 | + |
| 75 | + test('options', () => { |
| 76 | + const options = [ |
| 77 | + { label: 'Smoked Gouda', value: 'Gouda' }, |
| 78 | + { value: 'Cheddar' }, |
| 79 | + { disabled: true, value: 'Swiss' }, |
| 80 | + ] |
| 81 | + const onChangeMock = jest.fn() |
| 82 | + renderWithTheme( |
| 83 | + <ButtonGroup |
| 84 | + options={options} |
| 85 | + value={['Cheddar']} |
| 86 | + onChange={onChangeMock} |
| 87 | + /> |
| 88 | + ) |
| 89 | + const goudaButton = screen.getByText('Smoked Gouda') |
| 90 | + const cheddarButton = screen.getByText('Cheddar') |
| 91 | + const swissButton = screen.getByText('Swiss') |
| 92 | + |
| 93 | + expect(goudaButton).toHaveAttribute('aria-pressed', 'false') |
| 94 | + expect(cheddarButton).toHaveAttribute('aria-pressed', 'true') |
| 95 | + expect(swissButton).toHaveAttribute('aria-pressed', 'false') |
| 96 | + expect(swissButton).toBeDisabled() |
| 97 | + |
| 98 | + fireEvent.click(goudaButton) |
| 99 | + expect(onChangeMock).toBeCalledWith(['Cheddar', 'Gouda']) |
| 100 | + onChangeMock.mockClear() |
| 101 | + fireEvent.click(cheddarButton) |
| 102 | + expect(onChangeMock).toHaveBeenCalledWith([]) |
| 103 | + onChangeMock.mockClear() |
| 104 | + // Disabled |
| 105 | + fireEvent.click(swissButton) |
| 106 | + expect(onChangeMock).not.toHaveBeenCalled() |
| 107 | + }) |
| 108 | + |
| 109 | + test('disabled', () => { |
| 110 | + const onChangeMock = jest.fn() |
| 111 | + renderWithTheme( |
| 112 | + <ButtonGroup disabled onChange={onChangeMock}> |
| 113 | + <ButtonItem>Apples</ButtonItem> |
64 | 114 | </ButtonGroup> |
65 | | - </form> |
66 | | - ) |
67 | | - |
68 | | - const apples = getByLabelText('Apples') |
69 | | - const oranges = getByLabelText('Oranges') |
70 | | - |
71 | | - expect(apples).toHaveAttribute('checked') |
72 | | - expect(oranges).not.toHaveAttribute('checked') |
73 | | - expect(getByText('Apples')).toHaveStyle(`color: ${theme.colors.key}`) |
74 | | - expect(getByText('Oranges')).toHaveStyle(`color: ${theme.colors.text3}`) |
75 | | - |
76 | | - expect(getByLabelText('Bananas')).toBeInTheDocument() |
77 | | - |
78 | | - Simulate.change(apples) |
79 | | - expect(onChangeMock).not.toHaveBeenCalled() |
80 | | - |
81 | | - Simulate.change(oranges) |
82 | | - expect(onChangeMock).toHaveBeenCalled() |
83 | | - |
84 | | - const form = container.querySelector('form') |
85 | | - if (form) { |
86 | | - const formData = new FormData(form) |
87 | | - expect(formData.get('fruits')).toEqual('Oranges') |
88 | | - } |
89 | | -}) |
90 | | - |
91 | | -test('<ButtonGroup/> controlled', () => { |
92 | | - const onChangeMock = jest.fn() |
93 | | - const { getByLabelText } = renderWithTheme( |
94 | | - <ButtonGroup value={['Bananas']} onChange={onChangeMock}> |
95 | | - <ButtonItem>Apples</ButtonItem> |
96 | | - <ButtonItem>Oranges</ButtonItem> |
97 | | - <ButtonItem>Bananas</ButtonItem> |
98 | | - </ButtonGroup> |
99 | | - ) |
100 | | - |
101 | | - const apples = getByLabelText('Apples') |
102 | | - const bananas = getByLabelText('Bananas') |
103 | | - const oranges = getByLabelText('Oranges') |
104 | | - |
105 | | - expect(bananas).toHaveAttribute('checked') |
106 | | - expect(oranges).not.toHaveAttribute('checked') |
107 | | - |
108 | | - Simulate.change(apples) |
109 | | - Simulate.change(bananas) |
110 | | - Simulate.change(oranges) |
111 | | - expect(onChangeMock).toHaveBeenCalledTimes(3) |
112 | | - expect(onChangeMock.mock.calls[0][0]).toEqual(['Bananas', 'Apples']) |
113 | | - expect(onChangeMock.mock.calls[1][0]).toEqual([]) |
114 | | - expect(onChangeMock.mock.calls[2][0]).toEqual(['Bananas', 'Oranges']) |
| 115 | + ) |
| 116 | + const applesButton = screen.getByText('Apples') |
| 117 | + fireEvent.click(applesButton) |
| 118 | + expect(onChangeMock).not.toHaveBeenCalled() |
| 119 | + }) |
115 | 120 | }) |
0 commit comments