Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to react-admin v5 #61

Merged
merged 12 commits into from
Jun 28, 2024
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Use Node.js LTS
uses: actions/setup-node@v1
with:
node-version: '16.x'
node-version: '18.x'
- uses: bahmutov/npm-install@v1
- name: Build
run: make build
Expand All @@ -39,15 +39,15 @@ jobs:
- name: Use Node.js LTS
uses: actions/setup-node@v1
with:
node-version: '16.x'
node-version: '18.x'
- uses: bahmutov/npm-install@v1
- name: Prepare env
run: cp -n ./packages/demo/.env.local-example ./packages/demo/.env
- name: Build
run: make build build-demo
- uses: supabase/[email protected]
with:
version: 1.38.6
version: 1.131.2
- name: Start Supabase local development setup
run: supabase start
- name: Setup database
Expand Down
1 change: 1 addition & 0 deletions cypress/e2e/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const login = (
email: string = '[email protected]',
password: string = 'password'
) => {
cy.wait(1000);
cy.findByLabelText('Email *').type(email);
cy.findByLabelText('Password *').type(password);
cy.findByText('Sign in').click();
Expand Down
24 changes: 24 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
globalSetup: './test-global-setup.js',
setupFilesAfterEnv: ['./test-setup.js'],
testEnvironment: 'jsdom',
testPathIgnorePatterns: [
'/node_modules/',
'/lib/',
'/esm/',
'/packages/demo',
],
transformIgnorePatterns: [
'[/\\\\]node_modules[/\\\\](?!(@hookform)/).+\\.(js|jsx|mjs|ts|tsx)$',
],
transform: {
// '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
'^.+\\.[tj]sx?$': [
'ts-jest',
{
isolatedModules: true,
useESM: true,
},
],
},
};
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@
"prettier": "~2.8.8",
"raf": "^3.4.1",
"supabase": "^1.131.2",
"typescript": "^4.9.5"
"typescript": "^4.9.5",
"ts-jest": "^29.1.0",
"whatwg-fetch": "^3.0.0"
},
"scripts": {
"build": "lerna run build",
"build-demo": "cd packages/demo && yarn build",
"run-demo": "cd packages/demo && yarn start",
"run-demo-prod": "cd packages/demo && yarn serve",
"test-unit": "lerna run test-unit",
"test-unit": "jest",
"test-e2e": "cypress run",
"test-e2e-local": "cypress open",
"watch": "lerna run --parallel watch",
Expand All @@ -58,8 +60,5 @@
},
"workspaces": [
"packages/*"
],
"jest": {
"testEnvironment": "jsdom"
}
]
}
16 changes: 8 additions & 8 deletions packages/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@
"private": true,
"dependencies": {
"@mui/icons-material": "^5.0.1",
"@mui/material": "^5.0.2",
"@mui/material": "^5.15.20",
"@nivo/bar": "^0.80.0",
"@nivo/core": "^0.80.0",
"@tanstack/react-query": "^5.45.1",
"@vitejs/plugin-react": "^2.2.0",
"clsx": "^1.1.1",
"date-fns": "^2.19.0",
"faker": "~5.4.0",
"lodash": "~4.17.5",
"prop-types": "^15.7.2",
"ra-data-fakerest": "^4.0.0",
"ra-data-fakerest": "^5.0.0",
"ra-supabase": "^2.3.0",
"react": "^18.2.0",
"react-admin": "^4.0.0",
"react-admin": "^5.0.0",
"react-beautiful-dnd": "^13.0.0",
"react-dom": "^18.2.0",
"react-error-boundary": "^3.1.4",
"react-query": "^3.32.1",
"react-router": "^6.1.0",
"react-router-dom": "^6.1.0",
"react-router": "^6.23.1",
"react-router-dom": "^6.23.1",
"vite": "^3.2.0"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/jest-dom": "^6.4.5",
"@testing-library/react": "^15.0.7",
"@testing-library/user-event": "^14.4.3",
"@types/faker": "^5.1.7",
"@types/jest": "^26.0.19",
Expand Down
2 changes: 1 addition & 1 deletion packages/demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
SetPasswordPage,
ForgotPasswordPage,
} from 'ra-supabase';
import { QueryClient } from 'react-query';
import { QueryClient } from '@tanstack/react-query';
import { authProvider } from './authProvider';
import Layout from './Layout';
import contacts from './contacts';
Expand Down
23 changes: 10 additions & 13 deletions packages/ra-supabase-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
"peerDependencies": {
"@raphiniert/ra-data-postgrest": "^2.0.0",
"@supabase/supabase-js": "^2.0.0",
"ra-core": "^4.7.0"
"ra-core": "^5.0.1"
},
"devDependencies": {
"@raphiniert/ra-data-postgrest": "^2.1.0",
"@supabase/supabase-js": "^2.43.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^14.4.3",
"ra-core": "^4.7.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router": "^6.7.0",
"react-router-dom": "^6.7.0"
"@supabase/supabase-js": "^2.43.5",
"@testing-library/jest-dom": "^6.4.5",
"@testing-library/react": "^15.0.7",
"@testing-library/user-event": "^14.5.2",
"ra-core": "^5.0.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router": "^6.23.1",
"react-router-dom": "^6.23.1"
},
"scripts": {
"build": "yarn run build-cjs && yarn run build-esm",
Expand All @@ -38,8 +38,5 @@
"watch": "tsc --outDir esm --module es2015 --watch",
"lint": "eslint --fix ./src",
"test-unit": "jest ./src"
},
"jest": {
"testEnvironment": "jsdom"
}
}
2 changes: 1 addition & 1 deletion packages/ra-supabase-core/src/authProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export const supabaseAuthProvider = (
return Promise.resolve();
},
async getPermissions() {
const { data, error } = await client.auth.getUser();
const { data, error } = await client.auth.getUser();
if (error) {
throw error;
}
Expand Down
79 changes: 50 additions & 29 deletions packages/ra-supabase-core/src/useRedirectIfAuthenticated.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { CoreAdminContext } from 'ra-core';
import { CoreAdminContext, TestMemoryRouter } from 'ra-core';
import { render, waitFor } from '@testing-library/react';
import { createMemoryHistory } from 'history';
import {
useRedirectIfAuthenticated,
UseRedirectIfAuthenticatedOptions,
Expand Down Expand Up @@ -29,19 +28,29 @@ describe('useRedirectIfAuthenticated', () => {
getPermissions: jest.fn(),
setPassword: jest.fn(),
};
const history = createMemoryHistory({ initialEntries: ['/login'] });
const push = jest.spyOn(history, 'push');

let location;
render(
<CoreAdminContext authProvider={authProvider} history={history}>
<UseRedirectIfAuthenticated />
</CoreAdminContext>
<TestMemoryRouter
initialEntries={['/login']}
locationCallback={l => {
location = l;
}}
>
<CoreAdminContext authProvider={authProvider}>
<UseRedirectIfAuthenticated />
</CoreAdminContext>
</TestMemoryRouter>
);

expect(authProvider.checkAuth).toHaveBeenCalled();
await waitFor(() => {
expect(push).toHaveBeenCalledTimes(0);
});
expect(location).toEqual(
expect.objectContaining({
hash: '',
pathname: '/login',
search: '',
})
);
});

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

let location;
render(
<CoreAdminContext authProvider={authProvider} history={history}>
<UseRedirectIfAuthenticated />
</CoreAdminContext>
<TestMemoryRouter
initialEntries={['/login']}
locationCallback={l => {
location = l;
}}
>
<CoreAdminContext authProvider={authProvider}>
<UseRedirectIfAuthenticated />
</CoreAdminContext>
</TestMemoryRouter>
);

expect(authProvider.checkAuth).toHaveBeenCalled();
await waitFor(() => {
expect(push).toHaveBeenCalledWith(
{
expect(location).toEqual(
expect.objectContaining({
hash: '',
pathname: '/',
search: '',
},
undefined,
{}
})
);
});
});
Expand All @@ -85,21 +98,29 @@ describe('useRedirectIfAuthenticated', () => {
getPermissions: jest.fn(),
setPassword: jest.fn(),
};
const history = createMemoryHistory({ initialEntries: ['/login'] });
const push = jest.spyOn(history, 'push');

let location;
render(
<CoreAdminContext authProvider={authProvider} history={history}>
<UseRedirectIfAuthenticated redirectTo="/dashboard" />
</CoreAdminContext>
<TestMemoryRouter
initialEntries={['/login']}
locationCallback={l => {
location = l;
}}
>
<CoreAdminContext authProvider={authProvider}>
<UseRedirectIfAuthenticated redirectTo="/dashboard" />
</CoreAdminContext>
</TestMemoryRouter>
);

expect(authProvider.checkAuth).toHaveBeenCalled();
await waitFor(() => {
expect(push).toHaveBeenCalledWith(
{ hash: '', pathname: '/dashboard', search: '' },
undefined,
{}
expect(location).toEqual(
expect.objectContaining({
hash: '',
pathname: '/dashboard',
search: '',
})
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const useRedirectIfAuthenticated = (
const checkAuth = useCheckAuth();

useEffect(() => {
checkAuth({}, false, undefined, true)
checkAuth({}, false, undefined)
.then(() => {
// already authenticated, redirect to the home page
navigate(redirectTo);
Expand Down
16 changes: 9 additions & 7 deletions packages/ra-supabase-core/src/useResetPassword.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { onError, OnSuccess, useAuthProvider, useNotify } from 'ra-core';
import { useMutation, UseMutationResult } from 'react-query';
import { OnError, OnSuccess, useAuthProvider, useNotify } from 'ra-core';
import { useMutation, UseMutationResult } from '@tanstack/react-query';
import { ResetPasswordParams, SupabaseAuthProvider } from './authProvider';

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

const mutation = useMutation<unknown, Error, ResetPasswordParams>(
params => {
const mutation = useMutation<unknown, Error, ResetPasswordParams>({
mutationFn: params => {
return authProvider.resetPassword(params);
},
{ onSuccess, onError, retry: false }
);
onSuccess,
onError,
retry: false,
});

return [mutation.mutate, mutation];
};

export type UseResetPasswordOptions = {
onSuccess?: OnSuccess;
onError?: onError;
onError?: OnError;
};
16 changes: 9 additions & 7 deletions packages/ra-supabase-core/src/useSetPassword.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
onError,
OnError,
OnSuccess,
useAuthProvider,
useNotify,
useRedirect,
} from 'ra-core';
import { useMutation, UseMutationResult } from 'react-query';
import { useMutation, UseMutationResult } from '@tanstack/react-query';
import { SetPasswordParams, SupabaseAuthProvider } from './authProvider';

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

const mutation = useMutation<unknown, Error, SetPasswordParams>(
params => {
const mutation = useMutation<unknown, Error, SetPasswordParams>({
mutationFn: params => {
return authProvider.setPassword(params);
},
{ onSuccess, onError, retry: false }
);
onSuccess,
onError,
retry: false,
});

return [mutation.mutate, mutation];
};

export type UseSetPasswordOptions = {
onSuccess?: OnSuccess;
onError?: onError;
onError?: OnError;
};
Loading
Loading