Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(test) O3-3853: Add Unit Tests for Datepicker Component #1126

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/framework/esm-styleguide/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"@openmrs/esm-translations": "workspace:*",
"@types/geopattern": "^1.2.9",
"autoprefixer": "^9.8.8",
"cross-env": "^7.0.3",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required?

"css-minimizer-webpack-plugin": "^1.2.0",
"i18next": "^21.10.0",
"jest": "^29.7.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -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
});
Comment on lines +8 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
});
Object.defineProperty(window, 'i18next', {
value: {
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(),
}
})

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


describe('OpenmrsDatePicker Component', () => {
// Test to ensure that the component renders without crashing
it('renders without crashing', () => {
render(<OpenmrsDatePicker />);
expect(screen.getByRole('group')).toBeInTheDocument();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the comment in #1130

});

// Test to verify the correct label is displayed when `labelText` prop is provided
it('displays the correct label when labelText is provided', () => {
render(<OpenmrsDatePicker labelText="Select Date" />);
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(<OpenmrsDatePicker defaultValue={new Date(2024, 7, 27)} />);
expect(container.querySelector('input')).toHaveValue('2024-08-27');
});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add a test case to ensure that the current date is highlighted in the calendar grid? We should test this for different locales

// Test to verify that the component does not crash when provided with an invalid date value
it('does not crash with invalid dates', () => {
render(<OpenmrsDatePicker value={null} />);
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(<OpenmrsDatePicker invalid={true} invalidText="Invalid Date" />);
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(<OpenmrsDatePicker onChange={() => {}} 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', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('handles edge cases gracefully', () => {
it('disables dates that are outside the given min and max range', () => {

const { container } = render(<OpenmrsDatePicker minDate={new Date(2020, 0, 1)} maxDate={new Date(2020, 11, 31)} />);

const input = container.querySelector('input');

if (input?.hasAttribute('aria-invalid')) {
expect(input).toHaveAttribute('aria-invalid', 'false');
} else {
expect(input).not.toHaveAttribute('aria-invalid');
}
Comment on lines +75 to +79
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the right way to do this. We should check that dates outside the min and max range are disabled.

});
});
Loading