2021-02-04 19:15:23 +01:00
|
|
|
import React from 'react';
|
2022-09-27 17:03:26 +02:00
|
|
|
import { fireEvent } from '@testing-library/react';
|
|
|
|
import configureMockStore from 'redux-mock-store';
|
|
|
|
import thunk from 'redux-thunk';
|
|
|
|
import mockState from '../../../../test/data/mock-state.json';
|
|
|
|
import { renderWithProvider } from '../../../../test/lib/render-helpers';
|
|
|
|
import AppHeader from '.';
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2022-09-27 17:03:26 +02:00
|
|
|
const mockShowNetworkDropdown = jest.fn();
|
|
|
|
const mockHideNetworkDropdown = jest.fn();
|
|
|
|
const mockToggleAccountMenu = jest.fn();
|
|
|
|
|
|
|
|
jest.mock('../../../store/actions', () => {
|
|
|
|
return {
|
|
|
|
showNetworkDropdown: () => mockShowNetworkDropdown,
|
|
|
|
hideNetworkDropdown: () => mockHideNetworkDropdown,
|
|
|
|
toggleAccountMenu: () => mockToggleAccountMenu,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2022-09-27 17:03:26 +02:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2022-09-27 17:03:26 +02:00
|
|
|
describe('App Header', () => {
|
|
|
|
afterEach(() => {
|
|
|
|
mockShowNetworkDropdown.mockClear();
|
|
|
|
mockHideNetworkDropdown.mockClear();
|
|
|
|
mockToggleAccountMenu.mockClear();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2022-09-27 17:03:26 +02:00
|
|
|
const store = configureMockStore([thunk])(mockState);
|
|
|
|
|
|
|
|
it('should match snapshot', () => {
|
|
|
|
const { container } = renderWithProvider(<AppHeader />, store);
|
|
|
|
expect(container).toMatchSnapshot();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('App Header Logo', () => {
|
|
|
|
it('routes to default route when logo is clicked', () => {
|
2022-09-27 17:03:26 +02:00
|
|
|
const { history, queryByTestId } = renderWithProvider(
|
|
|
|
<AppHeader />,
|
|
|
|
store,
|
|
|
|
'/different-route',
|
|
|
|
);
|
|
|
|
expect(history.location.pathname).toStrictEqual('/different-route');
|
|
|
|
|
|
|
|
const appLogo = queryByTestId('app-header-logo');
|
|
|
|
fireEvent.click(appLogo);
|
|
|
|
|
|
|
|
expect(history.location.pathname).toStrictEqual('/');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('Network', () => {
|
|
|
|
it('shows network dropdown when networkDropdownOpen is false', () => {
|
2022-09-27 17:03:26 +02:00
|
|
|
const { queryByTestId } = renderWithProvider(<AppHeader />, store);
|
|
|
|
|
|
|
|
const networkDisplay = queryByTestId('network-display');
|
|
|
|
|
|
|
|
fireEvent.click(networkDisplay);
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2022-09-27 17:03:26 +02:00
|
|
|
expect(mockShowNetworkDropdown).toHaveBeenCalled();
|
|
|
|
expect(mockHideNetworkDropdown).not.toHaveBeenCalled();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('hides network dropdown when networkDropdownOpen is true', () => {
|
2022-09-27 17:03:26 +02:00
|
|
|
const openNetworkDropdownState = {
|
|
|
|
...mockState,
|
|
|
|
appState: {
|
|
|
|
networkDropdownOpen: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const openNetworkDropdownStore = configureMockStore([thunk])(
|
|
|
|
openNetworkDropdownState,
|
|
|
|
);
|
|
|
|
|
|
|
|
const { queryByTestId } = renderWithProvider(
|
|
|
|
<AppHeader />,
|
|
|
|
openNetworkDropdownStore,
|
|
|
|
);
|
|
|
|
|
|
|
|
const networkDisplay = queryByTestId('network-display');
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2022-09-27 17:03:26 +02:00
|
|
|
fireEvent.click(networkDisplay);
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2022-09-27 17:03:26 +02:00
|
|
|
expect(mockShowNetworkDropdown).not.toHaveBeenCalled();
|
|
|
|
expect(mockHideNetworkDropdown).toHaveBeenCalled();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('hides network indicator', () => {
|
2022-09-27 17:03:26 +02:00
|
|
|
const props = {
|
|
|
|
hideNetworkIndicator: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const { queryByTestId } = renderWithProvider(
|
|
|
|
<AppHeader {...props} />,
|
|
|
|
store,
|
|
|
|
);
|
|
|
|
|
|
|
|
const networkDisplay = queryByTestId('network-display');
|
|
|
|
|
|
|
|
expect(networkDisplay).not.toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('Account Menu', () => {
|
|
|
|
it('toggles account menu', () => {
|
2022-09-27 17:03:26 +02:00
|
|
|
const { queryByTestId } = renderWithProvider(<AppHeader />, store);
|
|
|
|
|
|
|
|
const accountMenuIcon = queryByTestId('account-menu-icon');
|
|
|
|
|
|
|
|
fireEvent.click(accountMenuIcon);
|
|
|
|
expect(mockToggleAccountMenu).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not render account menu icon if isUnlocked is false', () => {
|
|
|
|
const lockedState = {
|
|
|
|
...mockState,
|
|
|
|
metamask: {
|
|
|
|
...mockState.metamask,
|
|
|
|
isUnlocked: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const lockedStore = configureMockStore([thunk])(lockedState);
|
|
|
|
|
|
|
|
const { queryByTestId } = renderWithProvider(<AppHeader />, lockedStore);
|
|
|
|
|
|
|
|
const accountMenuIcon = queryByTestId('account-menu-icon');
|
|
|
|
|
|
|
|
expect(accountMenuIcon).not.toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('does not toggle account menu when disabled', () => {
|
2022-09-27 17:03:26 +02:00
|
|
|
const props = {
|
|
|
|
disabled: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const { queryByTestId } = renderWithProvider(
|
|
|
|
<AppHeader {...props} />,
|
|
|
|
store,
|
|
|
|
);
|
|
|
|
|
|
|
|
const accountMenuIcon = queryByTestId('account-menu-icon');
|
|
|
|
|
|
|
|
fireEvent.click(accountMenuIcon);
|
|
|
|
expect(mockToggleAccountMenu).not.toHaveBeenCalled();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|