From 3709a0502ccf979f1acbe67413fdeb43c0c749e9 Mon Sep 17 00:00:00 2001 From: Josselin TILLAY Date: Mon, 18 Dec 2023 09:36:53 +0100 Subject: [PATCH] :white_check_mark: DateField - add tests --- src/__tests__/components/DateField.test.tsx | 117 ++++++++ .../__snapshots__/DateField.test.tsx.snap | 279 ++++++++++++++++++ 2 files changed, 396 insertions(+) create mode 100644 src/__tests__/components/DateField.test.tsx create mode 100644 src/__tests__/components/__snapshots__/DateField.test.tsx.snap diff --git a/src/__tests__/components/DateField.test.tsx b/src/__tests__/components/DateField.test.tsx new file mode 100644 index 00000000..36e24b96 --- /dev/null +++ b/src/__tests__/components/DateField.test.tsx @@ -0,0 +1,117 @@ +import React from 'react'; +import { fireEvent, render, act } from '@testing-library/react-native'; +import { ThemeProvider } from '../../styles/themes'; +import { DateField } from '../../components'; + +const mockedCallback = jest.fn(); +const mockedTestID = 'mockedTestID'; + +describe('MODULE | DateField', () => { + it('component renders correctly in empty mode', () => { + const tree = render( + + + , + ); + expect(tree.toJSON()).toMatchSnapshot(); + }); + + it('component renders correctly empty focused state', async () => { + const tree = render( + + + , + ); + + await act(async () => { + fireEvent(tree.getByTestId(mockedTestID), 'focus'); + }); + + expect(tree.toJSON()).toMatchSnapshot(); + }); + + it('component renders correctly filled focused state', async () => { + const tree = render( + + + , + ); + + await act(async () => { + fireEvent(tree.getByTestId(mockedTestID), 'focus'); + }); + + expect(tree.toJSON()).toMatchSnapshot(); + }); + + it('component renders correctly filled unfocused state', async () => { + const tree = render( + + + , + ); + + expect(tree.toJSON()).toMatchSnapshot(); + }); + + it('component renders correctly in error state', async () => { + const tree = render( + + + , + ); + + expect(tree.toJSON()).toMatchSnapshot(); + }); + + it('fires callback when on input value change', async () => { + const tree = render( + + + , + ); + + await act(async () => { + fireEvent.changeText(tree.getByTestId(mockedTestID), '12'); + }); + + expect(mockedCallback).toHaveBeenCalledWith('12'); + }); + + it('shouldnt send more than 2 characters', async () => { + const tree = render( + + + , + ); + + await act(async () => { + fireEvent.changeText(tree.getByTestId(mockedTestID), '1234'); + }); + + expect(mockedCallback).toHaveBeenCalledWith('12'); + }); + + it('shouldnt accept non number characters', async () => { + const tree = render( + + + , + ); + + await act(async () => { + fireEvent.changeText(tree.getByTestId(mockedTestID), '1.'); + }); + + expect(mockedCallback).toHaveBeenCalledWith('1'); + }); +}); diff --git a/src/__tests__/components/__snapshots__/DateField.test.tsx.snap b/src/__tests__/components/__snapshots__/DateField.test.tsx.snap new file mode 100644 index 00000000..1f89916f --- /dev/null +++ b/src/__tests__/components/__snapshots__/DateField.test.tsx.snap @@ -0,0 +1,279 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`MODULE | DateField component renders correctly empty focused state 1`] = ` + + + + + +`; + +exports[`MODULE | DateField component renders correctly filled focused state 1`] = ` + + + + + +`; + +exports[`MODULE | DateField component renders correctly filled unfocused state 1`] = ` + + + + + +`; + +exports[`MODULE | DateField component renders correctly in empty mode 1`] = ` + + + + + +`; + +exports[`MODULE | DateField component renders correctly in error state 1`] = ` + + + + + +`;