mirror of
https://github.com/strapi/strapi.git
synced 2025-11-16 18:19:34 +00:00
Add tests
Signed-off-by: soupette <cyril@strapi.io>
This commit is contained in:
parent
f5ea60ae1f
commit
a6e64e3c60
@ -3,7 +3,6 @@ import { auth } from '@strapi/helper-plugin';
|
|||||||
|
|
||||||
const instance = axios.create({
|
const instance = axios.create({
|
||||||
baseURL: process.env.STRAPI_ADMIN_BACKEND_URL,
|
baseURL: process.env.STRAPI_ADMIN_BACKEND_URL,
|
||||||
timeout: 1000,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
instance.interceptors.request.use(
|
instance.interceptors.request.use(
|
||||||
|
|||||||
@ -109,7 +109,7 @@ export const SingleSignOn = () => {
|
|||||||
<Stack size={12}>
|
<Stack size={12}>
|
||||||
<Box background="neutral0" padding={6} shadow="filterShadow" hasRadius>
|
<Box background="neutral0" padding={6} shadow="filterShadow" hasRadius>
|
||||||
<Grid gap={4}>
|
<Grid gap={4}>
|
||||||
<GridItem col="6" xs="12">
|
<GridItem col="6" m="6" s="12">
|
||||||
<ToggleCheckbox
|
<ToggleCheckbox
|
||||||
aria-label="autoRegister"
|
aria-label="autoRegister"
|
||||||
data-testid="autoRegister"
|
data-testid="autoRegister"
|
||||||
@ -140,7 +140,7 @@ export const SingleSignOn = () => {
|
|||||||
})}
|
})}
|
||||||
</ToggleCheckbox>
|
</ToggleCheckbox>
|
||||||
</GridItem>
|
</GridItem>
|
||||||
<GridItem col="6" xs="12">
|
<GridItem col="6" m="6" s="12">
|
||||||
<Select
|
<Select
|
||||||
disabled={!canUpdate}
|
disabled={!canUpdate}
|
||||||
hint={formatMessage({
|
hint={formatMessage({
|
||||||
|
|||||||
@ -250,6 +250,7 @@ describe('Admin | ee | SettingsPage | SSO', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
<main
|
<main
|
||||||
|
aria-labelledby="title"
|
||||||
class="c0"
|
class="c0"
|
||||||
id="main-content"
|
id="main-content"
|
||||||
tabindex="-1"
|
tabindex="-1"
|
||||||
|
|||||||
@ -1,13 +1,11 @@
|
|||||||
import React, { useEffect, useReducer, useRef } from 'react';
|
import React, { useEffect, useReducer, useRef } from 'react';
|
||||||
import { Helmet } from 'react-helmet';
|
import { Helmet } from 'react-helmet';
|
||||||
import isEqual from 'lodash/isEqual';
|
|
||||||
import { useIntl } from 'react-intl';
|
import { useIntl } from 'react-intl';
|
||||||
import {
|
import {
|
||||||
CheckPagePermissions,
|
CheckPagePermissions,
|
||||||
LoadingIndicatorPage,
|
LoadingIndicatorPage,
|
||||||
useNotification,
|
|
||||||
request,
|
|
||||||
useFocusWhenNavigate,
|
useFocusWhenNavigate,
|
||||||
|
useNotification,
|
||||||
useOverlayBlocker,
|
useOverlayBlocker,
|
||||||
} from '@strapi/helper-plugin';
|
} from '@strapi/helper-plugin';
|
||||||
import { CheckIcon } from '@strapi/icons';
|
import { CheckIcon } from '@strapi/icons';
|
||||||
@ -25,49 +23,54 @@ import {
|
|||||||
ToggleCheckbox,
|
ToggleCheckbox,
|
||||||
H3,
|
H3,
|
||||||
} from '@strapi/parts';
|
} from '@strapi/parts';
|
||||||
import { getRequestUrl, getTrad } from '../../utils';
|
import axios from 'axios';
|
||||||
|
import isEqual from 'lodash/isEqual';
|
||||||
|
import { axiosInstance, getRequestUrl, getTrad } from '../../utils';
|
||||||
import init from './init';
|
import init from './init';
|
||||||
import reducer, { initialState } from './reducer';
|
import reducer, { initialState } from './reducer';
|
||||||
import pluginPermissions from '../../permissions';
|
import pluginPermissions from '../../permissions';
|
||||||
|
|
||||||
export const SettingsPage = () => {
|
export const SettingsPage = () => {
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
|
const { lockApp, unlockApp } = useOverlayBlocker();
|
||||||
|
const toggleNotification = useNotification();
|
||||||
|
useFocusWhenNavigate();
|
||||||
|
|
||||||
const [{ initialData, isLoading, isSubmiting, modifiedData }, dispatch] = useReducer(
|
const [{ initialData, isLoading, isSubmiting, modifiedData }, dispatch] = useReducer(
|
||||||
reducer,
|
reducer,
|
||||||
initialState,
|
initialState,
|
||||||
init
|
init
|
||||||
);
|
);
|
||||||
|
|
||||||
const { lockApp, unlockApp } = useOverlayBlocker();
|
|
||||||
|
|
||||||
const isMounted = useRef(true);
|
const isMounted = useRef(true);
|
||||||
const getDataRef = useRef();
|
|
||||||
useFocusWhenNavigate();
|
|
||||||
const toggleNotification = useNotification();
|
|
||||||
|
|
||||||
const abortController = new AbortController();
|
useEffect(() => {
|
||||||
|
const CancelToken = axios.CancelToken;
|
||||||
|
const source = CancelToken.source();
|
||||||
|
|
||||||
getDataRef.current = async () => {
|
const getData = async () => {
|
||||||
try {
|
try {
|
||||||
const { signal } = abortController;
|
const {
|
||||||
const { data } = await request(getRequestUrl('settings', { method: 'GET', signal }));
|
data: { data },
|
||||||
|
} = await axiosInstance.get(getRequestUrl('settings'), {
|
||||||
|
cancelToken: source.token,
|
||||||
|
});
|
||||||
|
|
||||||
if (isMounted.current) {
|
|
||||||
dispatch({
|
dispatch({
|
||||||
type: 'GET_DATA_SUCCEEDED',
|
type: 'GET_DATA_SUCCEEDED',
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
if (isMounted.current) {
|
||||||
getDataRef.current();
|
getData();
|
||||||
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
abortController.abort();
|
source.cancel('Operation canceled by the user.');
|
||||||
isMounted.current = false;
|
isMounted.current = false;
|
||||||
};
|
};
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
@ -87,16 +90,11 @@ export const SettingsPage = () => {
|
|||||||
dispatch({ type: 'ON_SUBMIT' });
|
dispatch({ type: 'ON_SUBMIT' });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await request(getRequestUrl('settings'), {
|
await axiosInstance.put(getRequestUrl('settings'), modifiedData);
|
||||||
method: 'PUT',
|
|
||||||
body: modifiedData,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (isMounted.current) {
|
|
||||||
dispatch({
|
dispatch({
|
||||||
type: 'SUBMIT_SUCCEEDED',
|
type: 'SUBMIT_SUCCEEDED',
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
toggleNotification({
|
toggleNotification({
|
||||||
type: 'success',
|
type: 'success',
|
||||||
@ -137,6 +135,7 @@ export const SettingsPage = () => {
|
|||||||
primaryAction={
|
primaryAction={
|
||||||
<Button
|
<Button
|
||||||
disabled={isSaveButtonDisabled}
|
disabled={isSaveButtonDisabled}
|
||||||
|
data-testid="save-button"
|
||||||
loading={isSubmiting}
|
loading={isSubmiting}
|
||||||
type="submit"
|
type="submit"
|
||||||
startIcon={<CheckIcon />}
|
startIcon={<CheckIcon />}
|
||||||
@ -171,7 +170,7 @@ export const SettingsPage = () => {
|
|||||||
</Box>
|
</Box>
|
||||||
</Row>
|
</Row>
|
||||||
<Grid gap={6}>
|
<Grid gap={6}>
|
||||||
<GridItem col="6" xs="12">
|
<GridItem col="6" s="12">
|
||||||
<ToggleCheckbox
|
<ToggleCheckbox
|
||||||
aria-label="responsiveDimensions"
|
aria-label="responsiveDimensions"
|
||||||
data-testid="responsiveDimensions"
|
data-testid="responsiveDimensions"
|
||||||
@ -202,7 +201,7 @@ export const SettingsPage = () => {
|
|||||||
})}
|
})}
|
||||||
</ToggleCheckbox>
|
</ToggleCheckbox>
|
||||||
</GridItem>
|
</GridItem>
|
||||||
<GridItem col="6" xs="12">
|
<GridItem col="6" s="12">
|
||||||
<ToggleCheckbox
|
<ToggleCheckbox
|
||||||
aria-label="sizeOptimization"
|
aria-label="sizeOptimization"
|
||||||
data-testid="sizeOptimization"
|
data-testid="sizeOptimization"
|
||||||
@ -228,7 +227,7 @@ export const SettingsPage = () => {
|
|||||||
})}
|
})}
|
||||||
</ToggleCheckbox>
|
</ToggleCheckbox>
|
||||||
</GridItem>
|
</GridItem>
|
||||||
<GridItem col="6" xs="12">
|
<GridItem col="6" s="12">
|
||||||
<ToggleCheckbox
|
<ToggleCheckbox
|
||||||
aria-label="autoOrientation"
|
aria-label="autoOrientation"
|
||||||
data-testid="autoOrientation"
|
data-testid="autoOrientation"
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@ -0,0 +1,64 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* Tests for SettingsPage
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
import React from 'react';
|
||||||
|
import { render, waitFor } from '@testing-library/react';
|
||||||
|
import { IntlProvider } from 'react-intl';
|
||||||
|
import { ThemeProvider, lightTheme } from '@strapi/parts';
|
||||||
|
import { SettingsPage } from '../index';
|
||||||
|
import server from './utils/server';
|
||||||
|
|
||||||
|
jest.mock('@strapi/helper-plugin', () => ({
|
||||||
|
...jest.requireActual('@strapi/helper-plugin'),
|
||||||
|
useNotification: jest.fn(),
|
||||||
|
useOverlayBlocker: () => ({ lockApp: jest.fn(), unlockApp: jest.fn() }),
|
||||||
|
useFocusWhenNavigate: jest.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
const App = (
|
||||||
|
<ThemeProvider theme={lightTheme}>
|
||||||
|
<IntlProvider locale="en" messages={{ en: {} }} textComponent="span">
|
||||||
|
<SettingsPage />
|
||||||
|
</IntlProvider>
|
||||||
|
</ThemeProvider>
|
||||||
|
);
|
||||||
|
|
||||||
|
describe('Upload | SettingsPage', () => {
|
||||||
|
beforeAll(() => server.listen());
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => server.resetHandlers());
|
||||||
|
|
||||||
|
afterAll(() => server.close());
|
||||||
|
|
||||||
|
it('renders and matches the snapshot', () => {
|
||||||
|
const { container } = render(App);
|
||||||
|
|
||||||
|
expect(container).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should display the form correctly with the initial values', async () => {
|
||||||
|
const { getAllByTestId } = render(App);
|
||||||
|
|
||||||
|
render(App);
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
const responsiveDimensions = getAllByTestId('responsiveDimensions');
|
||||||
|
const sizeOptimizations = getAllByTestId('sizeOptimization');
|
||||||
|
const autoOrientations = getAllByTestId('autoOrientation');
|
||||||
|
// const saveButtons = getAllByTestId('save-button');
|
||||||
|
|
||||||
|
expect(responsiveDimensions[0].checked).toBe(true);
|
||||||
|
expect(autoOrientations[0].checked).toBe(true);
|
||||||
|
expect(sizeOptimizations[0].checked).toBe(false);
|
||||||
|
// FIXME
|
||||||
|
// expect(saveButtons[0]).toBeDisabled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -76,6 +76,7 @@ describe('MEDIA LIBRARY | pages | SettingsPage | reducer', () => {
|
|||||||
responsiveDimensions: true,
|
responsiveDimensions: true,
|
||||||
},
|
},
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
|
isSubmiting: true,
|
||||||
modifiedData: {
|
modifiedData: {
|
||||||
responsiveDimensions: false,
|
responsiveDimensions: false,
|
||||||
},
|
},
|
||||||
@ -83,6 +84,7 @@ describe('MEDIA LIBRARY | pages | SettingsPage | reducer', () => {
|
|||||||
const expected = {
|
const expected = {
|
||||||
initialData: { responsiveDimensions: false },
|
initialData: { responsiveDimensions: false },
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
|
isSubmiting: false,
|
||||||
modifiedData: { responsiveDimensions: false },
|
modifiedData: { responsiveDimensions: false },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,17 @@
|
|||||||
|
import { setupServer } from 'msw/node';
|
||||||
|
import { rest } from 'msw';
|
||||||
|
|
||||||
|
const handlers = [
|
||||||
|
rest.get('*/settings', (req, res, ctx) => {
|
||||||
|
return res(
|
||||||
|
ctx.status(200),
|
||||||
|
ctx.json({
|
||||||
|
data: { autoOrientation: true, responsiveDimensions: true, sizeOptimization: false },
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
|
const server = setupServer(...handlers);
|
||||||
|
|
||||||
|
export default server;
|
||||||
23
packages/core/upload/admin/src/utils/axiosInstance.js
Normal file
23
packages/core/upload/admin/src/utils/axiosInstance.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import axios from 'axios';
|
||||||
|
import { auth } from '@strapi/helper-plugin';
|
||||||
|
|
||||||
|
const instance = axios.create({
|
||||||
|
baseURL: process.env.STRAPI_ADMIN_BACKEND_URL,
|
||||||
|
});
|
||||||
|
|
||||||
|
instance.interceptors.request.use(
|
||||||
|
async config => {
|
||||||
|
config.headers = {
|
||||||
|
Authorization: `Bearer ${auth.getToken()}`,
|
||||||
|
Accept: 'application/json',
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
};
|
||||||
|
|
||||||
|
return config;
|
||||||
|
},
|
||||||
|
error => {
|
||||||
|
Promise.reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
export default instance;
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
export { default as axiosInstance } from './axiosInstance';
|
||||||
export { default as canDownloadFile } from './canDownloadFile';
|
export { default as canDownloadFile } from './canDownloadFile';
|
||||||
export { default as compactParams } from './compactParams';
|
export { default as compactParams } from './compactParams';
|
||||||
export { default as createFileToDownloadName } from './createFileToDownloadName';
|
export { default as createFileToDownloadName } from './createFileToDownloadName';
|
||||||
|
|||||||
@ -26,6 +26,7 @@ const makeApp = (layout = { forms: [], schema: {} }, isOpen = false) => {
|
|||||||
onSubmit={jest.fn()}
|
onSubmit={jest.fn()}
|
||||||
isSubmiting={false}
|
isSubmiting={false}
|
||||||
initialData={{}}
|
initialData={{}}
|
||||||
|
providerToEditName="test"
|
||||||
/>
|
/>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
</IntlProvider>
|
</IntlProvider>
|
||||||
@ -83,7 +84,7 @@ describe('<FormModal />', () => {
|
|||||||
intlLabel: { id: 'enabled', defaultMessage: 'Enabled' },
|
intlLabel: { id: 'enabled', defaultMessage: 'Enabled' },
|
||||||
name: 'enabled',
|
name: 'enabled',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
description: { id: 'test', description: 'test' },
|
description: { id: 'test', defaultMessage: 'test' },
|
||||||
size: 6,
|
size: 6,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import { auth } from '@strapi/helper-plugin';
|
|||||||
|
|
||||||
const instance = axios.create({
|
const instance = axios.create({
|
||||||
baseURL: process.env.STRAPI_ADMIN_BACKEND_URL,
|
baseURL: process.env.STRAPI_ADMIN_BACKEND_URL,
|
||||||
timeout: 1000,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
instance.interceptors.request.use(
|
instance.interceptors.request.use(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user