-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathValue.test.tsx
87 lines (73 loc) · 3 KB
/
Value.test.tsx
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
import React, { type ComponentProps } from 'react';
import Value from '../src/components/Value';
import { render } from '@testing-library/react';
import type { CallbackFn, SelectedOption } from '../src/types';
import { PLACEHOLDER_DEFAULT, EMPTY_ARRAY } from '../src/constants';
import { renderOptionLabelMock, renderMultiOptionsMock, getSelectedOptionSingle, ThemeWrapper } from './helpers';
type ValueProps = ComponentProps<typeof Value>;
// ============================================
// Helper functions for Value component
// ============================================
const renderValue = (props: ValueProps) => {
return render(
<ThemeWrapper>
<Value {...props} />
</ThemeWrapper>
);
};
const rerenderValue = (props: ValueProps, rerender: CallbackFn): void => {
rerender(
<ThemeWrapper>
<Value {...props} />
</ThemeWrapper>
);
};
const removeSelectedOptionSpy = jest.fn();
const renderOptionLabelSpy = renderOptionLabelMock;
const renderMultiOptionsSpy = renderMultiOptionsMock;
const BASE_PROPS: ValueProps = {
isMulti: false,
hasInput: false,
focusedMultiValue: null,
selectedOption: EMPTY_ARRAY,
renderMultiOptions: undefined,
placeholder: PLACEHOLDER_DEFAULT,
renderOptionLabel: renderOptionLabelSpy,
removeSelectedOption: removeSelectedOptionSpy
} as const;
// ============================================
// Test cases
// ============================================
test('"placeholder" text displays when no option is selected', () => {
const { getByText } = renderValue(BASE_PROPS);
expect(getByText(PLACEHOLDER_DEFAULT)).toBeInTheDocument();
});
test('component renders NULL if "hasInput" is true AND ("isMulti" !== true OR ("isMulti" === true AND "selectedOptions" is empty))', () => {
// Render with truthy "inputValue" and "isMulti" = false
const singleProps = { ...BASE_PROPS, hasInput: true };
const { container, rerender } = renderValue(singleProps);
expect(container.hasChildNodes()).toBeFalsy();
// Re-render with truthy "inputValue" and "isMulti" = true
const multiProps = { ...singleProps, isMulti: true };
rerenderValue(multiProps, rerender);
expect(container.hasChildNodes()).toBeFalsy();
});
test('"renderOptionLabel" callback should be executed when an option is selected and should render the selected option label text', () => {
const selectedOption = getSelectedOptionSingle();
const props = { ...BASE_PROPS, selectedOption };
const { getByText } = renderValue(props);
expect(renderOptionLabelSpy).toHaveBeenCalledTimes(1);
selectedOption.forEach(({ label }: SelectedOption) => {
expect(getByText(String(label))).toBeInTheDocument();
});
});
test('"renderMultiOptions" callback should be executed when "isMulti" = true and "renderMultiOptions" is a function and at least one option is selected', () => {
const props = {
...BASE_PROPS,
isMulti: true,
selectedOption: getSelectedOptionSingle(),
renderMultiOptions: renderMultiOptionsSpy
};
renderValue(props);
expect(renderMultiOptionsSpy).toHaveBeenCalledTimes(1);
});