2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2019-01-08 15:14:00 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-28 14:39:51 +00:00
|
|
|
import Vuex from 'vuex';
|
2019-09-09 11:33:39 +01:00
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
import { UsersApiGql } from '@/api/users';
|
2019-09-09 11:33:39 +01:00
|
|
|
import { makeUsersModule, USER_ACTIONS, USER_MUTATIONS } from '@/store/modules/users';
|
2019-08-14 19:11:18 +01:00
|
|
|
import { UpdatedUser, User } from '@/types/users';
|
2019-09-09 11:33:39 +01:00
|
|
|
import { createLocalVue } from '@vue/test-utils';
|
2019-08-14 19:11:18 +01:00
|
|
|
|
|
|
|
const Vue = createLocalVue();
|
|
|
|
const usersApi = new UsersApiGql();
|
|
|
|
const usersModule = makeUsersModule(usersApi);
|
|
|
|
const { UPDATE, GET, CLEAR } = USER_ACTIONS;
|
|
|
|
|
|
|
|
Vue.use(Vuex);
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
const store = new Vuex.Store(usersModule);
|
2019-01-08 15:14:00 +00:00
|
|
|
|
|
|
|
describe('mutations', () => {
|
2019-02-20 13:33:56 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
createLocalVue().use(Vuex);
|
|
|
|
});
|
2019-08-14 19:11:18 +01:00
|
|
|
it('Set user', () => {
|
|
|
|
const user = new User('1', 'fullName', 'shortName', 'example@email.com');
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
store.commit(USER_MUTATIONS.SET_USER, user);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
expect(store.state.id).toBe(user.id);
|
|
|
|
expect(store.state.email).toBe(user.email);
|
|
|
|
expect(store.state.fullName).toBe(user.fullName);
|
|
|
|
expect(store.state.shortName).toBe(user.shortName);
|
|
|
|
});
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
it('clear user', () => {
|
|
|
|
store.commit(USER_MUTATIONS.CLEAR);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
expect(store.state.id).toBe('');
|
|
|
|
expect(store.state.email).toBe('');
|
|
|
|
expect(store.state.fullName).toBe('');
|
|
|
|
expect(store.state.shortName).toBe('');
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
it('Update user', () => {
|
|
|
|
const user = new UpdatedUser('fullName', 'shortName');
|
|
|
|
|
|
|
|
store.commit(USER_MUTATIONS.UPDATE_USER, user);
|
|
|
|
|
|
|
|
expect(store.state.fullName).toBe(user.fullName);
|
|
|
|
expect(store.state.shortName).toBe(user.shortName);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('actions', () => {
|
2019-02-20 13:33:56 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
it('success update account', async () => {
|
2019-08-14 19:11:18 +01:00
|
|
|
jest.spyOn(usersApi, 'update').mockReturnValue(
|
|
|
|
Promise.resolve()
|
2019-02-21 11:01:52 +00:00
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
const user = new UpdatedUser('fullName1', 'shortName2');
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
await store.dispatch(UPDATE, user);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
expect(store.state.fullName).toBe('fullName1');
|
|
|
|
expect(store.state.shortName).toBe('shortName2');
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
it('update throws an error when api call fails', async () => {
|
|
|
|
jest.spyOn(usersApi, 'update').mockImplementation(() => { throw new Error(); });
|
|
|
|
const newUser = new UpdatedUser('', '');
|
|
|
|
const oldUser = store.getters.user;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(UPDATE, newUser);
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
|
|
|
expect(store.state.fullName).toBe(oldUser.fullName);
|
|
|
|
expect(store.state.shortName).toBe(oldUser.shortName);
|
|
|
|
}
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-19 19:12:23 +01:00
|
|
|
it('clears state', async () => {
|
|
|
|
await store.dispatch(CLEAR);
|
|
|
|
|
|
|
|
expect(store.state.fullName).toBe('');
|
|
|
|
expect(store.state.shortName).toBe('');
|
|
|
|
expect(store.state.email).toBe('');
|
|
|
|
expect(store.state.partnerId).toBe('');
|
|
|
|
expect(store.state.id).toBe('');
|
|
|
|
});
|
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
it('success get user', async () => {
|
2019-08-14 19:11:18 +01:00
|
|
|
const user = new User('2', 'newFullName', 'newShortName', 'example2@email.com');
|
|
|
|
|
|
|
|
jest.spyOn(usersApi, 'get').mockReturnValue(
|
|
|
|
Promise.resolve(user)
|
2019-02-21 11:01:52 +00:00
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
await store.dispatch(GET);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
expect(store.state.id).toBe(user.id);
|
|
|
|
expect(store.state.shortName).toBe(user.shortName);
|
|
|
|
expect(store.state.fullName).toBe(user.fullName);
|
|
|
|
expect(store.state.email).toBe(user.email);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
it('get throws an error when api call fails', async () => {
|
|
|
|
const user = store.getters.user;
|
|
|
|
jest.spyOn(usersApi, 'get').mockImplementation(() => { throw new Error(); });
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(GET);
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
|
|
|
expect(store.state.fullName).toBe(user.fullName);
|
|
|
|
expect(store.state.shortName).toBe(user.shortName);
|
|
|
|
}
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('getters', () => {
|
2019-02-20 13:33:56 +00:00
|
|
|
it('user model', function () {
|
2019-08-14 19:11:18 +01:00
|
|
|
const retrievedUser = store.getters.user;
|
|
|
|
|
|
|
|
expect(retrievedUser.id).toBe(store.state.id);
|
|
|
|
expect(retrievedUser.fullName).toBe(store.state.fullName);
|
|
|
|
expect(retrievedUser.shortName).toBe(store.state.shortName);
|
|
|
|
expect(retrievedUser.email).toBe(store.state.email);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('user name', function () {
|
2019-08-14 19:11:18 +01:00
|
|
|
const retrievedUserName = store.getters.userName;
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
expect(retrievedUserName).toBe(store.state.getFullName());
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|