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) { if (!errors) {
try { try {
lockApp(); lockApp();
@ -116,7 +117,7 @@ const useSettingsForm = (endPoint, schema, cbSuccess, fieldsToPick) => {
}); });
} catch (err) { } catch (err) {
const data = err?.response?.payload ?? { data: {} }; const data = err?.response?.payload ?? { data: {} };
if (!!data?.data && typeof data.data === 'string') { if (!!data?.data && typeof data.data === 'string') {
toggleNotification({ toggleNotification({
type: 'warning', 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 <label
class="c8 c24 c25" class="c8 c24 c25"
for=":r6:" for="5"
> >
Local authentication lock-out Local authentication lock-out
</label> </label>
<div <div
aria-autocomplete="none" aria-autocomplete="none"
aria-controls="radix-:r9:" aria-controls="radix-1"
aria-describedby=":r6:-hint :r6:-error" aria-describedby="5-hint 5-error"
aria-disabled="false" aria-disabled="false"
aria-expanded="false" aria-expanded="false"
aria-label="Local authentication lock-out" aria-label="Local authentication lock-out"
class="c47 c39 c40" class="c47 c39 c40"
data-state="closed" data-state="closed"
dir="ltr" dir="ltr"
id=":r6:" id="5"
overflow="hidden" overflow="hidden"
role="combobox" role="combobox"
tabindex="0" tabindex="0"
@ -964,7 +964,7 @@ exports[`Admin | ee | SettingsPage | SSO renders and matches the snapshot 1`] =
</div> </div>
<p <p
class="c8 c37" class="c8 c37"
id=":r6:-hint" id="5-hint"
> >
Select the roles for which you want to disable the local authentication Select the roles for which you want to disable the local authentication
</p> </p>