add useSettingsForm get test

This commit is contained in:
Simone Taeggi 2023-05-28 23:18:21 +02:00
parent a06a9b8fe8
commit 45b9631494
3 changed files with 120 additions and 6 deletions

View File

@ -82,6 +82,7 @@ const useSettingsForm = (endPoint, schema, cbSuccess, fieldsToPick) => {
});
if (!errors) {
try {
lockApp();
@ -116,7 +117,7 @@ const useSettingsForm = (endPoint, schema, cbSuccess, fieldsToPick) => {
});
} catch (err) {
const data = err?.response?.payload ?? { data: {} };
if (!!data?.data && typeof data.data === 'string') {
toggleNotification({
type: 'warning',

View File

@ -0,0 +1,113 @@
import * as React from 'react';
import { setupServer } from 'msw/node';
import { rest } from 'msw';
import { renderHook, act } from '@testing-library/react-hooks';
import { IntlProvider } from 'react-intl';
import useSettingsForm from '../index';
const toggleNotification = jest.fn();
jest.mock('@strapi/helper-plugin', () => ({
...jest.requireActual('@strapi/helper-plugin'),
useNotification: jest.fn().mockImplementation(() => toggleNotification),
useOverlayBlocker: () => ({ lockApp: jest.fn(), unlockApp: jest.fn() }),
}));
jest.mock('../../../utils', () => ({
...jest.requireActual('../../../utils'),
checkFormValidity: () => (null),
}));
const server = setupServer(
rest.get('*/providers/options', (req, res, ctx) =>
res(
ctx.status(200),
ctx.json({
data: {
autoRegister: false,
defaultRole: "1",
ssoLockedRoles: ["1","2"]
}
})
)
),
rest.put('*/providers/options', (req, res, ctx) => {
res(
ctx.status(200),
ctx.json({
data: {
data: {
autoRegister: false,
defaultRole: "1",
ssoLockedRoles: ["1","2","3"]
}
}
})
)
})
);
const setup = (...args) =>
renderHook(() => useSettingsForm(...args), {
wrapper({ children }) {
return (
<IntlProvider locale="en" messages={{}}>
{children}
</IntlProvider>
);
}
})
describe('useSettingsForm', () => {
beforeAll(() => {
server.listen();
});
afterAll(() => {
server.close();
});
test('fetches all the providers options', async () => {
const { result, waitFor } = setup('/admin/providers/options', {
validate: jest.fn()
}, jest.fn(), ['autoRegister', 'defaultRole', 'ssoLockedRoles'] );
expect(result.current[0].isLoading).toBe(true);
expect(result.current[0].formErrors).toStrictEqual({});
expect(result.current[0].initialData).toStrictEqual({});
expect(result.current[0].modifiedData).toStrictEqual({});
expect(result.current[0].showHeaderButtonLoader).toBeFalsy();
expect(result.current[0].showHeaderLoader).toBeTruthy();
await waitFor(() => expect(result.current[0].isLoading).toBe(false));
expect(result.current[0].formErrors).toStrictEqual({});
expect(result.current[0].initialData).toStrictEqual(
expect.objectContaining({
autoRegister: false,
defaultRole: "1",
ssoLockedRoles: ["1","2"]
})
);
expect(result.current[0].modifiedData).toStrictEqual(
expect.objectContaining({
autoRegister: false,
defaultRole: "1",
ssoLockedRoles: ["1","2"]
})
);
expect(result.current[0].showHeaderButtonLoader).toBeFalsy();
expect(result.current[0].showHeaderLoader).toBeFalsy();
});
test('submit new providers options', async () => {
const cbSucc = jest.fn()
const { result, waitFor } = setup('/admin/providers/options', {}, cbSucc, ['autoRegister', 'defaultRole', 'ssoLockedRoles'] );
await waitFor(() => expect(result.current[0].isLoading).toBe(false));
const e = { preventDefault: jest.fn() };
await act(async () => {
await result.current[2].handleSubmit(e);
});
});
});

View File

@ -863,21 +863,21 @@ exports[`Admin | ee | SettingsPage | SSO renders and matches the snapshot 1`] =
>
<label
class="c8 c24 c25"
for=":r6:"
for="5"
>
Local authentication lock-out
</label>
<div
aria-autocomplete="none"
aria-controls="radix-:r9:"
aria-describedby=":r6:-hint :r6:-error"
aria-controls="radix-1"
aria-describedby="5-hint 5-error"
aria-disabled="false"
aria-expanded="false"
aria-label="Local authentication lock-out"
class="c47 c39 c40"
data-state="closed"
dir="ltr"
id=":r6:"
id="5"
overflow="hidden"
role="combobox"
tabindex="0"
@ -964,7 +964,7 @@ exports[`Admin | ee | SettingsPage | SSO renders and matches the snapshot 1`] =
</div>
<p
class="c8 c37"
id=":r6:-hint"
id="5-hint"
>
Select the roles for which you want to disable the local authentication
</p>