Skip to content

Commit f50dc68

Browse files
committed
fix tests
1 parent 745c938 commit f50dc68

1 file changed

Lines changed: 36 additions & 23 deletions

File tree

tests/unit/ConfirmContentTest.tsx

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,32 @@ import {render} from '@testing-library/react-native';
22

33
import ConfirmContent from '@components/ConfirmContent';
44

5+
import CONST from '@src/CONST';
6+
57
import React from 'react';
68

79
type ButtonProps = {
8-
success?: boolean;
9-
danger?: boolean;
10-
text?: string;
10+
variant?: string;
1111
onPress?: () => void;
12+
children?: React.ReactNode;
1213
[key: string]: unknown;
1314
};
1415

1516
const mockButtonSpy = jest.fn<void, [ButtonProps]>();
1617

17-
jest.mock('@components/Button', () => {
18+
jest.mock('@components/ButtonComposed', () => {
1819
const ReactLib = jest.requireActual<typeof React>('react');
20+
const MockButton = (props: ButtonProps) => {
21+
mockButtonSpy(props);
22+
return ReactLib.createElement('mock-button', props);
23+
};
1924
return {
2025
__esModule: true,
21-
default: (props: ButtonProps) => {
22-
mockButtonSpy(props);
23-
return ReactLib.createElement('mock-button', props);
24-
},
26+
default: Object.assign(MockButton, {
27+
Text: () => null,
28+
Icon: () => null,
29+
KeyboardShortcut: () => null,
30+
}),
2531
};
2632
});
2733

@@ -65,12 +71,9 @@ describe('ConfirmContent', () => {
6571
mockButtonSpy.mockClear();
6672
});
6773

68-
function getConfirmButtonProps(shouldStackButtons: boolean): ButtonProps | undefined {
74+
function getConfirmButtonProps(onConfirm: () => void): ButtonProps | undefined {
6975
const calls = mockButtonSpy.mock.calls;
70-
if (shouldStackButtons) {
71-
return calls.find((call) => call[0].pressOnEnter)?.[0];
72-
}
73-
return calls.find((call) => call[0].pressOnEnter)?.[0];
76+
return calls.find((call) => call[0].onPress === onConfirm)?.[0];
7477
}
7578

7679
const testCases = [
@@ -84,15 +87,26 @@ describe('ConfirmContent', () => {
8487
{shouldShowCancelButton: true, danger: true, success: true, expectedSuccess: false},
8588
];
8689

90+
function expectedVariant(danger: boolean, expectedSuccess: boolean): string | undefined {
91+
if (danger) {
92+
return CONST.BUTTON_VARIANT.DANGER;
93+
}
94+
if (expectedSuccess) {
95+
return CONST.BUTTON_VARIANT.SUCCESS;
96+
}
97+
return undefined;
98+
}
99+
87100
describe('stacked buttons (shouldStackButtons=true)', () => {
88101
it.each(testCases)(
89-
'confirm button success=$expectedSuccess when shouldShowCancelButton=$shouldShowCancelButton, danger=$danger, success=$success',
102+
'confirm button variant=$expectedSuccess when shouldShowCancelButton=$shouldShowCancelButton, danger=$danger, success=$success',
90103
({shouldShowCancelButton, danger, success, expectedSuccess}) => {
91104
mockButtonSpy.mockClear();
105+
const onConfirm = jest.fn();
92106
render(
93107
<ConfirmContent
94108
title="Test"
95-
onConfirm={jest.fn()}
109+
onConfirm={onConfirm}
96110
isVisible
97111
shouldStackButtons
98112
shouldShowCancelButton={shouldShowCancelButton}
@@ -101,22 +115,22 @@ describe('ConfirmContent', () => {
101115
/>,
102116
);
103117

104-
const confirmProps = getConfirmButtonProps(true);
105-
expect(confirmProps?.success).toBe(expectedSuccess);
106-
expect(confirmProps?.danger).toBe(danger);
118+
const confirmProps = getConfirmButtonProps(onConfirm);
119+
expect(confirmProps?.variant).toBe(expectedVariant(danger, expectedSuccess));
107120
},
108121
);
109122
});
110123

111124
describe('side-by-side buttons (shouldStackButtons=false)', () => {
112125
it.each(testCases)(
113-
'confirm button success=$expectedSuccess when shouldShowCancelButton=$shouldShowCancelButton, danger=$danger, success=$success',
126+
'confirm button variant=$expectedSuccess when shouldShowCancelButton=$shouldShowCancelButton, danger=$danger, success=$success',
114127
({shouldShowCancelButton, danger, success, expectedSuccess}) => {
115128
mockButtonSpy.mockClear();
129+
const onConfirm = jest.fn();
116130
render(
117131
<ConfirmContent
118132
title="Test"
119-
onConfirm={jest.fn()}
133+
onConfirm={onConfirm}
120134
isVisible
121135
shouldStackButtons={false}
122136
shouldShowCancelButton={shouldShowCancelButton}
@@ -125,9 +139,8 @@ describe('ConfirmContent', () => {
125139
/>,
126140
);
127141

128-
const confirmProps = getConfirmButtonProps(false);
129-
expect(confirmProps?.success).toBe(expectedSuccess);
130-
expect(confirmProps?.danger).toBe(danger);
142+
const confirmProps = getConfirmButtonProps(onConfirm);
143+
expect(confirmProps?.variant).toBe(expectedVariant(danger, expectedSuccess));
131144
},
132145
);
133146
});

0 commit comments

Comments
 (0)