Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Commit

Permalink
fix: Header Component - Replaced class by func. comp (#142)
Browse files Browse the repository at this point in the history
* refactor: replaced class by func.comp

* refactor: replacing jest test by react-testing-library

* refactor: added test todos

* feat: added more unit tests

* fix: fixed tooltip import

* fix: fixed test

* fix: fixed typo

* fix: fixed imports
  • Loading branch information
priscilawebdev authored and juanpicado committed Oct 8, 2019
1 parent ae73772 commit d1ce828
Show file tree
Hide file tree
Showing 12 changed files with 771 additions and 365 deletions.
197 changes: 105 additions & 92 deletions src/components/Header/Header.test.tsx
Original file line number Diff line number Diff line change
@@ -1,125 +1,138 @@
import React from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import { shallow } from 'enzyme';
import { render, fireEvent, waitForElementToBeRemoved, waitForElement } from '@testing-library/react';

import Header from './Header';

const headerProps = {
username: 'verddacio-user',
scope: 'test scope',
withoutSearch: true,
handleToggleLoginModal: jest.fn(),
handleLogout: jest.fn(),
};

/* eslint-disable react/jsx-no-bind*/
describe('<Header /> component with logged in state', () => {
let wrapper;
let routerWrapper;
let instance;
let props;

beforeEach(() => {
props = {
username: 'test user',
handleLogout: jest.fn(),
logo: '',
onToggleLoginModal: jest.fn(),
scope: 'test scope',
withoutSearch: true,
};
routerWrapper = shallow(
test('should load the component in logged out state', () => {
const { container, queryByTestId, getByText } = render(
<Router>
<Header onLogout={headerProps.handleLogout} onToggleLoginModal={headerProps.handleToggleLoginModal} scope={headerProps.scope} />
</Router>
);

expect(container.firstChild).toMatchSnapshot();
expect(queryByTestId('header--menu-acountcircle')).toBeNull();
expect(getByText('Login')).toBeTruthy();
});

test('should load the component in logged in state', () => {
const { container, getByTestId, queryByText } = render(
<Router>
<Header
logo={props.logo}
onLogout={props.handleLogout}
onToggleLoginModal={props.onToggleLoginModal}
scope={props.scope}
username={props.username}
withoutSearch={props.withoutSearch}
onLogout={headerProps.handleLogout}
onToggleLoginModal={headerProps.handleToggleLoginModal}
scope={headerProps.scope}
username={headerProps.username}
/>
</Router>
);
wrapper = routerWrapper.find(Header).dive();
instance = wrapper.instance();

expect(container.firstChild).toMatchSnapshot();
expect(getByTestId('header--menu-acountcircle')).toBeTruthy();
expect(queryByText('Login')).toBeNull();
});

test('should load the component in logged in state', () => {
const state = {
openInfoDialog: false,
packages: undefined,
registryUrl: 'http://localhost',
showMobileNavBar: false,
};

expect(wrapper.state()).toEqual(state);
expect(routerWrapper.html()).toMatchSnapshot();
test('should open login dialog', async () => {
const { getByText } = render(
<Router>
<Header onLogout={headerProps.handleLogout} onToggleLoginModal={headerProps.handleToggleLoginModal} scope={headerProps.scope} />
</Router>
);

const loginBtn = getByText('Login');
fireEvent.click(loginBtn);
expect(headerProps.handleToggleLoginModal).toHaveBeenCalled();
});

test('handleLoggedInMenu: set anchorEl to html element value in state', () => {
// creates a sample menu
const div = document.createElement('div');
const text = document.createTextNode('sample menu');
div.appendChild(text);
test('should logout the user', async () => {
const { getByText, getByTestId } = render(
<Router>
<Header
onLogout={headerProps.handleLogout}
onToggleLoginModal={headerProps.handleToggleLoginModal}
scope={headerProps.scope}
username={headerProps.username}
/>
</Router>
);

const event = {
currentTarget: div,
};
const headerMenuAccountCircle = getByTestId('header--menu-acountcircle');
fireEvent.click(headerMenuAccountCircle);

instance.handleLoggedInMenu(event);
expect(wrapper.state('anchorEl')).toEqual(div);
// wait for button Logout's appearance and return the element
const logoutBtn = await waitForElement(() => getByText('Logout'));
fireEvent.click(logoutBtn);
expect(headerProps.handleLogout).toHaveBeenCalled();
});
});

describe('<Header /> component with logged out state', () => {
let wrapper;
let routerWrapper;
let instance;
let props;

beforeEach(() => {
props = {
handleLogout: jest.fn(),
onToggleLoginModal: jest.fn(),
scope: 'test scope',
logo: '',
withoutSearch: true,
};
routerWrapper = shallow(
test("The question icon should open a new tab of verdaccio's website - installation doc", async () => {
const { getByTestId } = render(
<Router>
<Header
logo={props.logo}
onLogout={props.handleLogout}
onToggleLoginModal={props.onToggleLoginModal}
scope={props.scope}
withoutSearch={props.withoutSearch}
onLogout={headerProps.handleLogout}
onToggleLoginModal={headerProps.handleToggleLoginModal}
scope={headerProps.scope}
username={headerProps.username}
/>
</Router>
);
wrapper = routerWrapper.find(Header).dive();
instance = wrapper.instance();
});

test('should load the component in logged out state', () => {
const state = {
openInfoDialog: false,
packages: undefined,
registryUrl: 'http://localhost',
showMobileNavBar: false,
};
expect(wrapper.state()).toEqual(state);
expect(routerWrapper.html()).toMatchSnapshot();
const documentationBtn = getByTestId('header--tooltip-documentation');
expect(documentationBtn.getAttribute('href')).toBe('https://verdaccio.org/docs/en/installation');
});

test('handleLoggedInMenuClose: set anchorEl value to null in state', () => {
instance.handleLoggedInMenuClose();
expect(wrapper.state('anchorEl')).toBeNull();
});
test('should open the registrationInfo modal when clicking on the info icon', async () => {
const { getByTestId } = render(
<Router>
<Header
onLogout={headerProps.handleLogout}
onToggleLoginModal={headerProps.handleToggleLoginModal}
scope={headerProps.scope}
username={headerProps.username}
/>
</Router>
);

test('handleOpenRegistryInfoDialog: set openInfoDialog to be truthy in state', () => {
instance.handleOpenRegistryInfoDialog();
expect(wrapper.state('openInfoDialog')).toBeTruthy();
});
const infoBtn = getByTestId('header--tooltip-info');
fireEvent.click(infoBtn);

test('handleCloseRegistryInfoDialog: set openInfoDialog to be falsy in state', () => {
instance.handleCloseRegistryInfoDialog();
expect(wrapper.state('openInfoDialog')).toBeFalsy();
// wait for registrationInfo modal appearance and return the element
const registrationInfoModal = await waitForElement(() => getByTestId('registryInfo--dialog'));
expect(registrationInfoModal).toBeTruthy();
});

test('handleToggleLogin: close/open popover menu', () => {
instance.handleToggleLogin();
expect(wrapper.state('anchorEl')).toBeNull();
expect(props.onToggleLoginModal).toHaveBeenCalled();
test('should close the registrationInfo modal when clicking on the button close', async () => {
const { getByTestId, getByText, queryByTestId } = render(
<Router>
<Header
onLogout={headerProps.handleLogout}
onToggleLoginModal={headerProps.handleToggleLoginModal}
scope={headerProps.scope}
username={headerProps.username}
/>
</Router>
);

const infoBtn = getByTestId('header--tooltip-info');
fireEvent.click(infoBtn);

// wait for Close's button of registrationInfo modal appearance and return the element
const closeBtn = await waitForElement(() => getByText('CLOSE'));
fireEvent.click(closeBtn);

const hasRegistrationInfoModalBeenRemoved = await waitForElementToBeRemoved(() => queryByTestId('registryInfo--dialog'));
expect(hasRegistrationInfoModalBeenRemoved).toBeTruthy();
});
test.todo('autocompletion should display suggestions according to the type value');
});
Loading

0 comments on commit d1ce828

Please sign in to comment.