From 00bd4ae04a6a432ab2ba68a6e1ce2cba170dd58d Mon Sep 17 00:00:00 2001 From: stanislavh Date: Thu, 16 Feb 2023 09:55:38 +0100 Subject: SONAR-17812 Upgrade intl to 6.2.5 --- .../src/main/js/components/intl/DateFormatter.tsx | 2 +- .../src/main/js/components/intl/DateFromNow.tsx | 2 +- .../main/js/components/intl/DateTimeFormatter.tsx | 7 +-- .../src/main/js/components/intl/TimeFormatter.tsx | 11 +++-- .../intl/__tests__/DateFormatter-test.tsx | 19 +++++--- .../components/intl/__tests__/DateFromNow-test.tsx | 51 +++++++++------------- .../intl/__tests__/DateTimeFormatter-test.tsx | 17 +++++--- .../intl/__tests__/TimeFormatter-test.tsx | 21 ++++++--- .../__snapshots__/DateFormatter-test.tsx.snap | 23 ---------- .../__snapshots__/DateFromNow-test.tsx.snap | 22 ---------- .../__snapshots__/DateTimeFormatter-test.tsx.snap | 14 ------ .../__snapshots__/TimeFormatter-test.tsx.snap | 22 ---------- .../intl/__tests__/__snapshots__/dateUtils-test.ts | 42 ------------------ .../js/components/intl/__tests__/dateUtils-test.ts | 42 ++++++++++++++++++ .../src/main/js/components/intl/dateUtils.ts | 4 +- 15 files changed, 116 insertions(+), 183 deletions(-) delete mode 100644 server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/DateFormatter-test.tsx.snap delete mode 100644 server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/DateFromNow-test.tsx.snap delete mode 100644 server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/DateTimeFormatter-test.tsx.snap delete mode 100644 server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/TimeFormatter-test.tsx.snap delete mode 100644 server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/dateUtils-test.ts create mode 100644 server/sonar-web/src/main/js/components/intl/__tests__/dateUtils-test.ts (limited to 'server/sonar-web/src/main/js/components') diff --git a/server/sonar-web/src/main/js/components/intl/DateFormatter.tsx b/server/sonar-web/src/main/js/components/intl/DateFormatter.tsx index 8f787ae6517..f83b2cebf76 100644 --- a/server/sonar-web/src/main/js/components/intl/DateFormatter.tsx +++ b/server/sonar-web/src/main/js/components/intl/DateFormatter.tsx @@ -43,7 +43,7 @@ export const longFormatterOption: FormatDateOptions = { export default function DateFormatter({ children, date, long }: DateFormatterProps) { return ( - {children} + {children ? (d) => <>{children(d)} : undefined} ); } diff --git a/server/sonar-web/src/main/js/components/intl/DateFromNow.tsx b/server/sonar-web/src/main/js/components/intl/DateFromNow.tsx index 5bafaa4cd8a..ca95cdb6c8a 100644 --- a/server/sonar-web/src/main/js/components/intl/DateFromNow.tsx +++ b/server/sonar-web/src/main/js/components/intl/DateFromNow.tsx @@ -58,7 +58,7 @@ export default function DateFromNow(props: DateFromNowProps) { {(formattedDate) => ( - {children as FormattedRelativeTime['props']['children']} + {(d) => <>{children(d)}} )} diff --git a/server/sonar-web/src/main/js/components/intl/DateTimeFormatter.tsx b/server/sonar-web/src/main/js/components/intl/DateTimeFormatter.tsx index e8faa804a07..6588210f500 100644 --- a/server/sonar-web/src/main/js/components/intl/DateTimeFormatter.tsx +++ b/server/sonar-web/src/main/js/components/intl/DateTimeFormatter.tsx @@ -25,6 +25,7 @@ import { ParsableDate } from '../../types/dates'; interface Props { children?: (formattedDate: string) => React.ReactNode; date: ParsableDate; + timeZone?: string; } export const formatterOption: FormatDateOptions = { @@ -35,10 +36,10 @@ export const formatterOption: FormatDateOptions = { minute: 'numeric', }; -export default function DateTimeFormatter({ children, date }: Props) { +export default function DateTimeFormatter({ children, date, ...rest }: Props) { return ( - - {children} + + {children ? (d) => <>{children(d)} : undefined} ); } diff --git a/server/sonar-web/src/main/js/components/intl/TimeFormatter.tsx b/server/sonar-web/src/main/js/components/intl/TimeFormatter.tsx index 036fdddbab6..2834f11b6bd 100644 --- a/server/sonar-web/src/main/js/components/intl/TimeFormatter.tsx +++ b/server/sonar-web/src/main/js/components/intl/TimeFormatter.tsx @@ -26,6 +26,7 @@ export interface TimeFormatterProps { children?: (formattedDate: string) => React.ReactNode; date: ParsableDate; long?: boolean; + timeZone?: string; } export const formatterOption: FormatDateOptions = { hour: 'numeric', minute: 'numeric' }; @@ -36,10 +37,14 @@ export const longFormatterOption: FormatDateOptions = { second: 'numeric', }; -export default function TimeFormatter({ children, date, long }: TimeFormatterProps) { +export default function TimeFormatter({ children, date, long, ...rest }: TimeFormatterProps) { return ( - - {children} + + {children ? (d) => <>{children(d)} : undefined} ); } diff --git a/server/sonar-web/src/main/js/components/intl/__tests__/DateFormatter-test.tsx b/server/sonar-web/src/main/js/components/intl/__tests__/DateFormatter-test.tsx index 1e0ba86ea3a..bd752e1a9bd 100644 --- a/server/sonar-web/src/main/js/components/intl/__tests__/DateFormatter-test.tsx +++ b/server/sonar-web/src/main/js/components/intl/__tests__/DateFormatter-test.tsx @@ -17,19 +17,26 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { shallow } from 'enzyme'; +import { screen } from '@testing-library/react'; import * as React from 'react'; +import { renderComponent } from '../../../helpers/testReactTestingUtils'; import DateFormatter, { DateFormatterProps } from '../DateFormatter'; it('should render correctly', () => { - expect(shallowRender()).toMatchSnapshot('standard'); - expect(shallowRender({ long: true })).toMatchSnapshot('long'); + renderDateFormatter({}, (formatted: string) => {formatted}); + expect(screen.getByText('Feb 20, 2020')).toBeInTheDocument(); + + renderDateFormatter({ long: true }); + expect(screen.getByText('February 20, 2020')).toBeInTheDocument(); }); -function shallowRender(overrides: Partial = {}) { - return shallow( +function renderDateFormatter( + overrides: Partial = {}, + children?: (d: string) => React.ReactNode +) { + return renderComponent( - {(formatted) => {formatted}} + {children} ); } diff --git a/server/sonar-web/src/main/js/components/intl/__tests__/DateFromNow-test.tsx b/server/sonar-web/src/main/js/components/intl/__tests__/DateFromNow-test.tsx index 8c1c927da45..e0eef0da53a 100644 --- a/server/sonar-web/src/main/js/components/intl/__tests__/DateFromNow-test.tsx +++ b/server/sonar-web/src/main/js/components/intl/__tests__/DateFromNow-test.tsx @@ -17,31 +17,27 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { shallow } from 'enzyme'; +import { screen } from '@testing-library/react'; import * as React from 'react'; -import { FormattedRelativeTime, IntlProvider } from 'react-intl'; +import { renderComponent } from '../../../helpers/testReactTestingUtils'; import DateFromNow, { DateFromNowProps } from '../DateFromNow'; -import DateTimeFormatter from '../DateTimeFormatter'; - -const date = '2020-02-20T20:20:20Z'; jest.mock('../dateUtils', () => ({ getRelativeTimeProps: jest.fn().mockReturnValue({ value: -1, unit: 'year' }), })); +const date = '2020-02-20T20:20:20Z'; + it('should render correctly', () => { - const wrapper = shallowRender(); + renderDateFromNow({ date }); - expect(wrapper).toMatchSnapshot(); - expect(wrapper.find(DateTimeFormatter).props().children!(date)).toMatchSnapshot('children'); + expect(screen.getByText('1 year ago')).toBeInTheDocument(); }); -it('should render correctly when there is no date', () => { - const children = jest.fn(); - - shallowRender({ date: undefined }, children); +it('should render correctly when there is no date or no children', () => { + renderDateFromNow({ date: undefined }); - expect(children).toHaveBeenCalledWith('never'); + expect(screen.getByText('never')).toBeInTheDocument(); }); it('should render correctly when the date is less than one hour in the past', () => { @@ -50,27 +46,20 @@ it('should render correctly when the date is less than one hour in the past', () const mockDateNow = jest .spyOn(Date, 'now') .mockImplementation(() => new Date(date) as unknown as number); - const children = jest.fn(); - shallowRender({ date: veryCloseDate, hourPrecision: true }, children) - .dive() // into DateTimeFormatter - .dive() // into DateFormatter - .dive() // into rendering function - .find(FormattedRelativeTime) - .props().children!(date); + renderDateFromNow({ date: veryCloseDate, hourPrecision: true }); - expect(children).toHaveBeenCalledWith('less_than_1_hour_ago'); + expect(screen.getByText('less_than_1_hour_ago')).toBeInTheDocument(); mockDateNow.mockRestore(); }); -function shallowRender(overrides: Partial = {}, children: jest.Mock = jest.fn()) { - return shallow( - - - {(formattedDate) => children(formattedDate)} - - - ) - .dive() // into the ContextProvider generated by IntlProvider - .dive(); // into the DateFromNow we actually want to render +function renderDateFromNow( + overrides: Partial = {}, + children: jest.Mock = jest.fn((d) => <>{d}) +) { + return renderComponent( + + {children} + + ); } diff --git a/server/sonar-web/src/main/js/components/intl/__tests__/DateTimeFormatter-test.tsx b/server/sonar-web/src/main/js/components/intl/__tests__/DateTimeFormatter-test.tsx index 2f2de9b3460..7caddd1f23e 100644 --- a/server/sonar-web/src/main/js/components/intl/__tests__/DateTimeFormatter-test.tsx +++ b/server/sonar-web/src/main/js/components/intl/__tests__/DateTimeFormatter-test.tsx @@ -17,18 +17,23 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { shallow } from 'enzyme'; +import { screen } from '@testing-library/react'; import * as React from 'react'; +import { renderComponent } from '../../../helpers/testReactTestingUtils'; import DateTimeFormatter from '../DateTimeFormatter'; it('should render correctly', () => { - expect(shallowRender()).toMatchSnapshot('standard'); + renderDateTimeFormatter(); + expect(screen.getByText('February 20, 2020, 8:20 PM')).toBeInTheDocument(); + + renderDateTimeFormatter((formatted: string) => Nice date: {formatted}); + expect(screen.getByText('Nice date: February 20, 2020, 8:20 PM')).toBeInTheDocument(); }); -function shallowRender() { - return shallow( - - {(formatted) => {formatted}} +function renderDateTimeFormatter(children?: (d: string) => React.ReactNode) { + return renderComponent( + + {children} ); } diff --git a/server/sonar-web/src/main/js/components/intl/__tests__/TimeFormatter-test.tsx b/server/sonar-web/src/main/js/components/intl/__tests__/TimeFormatter-test.tsx index 4340f37afde..8557b317c94 100644 --- a/server/sonar-web/src/main/js/components/intl/__tests__/TimeFormatter-test.tsx +++ b/server/sonar-web/src/main/js/components/intl/__tests__/TimeFormatter-test.tsx @@ -17,19 +17,26 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { shallow } from 'enzyme'; +import { screen } from '@testing-library/react'; import * as React from 'react'; +import { renderComponent } from '../../../helpers/testReactTestingUtils'; import TimeFormatter, { TimeFormatterProps } from '../TimeFormatter'; it('should render correctly', () => { - expect(shallowRender()).toMatchSnapshot('standard'); - expect(shallowRender({ long: true })).toMatchSnapshot('long'); + renderTimeFormatter({}, (formatted: string) => {formatted}); + expect(screen.getByText('8:20 PM')).toBeInTheDocument(); + + renderTimeFormatter({ long: true }); + expect(screen.getByText('8:20:20 PM')).toBeInTheDocument(); }); -function shallowRender(overrides: Partial = {}) { - return shallow( - - {(formatted) => {formatted}} +function renderTimeFormatter( + overrides: Partial = {}, + children?: (d: string) => React.ReactNode +) { + return renderComponent( + + {children} ); } diff --git a/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/DateFormatter-test.tsx.snap b/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/DateFormatter-test.tsx.snap deleted file mode 100644 index 2f603c6c1bd..00000000000 --- a/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/DateFormatter-test.tsx.snap +++ /dev/null @@ -1,23 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should render correctly: long 1`] = ` - - - -`; - -exports[`should render correctly: standard 1`] = ` - - - -`; diff --git a/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/DateFromNow-test.tsx.snap b/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/DateFromNow-test.tsx.snap deleted file mode 100644 index f893ec98cf7..00000000000 --- a/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/DateFromNow-test.tsx.snap +++ /dev/null @@ -1,22 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should render correctly 1`] = ` - - - -`; - -exports[`should render correctly: children 1`] = ` - - - [Function] - - -`; diff --git a/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/DateTimeFormatter-test.tsx.snap b/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/DateTimeFormatter-test.tsx.snap deleted file mode 100644 index c991112f771..00000000000 --- a/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/DateTimeFormatter-test.tsx.snap +++ /dev/null @@ -1,14 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should render correctly: standard 1`] = ` - - - -`; diff --git a/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/TimeFormatter-test.tsx.snap b/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/TimeFormatter-test.tsx.snap deleted file mode 100644 index 7c19be3a479..00000000000 --- a/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/TimeFormatter-test.tsx.snap +++ /dev/null @@ -1,22 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should render correctly: long 1`] = ` - - - -`; - -exports[`should render correctly: standard 1`] = ` - - - -`; diff --git a/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/dateUtils-test.ts b/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/dateUtils-test.ts deleted file mode 100644 index 9661ab376e5..00000000000 --- a/server/sonar-web/src/main/js/components/intl/__tests__/__snapshots__/dateUtils-test.ts +++ /dev/null @@ -1,42 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -import { getRelativeTimeProps } from '../../dateUtils'; - -const mockDateNow = jest.spyOn(Date, 'now'); - -describe('getRelativeTimeProps', () => { - mockDateNow.mockImplementation(() => new Date('2021-02-20T20:20:20Z').getTime()); - - it.each([ - ['year', '2020-02-19T20:20:20Z', -1], - ['month', '2020-11-18T20:20:20Z', -3], - ['day', '2021-02-18T18:20:20Z', -2], - ])('should return the correct props for dates older than a %s', (unit, date, value) => { - expect(getRelativeTimeProps(date)).toEqual({ value, unit }); - }); - - it('should return the correct props for dates from less than a day ago', () => { - expect(getRelativeTimeProps('2021-02-20T20:19:45Z')).toEqual({ - value: -35, - unit: 'second', - updateIntervalInSeconds: 10, - }); - }); -}); diff --git a/server/sonar-web/src/main/js/components/intl/__tests__/dateUtils-test.ts b/server/sonar-web/src/main/js/components/intl/__tests__/dateUtils-test.ts new file mode 100644 index 00000000000..2d651cee59d --- /dev/null +++ b/server/sonar-web/src/main/js/components/intl/__tests__/dateUtils-test.ts @@ -0,0 +1,42 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import { getRelativeTimeProps } from '../dateUtils'; + +const mockDateNow = jest.spyOn(Date, 'now'); + +describe('getRelativeTimeProps', () => { + mockDateNow.mockImplementation(() => new Date('2021-02-20T20:20:20Z').getTime()); + + it.each([ + ['year', '2020-02-19T20:20:20Z', -1], + ['month', '2020-11-18T20:20:20Z', -3], + ['day', '2021-02-18T18:20:20Z', -2], + ])('should return the correct props for dates older than a %s', (unit, date, value) => { + expect(getRelativeTimeProps(date)).toEqual({ value, unit }); + }); + + it('should return the correct props for dates from less than a day ago', () => { + expect(getRelativeTimeProps('2021-02-20T20:19:45Z')).toEqual({ + value: -35, + unit: 'second', + updateIntervalInSeconds: 10, + }); + }); +}); diff --git a/server/sonar-web/src/main/js/components/intl/dateUtils.ts b/server/sonar-web/src/main/js/components/intl/dateUtils.ts index 110d4b57b27..513a423fb90 100644 --- a/server/sonar-web/src/main/js/components/intl/dateUtils.ts +++ b/server/sonar-web/src/main/js/components/intl/dateUtils.ts @@ -23,7 +23,7 @@ import { differenceInSeconds, differenceInYears, } from 'date-fns'; -import { FormattedRelativeTime } from 'react-intl'; +import { Props as FormattedRelativeTimeProps } from 'react-intl/src/components/relative'; import { parseDate } from '../../helpers/dates'; import { ParsableDate } from '../../types/dates'; @@ -31,7 +31,7 @@ const UPDATE_INTERVAL_IN_SECONDS = 10; export function getRelativeTimeProps( parsableDate: ParsableDate -): Pick { +): Pick { const date = parseDate(parsableDate); const y = differenceInYears(date, Date.now()); -- cgit v1.2.3