From a4d09fcbd595425367e30330d8fc429f76194869 Mon Sep 17 00:00:00 2001 From: NY Samnang Date: Fri, 22 Mar 2024 12:08:09 +0700 Subject: [PATCH] Cover more unit testing --- __tests__/RBSheet.test.js | 90 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 7 deletions(-) diff --git a/__tests__/RBSheet.test.js b/__tests__/RBSheet.test.js index 6b6d02e..ea59018 100644 --- a/__tests__/RBSheet.test.js +++ b/__tests__/RBSheet.test.js @@ -1,28 +1,104 @@ import React from 'react'; -import {render, act} from '@testing-library/react-native'; +import { + render, + act, + fireEvent, + waitFor, + cleanup, +} from '@testing-library/react-native'; import RBSheet from '../src'; -describe('', () => { +describe('React Native Raw Bottom Sheet ', () => { let ref; let getByTestId; beforeEach(() => { ref = React.createRef(); getByTestId = render().getByTestId; - }); - - it('should render correctly', () => { - getByTestId = render().getByTestId; - act(() => { ref.current.open(); }); + }); + afterEach(cleanup); + + it('should render correctly', () => { expect(getByTestId('Modal')).toBeTruthy(); expect(getByTestId('KeyboardAvoidingView')).toBeTruthy(); expect(getByTestId('TouchableOpacity')).toBeTruthy(); expect(getByTestId('AnimatedView')).toBeTruthy(); + }); + + it('should render draggable correctly', () => { + getByTestId = render().getByTestId; + act(() => { + ref.current.open(); + }); expect(getByTestId('DraggableView')).toBeTruthy(); expect(getByTestId('DraggableIcon')).toBeTruthy(); }); + + it('should handle open and close method', async () => { + expect(getByTestId('Modal')).toBeTruthy(); + act(() => { + ref.current.close(); + }); + await waitFor(() => + expect(() => getByTestId('Modal')).toThrow( + 'Unable to find an element with testID: Modal', + ), + ); + }); + + it('should handle open and close callbacks', async () => { + const onOpen = jest.fn(); + const onClose = jest.fn(); + getByTestId = render( + , + ).getByTestId; + act(() => { + ref.current.open(); + }); + await waitFor(() => expect(onOpen).toHaveBeenCalled()); + act(() => { + ref.current.close(); + }); + await waitFor(() => expect(onClose).toHaveBeenCalled()); + }); + + it('should not close on press mask', () => { + fireEvent.press(getByTestId('TouchableOpacity')); + expect(getByTestId('Modal')).toBeTruthy(); + }); + + it('should close on press mask', async () => { + getByTestId = render().getByTestId; + act(() => { + ref.current.open(); + }); + fireEvent.press(getByTestId('TouchableOpacity')); + await waitFor(() => + expect(() => getByTestId('Modal')).toThrow( + 'Unable to find an element with testID: Modal', + ), + ); + }); + + it('should not close on press back', () => { + fireEvent(getByTestId('Modal'), 'requestClose'); + expect(getByTestId('Modal')).toBeTruthy(); + }); + + it('should close on press back', async () => { + getByTestId = render().getByTestId; + act(() => { + ref.current.open(); + }); + fireEvent(getByTestId('Modal'), 'requestClose'); + await waitFor(() => + expect(() => getByTestId('Modal')).toThrow( + 'Unable to find an element with testID: Modal', + ), + ); + }); });