-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9657 from marmelab/react-router-6.22.0
Upgrade `react-router` to 6.22.0, use data router, stabilize `useWarnWhenUnsavedChanges` and remove `<Admin history>` prop
- Loading branch information
Showing
114 changed files
with
8,353 additions
and
2,891 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
packages/ra-core/codemods/__testfixtures__/replace-Admin-history-Authenticated.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import * as React from 'react'; | ||
import expect from 'expect'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import { createMemoryHistory } from 'history'; | ||
import { Routes, Route, useLocation } from 'react-router-dom'; | ||
|
||
// @ts-ignore | ||
import { memoryStore } from '../store'; | ||
// @ts-ignore | ||
import { CoreAdminContext } from '../core'; | ||
// @ts-ignore | ||
import { useNotificationContext } from '../notification'; | ||
// @ts-ignore | ||
import { Authenticated } from './Authenticated'; | ||
|
||
describe('<Authenticated>', () => { | ||
const Foo = () => <div>Foo</div>; | ||
|
||
it('should render its child by default', async () => { | ||
const authProvider = { | ||
login: () => Promise.reject('bad method'), | ||
logout: () => Promise.reject('bad method'), | ||
checkAuth: jest.fn().mockResolvedValueOnce(''), | ||
checkError: () => Promise.reject('bad method'), | ||
getPermissions: () => Promise.reject('bad method'), | ||
}; | ||
const store = memoryStore(); | ||
const reset = jest.spyOn(store, 'reset'); | ||
|
||
render( | ||
<CoreAdminContext authProvider={authProvider} store={store}> | ||
<Authenticated> | ||
<Foo /> | ||
</Authenticated> | ||
</CoreAdminContext> | ||
); | ||
expect(screen.queryByText('Foo')).not.toBeNull(); | ||
expect(reset).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('should logout, redirect to login and show a notification after a tick if the auth fails', async () => { | ||
const authProvider = { | ||
login: jest.fn().mockResolvedValue(''), | ||
logout: jest.fn().mockResolvedValue(''), | ||
checkAuth: jest.fn().mockRejectedValue(undefined), | ||
checkError: jest.fn().mockResolvedValue(''), | ||
getPermissions: jest.fn().mockResolvedValue(''), | ||
}; | ||
const store = memoryStore(); | ||
const reset = jest.spyOn(store, 'reset'); | ||
const history = createMemoryHistory(); | ||
|
||
const Login = () => { | ||
const location = useLocation(); | ||
return ( | ||
<div aria-label="nextPathname"> | ||
{(location.state as any).nextPathname} | ||
</div> | ||
); | ||
}; | ||
|
||
let notificationsSpy; | ||
const Notification = () => { | ||
const { notifications } = useNotificationContext(); | ||
React.useEffect(() => { | ||
notificationsSpy = notifications; | ||
}, [notifications]); | ||
return null; | ||
}; | ||
|
||
render( | ||
<CoreAdminContext | ||
authProvider={authProvider} | ||
store={store} | ||
history={history} | ||
> | ||
<Notification /> | ||
<Routes> | ||
<Route | ||
path="/" | ||
element={ | ||
<Authenticated> | ||
<Foo /> | ||
</Authenticated> | ||
} | ||
/> | ||
<Route path="/login" element={<Login />} /> | ||
</Routes> | ||
</CoreAdminContext> | ||
); | ||
await waitFor(() => { | ||
expect(authProvider.checkAuth.mock.calls[0][0]).toEqual({}); | ||
expect(authProvider.logout.mock.calls[0][0]).toEqual({}); | ||
expect(reset).toHaveBeenCalledTimes(1); | ||
expect(notificationsSpy).toEqual([ | ||
{ | ||
message: 'ra.auth.auth_check_error', | ||
type: 'error', | ||
notificationOptions: {}, | ||
}, | ||
]); | ||
expect(screen.getByLabelText('nextPathname').innerHTML).toEqual( | ||
'/' | ||
); | ||
}); | ||
}); | ||
}); |
104 changes: 104 additions & 0 deletions
104
packages/ra-core/codemods/__testfixtures__/replace-Admin-history-Authenticated.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import * as React from 'react'; | ||
import expect from 'expect'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import { TestMemoryRouter } from 'react-admin'; | ||
import { Routes, Route, useLocation } from 'react-router-dom'; | ||
|
||
// @ts-ignore | ||
import { memoryStore } from '../store'; | ||
// @ts-ignore | ||
import { CoreAdminContext } from '../core'; | ||
// @ts-ignore | ||
import { useNotificationContext } from '../notification'; | ||
// @ts-ignore | ||
import { Authenticated } from './Authenticated'; | ||
|
||
describe('<Authenticated>', () => { | ||
const Foo = () => <div>Foo</div>; | ||
|
||
it('should render its child by default', async () => { | ||
const authProvider = { | ||
login: () => Promise.reject('bad method'), | ||
logout: () => Promise.reject('bad method'), | ||
checkAuth: jest.fn().mockResolvedValueOnce(''), | ||
checkError: () => Promise.reject('bad method'), | ||
getPermissions: () => Promise.reject('bad method'), | ||
}; | ||
const store = memoryStore(); | ||
const reset = jest.spyOn(store, 'reset'); | ||
|
||
render( | ||
<CoreAdminContext authProvider={authProvider} store={store}> | ||
<Authenticated> | ||
<Foo /> | ||
</Authenticated> | ||
</CoreAdminContext> | ||
); | ||
expect(screen.queryByText('Foo')).not.toBeNull(); | ||
expect(reset).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('should logout, redirect to login and show a notification after a tick if the auth fails', async () => { | ||
const authProvider = { | ||
login: jest.fn().mockResolvedValue(''), | ||
logout: jest.fn().mockResolvedValue(''), | ||
checkAuth: jest.fn().mockRejectedValue(undefined), | ||
checkError: jest.fn().mockResolvedValue(''), | ||
getPermissions: jest.fn().mockResolvedValue(''), | ||
}; | ||
const store = memoryStore(); | ||
const reset = jest.spyOn(store, 'reset'); | ||
|
||
const Login = () => { | ||
const location = useLocation(); | ||
return ( | ||
<div aria-label="nextPathname"> | ||
{(location.state as any).nextPathname} | ||
</div> | ||
); | ||
}; | ||
|
||
let notificationsSpy; | ||
const Notification = () => { | ||
const { notifications } = useNotificationContext(); | ||
React.useEffect(() => { | ||
notificationsSpy = notifications; | ||
}, [notifications]); | ||
return null; | ||
}; | ||
|
||
render( | ||
<TestMemoryRouter> | ||
<CoreAdminContext authProvider={authProvider} store={store}> | ||
<Notification /> | ||
<Routes> | ||
<Route | ||
path="/" | ||
element={ | ||
<Authenticated> | ||
<Foo /> | ||
</Authenticated> | ||
} | ||
/> | ||
<Route path="/login" element={<Login />} /> | ||
</Routes> | ||
</CoreAdminContext> | ||
</TestMemoryRouter> | ||
); | ||
await waitFor(() => { | ||
expect(authProvider.checkAuth.mock.calls[0][0]).toEqual({}); | ||
expect(authProvider.logout.mock.calls[0][0]).toEqual({}); | ||
expect(reset).toHaveBeenCalledTimes(1); | ||
expect(notificationsSpy).toEqual([ | ||
{ | ||
message: 'ra.auth.auth_check_error', | ||
type: 'error', | ||
notificationOptions: {}, | ||
}, | ||
]); | ||
expect(screen.getByLabelText('nextPathname').innerHTML).toEqual( | ||
'/' | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.