2017-12-01 20:04:01 +01:00
|
|
|
/**
|
2018-09-24 22:04:37 +02:00
|
|
|
* @prettier
|
|
|
|
* @flow
|
2017-12-01 20:04:01 +01:00
|
|
|
*/
|
2018-09-24 22:04:37 +02:00
|
|
|
|
2017-12-01 20:04:01 +01:00
|
|
|
import React from 'react';
|
2019-01-23 07:43:23 +01:00
|
|
|
import { BrowserRouter as Router } from 'react-router-dom';
|
|
|
|
import { shallow } from 'enzyme';
|
2018-07-17 21:22:44 +02:00
|
|
|
import Header from '../../../../src/webui/components/Header';
|
2017-12-03 11:14:17 +01:00
|
|
|
|
2018-09-24 22:04:37 +02:00
|
|
|
describe('<Header /> component with logged in state', () => {
|
|
|
|
let wrapper;
|
2019-01-23 07:43:23 +01:00
|
|
|
let routerWrapper;
|
|
|
|
let instance;
|
2018-09-24 22:04:37 +02:00
|
|
|
let props;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
props = {
|
|
|
|
username: 'test user',
|
|
|
|
handleLogout: jest.fn(),
|
2019-01-20 19:25:50 +01:00
|
|
|
logo: '',
|
2018-12-15 09:18:26 +01:00
|
|
|
onToggleLoginModal: jest.fn(),
|
2018-09-24 22:04:37 +02:00
|
|
|
scope: 'test scope',
|
2018-10-27 21:19:45 +02:00
|
|
|
withoutSearch: true,
|
2018-09-24 22:04:37 +02:00
|
|
|
};
|
2019-01-23 07:43:23 +01:00
|
|
|
routerWrapper = shallow(
|
|
|
|
<Router>
|
|
|
|
<Header {...props} />
|
|
|
|
</Router>
|
|
|
|
);
|
|
|
|
wrapper = routerWrapper.find(Header).dive();
|
|
|
|
instance = wrapper.instance();
|
2018-09-24 22:04:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should load the component in logged in state', () => {
|
2018-10-27 21:19:45 +02:00
|
|
|
const state = {
|
|
|
|
openInfoDialog: false,
|
|
|
|
packages: undefined,
|
|
|
|
registryUrl: 'http://localhost',
|
|
|
|
showMobileNavBar: false,
|
|
|
|
};
|
2019-01-23 07:43:23 +01:00
|
|
|
|
2018-09-24 22:04:37 +02:00
|
|
|
expect(wrapper.state()).toEqual(state);
|
2019-01-23 07:43:23 +01:00
|
|
|
expect(routerWrapper.html()).toMatchSnapshot();
|
2018-09-24 22:04:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2017-12-01 20:04:01 +01:00
|
|
|
|
2018-09-24 22:04:37 +02:00
|
|
|
const event = {
|
|
|
|
currentTarget: div,
|
|
|
|
};
|
|
|
|
|
2019-01-23 07:43:23 +01:00
|
|
|
instance.handleLoggedInMenu(event);
|
2018-09-24 22:04:37 +02:00
|
|
|
expect(wrapper.state('anchorEl')).toEqual(div);
|
2017-12-01 20:04:01 +01:00
|
|
|
});
|
2018-09-24 22:04:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('<Header /> component with logged out state', () => {
|
|
|
|
let wrapper;
|
2019-01-23 07:43:23 +01:00
|
|
|
let routerWrapper;
|
|
|
|
let instance;
|
2018-09-24 22:04:37 +02:00
|
|
|
let props;
|
2017-12-01 20:04:01 +01:00
|
|
|
|
2018-09-24 22:04:37 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
props = {
|
2018-08-20 16:29:47 +02:00
|
|
|
handleLogout: jest.fn(),
|
2018-12-15 09:18:26 +01:00
|
|
|
onToggleLoginModal: jest.fn(),
|
2018-09-24 22:04:37 +02:00
|
|
|
scope: 'test scope',
|
2019-01-20 19:25:50 +01:00
|
|
|
logo: '',
|
2018-10-27 21:19:45 +02:00
|
|
|
withoutSearch: true,
|
2018-09-24 22:04:37 +02:00
|
|
|
};
|
2019-01-23 07:43:23 +01:00
|
|
|
routerWrapper = shallow(
|
|
|
|
<Router>
|
|
|
|
<Header {...props} />
|
|
|
|
</Router>
|
|
|
|
);
|
|
|
|
wrapper = routerWrapper.find(Header).dive();
|
|
|
|
instance = wrapper.instance();
|
2018-09-24 22:04:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should load the component in logged out state', () => {
|
2018-10-27 21:19:45 +02:00
|
|
|
const state = {
|
|
|
|
openInfoDialog: false,
|
|
|
|
packages: undefined,
|
|
|
|
registryUrl: 'http://localhost',
|
|
|
|
showMobileNavBar: false,
|
|
|
|
};
|
2018-09-24 22:04:37 +02:00
|
|
|
expect(wrapper.state()).toEqual(state);
|
2019-01-23 07:43:23 +01:00
|
|
|
expect(routerWrapper.html()).toMatchSnapshot();
|
2017-12-02 15:01:06 +01:00
|
|
|
});
|
2018-08-01 15:00:39 +02:00
|
|
|
|
2018-09-24 22:04:37 +02:00
|
|
|
test('handleLoggedInMenuClose: set anchorEl value to null in state', () => {
|
2019-01-23 07:43:23 +01:00
|
|
|
instance.handleLoggedInMenuClose();
|
2018-09-24 22:04:37 +02:00
|
|
|
expect(wrapper.state('anchorEl')).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('handleOpenRegistryInfoDialog: set openInfoDialog to be truthy in state', () => {
|
2019-01-23 07:43:23 +01:00
|
|
|
instance.handleOpenRegistryInfoDialog();
|
2018-09-24 22:04:37 +02:00
|
|
|
expect(wrapper.state('openInfoDialog')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('handleCloseRegistryInfoDialog: set openInfoDialog to be falsy in state', () => {
|
2019-01-23 07:43:23 +01:00
|
|
|
instance.handleCloseRegistryInfoDialog();
|
2018-09-24 22:04:37 +02:00
|
|
|
expect(wrapper.state('openInfoDialog')).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('handleToggleLogin: close/open popover menu', () => {
|
2019-01-23 07:43:23 +01:00
|
|
|
instance.handleToggleLogin();
|
2018-09-24 22:04:37 +02:00
|
|
|
expect(wrapper.state('anchorEl')).toBeNull();
|
2018-12-15 09:18:26 +01:00
|
|
|
expect(props.onToggleLoginModal).toHaveBeenCalled();
|
2018-08-01 15:00:39 +02:00
|
|
|
});
|
2018-09-24 22:04:37 +02:00
|
|
|
});
|