added tests and refacto

This commit is contained in:
ronronscelestes 2022-03-25 17:21:49 +01:00
parent 292dcb0f37
commit 964b804d34
5 changed files with 121 additions and 14 deletions

View File

@ -13,19 +13,11 @@ const ConfigurationsProvider = ({
const [{ menuLogo }, dispatch] = useReducer(reducer, initialState);
const setMenuLogo = logo => {
if (logo) {
return dispatch({
type: 'CHANGE_LOGO',
logo,
isCustom: true,
});
}
return dispatch({
type: 'CHANGE_LOGO',
logoType: 'menuLogo',
logo: defaultMenuLogo,
isCustom: false,
logo: logo || defaultMenuLogo,
isCustom: !!logo,
});
};

View File

@ -18,7 +18,9 @@ const reducer = (state = initialState, action) =>
produce(state, draftState => {
switch (action.type) {
case 'CHANGE_LOGO': {
draftState[action.logoType].logo = action.logo;
if (draftState[action.logoType]) {
draftState[action.logoType] = { logo: action.logo, isCustom: action.isCustom };
}
break;
}
default: {

View File

@ -0,0 +1,69 @@
import React from 'react';
import { render, fireEvent, screen } from '@testing-library/react';
import ConfigurationsProvider from '../index';
import { useConfigurations } from '../../../hooks';
describe('LanguageProvider', () => {
it('should not crash', () => {
const { container } = render(
<ConfigurationsProvider
authLogo="strapi.jpg"
menuLogo="strapi.jpg"
showReleaseNotification={false}
showTutorials={false}
>
<div>Test</div>
</ConfigurationsProvider>
);
expect(container.firstChild).toMatchInlineSnapshot(`
<div>
Test
</div>
`);
});
it.only('should update menuLogo with setMenuLogo', () => {
const Test = () => {
const { setMenuLogo, menuLogo } = useConfigurations();
console.log('tests', menuLogo);
return (
<div>
<button type="button" onClick={() => setMenuLogo('michka.jpg')}>
Change logo
</button>
<div>{menuLogo.logo}</div>
</div>
);
};
const { container } = render(
<ConfigurationsProvider
authLogo="strapi-auth.jpg"
menuLogo="strapi-menu.jpg"
showReleaseNotification={false}
showTutorials={false}
>
<Test />
</ConfigurationsProvider>
);
fireEvent.click(screen.getByText('Change logo'));
expect(container).toMatchInlineSnapshot(`
<div>
<div>
<button
type="button"
>
Change logo
</button>
<div>
strapi-menu.jpg
</div>
</div>
</div>
`);
});
});

View File

@ -0,0 +1,44 @@
import reducer, { initialState } from '../reducer';
describe('ConfigurationsProvider | reducer', () => {
let state;
beforeEach(() => {
state = initialState;
});
it('should return the initialState', () => {
const action = { type: undefined };
expect(reducer(state, action)).toEqual(initialState);
});
it('should change logo if logoType exists', () => {
const action = {
type: 'CHANGE_LOGO',
logoType: 'menuLogo',
logo: 'strapi.jpeg',
isCustom: false,
};
const expected = {
menuLogo: {
logo: 'strapi.jpeg',
isCustom: false,
},
};
expect(reducer(state, action)).toEqual(expected);
});
it('should return state if logoType does not exist', () => {
const action = {
type: 'CHANGE_LOGO',
logoType: 'totoLogo',
logo: 'strapi.jpeg',
isCustom: false,
};
expect(reducer(state, action)).toEqual(initialState);
});
});

View File

@ -2,10 +2,10 @@ import { useContext, useRef } from 'react';
import { ConfigurationsContext } from '../../contexts';
const useConfigurations = () => {
const context = useContext(ConfigurationsContext);
const contextRef = useRef(context);
const { setMenuLogo, ...rest } = useContext(ConfigurationsContext);
const setMenuLogoRef = useRef(setMenuLogo);
return contextRef.current;
return { setMenuLogo: setMenuLogoRef.current, ...rest };
};
export default useConfigurations;