2023-04-06 16:38:48 +01:00
|
|
|
// Copyright (C) 2023 Storj Labs, Inc.
|
2019-01-08 15:14:00 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2023-04-06 16:38:48 +01:00
|
|
|
import { createPinia, setActivePinia } from 'pinia';
|
2019-09-09 11:33:39 +01:00
|
|
|
|
2019-11-22 17:03:15 +00:00
|
|
|
import { AuthHttpApi } from '@/api/auth';
|
2019-08-14 19:11:18 +01:00
|
|
|
import { UpdatedUser, User } from '@/types/users';
|
2023-04-06 16:38:48 +01:00
|
|
|
import { useUsersStore } from '@/store/modules/usersStore';
|
2019-08-14 19:11:18 +01:00
|
|
|
|
2023-04-06 16:38:48 +01:00
|
|
|
describe('actions', () => {
|
2019-02-20 13:33:56 +00:00
|
|
|
beforeEach(() => {
|
2023-04-06 16:38:48 +01:00
|
|
|
setActivePinia(createPinia());
|
|
|
|
jest.resetAllMocks();
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2023-04-06 16:38:48 +01:00
|
|
|
|
|
|
|
it('set user', () => {
|
|
|
|
const store = useUsersStore();
|
2022-01-05 11:07:03 +00:00
|
|
|
const user = new User('1', 'fullName', 'shortName', 'user@example.com');
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-06 16:38:48 +01:00
|
|
|
store.setUser(user);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2021-07-08 17:43:09 +01:00
|
|
|
expect(store.state.user.id).toBe(user.id);
|
|
|
|
expect(store.state.user.email).toBe(user.email);
|
|
|
|
expect(store.state.user.fullName).toBe(user.fullName);
|
|
|
|
expect(store.state.user.shortName).toBe(user.shortName);
|
2019-08-14 19:11:18 +01:00
|
|
|
});
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
it('clear user', () => {
|
2023-04-06 16:38:48 +01:00
|
|
|
const store = useUsersStore();
|
|
|
|
|
|
|
|
const user = new User('1', 'fullName', 'shortName', 'user@example.com');
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-06 16:38:48 +01:00
|
|
|
store.setUser(user);
|
|
|
|
expect(store.state.user.id).toBe(user.id);
|
|
|
|
|
|
|
|
store.clear();
|
2021-07-08 17:43:09 +01:00
|
|
|
expect(store.state.user.id).toBe('');
|
|
|
|
expect(store.state.user.email).toBe('');
|
|
|
|
expect(store.state.user.fullName).toBe('');
|
|
|
|
expect(store.state.user.shortName).toBe('');
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2023-04-06 16:38:48 +01:00
|
|
|
it('update user', async () => {
|
|
|
|
const store = useUsersStore();
|
2019-08-14 19:11:18 +01:00
|
|
|
const user = new UpdatedUser('fullName', 'shortName');
|
|
|
|
|
2023-04-06 16:38:48 +01:00
|
|
|
jest.spyOn(AuthHttpApi.prototype, 'update').mockImplementation(() => Promise.resolve());
|
|
|
|
|
|
|
|
await store.updateUser(user);
|
2019-08-14 19:11:18 +01:00
|
|
|
|
2021-07-08 17:43:09 +01:00
|
|
|
expect(store.state.user.fullName).toBe(user.fullName);
|
|
|
|
expect(store.state.user.shortName).toBe(user.shortName);
|
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 () => {
|
2023-04-06 16:38:48 +01:00
|
|
|
const store = useUsersStore();
|
2019-08-14 19:11:18 +01:00
|
|
|
const newUser = new UpdatedUser('', '');
|
2023-04-06 16:38:48 +01:00
|
|
|
const oldUser = store.state.user;
|
|
|
|
|
|
|
|
jest.spyOn(AuthHttpApi.prototype, 'update').mockImplementation(() => { throw new Error(); });
|
2019-08-14 19:11:18 +01:00
|
|
|
|
|
|
|
try {
|
2023-04-06 16:38:48 +01:00
|
|
|
await store.updateUser(newUser);
|
2019-08-14 19:11:18 +01:00
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
2021-07-08 17:43:09 +01:00
|
|
|
expect(store.state.user.fullName).toBe(oldUser.fullName);
|
|
|
|
expect(store.state.user.shortName).toBe(oldUser.shortName);
|
2019-08-14 19:11:18 +01:00
|
|
|
}
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('success get user', async () => {
|
2023-04-06 16:38:48 +01:00
|
|
|
const store = useUsersStore();
|
2022-01-05 11:07:03 +00:00
|
|
|
const user = new User('2', 'newFullName', 'newShortName', 'user2@example.com');
|
2019-08-14 19:11:18 +01:00
|
|
|
|
2023-04-06 16:38:48 +01:00
|
|
|
jest.spyOn(AuthHttpApi.prototype, 'get').mockReturnValue(
|
2019-09-13 15:58:18 +01:00
|
|
|
Promise.resolve(user),
|
2019-02-21 11:01:52 +00:00
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-06 16:38:48 +01:00
|
|
|
await store.getUser();
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2021-07-08 17:43:09 +01:00
|
|
|
expect(store.state.user.id).toBe(user.id);
|
|
|
|
expect(store.state.user.shortName).toBe(user.shortName);
|
|
|
|
expect(store.state.user.fullName).toBe(user.fullName);
|
|
|
|
expect(store.state.user.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 () => {
|
2023-04-06 16:38:48 +01:00
|
|
|
const store = useUsersStore();
|
|
|
|
const user = store.state.user;
|
|
|
|
|
|
|
|
jest.spyOn(AuthHttpApi.prototype, 'get').mockImplementation(() => { throw new Error(); });
|
2019-08-14 19:11:18 +01:00
|
|
|
|
|
|
|
try {
|
2023-04-06 16:38:48 +01:00
|
|
|
await store.getUser();
|
2019-08-14 19:11:18 +01:00
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
2021-07-08 17:43:09 +01:00
|
|
|
expect(store.state.user.fullName).toBe(user.fullName);
|
|
|
|
expect(store.state.user.shortName).toBe(user.shortName);
|
2019-08-14 19:11:18 +01:00
|
|
|
}
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('getters', () => {
|
2023-04-06 16:38:48 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
setActivePinia(createPinia());
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('user name', function () {
|
2023-04-06 16:38:48 +01:00
|
|
|
const store = useUsersStore();
|
|
|
|
const retrievedUserName = store.userName;
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2021-07-08 17:43:09 +01:00
|
|
|
expect(retrievedUserName).toBe(store.state.user.getFullName());
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|