|
1 | | -import * as React from 'react'; |
| 1 | +import { |
| 2 | + act, |
| 3 | + fireEvent, |
| 4 | + render, |
| 5 | + screen, |
| 6 | + waitFor, |
| 7 | +} from '@testing-library/react'; |
2 | 8 | import expect from 'expect'; |
3 | | -import { act, render, screen, waitFor } from '@testing-library/react'; |
4 | | -import { Routes, Route } from 'react-router'; |
5 | 9 | import { createMemoryHistory } from 'history'; |
| 10 | +import * as React from 'react'; |
| 11 | +import { MemoryRouter, Route, Routes } from 'react-router'; |
6 | 12 |
|
7 | | -import { EditController } from './EditController'; |
8 | | -import { DataProvider } from '../../types'; |
| 13 | +import { |
| 14 | + EditContextProvider, |
| 15 | + SaveContextProvider, |
| 16 | + useEditController, |
| 17 | +} from '..'; |
9 | 18 | import { CoreAdminContext } from '../../core'; |
10 | | -import { useNotificationContext } from '../../notification'; |
11 | | -import { SaveContextProvider } from '..'; |
| 19 | +import { testDataProvider, useUpdate } from '../../dataProvider'; |
12 | 20 | import undoableEventEmitter from '../../dataProvider/undoableEventEmitter'; |
| 21 | +import { Form, InputProps, useInput } from '../../form'; |
| 22 | +import { useNotificationContext } from '../../notification'; |
| 23 | +import { DataProvider } from '../../types'; |
13 | 24 | import { Middleware, useRegisterMutationMiddleware } from '../saveContext'; |
14 | | -import { testDataProvider, useUpdate } from '../../dataProvider'; |
| 25 | +import { EditController } from './EditController'; |
15 | 26 |
|
16 | 27 | describe('useEditController', () => { |
17 | 28 | const defaultProps = { |
@@ -940,4 +951,63 @@ describe('useEditController', () => { |
940 | 951 | previousData: { id: 12 }, |
941 | 952 | }); |
942 | 953 | }); |
| 954 | + |
| 955 | + it('should allow custom redirect with warnWhenUnsavedChanges in pessimistic mode', async () => { |
| 956 | + const dataProvider = testDataProvider({ |
| 957 | + getOne: () => Promise.resolve({ data: { id: 123 } } as any), |
| 958 | + update: (_, { id, data }) => |
| 959 | + new Promise(resolve => |
| 960 | + setTimeout( |
| 961 | + () => resolve({ data: { id, ...data } } as any), |
| 962 | + 300 |
| 963 | + ) |
| 964 | + ), |
| 965 | + }); |
| 966 | + const Input = (props: InputProps) => { |
| 967 | + const name = props.source; |
| 968 | + const { field } = useInput(props); |
| 969 | + return ( |
| 970 | + <> |
| 971 | + <label htmlFor={name}>{name}</label> |
| 972 | + <input id={name} type="text" {...field} /> |
| 973 | + </> |
| 974 | + ); |
| 975 | + }; |
| 976 | + const EditView = () => { |
| 977 | + const controllerProps = useEditController({ |
| 978 | + ...defaultProps, |
| 979 | + id: 123, |
| 980 | + redirect: 'show', |
| 981 | + mutationMode: 'pessimistic', |
| 982 | + }); |
| 983 | + return ( |
| 984 | + <EditContextProvider value={controllerProps}> |
| 985 | + <Form warnWhenUnsavedChanges> |
| 986 | + <> |
| 987 | + <div>Edit</div> |
| 988 | + <Input source="foo" /> |
| 989 | + <input type="submit" value="Submit" /> |
| 990 | + </> |
| 991 | + </Form> |
| 992 | + </EditContextProvider> |
| 993 | + ); |
| 994 | + }; |
| 995 | + const ShowView = () => <div>Show</div>; |
| 996 | + render( |
| 997 | + <MemoryRouter initialEntries={['/posts/123']}> |
| 998 | + <CoreAdminContext dataProvider={dataProvider}> |
| 999 | + <Routes> |
| 1000 | + <Route path="/posts/123" element={<EditView />} /> |
| 1001 | + <Route path="/posts/123/show" element={<ShowView />} /> |
| 1002 | + </Routes> |
| 1003 | + </CoreAdminContext> |
| 1004 | + </MemoryRouter> |
| 1005 | + ); |
| 1006 | + await screen.findByText('Edit'); |
| 1007 | + fireEvent.change(await screen.findByLabelText('foo'), { |
| 1008 | + target: { value: 'bar' }, |
| 1009 | + }); |
| 1010 | + fireEvent.click(screen.getByText('Submit')); |
| 1011 | + expect(await screen.findByText('Show')).not.toBeNull(); |
| 1012 | + }); |
943 | 1013 | }); |
0 commit comments