|
1 | 1 | import React from 'react'; |
2 | 2 | import { BrowserRouter as Router } from 'react-router-dom'; |
3 | | -import { shallow } from 'enzyme'; |
| 3 | +import { render, fireEvent, waitForElementToBeRemoved, waitForElement } from '@testing-library/react'; |
4 | 4 |
|
5 | 5 | import Header from './Header'; |
6 | 6 |
|
| 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*/ |
7 | 16 | 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( |
23 | 31 | <Router> |
24 | 32 | <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} |
31 | 37 | /> |
32 | 38 | </Router> |
33 | 39 | ); |
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(); |
36 | 44 | }); |
37 | 45 |
|
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(); |
48 | 56 | }); |
49 | 57 |
|
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 | + ); |
55 | 69 |
|
56 | | - const event = { |
57 | | - currentTarget: div, |
58 | | - }; |
| 70 | + const headerMenuAccountCircle = getByTestId('header--menu-acountcircle'); |
| 71 | + fireEvent.click(headerMenuAccountCircle); |
59 | 72 |
|
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(); |
62 | 77 | }); |
63 | | -}); |
64 | 78 |
|
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( |
80 | 81 | <Router> |
81 | 82 | <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} |
87 | 87 | /> |
88 | 88 | </Router> |
89 | 89 | ); |
90 | | - wrapper = routerWrapper.find(Header).dive(); |
91 | | - instance = wrapper.instance(); |
92 | | - }); |
93 | 90 |
|
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'); |
103 | 93 | }); |
104 | 94 |
|
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 | + ); |
109 | 106 |
|
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); |
114 | 109 |
|
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(); |
118 | 113 | }); |
119 | 114 |
|
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(); |
124 | 136 | }); |
| 137 | + test.todo('autocompletion should display suggestions according to the type value'); |
125 | 138 | }); |
0 commit comments