2017-12-01 14:04:01 -05:00
|
|
|
/**
|
|
|
|
* Header component
|
|
|
|
*/
|
|
|
|
import React from 'react';
|
2017-12-02 09:01:06 -05:00
|
|
|
import { shallow, mount } from 'enzyme';
|
2018-06-17 06:34:59 -05:00
|
|
|
import Header from '../../../../src/webui/src/components/Header';
|
2017-12-01 14:04:01 -05:00
|
|
|
import { BrowserRouter } from 'react-router-dom';
|
2018-06-17 06:34:59 -05:00
|
|
|
import storage from '../../../../src/webui/utils/storage';
|
2017-12-01 14:04:01 -05:00
|
|
|
|
2018-06-17 06:34:59 -05:00
|
|
|
jest.mock('../../../../src/webui/utils/api', () => ({
|
2018-03-21 15:44:19 -05:00
|
|
|
request: require('./__mocks__/api').default.request,
|
2017-12-03 05:14:17 -05:00
|
|
|
}));
|
|
|
|
|
2017-12-01 14:04:01 -05:00
|
|
|
describe('<Header /> component shallow', () => {
|
|
|
|
let wrapper;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
wrapper = shallow(
|
|
|
|
<BrowserRouter>
|
|
|
|
<Header />
|
|
|
|
</BrowserRouter>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should check the initial state', () => {
|
|
|
|
const state = {
|
|
|
|
showLogin: false,
|
|
|
|
username: '',
|
|
|
|
password: '',
|
|
|
|
logo: '',
|
|
|
|
loginError: null
|
|
|
|
};
|
|
|
|
const HeaderWrapper = wrapper.find(Header).dive();
|
|
|
|
expect(HeaderWrapper.state()).toEqual(state);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should toggleLogin modal', () => {
|
|
|
|
const HeaderWrapper = wrapper.find(Header).dive();
|
|
|
|
const toggleLoginModal = HeaderWrapper.instance().toggleLoginModal;
|
|
|
|
|
|
|
|
expect(toggleLoginModal()).toBeUndefined();
|
|
|
|
expect(HeaderWrapper.state('showLogin')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handleInput set state', () => {
|
|
|
|
const HeaderWrapper = wrapper.find(Header).dive();
|
|
|
|
const handleInput = HeaderWrapper.instance().handleInput;
|
|
|
|
|
|
|
|
expect(handleInput('username', 'xyz')).toBeUndefined();
|
|
|
|
expect(HeaderWrapper.state('username')).toEqual('xyz');
|
|
|
|
|
|
|
|
expect(handleInput('password', '1234')).toBeUndefined();
|
|
|
|
expect(HeaderWrapper.state('password')).toEqual('1234');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handleSubmit - should give error for blank username and password', () => {
|
|
|
|
const HeaderWrapper = wrapper.find(Header).dive();
|
2018-06-28 18:15:54 -05:00
|
|
|
const {handleSubmit} = HeaderWrapper.instance();
|
2017-12-01 14:04:01 -05:00
|
|
|
const error = {
|
|
|
|
description: "Username or password can't be empty!",
|
|
|
|
title: 'Unable to login',
|
|
|
|
type: 'error'
|
|
|
|
};
|
2018-05-06 10:50:17 -05:00
|
|
|
expect(handleSubmit({ preventDefault: () => {} })).toBeDefined();
|
2017-12-01 14:04:01 -05:00
|
|
|
expect(HeaderWrapper.state('loginError')).toEqual(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handleSubmit - should login successfully', () => {
|
|
|
|
const HeaderWrapper = wrapper.find(Header).dive();
|
2018-06-28 18:15:54 -05:00
|
|
|
const {handleSubmit} = HeaderWrapper.instance();
|
|
|
|
const event = {preventDefault: () => {}}
|
|
|
|
const spy = jest.spyOn(event, 'preventDefault');
|
|
|
|
|
2017-12-01 14:04:01 -05:00
|
|
|
HeaderWrapper.setState({ username: 'sam', password: '1234' });
|
|
|
|
|
2018-06-28 18:15:54 -05:00
|
|
|
handleSubmit(event).then(() => {
|
|
|
|
expect(spy).toHaveBeenCalled();
|
2017-12-01 14:04:01 -05:00
|
|
|
expect(storage.getItem('token')).toEqual('TEST_TOKEN');
|
|
|
|
expect(storage.getItem('username')).toEqual('sam');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handleSubmit - login should failed with 401', () => {
|
|
|
|
const HeaderWrapper = wrapper.find(Header).dive();
|
2018-06-28 18:15:54 -05:00
|
|
|
const {handleSubmit} = HeaderWrapper.instance();
|
2018-03-21 15:44:19 -05:00
|
|
|
const errorObject = {
|
2017-12-01 14:04:01 -05:00
|
|
|
title: 'Unable to login',
|
|
|
|
type: 'error',
|
2018-06-28 18:15:54 -05:00
|
|
|
description: 'bad username/password, access denied'
|
2017-12-01 14:04:01 -05:00
|
|
|
};
|
2018-06-28 18:15:54 -05:00
|
|
|
const event = { preventDefault: () => { } }
|
|
|
|
const spy = jest.spyOn(event, 'preventDefault');
|
2017-12-01 14:04:01 -05:00
|
|
|
HeaderWrapper.setState({ username: 'sam', password: '12345' });
|
|
|
|
|
2018-06-28 18:15:54 -05:00
|
|
|
handleSubmit(event).then(() => {
|
|
|
|
expect(spy).toHaveBeenCalled();
|
2018-03-21 15:44:19 -05:00
|
|
|
expect(HeaderWrapper.state('loginError')).toEqual(errorObject);
|
2017-12-01 14:04:01 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handleSubmit - login should failed with when no data is sent', () => {
|
|
|
|
const HeaderWrapper = wrapper.find(Header).dive();
|
2018-06-28 18:15:54 -05:00
|
|
|
const {handleSubmit} = HeaderWrapper.instance();
|
2017-12-01 14:04:01 -05:00
|
|
|
const error = {
|
|
|
|
title: 'Unable to login',
|
|
|
|
type: 'error',
|
2017-12-02 06:17:45 -05:00
|
|
|
description: "Username or password can't be empty!"
|
2017-12-01 14:04:01 -05:00
|
|
|
};
|
2018-06-28 18:15:54 -05:00
|
|
|
const event = { preventDefault: () => { } }
|
|
|
|
const spy = jest.spyOn(event, 'preventDefault');
|
|
|
|
|
2017-12-01 14:04:01 -05:00
|
|
|
HeaderWrapper.setState({});
|
|
|
|
|
2018-06-28 18:15:54 -05:00
|
|
|
handleSubmit(event).then(() => {
|
|
|
|
expect(spy).toHaveBeenCalled();
|
2017-12-01 14:04:01 -05:00
|
|
|
expect(HeaderWrapper.state('loginError')).toEqual(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-12-02 09:01:06 -05:00
|
|
|
|
|
|
|
describe('<Header /> snapshot test', () => {
|
|
|
|
it('shoud match snapshot', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<BrowserRouter>
|
|
|
|
<Header />
|
|
|
|
</BrowserRouter>
|
|
|
|
);
|
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|