Skip to content

Commit f5dfe6d

Browse files
authored
Merge pull request #61 from mrkpatchaa/main
Upgrade to react-admin v5
2 parents 6e8e321 + 1746024 commit f5dfe6d

18 files changed

+612
-641
lines changed

.github/workflows/test.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Use Node.js LTS
1919
uses: actions/setup-node@v1
2020
with:
21-
node-version: '16.x'
21+
node-version: '18.x'
2222
- uses: bahmutov/npm-install@v1
2323
- name: Build
2424
run: make build
@@ -39,15 +39,15 @@ jobs:
3939
- name: Use Node.js LTS
4040
uses: actions/setup-node@v1
4141
with:
42-
node-version: '16.x'
42+
node-version: '18.x'
4343
- uses: bahmutov/npm-install@v1
4444
- name: Prepare env
4545
run: cp -n ./packages/demo/.env.local-example ./packages/demo/.env
4646
- name: Build
4747
run: make build build-demo
4848
- uses: supabase/setup-cli@v1.1.5
4949
with:
50-
version: 1.38.6
50+
version: 1.131.2
5151
- name: Start Supabase local development setup
5252
run: supabase start
5353
- name: Setup database

cypress/e2e/login.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export const login = (
22
email: string = 'janedoe@atomic.dev',
33
password: string = 'password'
44
) => {
5+
cy.wait(1000);
56
cy.findByLabelText('Email *').type(email);
67
cy.findByLabelText('Password *').type(password);
78
cy.findByText('Sign in').click();

jest.config.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module.exports = {
2+
globalSetup: './test-global-setup.js',
3+
setupFilesAfterEnv: ['./test-setup.js'],
4+
testEnvironment: 'jsdom',
5+
testPathIgnorePatterns: [
6+
'/node_modules/',
7+
'/lib/',
8+
'/esm/',
9+
'/packages/demo',
10+
],
11+
transformIgnorePatterns: [
12+
'[/\\\\]node_modules[/\\\\](?!(@hookform)/).+\\.(js|jsx|mjs|ts|tsx)$',
13+
],
14+
transform: {
15+
// '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
16+
'^.+\\.[tj]sx?$': [
17+
'ts-jest',
18+
{
19+
isolatedModules: true,
20+
useESM: true,
21+
},
22+
],
23+
},
24+
};

package.json

+5-6
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@
3939
"prettier": "~2.8.8",
4040
"raf": "^3.4.1",
4141
"supabase": "^1.131.2",
42-
"typescript": "^4.9.5"
42+
"typescript": "^4.9.5",
43+
"ts-jest": "^29.1.0",
44+
"whatwg-fetch": "^3.0.0"
4345
},
4446
"scripts": {
4547
"build": "lerna run build",
4648
"build-demo": "cd packages/demo && yarn build",
4749
"run-demo": "cd packages/demo && yarn start",
4850
"run-demo-prod": "cd packages/demo && yarn serve",
49-
"test-unit": "lerna run test-unit",
51+
"test-unit": "jest",
5052
"test-e2e": "cypress run",
5153
"test-e2e-local": "cypress open",
5254
"watch": "lerna run --parallel watch",
@@ -58,8 +60,5 @@
5860
},
5961
"workspaces": [
6062
"packages/*"
61-
],
62-
"jest": {
63-
"testEnvironment": "jsdom"
64-
}
63+
]
6564
}

packages/demo/package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@
44
"private": true,
55
"dependencies": {
66
"@mui/icons-material": "^5.0.1",
7-
"@mui/material": "^5.0.2",
7+
"@mui/material": "^5.15.20",
88
"@nivo/bar": "^0.80.0",
99
"@nivo/core": "^0.80.0",
10+
"@tanstack/react-query": "^5.45.1",
1011
"@vitejs/plugin-react": "^2.2.0",
1112
"clsx": "^1.1.1",
1213
"date-fns": "^2.19.0",
1314
"faker": "~5.4.0",
1415
"lodash": "~4.17.5",
1516
"prop-types": "^15.7.2",
16-
"ra-data-fakerest": "^4.0.0",
17+
"ra-data-fakerest": "^5.0.0",
1718
"ra-supabase": "^2.3.0",
1819
"react": "^18.2.0",
19-
"react-admin": "^4.0.0",
20+
"react-admin": "^5.0.0",
2021
"react-beautiful-dnd": "^13.0.0",
2122
"react-dom": "^18.2.0",
2223
"react-error-boundary": "^3.1.4",
23-
"react-query": "^3.32.1",
24-
"react-router": "^6.1.0",
25-
"react-router-dom": "^6.1.0",
24+
"react-router": "^6.23.1",
25+
"react-router-dom": "^6.23.1",
2626
"vite": "^3.2.0"
2727
},
2828
"devDependencies": {
29-
"@testing-library/jest-dom": "^5.16.5",
30-
"@testing-library/react": "^13.4.0",
29+
"@testing-library/jest-dom": "^6.4.5",
30+
"@testing-library/react": "^15.0.7",
3131
"@testing-library/user-event": "^14.4.3",
3232
"@types/faker": "^5.1.7",
3333
"@types/jest": "^26.0.19",

packages/demo/src/App.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
SetPasswordPage,
1717
ForgotPasswordPage,
1818
} from 'ra-supabase';
19-
import { QueryClient } from 'react-query';
19+
import { QueryClient } from '@tanstack/react-query';
2020
import { authProvider } from './authProvider';
2121
import Layout from './Layout';
2222
import contacts from './contacts';

packages/ra-supabase-core/package.json

+10-13
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717
"peerDependencies": {
1818
"@raphiniert/ra-data-postgrest": "^2.0.0",
1919
"@supabase/supabase-js": "^2.0.0",
20-
"ra-core": "^4.7.0"
20+
"ra-core": "^5.0.1"
2121
},
2222
"devDependencies": {
2323
"@raphiniert/ra-data-postgrest": "^2.1.0",
24-
"@supabase/supabase-js": "^2.43.1",
25-
"@testing-library/jest-dom": "^5.16.5",
26-
"@testing-library/react": "^13.4.0",
27-
"@testing-library/user-event": "^14.4.3",
28-
"ra-core": "^4.7.0",
29-
"react": "^18.2.0",
30-
"react-dom": "^18.2.0",
31-
"react-router": "^6.7.0",
32-
"react-router-dom": "^6.7.0"
24+
"@supabase/supabase-js": "^2.43.5",
25+
"@testing-library/jest-dom": "^6.4.5",
26+
"@testing-library/react": "^15.0.7",
27+
"@testing-library/user-event": "^14.5.2",
28+
"ra-core": "^5.0.1",
29+
"react": "^18.3.1",
30+
"react-dom": "^18.3.1",
31+
"react-router": "^6.23.1",
32+
"react-router-dom": "^6.23.1"
3333
},
3434
"scripts": {
3535
"build": "yarn run build-cjs && yarn run build-esm",
@@ -38,8 +38,5 @@
3838
"watch": "tsc --outDir esm --module es2015 --watch",
3939
"lint": "eslint --fix ./src",
4040
"test-unit": "jest ./src"
41-
},
42-
"jest": {
43-
"testEnvironment": "jsdom"
4441
}
4542
}

packages/ra-supabase-core/src/authProvider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export const supabaseAuthProvider = (
139139
return Promise.resolve();
140140
},
141141
async getPermissions() {
142-
const { data, error } = await client.auth.getUser();
142+
const { data, error } = await client.auth.getUser();
143143
if (error) {
144144
throw error;
145145
}

packages/ra-supabase-core/src/useRedirectIfAuthenticated.test.tsx

+50-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as React from 'react';
2-
import { CoreAdminContext } from 'ra-core';
2+
import { CoreAdminContext, TestMemoryRouter } from 'ra-core';
33
import { render, waitFor } from '@testing-library/react';
4-
import { createMemoryHistory } from 'history';
54
import {
65
useRedirectIfAuthenticated,
76
UseRedirectIfAuthenticatedOptions,
@@ -29,19 +28,29 @@ describe('useRedirectIfAuthenticated', () => {
2928
getPermissions: jest.fn(),
3029
setPassword: jest.fn(),
3130
};
32-
const history = createMemoryHistory({ initialEntries: ['/login'] });
33-
const push = jest.spyOn(history, 'push');
3431

32+
let location;
3533
render(
36-
<CoreAdminContext authProvider={authProvider} history={history}>
37-
<UseRedirectIfAuthenticated />
38-
</CoreAdminContext>
34+
<TestMemoryRouter
35+
initialEntries={['/login']}
36+
locationCallback={l => {
37+
location = l;
38+
}}
39+
>
40+
<CoreAdminContext authProvider={authProvider}>
41+
<UseRedirectIfAuthenticated />
42+
</CoreAdminContext>
43+
</TestMemoryRouter>
3944
);
4045

4146
expect(authProvider.checkAuth).toHaveBeenCalled();
42-
await waitFor(() => {
43-
expect(push).toHaveBeenCalledTimes(0);
44-
});
47+
expect(location).toEqual(
48+
expect.objectContaining({
49+
hash: '',
50+
pathname: '/login',
51+
search: '',
52+
})
53+
);
4554
});
4655

4756
test('should redirect users if they are authenticated', async () => {
@@ -53,25 +62,29 @@ describe('useRedirectIfAuthenticated', () => {
5362
getPermissions: jest.fn(),
5463
setPassword: jest.fn(),
5564
};
56-
const history = createMemoryHistory({ initialEntries: ['/login'] });
57-
const push = jest.spyOn(history, 'push');
5865

66+
let location;
5967
render(
60-
<CoreAdminContext authProvider={authProvider} history={history}>
61-
<UseRedirectIfAuthenticated />
62-
</CoreAdminContext>
68+
<TestMemoryRouter
69+
initialEntries={['/login']}
70+
locationCallback={l => {
71+
location = l;
72+
}}
73+
>
74+
<CoreAdminContext authProvider={authProvider}>
75+
<UseRedirectIfAuthenticated />
76+
</CoreAdminContext>
77+
</TestMemoryRouter>
6378
);
6479

6580
expect(authProvider.checkAuth).toHaveBeenCalled();
6681
await waitFor(() => {
67-
expect(push).toHaveBeenCalledWith(
68-
{
82+
expect(location).toEqual(
83+
expect.objectContaining({
6984
hash: '',
7085
pathname: '/',
7186
search: '',
72-
},
73-
undefined,
74-
{}
87+
})
7588
);
7689
});
7790
});
@@ -85,21 +98,29 @@ describe('useRedirectIfAuthenticated', () => {
8598
getPermissions: jest.fn(),
8699
setPassword: jest.fn(),
87100
};
88-
const history = createMemoryHistory({ initialEntries: ['/login'] });
89-
const push = jest.spyOn(history, 'push');
90101

102+
let location;
91103
render(
92-
<CoreAdminContext authProvider={authProvider} history={history}>
93-
<UseRedirectIfAuthenticated redirectTo="/dashboard" />
94-
</CoreAdminContext>
104+
<TestMemoryRouter
105+
initialEntries={['/login']}
106+
locationCallback={l => {
107+
location = l;
108+
}}
109+
>
110+
<CoreAdminContext authProvider={authProvider}>
111+
<UseRedirectIfAuthenticated redirectTo="/dashboard" />
112+
</CoreAdminContext>
113+
</TestMemoryRouter>
95114
);
96115

97116
expect(authProvider.checkAuth).toHaveBeenCalled();
98117
await waitFor(() => {
99-
expect(push).toHaveBeenCalledWith(
100-
{ hash: '', pathname: '/dashboard', search: '' },
101-
undefined,
102-
{}
118+
expect(location).toEqual(
119+
expect.objectContaining({
120+
hash: '',
121+
pathname: '/dashboard',
122+
search: '',
123+
})
103124
);
104125
});
105126
});

packages/ra-supabase-core/src/useRedirectIfAuthenticated.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const useRedirectIfAuthenticated = (
1919
const checkAuth = useCheckAuth();
2020

2121
useEffect(() => {
22-
checkAuth({}, false, undefined, true)
22+
checkAuth({}, false, undefined)
2323
.then(() => {
2424
// already authenticated, redirect to the home page
2525
navigate(redirectTo);

packages/ra-supabase-core/src/useResetPassword.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { onError, OnSuccess, useAuthProvider, useNotify } from 'ra-core';
2-
import { useMutation, UseMutationResult } from 'react-query';
1+
import { OnError, OnSuccess, useAuthProvider, useNotify } from 'ra-core';
2+
import { useMutation, UseMutationResult } from '@tanstack/react-query';
33
import { ResetPasswordParams, SupabaseAuthProvider } from './authProvider';
44

55
/**
@@ -42,17 +42,19 @@ export const useResetPassword = (
4242
onError = error => notify(error.message, { type: 'error' }),
4343
} = options || {};
4444

45-
const mutation = useMutation<unknown, Error, ResetPasswordParams>(
46-
params => {
45+
const mutation = useMutation<unknown, Error, ResetPasswordParams>({
46+
mutationFn: params => {
4747
return authProvider.resetPassword(params);
4848
},
49-
{ onSuccess, onError, retry: false }
50-
);
49+
onSuccess,
50+
onError,
51+
retry: false,
52+
});
5153

5254
return [mutation.mutate, mutation];
5355
};
5456

5557
export type UseResetPasswordOptions = {
5658
onSuccess?: OnSuccess;
57-
onError?: onError;
59+
onError?: OnError;
5860
};

packages/ra-supabase-core/src/useSetPassword.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {
2-
onError,
2+
OnError,
33
OnSuccess,
44
useAuthProvider,
55
useNotify,
66
useRedirect,
77
} from 'ra-core';
8-
import { useMutation, UseMutationResult } from 'react-query';
8+
import { useMutation, UseMutationResult } from '@tanstack/react-query';
99
import { SetPasswordParams, SupabaseAuthProvider } from './authProvider';
1010

1111
/**
@@ -49,17 +49,19 @@ export const useSetPassword = (
4949
onError = error => notify(error.message, { type: 'error' }),
5050
} = options || {};
5151

52-
const mutation = useMutation<unknown, Error, SetPasswordParams>(
53-
params => {
52+
const mutation = useMutation<unknown, Error, SetPasswordParams>({
53+
mutationFn: params => {
5454
return authProvider.setPassword(params);
5555
},
56-
{ onSuccess, onError, retry: false }
57-
);
56+
onSuccess,
57+
onError,
58+
retry: false,
59+
});
5860

5961
return [mutation.mutate, mutation];
6062
};
6163

6264
export type UseSetPasswordOptions = {
6365
onSuccess?: OnSuccess;
64-
onError?: onError;
66+
onError?: OnError;
6567
};

0 commit comments

Comments
 (0)