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.
|
|
|
|
|
|
|
|
import { usersModule } from '@/store/modules/users';
|
2019-01-28 14:39:51 +00:00
|
|
|
import * as api from '@/api/users';
|
2019-01-08 15:14:00 +00:00
|
|
|
import { changePasswordRequest, deleteAccountRequest, getUserRequest, updateAccountRequest } from '@/api/users';
|
|
|
|
import { USER_MUTATIONS } from '@/store/mutationConstants';
|
2019-01-28 14:39:51 +00:00
|
|
|
import Vuex from 'vuex';
|
|
|
|
import { createLocalVue } from '@vue/test-utils';
|
2019-07-18 14:39:39 +01:00
|
|
|
import { RequestResponse } from '@/types/response';
|
|
|
|
import { User } from '@/types/users';
|
2019-01-28 14:39:51 +00:00
|
|
|
|
|
|
|
const mutations = usersModule.mutations;
|
2019-01-08 15:14:00 +00:00
|
|
|
|
|
|
|
describe('mutations', () => {
|
2019-02-20 13:33:56 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
createLocalVue().use(Vuex);
|
|
|
|
});
|
|
|
|
it('Set user info', () => {
|
|
|
|
const state = {
|
|
|
|
user: {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: '',
|
|
|
|
shortName: '',
|
2019-03-20 16:16:30 +00:00
|
|
|
email: '',
|
2019-02-20 13:33:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const store = new Vuex.Store({state, mutations});
|
|
|
|
|
|
|
|
const user = {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: 'fullName',
|
|
|
|
shortName: 'shortName',
|
2019-03-20 16:16:30 +00:00
|
|
|
email: 'email',
|
2019-02-20 13:33:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
store.commit(USER_MUTATIONS.SET_USER_INFO, user);
|
|
|
|
|
|
|
|
expect(state.user.email).toBe('email');
|
2019-03-27 12:33:32 +00:00
|
|
|
expect(state.user.fullName).toBe('fullName');
|
|
|
|
expect(state.user.shortName).toBe('shortName');
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('clear user info', () => {
|
|
|
|
const state = {
|
|
|
|
user: {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: 'fullName',
|
|
|
|
shortName: 'shortName',
|
2019-02-20 13:33:56 +00:00
|
|
|
email: 'email',
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const store = new Vuex.Store({state, mutations});
|
|
|
|
|
|
|
|
store.commit(USER_MUTATIONS.REVERT_TO_DEFAULT_USER_INFO);
|
|
|
|
|
|
|
|
expect(state.user.email).toBe('');
|
2019-03-27 12:33:32 +00:00
|
|
|
expect(state.user.fullName).toBe('');
|
|
|
|
expect(state.user.shortName).toBe('');
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Update user info', () => {
|
|
|
|
const state = {
|
|
|
|
user: {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: '',
|
|
|
|
shortName: '',
|
2019-03-20 16:16:30 +00:00
|
|
|
email: '',
|
2019-02-20 13:33:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
const user = {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: 'fullName',
|
|
|
|
shortName: 'shortName',
|
2019-03-20 16:16:30 +00:00
|
|
|
email: 'email',
|
2019-02-20 13:33:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const store = new Vuex.Store({state, mutations});
|
|
|
|
|
|
|
|
store.commit(USER_MUTATIONS.UPDATE_USER_INFO, user);
|
|
|
|
|
|
|
|
expect(state.user.email).toBe('email');
|
2019-03-27 12:33:32 +00:00
|
|
|
expect(state.user.fullName).toBe('fullName');
|
|
|
|
expect(state.user.shortName).toBe('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-02-21 11:01:52 +00:00
|
|
|
jest.spyOn(api, 'updateAccountRequest').mockReturnValue(
|
|
|
|
Promise.resolve(<RequestResponse<User>>{
|
|
|
|
isSuccess: true, data: {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: 'fullName',
|
|
|
|
shortName: 'shortName',
|
2019-02-21 11:01:52 +00:00
|
|
|
email: 'email',
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
const commit = jest.fn();
|
|
|
|
const user = {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: '',
|
|
|
|
shortName: '',
|
2019-03-20 16:16:30 +00:00
|
|
|
email: '',
|
2019-02-20 13:33:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const dispatchResponse = await usersModule.actions.updateAccount({commit}, user);
|
|
|
|
|
|
|
|
expect(dispatchResponse.isSuccess).toBeTruthy();
|
|
|
|
expect(commit).toHaveBeenCalledWith(USER_MUTATIONS.UPDATE_USER_INFO, {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: 'fullName',
|
|
|
|
shortName: 'shortName',
|
2019-03-20 16:16:30 +00:00
|
|
|
email: 'email',
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('error update account', async () => {
|
|
|
|
jest.spyOn(api, 'updateAccountRequest').mockReturnValue(
|
2019-02-21 11:01:52 +00:00
|
|
|
Promise.resolve(<RequestResponse<User>>{
|
|
|
|
isSuccess: false
|
|
|
|
})
|
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
const commit = jest.fn();
|
|
|
|
const user = {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: '',
|
|
|
|
shortName: '',
|
2019-03-20 16:16:30 +00:00
|
|
|
email: '',
|
2019-02-20 13:33:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const dispatchResponse = await usersModule.actions.updateAccount({commit}, user);
|
|
|
|
|
|
|
|
expect(dispatchResponse.isSuccess).toBeFalsy();
|
|
|
|
expect(commit).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('password change', async () => {
|
2019-02-20 15:56:05 +00:00
|
|
|
jest.spyOn(api, 'changePasswordRequest').mockReturnValue(
|
2019-02-21 11:01:52 +00:00
|
|
|
Promise.resolve(<RequestResponse<null>>{
|
|
|
|
isSuccess: true
|
|
|
|
})
|
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
const commit = jest.fn();
|
|
|
|
const updatePasswordModel = {oldPassword: 'o', newPassword: 'n'};
|
|
|
|
|
|
|
|
const requestResponse = await usersModule.actions.changePassword({commit}, updatePasswordModel);
|
|
|
|
|
|
|
|
expect(requestResponse.isSuccess).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('delete account', async () => {
|
2019-02-21 11:01:52 +00:00
|
|
|
jest.spyOn(api, 'deleteAccountRequest').mockReturnValue(
|
|
|
|
Promise.resolve(<RequestResponse<null>>{
|
|
|
|
isSuccess: true
|
|
|
|
})
|
|
|
|
);
|
2019-02-20 15:56:05 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const commit = jest.fn();
|
|
|
|
const password = '';
|
|
|
|
|
|
|
|
const dispatchResponse = await usersModule.actions.deleteAccount(commit, password);
|
|
|
|
|
|
|
|
expect(dispatchResponse.isSuccess).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('success get user', async () => {
|
2019-02-21 11:01:52 +00:00
|
|
|
jest.spyOn(api, 'getUserRequest').mockReturnValue(
|
|
|
|
Promise.resolve(<RequestResponse<User>>{
|
|
|
|
isSuccess: true,
|
|
|
|
data: {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: '',
|
|
|
|
shortName: '',
|
2019-03-20 16:16:30 +00:00
|
|
|
email: '',
|
2019-02-21 11:01:52 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
const commit = jest.fn();
|
|
|
|
|
|
|
|
const requestResponse = await usersModule.actions.getUser({commit});
|
|
|
|
|
|
|
|
expect(requestResponse.isSuccess).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('error get user', async () => {
|
2019-02-21 11:01:52 +00:00
|
|
|
jest.spyOn(api, 'getUserRequest').mockReturnValue(
|
|
|
|
Promise.resolve(<RequestResponse<User>>{
|
|
|
|
isSuccess: false
|
|
|
|
})
|
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
const commit = jest.fn();
|
|
|
|
|
|
|
|
const requestResponse = await usersModule.actions.getUser({commit});
|
|
|
|
|
|
|
|
expect(requestResponse.isSuccess).toBeFalsy();
|
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('getters', () => {
|
2019-02-20 13:33:56 +00:00
|
|
|
it('user model', function () {
|
|
|
|
const state = {
|
|
|
|
user: {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: 'fullName',
|
|
|
|
shortName: 'shortName',
|
2019-02-20 13:33:56 +00:00
|
|
|
email: 'email',
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const retrievedUser = usersModule.getters.user(state);
|
|
|
|
|
2019-03-27 12:33:32 +00:00
|
|
|
expect(retrievedUser.fullName).toBe('fullName');
|
|
|
|
expect(retrievedUser.shortName).toBe('shortName');
|
2019-02-20 13:33:56 +00:00
|
|
|
expect(retrievedUser.email).toBe('email');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('user name', function () {
|
|
|
|
const state = {
|
|
|
|
user: {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: 'John',
|
|
|
|
shortName: 'Doe'
|
2019-02-20 13:33:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const retrievedUserName = usersModule.getters.userName(state);
|
|
|
|
|
|
|
|
expect(retrievedUserName).toBe('John Doe');
|
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|