-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathDialog.test.tsx
More file actions
168 lines (140 loc) · 4.45 KB
/
Dialog.test.tsx
File metadata and controls
168 lines (140 loc) · 4.45 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import React from 'react';
import {
Text,
StyleSheet,
Platform,
BackHandler as RNBackHandler,
BackHandlerStatic as RNBackHandlerStatic,
} from 'react-native';
import { act, fireEvent, render } from '@testing-library/react-native';
import Dialog from '../../components/Dialog/Dialog';
import Button from '../Button/Button';
interface BackHandlerStatic extends RNBackHandlerStatic {
mockPressBack(): void;
}
const BackHandler = RNBackHandler as BackHandlerStatic;
jest.mock('react-native/Libraries/Utilities/BackHandler', () =>
// eslint-disable-next-line jest/no-mocks-import
require('../../utils/__mocks__/BackHandler')
);
describe('Dialog', () => {
it('should render passed children', () => {
const { getByTestId } = render(
<Dialog visible testID="dialog">
<Text>This is simple dialog</Text>
</Dialog>
);
expect(getByTestId('dialog')).toHaveTextContent('This is simple dialog');
});
it('should call onDismiss when dismissable', () => {
const onDismiss = jest.fn();
const { getByTestId } = render(
<Dialog visible onDismiss={onDismiss} dismissable testID="dialog">
<Text>This is simple dialog</Text>
</Dialog>
);
fireEvent.press(getByTestId('dialog-backdrop'));
act(() => {
jest.runAllTimers();
});
expect(onDismiss).toHaveBeenCalledTimes(1);
});
it('should not call onDismiss when dismissable is false', () => {
const onDismiss = jest.fn();
const { getByTestId } = render(
<Dialog visible onDismiss={onDismiss} dismissable={false} testID="dialog">
<Text>This is simple dialog</Text>
</Dialog>
);
fireEvent.press(getByTestId('dialog-backdrop'));
act(() => {
jest.runAllTimers();
});
expect(onDismiss).toHaveBeenCalledTimes(0);
});
it('should call onDismiss on Android back button when dismissable is false but dismissableBackButton is true', () => {
Platform.OS = 'android';
const onDismiss = jest.fn();
const { getByTestId } = render(
<Dialog
visible
onDismiss={onDismiss}
dismissable={false}
dismissableBackButton
testID="dialog"
>
<Text>This is simple dialog</Text>
</Dialog>
);
fireEvent.press(getByTestId('dialog-backdrop'));
act(() => {
jest.runAllTimers();
});
expect(onDismiss).toHaveBeenCalledTimes(0);
act(() => {
BackHandler.mockPressBack();
jest.runAllTimers();
});
expect(onDismiss).toHaveBeenCalledTimes(1);
});
it('should apply top margin to the first child if the dialog is V3', () => {
const { getByTestId } = render(
<Dialog visible={true}>
<Dialog.Title testID="dialog-content">
<Text>Test Dialog Content</Text>
</Dialog.Title>
</Dialog>
);
expect(getByTestId('dialog-content')).toHaveStyle({
marginTop: 24,
});
});
});
describe('DialogActions', () => {
it('should render passed children', () => {
const { getByTestId } = render(
<Dialog.Actions>
<Button testID="button-cancel">Cancel</Button>
<Button testID="button-ok">Ok</Button>
</Dialog.Actions>
);
expect(getByTestId('button-cancel')).toBeDefined();
expect(getByTestId('button-ok')).toBeDefined();
});
it('should apply default styles', () => {
const { getByTestId } = render(
<Dialog.Actions testID="dialog-actions">
<Button>Cancel</Button>
<Button>Ok</Button>
</Dialog.Actions>
);
const dialogActionsContainer = getByTestId('dialog-actions');
const dialogActionButtons = dialogActionsContainer.children;
expect(dialogActionsContainer).toHaveStyle({
paddingBottom: 24,
paddingHorizontal: 24,
});
expect(dialogActionButtons[0]).toHaveStyle({ marginRight: 8 });
expect(dialogActionButtons[1]).toHaveStyle({ marginRight: 0 });
});
it('should apply custom styles', () => {
const { getByTestId } = render(
<Dialog.Actions testID="dialog-actions">
<Button style={styles.spacing}>Cancel</Button>
<Button style={styles.noSpacing}>Ok</Button>
</Dialog.Actions>
);
const dialogActionsContainer = getByTestId('dialog-actions');
const dialogActionButtons = dialogActionsContainer.children;
expect(dialogActionButtons[0]).toHaveStyle({ margin: 10 });
expect(dialogActionButtons[1]).toHaveStyle({ margin: 0 });
});
});
const styles = StyleSheet.create({
spacing: {
margin: 10,
},
noSpacing: {
margin: 0,
},
});