Skip to content

Commit

Permalink
⬆️(frontend) upgrade to react-router v7
Browse files Browse the repository at this point in the history
  • Loading branch information
jbpenrath committed Dec 3, 2024
1 parent cf84496 commit 170267c
Show file tree
Hide file tree
Showing 72 changed files with 1,803 additions and 2,067 deletions.
7 changes: 7 additions & 0 deletions src/frontend/jest/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import { FactoryConfig } from 'utils/test/factories/factories';
global.Request = Request as any;
global.Response = Response as any;

// https://github.com/remix-run/react-router/issues/12363#issuecomment-2496226528
if (!globalThis.TextEncoder || !globalThis.TextDecoder) {
const { TextDecoder, TextEncoder } = require('node:util');
globalThis.TextEncoder = TextEncoder;
globalThis.TextDecoder = TextDecoder;
}

/*
* A little trick to prevent so package to be reset when using `jest.resetModules()`.
* https://github.com/facebook/jest/issues/8987#issuecomment-584898030
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/api/lms/joanie.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { matchPath, PathMatch } from 'react-router-dom';
import { matchPath, PathMatch } from 'react-router';
import JoanieApi from 'api/joanie';
import { LMSBackend } from 'types/commonDataProps';
import { APIBackend, APILms } from 'types/api';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/components/CourseGlimpse/CourseLink.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Link } from 'react-router-dom';
import { Link } from 'react-router';
import { FunctionComponent, ReactElement } from 'react';

interface CourseLinkProps {
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/components/CourseGlimpse/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { render, screen } from '@testing-library/react';
import { IntlProvider } from 'react-intl';
import { MemoryRouter } from 'react-router-dom';
import { MemoryRouter } from 'react-router';
import { CommonDataProps } from 'types/commonDataProps';
import { RichieContextFactory } from 'utils/test/factories/richie';
import { CourseStateTextEnum } from 'types';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/components/CourseGlimpse/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IntlShape } from 'react-intl';
import { generatePath } from 'react-router-dom';
import { generatePath } from 'react-router';
import { Course as RichieCourse, isRichieCourse } from 'types/Course';
import {
CourseListItem as JoanieCourse,
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/components/ProtectedRoute/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { screen } from '@testing-library/dom';
import userEvent from '@testing-library/user-event';
import { Link, RouteObject } from 'react-router-dom';
import { Link, RouteObject } from 'react-router';
import { render } from 'utils/test/render';
import { RouterWrapper } from 'utils/test/wrappers/RouterWrapper';
import ProtectedRoute from '.';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/components/ProtectedRoute/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Navigate, Outlet } from 'react-router-dom';
import { Navigate, Outlet } from 'react-router';

interface ProtectedRouteProps {
isAllowed: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import { Button } from '@openfun/cunningham-react';
import { generatePath } from 'react-router-dom';
import { generatePath } from 'react-router';
import { SuccessIcon } from 'components/SuccessIcon';
import { getDashboardBasename } from 'widgets/Dashboard/hooks/useDashboardRouter/getDashboardBasename';
import { useSaleTunnelContext } from 'components/SaleTunnel/GenericSaleTunnel';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PropsWithChildren } from 'react';
import { render, screen, within } from '@testing-library/react';
import { IntlProvider } from 'react-intl';
import { MemoryRouter } from 'react-router-dom';
import { MemoryRouter } from 'react-router';
import userEvent from '@testing-library/user-event';
import { QueryClientProvider } from '@tanstack/react-query';
import fetchMock from 'fetch-mock';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PropsWithChildren } from 'react';
import { render, screen } from '@testing-library/react';
import { IntlProvider } from 'react-intl';
import { MemoryRouter } from 'react-router-dom';
import { MemoryRouter } from 'react-router';
import userEvent from '@testing-library/user-event';
import { QueryClientProvider } from '@tanstack/react-query';
import { faker } from '@faker-js/faker';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/components/SignContractButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button } from '@openfun/cunningham-react';
import { FormattedMessage, defineMessages } from 'react-intl';
import { useState } from 'react';
import { generatePath } from 'react-router-dom';
import { generatePath } from 'react-router';
import { LearnerContractFrame } from 'components/ContractFrame';
import {
Contract,
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/components/Tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
cloneElement,
useState,
} from 'react';
import { useNavigate } from 'react-router-dom';
import { useNavigate } from 'react-router';
import { noop } from 'utils';

interface TabProps extends PropsWithChildren {
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/hooks/useDefaultOrganizationId/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useParams, useSearchParams } from 'react-router-dom';
import { useParams, useSearchParams } from 'react-router';
import { useOrganizations } from 'hooks/useOrganizations';
import { CourseProductRelation, Organization } from 'types/Joanie';

Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/hooks/useLearnerCoursesSearch/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import { useSearchParams } from 'react-router';
import { Enrollment, CredentialOrder, OrderState, ProductType } from 'types/Joanie';
import { Maybe, Nullable } from 'types/utils';
import { useOrdersEnrollments } from 'pages/DashboardCourses/useOrdersEnrollments';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/hooks/useTeacherCoursesSearch/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState } from 'react';
import { useParams, useSearchParams } from 'react-router-dom';
import { useParams, useSearchParams } from 'react-router';
import { useCourseProductUnion } from 'hooks/useCourseProductUnion';
import { CourseListItem, CourseProductRelationLight, ProductType } from 'types/Joanie';
import { Maybe, Nullable } from 'types/utils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useParams } from 'react-router-dom';
import { useParams } from 'react-router';
import Banner, { BannerType } from 'components/Banner';
import { Spinner } from 'components/Spinner';
import { useAddress } from 'hooks/useAddresses';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getByText, queryByText, screen, waitFor } from '@testing-library/react';
import fetchMock from 'fetch-mock';
import userEvent from '@testing-library/user-event';
import { Outlet, generatePath } from 'react-router-dom';
import { Outlet, generatePath } from 'react-router';
import {
UserFactory,
RichieContextFactory as mockRichieContextFactory,
Expand Down Expand Up @@ -267,7 +267,7 @@ describe('<DahsboardEditCreditCard/>', () => {
);

// Redirected to the list route.
screen.getByText('Credit cards');
await screen.findByText('Credit cards');
const creditCardsContainers = await screen.findAllByTestId(/dashboard-credit-card__/);
expect(creditCardsContainers.length).toEqual(6);

Expand Down Expand Up @@ -333,7 +333,7 @@ describe('<DahsboardEditCreditCard/>', () => {
expect(screen.queryByText('An error occurred', { exact: false })).toBeNull();

// Redirected to the list route.
screen.getByText('Credit cards');
await screen.findByText('Credit cards');

await waitFor(() => {
// Assert that other credit cards appear in the list.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useParams } from 'react-router-dom';
import { useParams } from 'react-router';
import { useDashboardNavigate } from 'widgets/Dashboard/hooks/useDashboardRouter';
import { useCreditCard } from 'hooks/useCreditCards';
import { Spinner } from 'components/Spinner';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/pages/DashboardOrderLayout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generatePath, Outlet, useParams } from 'react-router-dom';
import { generatePath, Outlet, useParams } from 'react-router';
import { useIntl } from 'react-intl';
import { useMemo } from 'react';
import { getDashboardRouteLabel } from 'widgets/Dashboard/utils/dashboardRoutes';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineMessages, useIntl } from 'react-intl';

import { DataGrid, usePagination } from '@openfun/cunningham-react';
import { useEffect, useMemo } from 'react';
import { useParams, useSearchParams } from 'react-router-dom';
import { useParams, useSearchParams } from 'react-router';
import { ContractHelper, ContractStatePoV } from 'utils/ContractHelper';
import { useOrganizationContracts } from 'hooks/useContracts';
import Banner, { BannerType } from 'components/Banner';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PropsWithChildren } from 'react';
import { IntlProvider } from 'react-intl';
import { QueryClientProvider } from '@tanstack/react-query';
import { renderHook, waitFor, act } from '@testing-library/react';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { MemoryRouter, Route, Routes } from 'react-router';
import { RichieContextFactory as mockRichieContextFactory } from 'utils/test/factories/richie';
import { createTestQueryClient } from 'utils/test/createTestQueryClient';
import JoanieSessionProvider from 'contexts/SessionContext/JoanieSessionProvider';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useMemo, useState } from 'react';
import { useParams, useSearchParams } from 'react-router-dom';
import { useParams, useSearchParams } from 'react-router';
import useDefaultOrganizationId from 'hooks/useDefaultOrganizationId';
import { ContractResourceQuery, ContractState } from 'types/Joanie';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useMemo, useState } from 'react';
import { useParams, useSearchParams } from 'react-router-dom';
import { useParams, useSearchParams } from 'react-router';
import useDefaultOrganizationId from 'hooks/useDefaultOrganizationId';
import {
CourseListItem,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FormattedMessage, defineMessages } from 'react-intl';

import { useParams, useSearchParams } from 'react-router-dom';
import { useParams, useSearchParams } from 'react-router';
import { SortModel, usePagination } from '@openfun/cunningham-react';
import { useEffect, useMemo, useState } from 'react';
import { TeacherDashboardCourseSidebar } from 'widgets/Dashboard/components/TeacherDashboardCourseSidebar';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { render, screen } from '@testing-library/react';
import { IntlProvider } from 'react-intl';
import { CunninghamProvider } from '@openfun/cunningham-react';
import { capitalize } from 'lodash-es';
import { MemoryRouter } from 'react-router-dom';
import { MemoryRouter } from 'react-router';
import { CourseRunFactory } from 'utils/test/factories/joanie';
import CourseRunList from '.';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IntlProvider, createIntl } from 'react-intl';
import { render, screen } from '@testing-library/react';
import { capitalize } from 'lodash-es';
import { MemoryRouter } from 'react-router-dom';
import { MemoryRouter } from 'react-router';
import { CourseRunFactory } from 'utils/test/factories/joanie';
import { buildCourseRunData, messages } from './utils';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FormattedMessage, defineMessages, useIntl } from 'react-intl';
import { useParams } from 'react-router-dom';
import { useParams } from 'react-router';

import { capitalize } from 'lodash-es';
import { DashboardLayout } from 'widgets/Dashboard/components/DashboardLayout';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineMessages, FormattedMessage } from 'react-intl';
import { useParams } from 'react-router-dom';
import { useParams } from 'react-router';
import { Spinner } from 'components/Spinner';
import { DashboardLayout } from 'widgets/Dashboard/components/DashboardLayout';
import { TeacherDashboardOrganizationSidebar } from 'widgets/Dashboard/components/TeacherDashboardOrganizationSidebar';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FormattedMessage, defineMessages } from 'react-intl';
import { useParams } from 'react-router-dom';
import { useParams } from 'react-router';

import { DashboardLayout } from 'widgets/Dashboard/components/DashboardLayout';
import { TeacherDashboardCourseSidebar } from 'widgets/Dashboard/components/TeacherDashboardCourseSidebar';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createMemoryRouter, RouterProvider } from 'react-router-dom';
import { createMemoryRouter, RouterProvider } from 'react-router';
import { QueryClientProvider } from '@tanstack/react-query';
import { render, screen } from '@testing-library/react';
import fetchMock from 'fetch-mock';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/utils/test/LocationDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useLocation } from 'react-router-dom';
import { useLocation } from 'react-router';

const LocationDisplay = () => {
const location = useLocation();
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/utils/test/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type RenderFunction = (
* the same contexts.
*
* The provided {@param element} is to be wrapped in :
* - react router (react-router-dom)
* - react router (react-router)
* - react i18n context (react-intl)
* - query context (react-query)
*
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/js/utils/test/wrappers/RouterWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PropsWithChildren } from 'react';
import { RouteObject, RouterProvider, createMemoryRouter } from 'react-router-dom';
import { RouteObject, RouterProvider, createMemoryRouter } from 'react-router';
import { Maybe } from 'types/utils';

export interface RouterWrapperProps extends PropsWithChildren {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta, StoryObj } from '@storybook/react';
import { createMemoryRouter, Outlet, RouteObject, RouterProvider } from 'react-router-dom';
import { createMemoryRouter, Outlet, RouteObject, RouterProvider } from 'react-router';
import { defineMessages } from 'react-intl';
import { DashboardBreadcrumbsProvider } from 'widgets/Dashboard/contexts/DashboardBreadcrumbsContext';
import { useBreadcrumbsPlaceholders } from 'hooks/useBreadcrumbsPlaceholders';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useContext, useMemo } from 'react';
import { useMatches } from 'react-router-dom';
import { useMatches } from 'react-router';
import { defineMessages, FormattedMessage, MessageDescriptor, useIntl } from 'react-intl';
import { IntlHelper } from 'utils/IntlHelper';
import { DashboardBreadcrumbsContext } from 'widgets/Dashboard/contexts/DashboardBreadcrumbsContext';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta, StoryObj } from '@storybook/react';
import { createMemoryRouter, RouterProvider } from 'react-router-dom';
import { createMemoryRouter, RouterProvider } from 'react-router';
import { faker } from '@faker-js/faker';
import { TargetCourseFactory } from 'utils/test/factories/joanie';
import { StorybookHelper } from 'utils/StorybookHelper';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import { generatePath } from 'react-router-dom';
import { generatePath } from 'react-router';
import { CredentialOrder, OrderState } from 'types/Joanie';
import { Icon, IconTypeEnum } from 'components/Icon';
import { CoursesHelper } from 'utils/CoursesHelper';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta, StoryObj } from '@storybook/react';
import { createMemoryRouter, RouterProvider } from 'react-router-dom';
import { createMemoryRouter, RouterProvider } from 'react-router';
import { CredentialOrder, OrderState, Product, TargetCourse } from 'types/Joanie';
import {
CredentialOrderFactory,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PropsWithChildren, ReactNode } from 'react';
import { useLocation } from 'react-router-dom';
import { useLocation } from 'react-router';
import c from 'classnames';
import { LearnerDashboardSidebar } from 'widgets/Dashboard/components/LearnerDashboardSidebar';
import { DashboardBreadcrumbs } from 'widgets/Dashboard/components/DashboardBreadcrumbs';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Outlet, useMatches } from 'react-router-dom';
import { Outlet, useMatches } from 'react-router';
import { DashboardRouteHandle } from 'widgets/Dashboard/hooks/useDashboardRouter';
import { DashboardLayout } from 'widgets/Dashboard/components/DashboardLayout';
import { DashboardBreadcrumbsProvider } from 'widgets/Dashboard/contexts/DashboardBreadcrumbsContext';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useParams } from 'react-router-dom';
import { useParams } from 'react-router';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import { useMemo } from 'react';
import { useOmniscientOrder } from 'hooks/useOrders';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSearchParams } from 'react-router-dom';
import { createSearchParams } from 'react-router';
import { useMemo } from 'react';
import { MenuLink } from 'widgets/Dashboard/components/DashboardSidebar';
import { ContractState, CourseProductRelation, Organization } from 'types/Joanie';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classNames from 'classnames';
import { NavLink, useLocation } from 'react-router-dom';
import { NavLink, useLocation } from 'react-router';
import Badge from 'components/Badge';
import { MenuLink } from '../..';
import { isMenuLinkActive } from '../../utils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Select, SelectHandle } from '@openfun/cunningham-react';
import { useMemo, useRef } from 'react';
import { defineMessages, useIntl } from 'react-intl';
import { matchPath, useLocation, useNavigate } from 'react-router-dom';
import { matchPath, useLocation, useNavigate } from 'react-router';
import { MenuLink } from 'widgets/Dashboard/components/DashboardSidebar';

const messages = defineMessages({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta, StoryObj } from '@storybook/react';
import { createMemoryRouter, RouterProvider } from 'react-router-dom';
import { createMemoryRouter, RouterProvider } from 'react-router';
import { UserFactory } from 'utils/test/factories/richie';
import { StorybookHelper } from 'utils/StorybookHelper';
import MenuNavLink from './components/MenuNavLink';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Location, matchPath, resolvePath } from 'react-router-dom';
import { Location, matchPath, resolvePath } from 'react-router';

export const isMenuLinkActive = (to: string, location: Location) => {
const path = resolvePath(to).pathname;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createMemoryRouter } from 'react-router-dom';
import { createMemoryRouter } from 'react-router';
import Dashboard from 'widgets/Dashboard';
import { getDashboardRoutes } from 'widgets/Dashboard/utils/dashboardRoutes';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta, StoryObj } from '@storybook/react';
import { createMemoryRouter, RouterProvider } from 'react-router-dom';
import { createMemoryRouter, RouterProvider } from 'react-router';
import { UserFactory } from 'utils/test/factories/richie';
import { LearnerDashboardSidebar } from 'widgets/Dashboard/components/LearnerDashboardSidebar';
import { StorybookHelper } from 'utils/StorybookHelper';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineMessages, useIntl } from 'react-intl';
import { useMemo } from 'react';
import { generatePath } from 'react-router-dom';
import { generatePath } from 'react-router';
import { getDashboardRouteLabel } from 'widgets/Dashboard/utils/dashboardRoutes';
import {
DashboardSidebar,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { screen } from '@testing-library/react';
import { Outlet } from 'react-router-dom';
import { Outlet } from 'react-router';
import { RichieContextFactory as mockRichieContextFactory } from 'utils/test/factories/richie';
import LocationDisplay from 'utils/test/LocationDisplay';
import { render } from 'utils/test/render';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generatePath, Navigate, NavigateProps, useParams } from 'react-router-dom';
import { generatePath, Navigate, NavigateProps, useParams } from 'react-router';

interface NavigateWithParamsProps extends NavigateProps {
to: string;
Expand Down
Loading

0 comments on commit 170267c

Please sign in to comment.