1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00
verdaccio/test/unit/webui/app.spec.js
Juan Picado @jotadeveloper 3639557118
feat: replaced element-react by Material-UI (#950) (#985)
* feat: added material-ui

refactor: replaced element-react by material-ui

refactor: updated snapshots

refactor: updated tests

* fix: modified validation.WIP

* refactor: modified tests.WIP

* test(fix): unit test for login and validat ecredentials

* chore(fix): e2e update css selectors

* test(fix): replace Object.values by supported syntax on node6
2018-09-06 21:26:54 +02:00

107 lines
3.1 KiB
JavaScript

import React from 'react';
import { mount } from 'enzyme';
import storage from '../../../src/webui/utils/storage';
import App from '../../../src/webui/app';
import { API_ERROR } from '../../../src/lib/constants';
import { generateTokenWithTimeRange } from './components/__mocks__/token';
jest.mock('../../../src/webui/utils/storage', () => {
class LocalStorageMock {
constructor() {
this.store = {};
}
clear() {
this.store = {};
}
getItem(key) {
return this.store[key] || null;
}
setItem(key, value) {
this.store[key] = value.toString();
}
removeItem(key) {
delete this.store[key];
}
}
return new LocalStorageMock();
});
jest.mock('element-theme-default', () => ({}));
jest.mock('../../../src/webui/utils/api', () => ({
request: require('./components/__mocks__/api').default.request
}));
describe('App', () => {
let wrapper;
beforeEach(() => {
wrapper = mount(<App />);
});
it('loadLogo: set logo url in state', async () => {
const { loadLogo } = wrapper.instance();
await loadLogo();
expect(wrapper.state().logoUrl).toEqual(
'http://localhost/-/static/logo.png'
);
});
it('toggleLoginModal: should toggle the value in state', () => {
const { toggleLoginModal } = wrapper.instance();
expect(wrapper.state().showLoginModal).toBeFalsy();
toggleLoginModal();
expect(wrapper.state('showLoginModal')).toBeTruthy();
expect(wrapper.state('error')).toEqual({});
});
it('isUserAlreadyLoggedIn: token already available in storage', async () => {
storage.setItem('username', 'verdaccio');
storage.setItem('token', generateTokenWithTimeRange(24));
const { isUserAlreadyLoggedIn } = wrapper.instance();
isUserAlreadyLoggedIn();
expect(wrapper.state('user').username).toEqual('verdaccio');
});
it('handleLogout - logouts the user and clear localstorage', () => {
const { handleLogout } = wrapper.instance();
storage.setItem('username', 'verdaccio');
storage.setItem('token', 'xxxx.TOKEN.xxxx');
handleLogout();
expect(handleLogout()).toBeUndefined();
expect(wrapper.state('user')).toEqual({});
expect(wrapper.state('isLoggedIn')).toBeFalsy();
});
it('doLogin - login the user successfully', async () => {
const { doLogin } = wrapper.instance();
await doLogin('sam', '1234');
const result = {
username: 'sam',
token: 'TEST_TOKEN'
};
expect(wrapper.state('user')).toEqual(result);
expect(wrapper.state('isUserLoggedIn')).toBeTruthy();
expect(wrapper.state('showLoginModal')).toBeFalsy();
expect(storage.getItem('username')).toEqual('sam');
expect(storage.getItem('token')).toEqual('TEST_TOKEN');
});
it('doLogin - authentication failure', async () => {
const { doLogin } = wrapper.instance();
await doLogin('sam', '12345');
console.log(API_ERROR.BAD_USERNAME_PASSWORD);
const result = {
description: 'bad username/password, access denied',
title: 'Unable to login',
type: 'error'
};
expect(wrapper.state('user')).toEqual({});
expect(wrapper.state('error')).toEqual(result);
});
});