-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsummit-dropdown.test.js
More file actions
120 lines (91 loc) · 3.85 KB
/
summit-dropdown.test.js
File metadata and controls
120 lines (91 loc) · 3.85 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* @jest-environment jsdom
*/
import React from 'react';
import "@testing-library/jest-dom";
import { render, screen, act } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import SummitDropdown from '..';
jest.mock('i18n-react/dist/i18n-react', () => ({
__esModule: true,
default: { translate: (key) => key },
}));
const summits = [
{ id: 1, name: 'Summit A', start_date: 1000 },
{ id: 2, name: 'Summit B', start_date: 2000 },
];
const defaultProps = {
summits,
actionLabel: 'Go',
actionClass: '',
onClick: jest.fn(),
};
function renderComponent(props = {}) {
return render(<SummitDropdown {...defaultProps} {...props} />);
}
describe('SummitDropdown summitValue state', () => {
beforeEach(() => {
defaultProps.onClick.mockClear();
jest.clearAllMocks();
});
test('summitValue is null on initial render', () => {
renderComponent();
expect(screen.getByRole('button', { name: 'Go' })).toBeDisabled();
});
test('handleChange sets summitValue when given a valid object', async () => {
const user = userEvent.setup();
renderComponent();
await user.click(screen.getByRole('textbox'));
await user.click(screen.getByText('Summit A'));
expect(screen.getByRole('button', { name: 'Go' })).toBeEnabled();
});
test('handleChange does not set summitValue when given a non-object', () => {
const ref = React.createRef();
render(<SummitDropdown {...defaultProps} ref={ref} />);
const option = { label: 'Summit A', value: 1 };
const invalidOption = 'not-an-object';
act(() => { ref.current.handleChange(invalidOption); });
expect(ref.current.state.summitValue).toBeNull();
act(() => { ref.current.handleChange(option); });
expect(ref.current.state.summitValue).toEqual(option);
act(() => { ref.current.handleChange(invalidOption); });
expect(ref.current.state.summitValue).toEqual(option);
});
test('handleChange does not set summitValue when given null', () => {
const ref = React.createRef();
render(<SummitDropdown {...defaultProps} ref={ref} />);
const option = { label: 'Summit A', value: 1 };
act(() => { ref.current.handleChange(null); });
expect(ref.current.state.summitValue).toBeNull();
act(() => { ref.current.handleChange(option); });
expect(ref.current.state.summitValue).toEqual(option);
act(() => { ref.current.handleChange(null); });
expect(ref.current.state.summitValue).toEqual(option);
});
test('handleClick does not call onClick when summitValue is null', async () => {
const user = userEvent.setup();
renderComponent();
// button is disabled — click is a no-op, onClick must NOT be called
await user.click(screen.getByRole('button', { name: 'Go' }));
expect(defaultProps.onClick).not.toHaveBeenCalled();
});
test('handleClick calls onClick with summit id when summitValue is set', async () => {
const user = userEvent.setup();
renderComponent();
await user.click(screen.getByRole('textbox'));
await user.click(screen.getByText('Summit A'));
await user.click(screen.getByRole('button', { name: 'Go' }));
expect(defaultProps.onClick).toHaveBeenCalledWith(1);
});
test('button is disabled when summitValue is null', () => {
renderComponent();
expect(screen.getByRole('button', { name: 'Go' })).toBeDisabled();
});
test('button is enabled after selecting a summit', async () => {
const user = userEvent.setup();
renderComponent();
await user.click(screen.getByRole('textbox'));
await user.click(screen.getByText('Summit A'));
expect(screen.getByRole('button', { name: 'Go' })).toBeEnabled();
});
});