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
Priscila 9d265996f9 Refactor: Moved Search to Header (#1064)
* refactor: Moved Search to Header

feat: added TxtField. WIP

refactor: replaced downshift by react-autosuggest

refactor: moved search's state

refactor: introduced weight 300 & 500

refactor: changed container css margin

refactor: made it more abstract

refactor: replaced name by label. changed css

refactor: removed aria

refactor: removed margin

fix: fixed flow  types

fix: fixed tags overlapping

fix: fixed search. WIP

refactor: removed useless library and added rect-router

refactor: fixed tests

* chore: remove tpm file

* feat: added component Loading

feat: added component Layout

refactor: changed css

refactor: added md prop

refactor: moved Header back to App

* chore: fix flow

* fix: update snapshot

fix: fixed componentDidMount parameter

refactor: added onKeyDown event

fix: fixed bad request

refactor: renamed interfaces files

refactor: refactor: logic display results

refactor: changed minor things

fix: fixed tests

fix: fixed tests
2018-10-27 21:19:45 +02:00

104 lines
3.0 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('../../../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', async () => {
const { handleLogout } = wrapper.instance();
storage.setItem('username', 'verdaccio');
storage.setItem('token', 'xxxx.TOKEN.xxxx');
await handleLogout();
expect(wrapper.state('user')).toEqual({});
expect(wrapper.state('isUserLoggedIn')).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('isUserLoggedIn')).toBeTruthy();
expect(wrapper.state('showLoginModal')).toBeFalsy();
expect(storage.getItem('username')).toEqual('sam');
expect(storage.getItem('token')).toEqual('TEST_TOKEN');
expect(wrapper.state('user')).toEqual(result);
});
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);
});
});