|
| 1 | +import React from 'react'; |
| 2 | +import { Text, StyleSheet } from 'react-native'; |
| 3 | + |
| 4 | +import { act, fireEvent, render } from '@testing-library/react-native'; |
| 5 | + |
| 6 | +import Dialog from '../../components/Dialog/Dialog'; |
| 7 | +import Button from '../Button/Button'; |
| 8 | + |
| 9 | +jest.mock('react-native-safe-area-context', () => ({ |
| 10 | + useSafeAreaInsets: () => ({ bottom: 44, left: 0, right: 0, top: 37 }), |
| 11 | +})); |
| 12 | + |
| 13 | +describe('Dialog', () => { |
| 14 | + it('should render passed children', () => { |
| 15 | + const { getByTestId } = render( |
| 16 | + <Dialog visible testID="dialog"> |
| 17 | + <Text>This is simple dialog</Text> |
| 18 | + </Dialog> |
| 19 | + ); |
| 20 | + |
| 21 | + expect(getByTestId('dialog')).toHaveTextContent('This is simple dialog'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should call onDismiss when dismissable', () => { |
| 25 | + const onDismiss = jest.fn(); |
| 26 | + const { getByTestId } = render( |
| 27 | + <Dialog visible onDismiss={onDismiss} dismissable testID="dialog"> |
| 28 | + <Text>This is simple dialog</Text> |
| 29 | + </Dialog> |
| 30 | + ); |
| 31 | + |
| 32 | + fireEvent.press(getByTestId('dialog-backdrop')); |
| 33 | + |
| 34 | + act(() => { |
| 35 | + jest.runAllTimers(); |
| 36 | + }); |
| 37 | + expect(onDismiss).toHaveBeenCalledTimes(1); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should apply top margin to the first child if the dialog is V3', () => { |
| 41 | + const { getByTestId } = render( |
| 42 | + <Dialog visible={true}> |
| 43 | + <Dialog.Title testID="dialog-content"> |
| 44 | + <Text>Test Dialog Content</Text> |
| 45 | + </Dialog.Title> |
| 46 | + </Dialog> |
| 47 | + ); |
| 48 | + |
| 49 | + expect(getByTestId('dialog-content')).toHaveStyle({ |
| 50 | + marginTop: 24, |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe('DialogActions', () => { |
| 56 | + it('should render passed children', () => { |
| 57 | + const { getByTestId } = render( |
| 58 | + <Dialog.Actions> |
| 59 | + <Button testID="button-cancel">Cancel</Button> |
| 60 | + <Button testID="button-ok">Ok</Button> |
| 61 | + </Dialog.Actions> |
| 62 | + ); |
| 63 | + |
| 64 | + expect(getByTestId('button-cancel')).toBeDefined(); |
| 65 | + expect(getByTestId('button-ok')).toBeDefined(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should apply default styles', () => { |
| 69 | + const { getByTestId } = render( |
| 70 | + <Dialog.Actions testID="dialog-actions"> |
| 71 | + <Button>Cancel</Button> |
| 72 | + <Button>Ok</Button> |
| 73 | + </Dialog.Actions> |
| 74 | + ); |
| 75 | + |
| 76 | + const dialogActionsContainer = getByTestId('dialog-actions'); |
| 77 | + const dialogActionButtons = dialogActionsContainer.children; |
| 78 | + |
| 79 | + expect(dialogActionsContainer).toHaveStyle({ |
| 80 | + paddingBottom: 24, |
| 81 | + paddingHorizontal: 24, |
| 82 | + }); |
| 83 | + expect(dialogActionButtons[0]).toHaveStyle({ marginRight: 8 }); |
| 84 | + expect(dialogActionButtons[1]).toHaveStyle({ marginRight: 0 }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should apply custom styles', () => { |
| 88 | + const { getByTestId } = render( |
| 89 | + <Dialog.Actions testID="dialog-actions"> |
| 90 | + <Button style={styles.spacing}>Cancel</Button> |
| 91 | + <Button style={styles.noSpacing}>Ok</Button> |
| 92 | + </Dialog.Actions> |
| 93 | + ); |
| 94 | + |
| 95 | + const dialogActionsContainer = getByTestId('dialog-actions'); |
| 96 | + const dialogActionButtons = dialogActionsContainer.children; |
| 97 | + |
| 98 | + expect(dialogActionButtons[0]).toHaveStyle({ margin: 10 }); |
| 99 | + expect(dialogActionButtons[1]).toHaveStyle({ margin: 0 }); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +const styles = StyleSheet.create({ |
| 104 | + spacing: { |
| 105 | + margin: 10, |
| 106 | + }, |
| 107 | + noSpacing: { |
| 108 | + margin: 0, |
| 109 | + }, |
| 110 | +}); |
0 commit comments