Skip to content

Commit 42a6320

Browse files
committed
refactor unit tests
1 parent 63ce8d1 commit 42a6320

11 files changed

+61
-66
lines changed

__tests__/AriaLiveRegion.test.tsx

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { type ComponentProps } from 'react';
22
import { render } from '@testing-library/react';
33
import AriaLiveRegion from '../src/components/AriaLiveRegion';
44
import { getSelectedOptionMulti, ThemeTestHOC } from './helpers';
5-
import type { FocusedOption, SelectedOption } from '../src/types';
5+
import type { AriaLiveAttribute, FocusedOption, SelectedOption } from '../src/types';
66
import { ARIA_LIVE_CONTEXT_ID, ARIA_LIVE_SELECTION_ID } from '../src/constants';
77

88
type AriaLiveRegionProps = ComponentProps<typeof AriaLiveRegion>;
@@ -19,29 +19,26 @@ const renderAriaLiveRegion = (props: AriaLiveRegionProps) => {
1919
);
2020
};
2121

22-
const createAriaLiveRegionProps = (): AriaLiveRegionProps => {
23-
const selectedOption: SelectedOption[] = getSelectedOptionMulti();
24-
const focusedOption: FocusedOption = { index: 0, ...selectedOption[0] };
25-
26-
return {
27-
focusedOption,
28-
selectedOption,
29-
menuOpen: true,
30-
isFocused: true,
31-
isSearchable: true,
32-
ariaLive: 'polite',
33-
inputValue: 'search query',
34-
optionCount: selectedOption.length
35-
};
36-
};
22+
const selectedOption: SelectedOption[] = getSelectedOptionMulti();
23+
const focusedOption: FocusedOption = { index: 0, ...selectedOption[0] };
24+
25+
const BASE_PROPS: AriaLiveRegionProps = {
26+
focusedOption,
27+
selectedOption,
28+
menuOpen: true,
29+
isFocused: true,
30+
isSearchable: true,
31+
ariaLive: 'polite',
32+
inputValue: 'search query',
33+
optionCount: selectedOption.length
34+
} as const;
3735

3836
// ============================================
3937
// Test cases
4038
// ============================================
4139

42-
test('AriaLiveRegion component mounts and renders without error & can query childNodes by id attributes', async () => {
43-
const props = createAriaLiveRegionProps();
44-
const { container } = renderAriaLiveRegion(props);
40+
test('AriaLiveRegion component mounts and renders without error & can query childNodes by id attributes', () => {
41+
const { container } = renderAriaLiveRegion(BASE_PROPS);
4542
const childNodeIds = [ARIA_LIVE_CONTEXT_ID, ARIA_LIVE_SELECTION_ID];
4643

4744
childNodeIds.forEach((id) => {
@@ -50,11 +47,10 @@ test('AriaLiveRegion component mounts and renders without error & can query chil
5047
});
5148
});
5249

53-
test('"ariaLive" prop can be passed as one of the accepted aria-live values and the root A11yText span element reflects it accordingly', async () => {
54-
const ariaLive: AriaLiveRegionProps['ariaLive'] = 'assertive';
55-
const props = createAriaLiveRegionProps();
56-
const mergedProps = { ...props, ariaLive };
57-
const { container } = renderAriaLiveRegion(mergedProps);
50+
test('"ariaLive" prop can be passed as one of the accepted aria-live values and the root A11yText span element reflects it accordingly', () => {
51+
const ariaLive: AriaLiveAttribute = 'assertive';
52+
const props = { ...BASE_PROPS, ariaLive };
53+
const { container } = renderAriaLiveRegion(props);
5854
const a11yTextRootSpanEl = container.firstChild;
5955
expect(a11yTextRootSpanEl).toHaveAttribute('aria-live', ariaLive);
6056
});

__tests__/AutosizeInput.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ const BASE_PROPS: AutosizeInputProps = {
4040
// Test cases
4141
// ============================================
4242

43-
test('input element has a static className (enables styling via classic CSS)', async () => {
43+
test('input element has a static className (enables styling via classic CSS)', () => {
4444
const { getByTestId } = renderAutosizeInput(BASE_PROPS);
4545
expect(getByTestId(AUTOSIZE_INPUT_TESTID!)).toHaveClass(AUTOSIZE_INPUT_CLS);
4646
});
4747

48-
test('input has functional, optional ARIA attributes', async () => {
48+
test('input has functional, optional ARIA attributes', () => {
4949
const props = {
5050
...BASE_PROPS,
5151
ariaLabel: 'test-label',
@@ -60,7 +60,7 @@ test('input has functional, optional ARIA attributes', async () => {
6060
});
6161
});
6262

63-
test('when "id" has a non-empty string value, input element should get an "id" attribute reflecting that value', async () => {
63+
test('when "id" has a non-empty string value, input element should get an "id" attribute reflecting that value', () => {
6464
const inputId = 'test-input-id';
6565
const props = { ...BASE_PROPS, id: inputId };
6666
const { getByTestId } = renderAutosizeInput(props);
@@ -76,7 +76,7 @@ test('when "readOnly" = true, the onChange event handler should not be attached
7676
expect(inputElement).toHaveAttribute('readonly');
7777
});
7878

79-
test('"blur" and "focus" events with callback handlers are attached to the input element', async () => {
79+
test('"blur" and "focus" events with callback handlers are attached to the input element', () => {
8080
const { getByTestId } = renderAutosizeInput(BASE_PROPS);
8181
const inputElement = getByTestId(AUTOSIZE_INPUT_TESTID!);
8282
fireEvent.blur(inputElement);

__tests__/IndicatorIcons.test.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const customIconFn = (props: Partial<IndicatorIconsProps>): ReactNode => {
4747
// Test cases
4848
// ============================================
4949

50-
test('clear icon has a static className (enables styling via classic CSS)', async () => {
50+
test('clear icon has a static className (enables styling via classic CSS)', () => {
5151
const { getByTestId } = renderIndicatorIcons(BASE_PROPS);
5252
const firstChildOfClearIconElement = getByTestId(CLEAR_ICON_TESTID!).firstChild;
5353
expect(firstChildOfClearIconElement).toHaveClass(CLEAR_ICON_CLS);
@@ -67,13 +67,13 @@ test('caret indicator has functioning "click" user interactions', async () => {
6767
expect(onCaretMouseDownSpy).toBeCalled();
6868
});
6969

70-
test('clear icon is not rendered and loading animation is rendered when "isLoading" = true', async () => {
70+
test('clear icon is not rendered and loading animation is rendered when "isLoading" = true', () => {
7171
const props = { ...BASE_PROPS, isLoading: true };
7272
const { queryByTestId } = renderIndicatorIcons(props);
7373
expect(queryByTestId(CLEAR_ICON_TESTID!)).toBeNull();
7474
});
7575

76-
test('loading can render as a custom node (instead of default LoadingDots.tsx component)', async () => {
76+
test('loading can render as a custom node (instead of default LoadingDots.tsx component)', () => {
7777
const loadingNodeText = 'loading-node';
7878
const loadingNode = <span>{loadingNodeText}</span>;
7979

@@ -87,15 +87,15 @@ test('loading can render as a custom node (instead of default LoadingDots.tsx co
8787
expect(getByText(loadingNodeText)).toBeInTheDocument();
8888
});
8989

90-
test('clear icon can render as a ReactNode', async () => {
90+
test('clear icon can render as a ReactNode', () => {
9191
const clearIconText = 'clear-icon-node';
9292
const clearIcon = <span>{clearIconText}</span>;
9393
const props = { ...BASE_PROPS, clearIcon };
9494
const { getByText } = renderIndicatorIcons(props);
9595
expect(getByText(clearIconText)).toBeInTheDocument();
9696
});
9797

98-
test('clear icon can render as a callback function with return type of ReactNode - callback accepts forwarded state props from wrapping component.', async () => {
98+
test('clear icon can render as a callback function with return type of ReactNode - callback accepts forwarded state props from wrapping component.', () => {
9999
const props = {
100100
...BASE_PROPS,
101101
menuOpen: true,
@@ -110,15 +110,15 @@ test('clear icon can render as a callback function with return type of ReactNode
110110
expect(getByTestId(forwardedStateId)).toBeInTheDocument();
111111
});
112112

113-
test('caret icon can render as a ReactNode', async () => {
113+
test('caret icon can render as a ReactNode', () => {
114114
const caretIconText = 'caret-icon-node';
115115
const caretIcon = <span>{caretIconText}</span>;
116116
const props = { ...BASE_PROPS, caretIcon };
117117
const { getByText } = renderIndicatorIcons(props);
118118
expect(getByText(caretIconText)).toBeInTheDocument();
119119
});
120120

121-
test('caret icon can render as a callback function with return type of ReactNode - callback accepts forwarded state props from wrapping component.', async () => {
121+
test('caret icon can render as a callback function with return type of ReactNode - callback accepts forwarded state props from wrapping component.', () => {
122122
const props = { ...BASE_PROPS, menuOpen: true, caretIcon: customIconFn };
123123
const { getByTestId } = renderIndicatorIcons(props);
124124

__tests__/LoadingDots.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const renderAriaLiveRegion = () => {
1919
// Test cases
2020
// ============================================
2121

22-
test('LoadingDots component mounts and renders without error', async () => {
22+
test('LoadingDots component mounts and renders without error', () => {
2323
const { container } = renderAriaLiveRegion();
2424
expect(container.hasChildNodes()).toBeTruthy();
2525
});

__tests__/MenuList.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const BASE_PROPS: MenuListProps = {
4545
// Test cases
4646
// ============================================
4747

48-
test('MenuList component mounts and renders successfully when "menuOptions" array has items', async () => {
48+
test('MenuList component mounts and renders successfully when "menuOptions" array has items', () => {
4949
const { getByText } = renderMenuList(BASE_PROPS);
5050

5151
// Assert react-window + Option.tsx renders each menuOption correctly
@@ -54,7 +54,7 @@ test('MenuList component mounts and renders successfully when "menuOptions" arra
5454
});
5555
});
5656

57-
test('The "itemKeySelector" property is used in "react-window" function property "itemKey" to select unqiue key based on property value rather than using default index for each option', async () => {
57+
test('The "itemKeySelector" property is used in "react-window" function property "itemKey" to select unqiue key based on property value rather than using default index for each option', () => {
5858
const props = { ...BASE_PROPS, itemKeySelector: 'value' };
5959
const { getByText } = renderMenuList(props);
6060

@@ -64,23 +64,23 @@ test('The "itemKeySelector" property is used in "react-window" function property
6464
});
6565
});
6666

67-
test('The "No Options" message element is NOT rendered when "menuOptions" length > 0', async () => {
67+
test('The "No Options" message element is NOT rendered when "menuOptions" length > 0', () => {
6868
const { queryByText } = renderMenuList(BASE_PROPS);
6969
expect(queryByText(BASE_PROPS.noOptionsMsg!)).toBeNull();
7070
});
7171

72-
test('The "No Options" message element is rendered when "menuOptions" length === 0', async () => {
72+
test('The "No Options" message element is rendered when "menuOptions" length === 0', () => {
7373
const props = { ...BASE_PROPS, menuOptions: [] };
7474
const { getByText } = renderMenuList(props);
7575
expect(getByText(props.noOptionsMsg!)).toBeInTheDocument();
7676
});
7777

78-
test('The "Loading" message element is NOT rendered when "isLoading" !== true', async () => {
78+
test('The "Loading" message element is NOT rendered when "isLoading" !== true', () => {
7979
const { queryByText } = renderMenuList(BASE_PROPS);
8080
expect(queryByText(BASE_PROPS.loadingMsg)).toBeNull();
8181
});
8282

83-
test('The "Loading" message element is rendered when "isLoading" === true', async () => {
83+
test('The "Loading" message element is rendered when "isLoading" === true', () => {
8484
const props = { ...BASE_PROPS, isLoading: true };
8585
const { getByText } = renderMenuList(props);
8686
expect(getByText(props.loadingMsg)).toBeInTheDocument();

__tests__/MultiValue.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ const renderMultiValue = (props: MultiValueProps) => {
2525
const removeSelectedOptionSpy = jest.fn();
2626
const renderOptionLabelSpy = renderOptionLabelMock;
2727

28-
const DATA: Option = getOptionSingle();
28+
const data: Option = getOptionSingle();
2929

3030
const BASE_PROPS: MultiValueProps = {
31-
data: DATA,
31+
data,
3232
isFocused: false,
33-
value: DATA.value,
33+
value: data.value,
3434
renderOptionLabel: renderOptionLabelSpy,
3535
removeSelectedOption: removeSelectedOptionSpy
3636
} as const;
@@ -39,7 +39,7 @@ const BASE_PROPS: MultiValueProps = {
3939
// Test cases
4040
// ============================================
4141

42-
test('"renderOptionLabel" callback should be executed and should render the selected option label text', async () => {
42+
test('"renderOptionLabel" callback should be executed and should render the selected option label text', () => {
4343
const { getByText } = renderMultiValue(BASE_PROPS);
4444
expect(renderOptionLabelSpy).toBeCalled();
4545
expect(getByText(BASE_PROPS.data.label)).toBeInTheDocument();

__tests__/Option.test.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ const createOptionProps = (
6262
// Test cases
6363
// ============================================
6464

65-
test('option parent element renders dynamic style attribute correctly', async () => {
65+
test('option parent element renders dynamic style attribute correctly', () => {
6666
const { props } = createOptionProps();
6767
const { container } = renderOption(props);
6868
const optionParentEl = container.querySelector('div');
6969
const optionCssProps = stringifyCSSProperties(OPTION_STYLE);
7070
expect(optionParentEl).toHaveAttribute('style', optionCssProps);
7171
});
7272

73-
test('"renderOptionLabel" callback should be executed and the result rendered to DOM', async () => {
73+
test('"renderOptionLabel" callback should be executed and the result rendered to DOM', () => {
7474
const { props, renderOptionLabelSpy } = createOptionProps();
7575
const { label } = props.data.menuOptions[props.index];
7676
const { getByText } = renderOption(props);
@@ -79,13 +79,11 @@ test('"renderOptionLabel" callback should be executed and the result rendered to
7979
});
8080

8181
test(`option with "isDisabled" = TRUE should have an onClick handler and the ${OPTION_DISABLED_CLS} class added to its classList`, async () => {
82-
const firstDisabledMenuOptionIndex = MENU_OPTIONS.findIndex((option) => !!option.isDisabled);
83-
const { props, onClickSelectOptionSpy } = createOptionProps(firstDisabledMenuOptionIndex);
82+
const firstDisabledIdx = MENU_OPTIONS.findIndex((x) => x.isDisabled);
83+
const { props, onClickSelectOptionSpy } = createOptionProps(firstDisabledIdx);
8484
const { user, container } = renderOption(props);
8585
const optionParentEl = container.querySelector('div') as HTMLDivElement;
86-
8786
await user.click(optionParentEl);
88-
8987
expect(onClickSelectOptionSpy).toBeCalled();
9088
expect(optionParentEl).toHaveClass(OPTION_DISABLED_CLS);
9189
});

__tests__/ReactSSR.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import React from 'react';
22
import { Select } from '../src';
33
import { renderToString } from 'react-dom/server';
44

5-
test('Select component can be rendered using react-dom/server', async () => {
6-
expect(() => renderToString(<Select />)).not.toThrow();
5+
test('Select component can be rendered using react-dom/server', () => {
6+
expect(() => renderToString(<Select />)).not.toThrowError();
77
});

__tests__/Select.test.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ const renderSelect = (props?: SelectProps) => ({
2727
// Test cases
2828
// ============================================
2929

30-
test('container elements have static className value (enables styling via classic CSS)', async () => {
30+
test('container elements have static className value (enables styling via classic CSS)', () => {
3131
const { getByTestId } = renderSelect();
3232
expect(getByTestId(SELECT_CONTAINER_TESTID!)).toHaveClass(SELECT_CONTAINER_CLS);
3333
expect(getByTestId(CONTROL_CONTAINER_TESTID!)).toHaveClass(CONTROL_CONTAINER_CLS);
3434
expect(getByTestId(MENU_CONTAINER_TESTID!)).toHaveClass(MENU_CONTAINER_CLS);
3535
});
3636

37-
test('id attributes are added to DOM if defined ("menuId", "selectId", "inputId" props)', async () => {
37+
test('id attributes are added to DOM if defined ("menuId", "selectId", "inputId" props)', () => {
3838
const props = {
3939
menuId: 'test-menu-id',
4040
inputId: 'test-input-id',
@@ -46,15 +46,15 @@ test('id attributes are added to DOM if defined ("menuId", "selectId", "inputId"
4646
expect(getByTestId(SELECT_CONTAINER_TESTID!)).toHaveAttribute('id', props.selectId);
4747
});
4848

49-
test('"onInputFocus" callback should be fired when input is focused (if a defined function)', async () => {
49+
test('"onInputFocus" callback should be fired when input is focused (if a defined function)', () => {
5050
const onFocusSpy = jest.fn();
5151
const props = { onInputFocus: onFocusSpy };
5252
const { getByTestId } = renderSelect(props);
5353
fireEvent.focus(getByTestId(AUTOSIZE_INPUT_TESTID!));
5454
expect(onFocusSpy).toBeCalled();
5555
});
5656

57-
test('"onInputBlur" callback should be fired on blur (if a defined function)', async () => {
57+
test('"onInputBlur" callback should be fired on blur (if a defined function)', () => {
5858
const onBlurSpy = jest.fn();
5959
const props = { onInputBlur: onBlurSpy };
6060
const { getByTestId } = renderSelect(props);
@@ -79,7 +79,8 @@ test('toggling the menu to open/close fires corresponding callbacks "onMenuOpen"
7979
expect(onMenuCloseSpy).toBeCalled();
8080
});
8181

82-
test('When "lazyLoadMenu" property = true, then menu components are only rendered in DOM when "menuOpen" state = true', async () => {
83-
const { queryByTestId } = renderSelect({ lazyLoadMenu: true });
82+
test('When "lazyLoadMenu" property = true, then menu components are only rendered in DOM when "menuOpen" state = true', () => {
83+
const props = { lazyLoadMenu: true };
84+
const { queryByTestId } = renderSelect(props);
8485
expect(queryByTestId(MENU_CONTAINER_TESTID!)).toBeNull();
8586
});

__tests__/Value.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const renderValue = (props: ValueProps) => {
2121

2222
const rerenderValue = (
2323
props: ValueProps,
24-
rerender: (...args: any[]) => void
24+
rerender: (...args: any[]) => any
2525
): void => {
2626
rerender(
2727
<ThemeTestHOC>
@@ -49,12 +49,12 @@ const BASE_PROPS: ValueProps = {
4949
// Test cases
5050
// ============================================
5151

52-
test('"placeholder" text displays when no option is selected', async () => {
52+
test('"placeholder" text displays when no option is selected', () => {
5353
const { getByText } = renderValue(BASE_PROPS);
5454
expect(getByText(PLACEHOLDER_DEFAULT)).toBeInTheDocument();
5555
});
5656

57-
test('component renders NULL if "hasInput" is true AND ("isMulti" !== true OR ("isMulti" === true AND "selectedOptions" is empty))', async () => {
57+
test('component renders NULL if "hasInput" is true AND ("isMulti" !== true OR ("isMulti" === true AND "selectedOptions" is empty))', () => {
5858
// Render with truthy "inputValue" and "isMulti" = false
5959
const singleProps = { ...BASE_PROPS, hasInput: true };
6060
const { container, rerender } = renderValue(singleProps);
@@ -66,7 +66,7 @@ test('component renders NULL if "hasInput" is true AND ("isMulti" !== true OR ("
6666
expect(container.hasChildNodes()).toBeFalsy();
6767
});
6868

69-
test('"renderOptionLabel" callback should be executed when an option is selected and should render the selected option label text', async () => {
69+
test('"renderOptionLabel" callback should be executed when an option is selected and should render the selected option label text', () => {
7070
const selectedOption = getSelectedOptionSingle();
7171
const props = { ...BASE_PROPS, selectedOption };
7272
const { getByText } = renderValue(props);
@@ -78,7 +78,7 @@ test('"renderOptionLabel" callback should be executed when an option is selected
7878
});
7979
});
8080

81-
test('"renderMultiOptions" callback should be executed when "isMulti" = true and "renderMultiOptions" is a function and at least one option is selected', async () => {
81+
test('"renderMultiOptions" callback should be executed when "isMulti" = true and "renderMultiOptions" is a function and at least one option is selected', () => {
8282
const props = {
8383
...BASE_PROPS,
8484
isMulti: true,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"jest-environment-jsdom": "^29.3.1",
8989
"jest-enzyme": "^7.1.2",
9090
"npm-run-all": "^4.1.5",
91-
"prettier": "^2.8.0",
91+
"prettier": "^2.8.1",
9292
"react": "^18.2.0",
9393
"react-dom": "^18.2.0",
9494
"react-syntax-highlighter": "^15.5.0",

0 commit comments

Comments
 (0)