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

Fix/solve snapshots tests failing node 18 #15436

Merged
merged 8 commits into from
Jan 16, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ jest.spyOn(axiosInstance, 'get').mockResolvedValue({

jest.spyOn(Date, 'now').mockImplementation(() => new Date('2015-10-01T08:00:00.000Z'));

// TO BE REMOVED: we have added this mock to prevent errors in the snapshots caused by the Unicode space character
// before AM/PM in the dates, after the introduction of node 18.13
jest.mock('react-intl', () => {
const reactIntl = jest.requireActual('react-intl');
const intl = reactIntl.createIntl({
locale: 'en',
});

intl.formatDate = jest.fn(() => '11/15/2021');
intl.formatTime = jest.fn(() => '12:00 AM');

return {
...reactIntl,
useIntl: () => intl,
};
});

const client = new QueryClient({
defaultOptions: {
queries: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,38 @@ import { createMemoryHistory } from 'history';
import qs from 'qs';
import FilterListURLQuery from '../index';

// TO BE REMOVED: we have added this mock to prevent errors in the snapshots caused by the Unicode space character
// before AM/PM in the dates, after the introduction of node 18.13
jest.mock('react-intl', () => {
const reactIntl = jest.requireActual('react-intl');
const intl = reactIntl.createIntl({
locale: 'en',
messages: {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you store these messages as a constant above please and then re-use them in the two cases they are used?

'components.FilterOptions.FILTER_TYPES.$eq': 'is',
'components.FilterOptions.FILTER_TYPES.$ne': 'is not',
'components.FilterOptions.FILTER_TYPES.$contains': 'contains (case sensitive)',
'components.FilterOptions.FILTER_TYPES.$notContains': 'does not contain (case sensitive)',
'components.FilterOptions.FILTER_TYPES.$gt': 'is greater than',
'components.FilterOptions.FILTER_TYPES.$gte': 'is greater than or equal to',
'components.FilterOptions.FILTER_TYPES.$lt': 'is lower than',
'components.FilterOptions.FILTER_TYPES.$lte': 'is lower than or equal to',
'components.FilterOptions.FILTER_TYPES.$startsWith': 'starts with',
'components.FilterOptions.FILTER_TYPES.$endsWith': 'ends with',
'components.FilterOptions.FILTER_TYPES.$null': 'is null',
'components.FilterOptions.FILTER_TYPES.$notNull': 'is not null',
},
textComponent: 'span',
});

intl.formatDate = jest.fn(() => 'Wednesday, September 1, 2021');
intl.formatTime = jest.fn(() => '12:45 AM');

return {
...reactIntl,
useIntl: () => intl,
};
});

const makeApp = (history, filtersSchema) => (
<Router history={history}>
<ThemeProvider theme={lightTheme}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { IntlProvider } from 'react-intl';
import { IntlProvider, useIntl } from 'react-intl';
import { ThemeProvider, lightTheme } from '@strapi/design-system';
import RelativeTime from '../index';

Expand All @@ -12,6 +12,17 @@ const App = (
</ThemeProvider>
);

// TO BE REMOVED: we have added this mock to prevent errors in the snapshots caused by the Unicode space character
// before AM/PM in the dates, after the introduction of node 18.13
jest.mock('react-intl', () => ({
...jest.requireActual('react-intl'),
useIntl: jest.fn(() => ({
formatDate: jest.fn(() => '10/1/2015'),
formatTime: jest.fn(() => '7:55 AM'),
formatRelativeTime: jest.fn(() => '5 minutes ago'),
})),
}));

describe('RelativeTime', () => {
beforeEach(() => {
jest.spyOn(Date, 'now').mockImplementation(() => new Date('2015-10-01 08:00:00'));
Expand All @@ -37,6 +48,11 @@ describe('RelativeTime', () => {
});

it('can display the relative time for a future date', () => {
useIntl.mockReturnValueOnce({
formatDate: jest.fn(() => '10/1/2015'),
formatTime: jest.fn(() => '7:50 AM'),
formatRelativeTime: jest.fn(() => 'in 5 minutes'),
});
jest.spyOn(Date, 'now').mockImplementation(() => new Date('2015-10-01 07:50:00'));

render(App);
Expand All @@ -45,6 +61,11 @@ describe('RelativeTime', () => {
});

it('can display the relative time for a past date', () => {
useIntl.mockReturnValueOnce({
formatDate: jest.fn(() => '10/1/2015'),
formatTime: jest.fn(() => '8:00 AM'),
formatRelativeTime: jest.fn(() => '5 minutes ago'),
});
jest.spyOn(Date, 'now').mockImplementation(() => new Date('2015-10-01 08:00:00'));

render(App);
Expand Down