Skip to content

Commit 9704ce4

Browse files
committed
fix unit test (Option.test.tsx)
1 parent 54d74f3 commit 9704ce4

File tree

8 files changed

+18
-22
lines changed

8 files changed

+18
-22
lines changed

__stories__/helpers/constants/theme.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Theme } from '../../../src';
22
import { createThemeOptions } from '../utils';
3-
import { mergeThemes } from '../../../src/utils';
3+
import { mergeDeep } from '../../../src/utils';
4+
import { DEFAULT_THEME } from '../../../src/constants';
45

56
// Normalize animation props as be default they are type of styled-component's "FlattenSimpleInterpolation"
67
const FADE_IN_KEYFRAMES_STR = 'FADE_IN_KEYFRAMES 0.25s ease-in-out';
@@ -71,7 +72,7 @@ export const ThemeConfigMap: Theme = {
7172
};
7273

7374
export const THEME_OPTIONS = createThemeOptions(ThemeEnum);
74-
export const THEME_DEFAULTS = mergeThemes(THEME_ANIMATIONS);
75+
export const THEME_DEFAULTS = mergeDeep(DEFAULT_THEME, THEME_ANIMATIONS);
7576

7677
export const THEME_CONFIG: Theme = {
7778
menu: {

__tests__/Option.test.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import React from 'react';
1+
import React, { type ComponentProps } from 'react';
22
import type { CSSProperties } from 'react';
33
import { render } from '@testing-library/react';
4+
import Option from '../src/components/Menu/Option';
45
import userEvent from '@testing-library/user-event';
56
import { OPTION_DISABLED_CLS } from '../src/constants';
6-
import Option, { type OptionProps } from '../src/components/Menu/Option';
77
import { MENU_OPTIONS, RENDER_OPTION_LABEL_MOCK, stringifyCSSProperties, ThemeTestHOC } from './helpers';
88

9+
type OptionProps = ComponentProps<typeof Option>;
10+
911
// ============================================
1012
// Helper functions & test data for Option.tsx component
1113
// ============================================

__tests__/helpers/ThemeTestHOC.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
2-
import { DEFAULT_THEME } from '../../src/utils';
32
import { ThemeProvider } from 'styled-components';
3+
import { DEFAULT_THEME } from '../../src/constants';
44

55
const ThemeTestHOC = ({ children }) => (
66
<ThemeProvider theme={DEFAULT_THEME}>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"react-toastify": "^9.1.1",
9898
"react-window": "^1.8.8",
9999
"rimraf": "^3.0.2",
100-
"rollup": "^3.2.5",
100+
"rollup": "^3.3.0",
101101
"rollup-plugin-terser": "^7.0.2",
102102
"styled-components": "^5.3.6",
103103
"typescript": "^4.8.4",

src/Select.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
MenuPositionEnum,
2121
FunctionDefaults,
2222
EMPTY_ARRAY,
23+
DEFAULT_THEME,
2324
SELECT_WRAPPER_ATTRS,
2425
PLACEHOLDER_DEFAULT,
2526
LOADING_MSG_DEFAULT,
@@ -34,7 +35,7 @@ import type { FixedSizeList } from 'react-window';
3435
import styled, { css, ThemeProvider, type DefaultTheme } from 'styled-components';
3536
import { Menu, Value, AriaLiveRegion, AutosizeInput, IndicatorIcons } from './components';
3637
import { useDebounce, useCallbackRef, useMenuOptions, useMountEffect, useUpdateEffect, useMenuPositioner } from './hooks';
37-
import { isBoolean, isFunction, mergeThemes, suppressEvent, normalizeValue, IS_TOUCH_DEVICE, isArrayWithLength } from './utils';
38+
import { isBoolean, isFunction, isPlainObject, mergeDeep, suppressEvent, normalizeValue, IS_TOUCH_DEVICE, isArrayWithLength } from './utils';
3839
import type {
3940
Theme,
4041
SelectRef,
@@ -257,7 +258,11 @@ const Select = forwardRef<SelectRef, SelectProps>((
257258
const [focusedOption, setFocusedOption] = useState<FocusedOption>(FOCUSED_OPTION_DEFAULT);
258259

259260
// Memoized DefaultTheme object for styled-components ThemeProvider
260-
const theme = useMemo<DefaultTheme>(() => mergeThemes(themeConfig), [themeConfig]);
261+
const theme = useMemo<DefaultTheme>(() => {
262+
return isPlainObject(themeConfig)
263+
? mergeDeep(DEFAULT_THEME, themeConfig)
264+
: DEFAULT_THEME;
265+
}, [themeConfig]);
261266

262267
// Memoized callback functions referencing optional function properties on Select.tsx
263268
const getOptionLabelFn = useMemo<OptionLabelCallback>(() => getOptionLabel || FunctionDefaults.OPTION_LABEL, [getOptionLabel]);

src/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './dom';
22
export * from './enums';
3+
export * from './theme';
34
export * from './styled';
45
export * from './defaults';

src/utils/theme.ts renamed to src/constants/theme.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import type { Theme } from '../types';
2-
import { mergeDeep, isPlainObject } from './common';
31
import type { DefaultTheme } from 'styled-components';
4-
import { BOUNCE_ANIMATION_CSS, FADE_IN_ANIMATION_CSS } from '../constants';
2+
import { BOUNCE_ANIMATION_CSS, FADE_IN_ANIMATION_CSS } from './styled';
53

64
/**
75
* A contextual styled-components DefaultTheme object with default key-value pairs for CSS props.
@@ -91,14 +89,4 @@ export const DEFAULT_THEME: DefaultTheme = {
9189
transition: 'color 0.2s ease-out'
9290
}
9391
}
94-
};
95-
96-
/**
97-
* If theme param is a valid object, attempt to merge with static DEFAULT_THEME.
98-
* A new object of DefaultTheme type is returned, with properties overriden as defined in theme param.
99-
*/
100-
export const mergeThemes = (theme?: Theme): DefaultTheme => {
101-
return isPlainObject(theme)
102-
? mergeDeep(DEFAULT_THEME, theme)
103-
: DEFAULT_THEME;
10492
};

src/utils/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from './menu';
2-
export * from './theme';
32
export * from './common';
43
export * from './device';

0 commit comments

Comments
 (0)