1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00
verdaccio/test/unit/webui/components/header.spec.js

117 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-12-01 20:04:01 +01:00
/**
* @prettier
* @flow
2017-12-01 20:04:01 +01: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';
import Header from '../../../../src/webui/components/Header';
describe('<Header /> component with logged in state', () => {
let wrapper;
2019-01-23 07:43:23 +01:00
let routerWrapper;
let instance;
let props;
beforeEach(() => {
props = {
username: 'test user',
handleLogout: jest.fn(),
logo: '',
2018-12-15 09:18:26 +01:00
onToggleLoginModal: jest.fn(),
scope: 'test scope',
withoutSearch: true,
};
2019-01-23 07:43:23 +01:00
routerWrapper = shallow(
<Router>
<Header {...props} />
</Router>
);
wrapper = routerWrapper.find(Header).dive();
instance = wrapper.instance();
});
test('should load the component in logged in state', () => {
const state = {
openInfoDialog: false,
packages: undefined,
registryUrl: 'http://localhost',
showMobileNavBar: false,
};
2019-01-23 07:43:23 +01:00
expect(wrapper.state()).toEqual(state);
2019-01-23 07:43:23 +01:00
expect(routerWrapper.html()).toMatchSnapshot();
});
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
const event = {
currentTarget: div,
};
2019-01-23 07:43:23 +01:00
instance.handleLoggedInMenu(event);
expect(wrapper.state('anchorEl')).toEqual(div);
2017-12-01 20:04:01 +01:00
});
});
describe('<Header /> component with logged out state', () => {
let wrapper;
2019-01-23 07:43:23 +01:00
let routerWrapper;
let instance;
let props;
2017-12-01 20:04:01 +01:00
beforeEach(() => {
props = {
handleLogout: jest.fn(),
2018-12-15 09:18:26 +01:00
onToggleLoginModal: jest.fn(),
scope: 'test scope',
logo: '',
withoutSearch: true,
};
2019-01-23 07:43:23 +01:00
routerWrapper = shallow(
<Router>
<Header {...props} />
</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);
2019-01-23 07:43:23 +01:00
expect(routerWrapper.html()).toMatchSnapshot();
});
test('handleLoggedInMenuClose: set anchorEl value to null in state', () => {
2019-01-23 07:43:23 +01:00
instance.handleLoggedInMenuClose();
expect(wrapper.state('anchorEl')).toBeNull();
});
test('handleOpenRegistryInfoDialog: set openInfoDialog to be truthy in state', () => {
2019-01-23 07:43:23 +01:00
instance.handleOpenRegistryInfoDialog();
expect(wrapper.state('openInfoDialog')).toBeTruthy();
});
test('handleCloseRegistryInfoDialog: set openInfoDialog to be falsy in state', () => {
2019-01-23 07:43:23 +01:00
instance.handleCloseRegistryInfoDialog();
expect(wrapper.state('openInfoDialog')).toBeFalsy();
});
test('handleToggleLogin: close/open popover menu', () => {
2019-01-23 07:43:23 +01:00
instance.handleToggleLogin();
expect(wrapper.state('anchorEl')).toBeNull();
2018-12-15 09:18:26 +01:00
expect(props.onToggleLoginModal).toHaveBeenCalled();
});
});