mirror of
				https://gitlab.nic.cz/turris/reforis/foris-js.git
				synced 2025-11-03 23:00:31 +01:00 
			
		
		
		
	Add tests for CustomizationContext
This commit is contained in:
		@@ -0,0 +1,53 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2019-2022 CZ.NIC z.s.p.o. (https://www.nic.cz/)
 | 
			
		||||
 *
 | 
			
		||||
 * This is free software, licensed under the GNU General Public License v3.
 | 
			
		||||
 * See /LICENSE for more information.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
import React from "react";
 | 
			
		||||
 | 
			
		||||
import { render, wait, getByText } from "customTestRender";
 | 
			
		||||
import mockAxios from "jest-mock-axios";
 | 
			
		||||
 | 
			
		||||
import {
 | 
			
		||||
    useCustomizationContext,
 | 
			
		||||
    CustomizationContextProvider,
 | 
			
		||||
} from "../CustomizationContext";
 | 
			
		||||
 | 
			
		||||
const CUSTOM = "Description / component for customized reForis (Shield)";
 | 
			
		||||
const ORIGINAL = "Description / component for original reForis (other devices)";
 | 
			
		||||
 | 
			
		||||
const CustomizationTest = () => {
 | 
			
		||||
    const { isCustomized } = useCustomizationContext();
 | 
			
		||||
 | 
			
		||||
    return <p>{isCustomized ? CUSTOM : ORIGINAL}</p>;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
describe("CustomizationContext", () => {
 | 
			
		||||
    let componentContainer;
 | 
			
		||||
    beforeEach(() => {
 | 
			
		||||
        const { container } = render(
 | 
			
		||||
            <CustomizationContextProvider>
 | 
			
		||||
                <CustomizationTest />
 | 
			
		||||
            </CustomizationContextProvider>
 | 
			
		||||
        );
 | 
			
		||||
        componentContainer = container;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it("should render component without customization", async () => {
 | 
			
		||||
        mockAxios.mockResponse({ data: {} });
 | 
			
		||||
 | 
			
		||||
        await wait(() => getByText(componentContainer, ORIGINAL));
 | 
			
		||||
 | 
			
		||||
        expect(componentContainer).toMatchSnapshot();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it("should render customized component", async () => {
 | 
			
		||||
        mockAxios.mockResponse({ data: { customization: "shield" } });
 | 
			
		||||
 | 
			
		||||
        await wait(() => getByText(componentContainer, CUSTOM));
 | 
			
		||||
 | 
			
		||||
        expect(componentContainer).toMatchSnapshot();
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
@@ -0,0 +1,17 @@
 | 
			
		||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
 | 
			
		||||
 | 
			
		||||
exports[`CustomizationContext should render component without customization 1`] = `
 | 
			
		||||
<div>
 | 
			
		||||
  <p>
 | 
			
		||||
    Description / component for original reForis (other devices)
 | 
			
		||||
  </p>
 | 
			
		||||
</div>
 | 
			
		||||
`;
 | 
			
		||||
 | 
			
		||||
exports[`CustomizationContext should render customized component 1`] = `
 | 
			
		||||
<div>
 | 
			
		||||
  <p>
 | 
			
		||||
    Description / component for customized reForis (Shield)
 | 
			
		||||
  </p>
 | 
			
		||||
</div>
 | 
			
		||||
`;
 | 
			
		||||
@@ -14,6 +14,7 @@ import { render } from "@testing-library/react";
 | 
			
		||||
import PropTypes from "prop-types";
 | 
			
		||||
 | 
			
		||||
import { AlertContextMock } from "./alertContextMock";
 | 
			
		||||
import { CustomizationContextMock } from "./cutomizationContextMock";
 | 
			
		||||
 | 
			
		||||
Wrapper.propTypes = {
 | 
			
		||||
    children: PropTypes.oneOfType([
 | 
			
		||||
@@ -25,9 +26,11 @@ Wrapper.propTypes = {
 | 
			
		||||
function Wrapper({ children }) {
 | 
			
		||||
    return (
 | 
			
		||||
        <AlertContextMock>
 | 
			
		||||
            <CustomizationContextMock>
 | 
			
		||||
                <StaticRouter>
 | 
			
		||||
                    <UIDReset>{children}</UIDReset>
 | 
			
		||||
                </StaticRouter>
 | 
			
		||||
            </CustomizationContextMock>
 | 
			
		||||
        </AlertContextMock>
 | 
			
		||||
    );
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										23
									
								
								src/testUtils/cutomizationContextMock.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/testUtils/cutomizationContextMock.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2019-2022 CZ.NIC z.s.p.o. (https://www.nic.cz/)
 | 
			
		||||
 *
 | 
			
		||||
 * This is free software, licensed under the GNU General Public License v3.
 | 
			
		||||
 * See /LICENSE for more information.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
import React from "react";
 | 
			
		||||
 | 
			
		||||
window.CustomizationContext = React.createContext();
 | 
			
		||||
 | 
			
		||||
const deviceDetails = {};
 | 
			
		||||
const isCustomized = jest.fn();
 | 
			
		||||
 | 
			
		||||
function CustomizationContextMock({ children }) {
 | 
			
		||||
    return (
 | 
			
		||||
        <CustomizationContext.Provider value={(deviceDetails, isCustomized)}>
 | 
			
		||||
            {children}
 | 
			
		||||
        </CustomizationContext.Provider>
 | 
			
		||||
    );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export { CustomizationContextMock };
 | 
			
		||||
		Reference in New Issue
	
	Block a user