Skip to content

Commit 3203cf2

Browse files
committed
add test file
1 parent 7f36ce3 commit 3203cf2

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

tests/unit/ButtonStyleUtilsTest.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import CONST from '@src/CONST';
2+
import type {ThemeStyles} from '@src/styles';
3+
import type {ThemeColors} from '@src/styles/theme/types';
4+
import createStyleUtils from '@src/styles/utils';
5+
6+
const mockTheme = {} as ThemeColors;
7+
8+
const mockStyles = {
9+
buttonExtraSmall: {height: 28},
10+
buttonSmall: {height: 32},
11+
buttonMedium: {height: 40},
12+
buttonLarge: {height: 48},
13+
ph0: {paddingHorizontal: 0},
14+
pl2: {paddingLeft: 8},
15+
pr2: {paddingRight: 8},
16+
pl3: {paddingLeft: 12},
17+
pr3: {paddingRight: 12},
18+
pl4: {paddingLeft: 16},
19+
pr4: {paddingRight: 16},
20+
buttonSuccess: {backgroundColor: 'green'},
21+
buttonDanger: {backgroundColor: 'red'},
22+
buttonOpacityDisabled: {opacity: 0.5},
23+
buttonDisabled: {backgroundColor: 'gray'},
24+
} as unknown as ThemeStyles;
25+
26+
const {getButtonSizeStyle, getButtonPaddingStyle, getButtonStyleWithIcon, getButtonVariantStyles} = createStyleUtils(mockTheme, mockStyles);
27+
28+
describe('getButtonSizeStyle', () => {
29+
it('returns undefined when size is not provided', () => {
30+
expect(getButtonSizeStyle(mockStyles)).toBeUndefined();
31+
});
32+
33+
it.each([
34+
[CONST.DROPDOWN_BUTTON_SIZE.EXTRA_SMALL, mockStyles.buttonExtraSmall],
35+
[CONST.DROPDOWN_BUTTON_SIZE.SMALL, mockStyles.buttonSmall],
36+
[CONST.DROPDOWN_BUTTON_SIZE.MEDIUM, mockStyles.buttonMedium],
37+
[CONST.DROPDOWN_BUTTON_SIZE.LARGE, mockStyles.buttonLarge],
38+
] as const)('returns correct style for size %s', (size, expected) => {
39+
expect(getButtonSizeStyle(mockStyles, size)).toBe(expected);
40+
});
41+
});
42+
43+
describe('getButtonPaddingStyle', () => {
44+
describe('when size is not provided', () => {
45+
it('returns buttonMedium merged with ph0 when has icon but no text', () => {
46+
expect(getButtonPaddingStyle(mockStyles, undefined, true, false)).toEqual({...mockStyles.buttonMedium, ...mockStyles.ph0});
47+
});
48+
49+
it('returns undefined when there is no icon', () => {
50+
expect(getButtonPaddingStyle(mockStyles, undefined, false, true)).toBeUndefined();
51+
});
52+
});
53+
54+
describe('when size is provided', () => {
55+
it('returns undefined when both icons are present (symmetric)', () => {
56+
expect(getButtonPaddingStyle(mockStyles, CONST.DROPDOWN_BUTTON_SIZE.MEDIUM, true, true, true)).toBeUndefined();
57+
});
58+
59+
it('returns undefined when both icons are absent (symmetric)', () => {
60+
expect(getButtonPaddingStyle(mockStyles, CONST.DROPDOWN_BUTTON_SIZE.MEDIUM, false, true, false)).toBeUndefined();
61+
});
62+
63+
it('returns undefined when both icon flags are undefined (symmetric)', () => {
64+
expect(getButtonPaddingStyle(mockStyles, CONST.DROPDOWN_BUTTON_SIZE.MEDIUM, undefined, true, undefined)).toBeUndefined();
65+
});
66+
67+
it('returns ph0 when icons are asymmetric and there is no text', () => {
68+
expect(getButtonPaddingStyle(mockStyles, CONST.DROPDOWN_BUTTON_SIZE.MEDIUM, true, false, false)).toBe(mockStyles.ph0);
69+
});
70+
71+
it.each([
72+
[CONST.DROPDOWN_BUTTON_SIZE.EXTRA_SMALL, true, mockStyles.pl2],
73+
[CONST.DROPDOWN_BUTTON_SIZE.EXTRA_SMALL, false, mockStyles.pr2],
74+
[CONST.DROPDOWN_BUTTON_SIZE.MEDIUM, true, mockStyles.pl3],
75+
[CONST.DROPDOWN_BUTTON_SIZE.MEDIUM, false, mockStyles.pr3],
76+
[CONST.DROPDOWN_BUTTON_SIZE.LARGE, true, mockStyles.pl4],
77+
[CONST.DROPDOWN_BUTTON_SIZE.LARGE, false, mockStyles.pr4],
78+
] as const)('size %s with hasIcon=%s returns correct padding', (size, hasIcon, expected) => {
79+
const shouldShowRightIcon = !hasIcon;
80+
expect(getButtonPaddingStyle(mockStyles, size, hasIcon, true, shouldShowRightIcon)).toBe(expected);
81+
});
82+
});
83+
});
84+
85+
describe('getButtonStyleWithIcon', () => {
86+
it('composes size style and padding style into an array', () => {
87+
expect(getButtonStyleWithIcon(mockStyles, CONST.DROPDOWN_BUTTON_SIZE.MEDIUM, true, true, false)).toEqual([mockStyles.buttonMedium, mockStyles.pl3]);
88+
});
89+
90+
it('returns array of undefineds when no arguments are provided', () => {
91+
expect(getButtonStyleWithIcon(mockStyles)).toEqual([undefined, undefined]);
92+
});
93+
});
94+
95+
describe('getButtonVariantStyles', () => {
96+
const variantStyles = getButtonVariantStyles(mockStyles);
97+
98+
it('returns correct normal variant styles', () => {
99+
expect(variantStyles.normal).toEqual({
100+
success: mockStyles.buttonSuccess,
101+
danger: mockStyles.buttonDanger,
102+
link: {},
103+
});
104+
});
105+
106+
it('returns correct disabled variant styles', () => {
107+
expect(variantStyles.disabled).toEqual({
108+
success: [mockStyles.buttonOpacityDisabled],
109+
danger: [mockStyles.buttonOpacityDisabled],
110+
link: [mockStyles.buttonOpacityDisabled, mockStyles.buttonDisabled],
111+
});
112+
});
113+
});

0 commit comments

Comments
 (0)