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';
|
|
|
|
|
|
|
|
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: {
|
|
|
|
firstName: '',
|
|
|
|
lastName: '',
|
|
|
|
email: ''
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const store = new Vuex.Store({state, mutations});
|
|
|
|
|
|
|
|
const user = {
|
|
|
|
firstName: 'firstName',
|
|
|
|
lastName: 'lastName',
|
|
|
|
email: 'email'
|
|
|
|
};
|
|
|
|
|
|
|
|
store.commit(USER_MUTATIONS.SET_USER_INFO, user);
|
|
|
|
|
|
|
|
expect(state.user.email).toBe('email');
|
|
|
|
expect(state.user.firstName).toBe('firstName');
|
|
|
|
expect(state.user.lastName).toBe('lastName');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('clear user info', () => {
|
|
|
|
const state = {
|
|
|
|
user: {
|
|
|
|
firstName: 'firstName',
|
|
|
|
lastName: 'lastName',
|
|
|
|
email: 'email',
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const store = new Vuex.Store({state, mutations});
|
|
|
|
|
|
|
|
store.commit(USER_MUTATIONS.REVERT_TO_DEFAULT_USER_INFO);
|
|
|
|
|
|
|
|
expect(state.user.email).toBe('');
|
|
|
|
expect(state.user.firstName).toBe('');
|
|
|
|
expect(state.user.lastName).toBe('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Update user info', () => {
|
|
|
|
const state = {
|
|
|
|
user: {
|
|
|
|
firstName: '',
|
|
|
|
lastName: '',
|
|
|
|
email: ''
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const user = {
|
|
|
|
firstName: 'firstName',
|
|
|
|
lastName: 'lastName',
|
|
|
|
email: 'email'
|
|
|
|
};
|
|
|
|
|
|
|
|
const store = new Vuex.Store({state, mutations});
|
|
|
|
|
|
|
|
store.commit(USER_MUTATIONS.UPDATE_USER_INFO, user);
|
|
|
|
|
|
|
|
expect(state.user.email).toBe('email');
|
|
|
|
expect(state.user.firstName).toBe('firstName');
|
|
|
|
expect(state.user.lastName).toBe('lastName');
|
|
|
|
});
|
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: {
|
|
|
|
firstName: 'firstName',
|
|
|
|
lastName: 'lastName',
|
|
|
|
email: 'email',
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
const commit = jest.fn();
|
|
|
|
const user = {
|
|
|
|
firstName: '',
|
|
|
|
lastName: '',
|
|
|
|
email: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
const dispatchResponse = await usersModule.actions.updateAccount({commit}, user);
|
|
|
|
|
|
|
|
expect(dispatchResponse.isSuccess).toBeTruthy();
|
|
|
|
expect(commit).toHaveBeenCalledWith(USER_MUTATIONS.UPDATE_USER_INFO, {
|
|
|
|
firstName: 'firstName',
|
|
|
|
lastName: 'lastName',
|
|
|
|
email: 'email'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
firstName: '',
|
|
|
|
lastName: '',
|
|
|
|
email: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
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: {
|
|
|
|
firstName: '',
|
|
|
|
lastName: '',
|
|
|
|
email: ''
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
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: {
|
|
|
|
firstName: 'firstName',
|
|
|
|
lastName: 'lastName',
|
|
|
|
email: 'email',
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const retrievedUser = usersModule.getters.user(state);
|
|
|
|
|
|
|
|
expect(retrievedUser.firstName).toBe('firstName');
|
|
|
|
expect(retrievedUser.lastName).toBe('lastName');
|
|
|
|
expect(retrievedUser.email).toBe('email');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('user name', function () {
|
|
|
|
const state = {
|
|
|
|
user: {
|
|
|
|
firstName: 'John',
|
|
|
|
lastName: 'Doe'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const retrievedUserName = usersModule.getters.userName(state);
|
|
|
|
|
|
|
|
expect(retrievedUserName).toBe('John Doe');
|
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|