From 0bd31ad1e568b14a7f215af6002733e28dc0028a Mon Sep 17 00:00:00 2001 From: Samuel Muli Date: Tue, 27 Aug 2024 20:02:51 +0300 Subject: [PATCH] test: Add unit tests for Datepicker component --- .../framework/esm-styleguide/package.json | 1 + .../src/datepicker/datepicker.test.tsx | 81 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 packages/framework/esm-styleguide/src/datepicker/datepicker.test.tsx diff --git a/packages/framework/esm-styleguide/package.json b/packages/framework/esm-styleguide/package.json index 258fedcdd..0dc33117f 100644 --- a/packages/framework/esm-styleguide/package.json +++ b/packages/framework/esm-styleguide/package.json @@ -70,6 +70,7 @@ "@openmrs/esm-translations": "workspace:*", "@types/geopattern": "^1.2.9", "autoprefixer": "^9.8.8", + "cross-env": "^7.0.3", "css-minimizer-webpack-plugin": "^1.2.0", "i18next": "^21.10.0", "jest": "^29.7.0", diff --git a/packages/framework/esm-styleguide/src/datepicker/datepicker.test.tsx b/packages/framework/esm-styleguide/src/datepicker/datepicker.test.tsx new file mode 100644 index 000000000..954f63b8b --- /dev/null +++ b/packages/framework/esm-styleguide/src/datepicker/datepicker.test.tsx @@ -0,0 +1,81 @@ +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import { OpenmrsDatePicker } from './index'; +import { type i18n as i18nType } from 'i18next'; + +// Mocking the i18next object +beforeAll(() => { + window.i18next = { + language: 'en', + changeLanguage: jest.fn(), + t: jest.fn((key) => key), // Simple mock for translation function + // Other properties and methods that might be required + init: jest.fn(), + getFixedT: jest.fn(), + exists: jest.fn(), + loadNamespaces: jest.fn(), + loadLanguages: jest.fn(), + use: jest.fn(), + on: jest.fn(), + off: jest.fn(), + dir: jest.fn(), + cloneInstance: jest.fn(), + } as unknown as i18nType; // Casting the mock to the correct i18n type +}); + +describe('OpenmrsDatePicker Component', () => { + // Test to ensure that the component renders without crashing + it('renders without crashing', () => { + render(); + expect(screen.getByRole('group')).toBeInTheDocument(); + }); + + // Test to verify the correct label is displayed when `labelText` prop is provided + it('displays the correct label when labelText is provided', () => { + render(); + expect(screen.getByText('Select Date')).toBeInTheDocument(); + }); + + // Test to check if the component displays the correct default value + it('displays the correct default value', () => { + const { container } = render(); + expect(container.querySelector('input')).toHaveValue('2024-08-27'); + }); + + // Test to verify that the component does not crash when provided with an invalid date value + it('does not crash with invalid dates', () => { + render(); + expect(screen.getByRole('group')).toBeInTheDocument(); + }); + + // Test to check if the invalid text is displayed correctly when the `invalid` prop is true + it('it shows invalid text when the text is invalid', () => { + render(); + expect(screen.getByText('Invalid Date')).toBeInTheDocument(); + }); + + // Test to verify that an error is logged when both `onChange` and `onChangeRaw` props are provided + it('throws an error if both onChange and onChangeRaw are provided', () => { + const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + render( {}} onChangeRaw={() => {}} />); + expect(consoleErrorSpy).toHaveBeenCalledWith( + 'An OpenmrsDatePicker component was created with both onChange and onChangeRaw handlers defined. Only onChangeRaw will be used.', + ); + + consoleErrorSpy.mockRestore(); + }); + + // Test to ensure the component handles edge cases like `minDate` and `maxDate` correctly + it('handles edge cases gracefully', () => { + const { container } = render(); + + const input = container.querySelector('input'); + + if (input?.hasAttribute('aria-invalid')) { + expect(input).toHaveAttribute('aria-invalid', 'false'); + } else { + expect(input).not.toHaveAttribute('aria-invalid'); + } + }); +});