/** *******************************************************************************************************************
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. *
******************************************************************************************************************** */
import React from 'react';
import { render, cleanup, fireEvent, act } from '@testing-library/react';
import Header from '../../components/Header';
import AppLayout, { useAppLayoutContext, Notification } from '.';
import useLocalStorage from 'react-use-localstorage';
const mockSetLocalStorage = jest.fn();
jest.mock('react-use-localstorage', () => ({
__esModule: true,
default: jest.fn(),
}));
jest.mock('@material-ui/core/styles/makeStyles', () => ({
__esModule: true,
default: jest.fn().mockImplementation(() => () => ({})),
}));
const header = ;
const navigation =
App Name
;
const helpPanel =
HelpPanel
;
const splitPanel =
SplitPanel
;
const breadcrumbsNode =
MockBreadcrumbs
;
const notifications: Notification[] = [
{
id: '1',
header: 'Failed to update 1 order',
type: 'error',
content: 'This is a dismissible error message with a button.',
buttonText: 'Retry',
onButtonClick: jest.fn(),
dismissible: true,
onDismiss: jest.fn(),
},
];
describe('AppLayout', () => {
beforeEach(() => {
jest.resetAllMocks();
(useLocalStorage as jest.Mock).mockReturnValue(['false', mockSetLocalStorage]);
});
afterEach(cleanup);
it('renders the headers', () => {
const { container } = render();
const headerTag = container.getElementsByTagName('header')[0];
expect(headerTag).toBeDefined();
expect(headerTag.textContent).toBe('App Title');
});
it('renders the breadcrumbs node', () => {
const { getByText } = render();
expect(getByText('MockBreadcrumbs')).toBeInTheDocument();
});
describe('NavSideBar', () => {
it('renders the nav sidebar node but no visible', () => {
const { getByText } = render();
expect(getByText('App Name')).toBeInTheDocument();
expect(getByText('App Name')).not.toBeVisible();
});
it('should set the local storage value true when users click the open nav drawer button', () => {
const { getByTestId } = render();
act(() => {
fireEvent.click(getByTestId('open-nav-drawer'));
});
expect(mockSetLocalStorage).toBeCalledWith('true');
});
it('should render the nav sidebar drawer visible when local storage returns true', () => {
(useLocalStorage as jest.Mock).mockReturnValue(['true', mockSetLocalStorage]);
const { getByTestId, getByText } = render();
act(() => {
fireEvent.click(getByTestId('open-nav-drawer'));
});
expect(getByText('App Name')).toBeVisible();
});
});
describe('HelpPanel', () => {
it('renders the help panel node', () => {
const { getByText } = render();
expect(getByText('HelpPanel')).toBeInTheDocument();
expect(getByText('HelpPanel')).not.toBeVisible();
});
it('should set the local storage value true when users click the button', () => {
const { getByTestId } = render();
act(() => {
fireEvent.click(getByTestId('open-helppanel-drawer'));
});
expect(mockSetLocalStorage).toBeCalledWith('true');
});
it('should render the help panel drawer visible when local storage returns true', () => {
(useLocalStorage as jest.Mock).mockReturnValue(['true', mockSetLocalStorage]);
const { getByTestId, getByText } = render();
act(() => {
fireEvent.click(getByTestId('open-helppanel-drawer'));
});
expect(getByText('HelpPanel')).toBeVisible();
});
it('should trigger open help panel when users call openHelpPanel helper method', () => {
const ContentNode = () => {
const { openHelpPanel } = useAppLayoutContext();
return (
);
};
const { getByTestId, getByText, queryByText } = render(
);
act(() => {
fireEvent.click(getByTestId('trigger-add-notifications'));
});
expect(queryByText('Your request 1 is being processed')).toBeNull();
expect(getByText('Your request 2 is being processed')).toBeVisible();
expect(getByText('Your request 3 is being processed')).toBeVisible();
expect(getByText('Your request 4 is being processed')).toBeVisible();
expect(notifications[0].onDismiss).toHaveBeenCalled();
act(() => {
fireEvent.click(getByTestId('trigger-remove-notification'));
});
expect(queryByText('Your request 1 is being processed')).toBeNull();
expect(getByText('Your request 2 is being processed')).toBeVisible();
expect(queryByText('Your request 3 is being processed')).toBeNull();
expect(getByText('Your request 4 is being processed')).toBeVisible();
expect(notifications[2].onDismiss).toHaveBeenCalled();
act(() => {
fireEvent.click(getByTestId('trigger-remove-all-notifications'));
});
expect(queryByText('Your request 1 is being processed')).toBeNull();
expect(queryByText('Your request 2 is being processed')).toBeNull();
expect(queryByText('Your request 3 is being processed')).toBeNull();
expect(queryByText('Your request 4 is being processed')).toBeNull();
expect(notifications[1].onDismiss).toHaveBeenCalled();
expect(notifications[3].onDismiss).toHaveBeenCalled();
});
});
});