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

Commit d1ce828

Browse files
priscilawebdevjuanpicado
authored andcommitted
fix: Header Component - Replaced class by func. comp (#142)
* 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
1 parent ae73772 commit d1ce828

File tree

12 files changed

+771
-365
lines changed

12 files changed

+771
-365
lines changed
Lines changed: 105 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,138 @@
11
import React from 'react';
22
import { BrowserRouter as Router } from 'react-router-dom';
3-
import { shallow } from 'enzyme';
3+
import { render, fireEvent, waitForElementToBeRemoved, waitForElement } from '@testing-library/react';
44

55
import Header from './Header';
66

7+
const headerProps = {
8+
username: 'verddacio-user',
9+
scope: 'test scope',
10+
withoutSearch: true,
11+
handleToggleLoginModal: jest.fn(),
12+
handleLogout: jest.fn(),
13+
};
14+
15+
/* eslint-disable react/jsx-no-bind*/
716
describe('<Header /> component with logged in state', () => {
8-
let wrapper;
9-
let routerWrapper;
10-
let instance;
11-
let props;
12-
13-
beforeEach(() => {
14-
props = {
15-
username: 'test user',
16-
handleLogout: jest.fn(),
17-
logo: '',
18-
onToggleLoginModal: jest.fn(),
19-
scope: 'test scope',
20-
withoutSearch: true,
21-
};
22-
routerWrapper = shallow(
17+
test('should load the component in logged out state', () => {
18+
const { container, queryByTestId, getByText } = render(
19+
<Router>
20+
<Header onLogout={headerProps.handleLogout} onToggleLoginModal={headerProps.handleToggleLoginModal} scope={headerProps.scope} />
21+
</Router>
22+
);
23+
24+
expect(container.firstChild).toMatchSnapshot();
25+
expect(queryByTestId('header--menu-acountcircle')).toBeNull();
26+
expect(getByText('Login')).toBeTruthy();
27+
});
28+
29+
test('should load the component in logged in state', () => {
30+
const { container, getByTestId, queryByText } = render(
2331
<Router>
2432
<Header
25-
logo={props.logo}
26-
onLogout={props.handleLogout}
27-
onToggleLoginModal={props.onToggleLoginModal}
28-
scope={props.scope}
29-
username={props.username}
30-
withoutSearch={props.withoutSearch}
33+
onLogout={headerProps.handleLogout}
34+
onToggleLoginModal={headerProps.handleToggleLoginModal}
35+
scope={headerProps.scope}
36+
username={headerProps.username}
3137
/>
3238
</Router>
3339
);
34-
wrapper = routerWrapper.find(Header).dive();
35-
instance = wrapper.instance();
40+
41+
expect(container.firstChild).toMatchSnapshot();
42+
expect(getByTestId('header--menu-acountcircle')).toBeTruthy();
43+
expect(queryByText('Login')).toBeNull();
3644
});
3745

38-
test('should load the component in logged in state', () => {
39-
const state = {
40-
openInfoDialog: false,
41-
packages: undefined,
42-
registryUrl: 'http://localhost',
43-
showMobileNavBar: false,
44-
};
45-
46-
expect(wrapper.state()).toEqual(state);
47-
expect(routerWrapper.html()).toMatchSnapshot();
46+
test('should open login dialog', async () => {
47+
const { getByText } = render(
48+
<Router>
49+
<Header onLogout={headerProps.handleLogout} onToggleLoginModal={headerProps.handleToggleLoginModal} scope={headerProps.scope} />
50+
</Router>
51+
);
52+
53+
const loginBtn = getByText('Login');
54+
fireEvent.click(loginBtn);
55+
expect(headerProps.handleToggleLoginModal).toHaveBeenCalled();
4856
});
4957

50-
test('handleLoggedInMenu: set anchorEl to html element value in state', () => {
51-
// creates a sample menu
52-
const div = document.createElement('div');
53-
const text = document.createTextNode('sample menu');
54-
div.appendChild(text);
58+
test('should logout the user', async () => {
59+
const { getByText, getByTestId } = render(
60+
<Router>
61+
<Header
62+
onLogout={headerProps.handleLogout}
63+
onToggleLoginModal={headerProps.handleToggleLoginModal}
64+
scope={headerProps.scope}
65+
username={headerProps.username}
66+
/>
67+
</Router>
68+
);
5569

56-
const event = {
57-
currentTarget: div,
58-
};
70+
const headerMenuAccountCircle = getByTestId('header--menu-acountcircle');
71+
fireEvent.click(headerMenuAccountCircle);
5972

60-
instance.handleLoggedInMenu(event);
61-
expect(wrapper.state('anchorEl')).toEqual(div);
73+
// wait for button Logout's appearance and return the element
74+
const logoutBtn = await waitForElement(() => getByText('Logout'));
75+
fireEvent.click(logoutBtn);
76+
expect(headerProps.handleLogout).toHaveBeenCalled();
6277
});
63-
});
6478

65-
describe('<Header /> component with logged out state', () => {
66-
let wrapper;
67-
let routerWrapper;
68-
let instance;
69-
let props;
70-
71-
beforeEach(() => {
72-
props = {
73-
handleLogout: jest.fn(),
74-
onToggleLoginModal: jest.fn(),
75-
scope: 'test scope',
76-
logo: '',
77-
withoutSearch: true,
78-
};
79-
routerWrapper = shallow(
79+
test("The question icon should open a new tab of verdaccio's website - installation doc", async () => {
80+
const { getByTestId } = render(
8081
<Router>
8182
<Header
82-
logo={props.logo}
83-
onLogout={props.handleLogout}
84-
onToggleLoginModal={props.onToggleLoginModal}
85-
scope={props.scope}
86-
withoutSearch={props.withoutSearch}
83+
onLogout={headerProps.handleLogout}
84+
onToggleLoginModal={headerProps.handleToggleLoginModal}
85+
scope={headerProps.scope}
86+
username={headerProps.username}
8787
/>
8888
</Router>
8989
);
90-
wrapper = routerWrapper.find(Header).dive();
91-
instance = wrapper.instance();
92-
});
9390

94-
test('should load the component in logged out state', () => {
95-
const state = {
96-
openInfoDialog: false,
97-
packages: undefined,
98-
registryUrl: 'http://localhost',
99-
showMobileNavBar: false,
100-
};
101-
expect(wrapper.state()).toEqual(state);
102-
expect(routerWrapper.html()).toMatchSnapshot();
91+
const documentationBtn = getByTestId('header--tooltip-documentation');
92+
expect(documentationBtn.getAttribute('href')).toBe('https://verdaccio.org/docs/en/installation');
10393
});
10494

105-
test('handleLoggedInMenuClose: set anchorEl value to null in state', () => {
106-
instance.handleLoggedInMenuClose();
107-
expect(wrapper.state('anchorEl')).toBeNull();
108-
});
95+
test('should open the registrationInfo modal when clicking on the info icon', async () => {
96+
const { getByTestId } = render(
97+
<Router>
98+
<Header
99+
onLogout={headerProps.handleLogout}
100+
onToggleLoginModal={headerProps.handleToggleLoginModal}
101+
scope={headerProps.scope}
102+
username={headerProps.username}
103+
/>
104+
</Router>
105+
);
109106

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

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

120-
test('handleToggleLogin: close/open popover menu', () => {
121-
instance.handleToggleLogin();
122-
expect(wrapper.state('anchorEl')).toBeNull();
123-
expect(props.onToggleLoginModal).toHaveBeenCalled();
115+
test('should close the registrationInfo modal when clicking on the button close', async () => {
116+
const { getByTestId, getByText, queryByTestId } = render(
117+
<Router>
118+
<Header
119+
onLogout={headerProps.handleLogout}
120+
onToggleLoginModal={headerProps.handleToggleLoginModal}
121+
scope={headerProps.scope}
122+
username={headerProps.username}
123+
/>
124+
</Router>
125+
);
126+
127+
const infoBtn = getByTestId('header--tooltip-info');
128+
fireEvent.click(infoBtn);
129+
130+
// wait for Close's button of registrationInfo modal appearance and return the element
131+
const closeBtn = await waitForElement(() => getByText('CLOSE'));
132+
fireEvent.click(closeBtn);
133+
134+
const hasRegistrationInfoModalBeenRemoved = await waitForElementToBeRemoved(() => queryByTestId('registryInfo--dialog'));
135+
expect(hasRegistrationInfoModalBeenRemoved).toBeTruthy();
124136
});
137+
test.todo('autocompletion should display suggestions according to the type value');
125138
});

0 commit comments

Comments
 (0)