0
0
Fork 0

front unit tests amends for darkMode and setUser modifications

This commit is contained in:
Carlos Jimenez Ruiz 2022-04-20 13:58:53 +02:00
parent 345b943869
commit 913be03e94
7 changed files with 93 additions and 61 deletions

View File

@ -10,38 +10,47 @@ jest.mock('vue-router', () => ({
}),
}));
describe('UserPanel', () => {
describe('onMounted', () => {
it.only('should define the user into state', async () => {
const userMock = {
user: {
id: 1,
name: 'myName',
nickname: 'myNickName'
},
roles: []
}
jest.spyOn(axios, 'get').mockResolvedValue({ data: userMock });
const { vm } = createWrapper(UserPanel);
describe('UserPanel onMounted()', () => {
it('should define the user into state', async () => {
const userMock = {
user: {
id: 1,
name: 'myName',
nickname: 'myNickName',
lang: 'en',
userConfig: {
darkMode: 'false'
}
},
roles: []
}
const expectedUserData = {
id: 1,
name: 'myName',
nickname: 'myNickName',
lang: 'en',
darkMode: 'false'
}
await flushPromises()
jest.spyOn(axios, 'get').mockResolvedValue({ data: userMock });
const { vm } = createWrapper(UserPanel);
const expectedUser = expect.objectContaining(userMock.user)
expect(vm.state.getUser().value).toEqual(expectedUser);
});
await flushPromises()
it('should logout and notify the expected error', async () => {
jest.spyOn(axios, 'get').mockRejectedValue(new Error('error'));
expect(vm.state.getUser().value).toEqual(expectedUserData);
});
const { vm } = createWrapper(UserPanel);
it('should logout and notify the expected error', async () => {
jest.spyOn(axios, 'get').mockRejectedValue(new Error('error'));
jest.spyOn(vm.quasar, 'notify');
const { vm } = createWrapper(UserPanel);
await flushPromises()
jest.spyOn(vm.quasar, 'notify');
expect(vm.quasar.notify).toHaveBeenCalledWith(expect.objectContaining(
{ 'type': 'negative' }
));
});
await flushPromises()
expect(vm.quasar.notify).toHaveBeenCalledWith(expect.objectContaining(
{ 'type': 'negative' }
));
});
});

View File

@ -19,10 +19,25 @@ describe('useRole', () => {
}
}
];
const expectedUser = { id: 1 }
const fetchedUser = {
id: 999,
name: `T'Challa`,
nickname: 'Black Panther',
lang: 'en',
userConfig: {
darkMode: false,
}
}
const expectedUser = {
id: 999,
name: `T'Challa`,
nickname: 'Black Panther',
lang: 'en',
darkMode: false,
}
const expectedRoles = ['salesPerson', 'admin']
jest.spyOn(axios, 'get').mockResolvedValue({
data: { roles: rolesData, user: expectedUser }
data: { roles: rolesData, user: fetchedUser }
});
jest.spyOn(role.state, 'setUser');

View File

@ -36,11 +36,15 @@ describe('session', () => {
id: 999,
name: `T'Challa`,
nickname: 'Black Panther',
lang: 'en',
darkMode: false,
}
const expectedUser = {
id: 0,
name: '',
nickname: '',
lang: '',
darkMode: null,
}
let user = state.getUser();
@ -60,20 +64,29 @@ describe('session', () => {
});
describe('login', () => {
it('should fetch the user roles and then set token in the sessionStorage', async () => {
const rolesData = [
{
role: {
name: 'salesPerson'
}
},
{
role: {
name: 'admin'
}
const expectedUser = {
id: 999,
name: `T'Challa`,
nickname: 'Black Panther',
lang: 'en',
userConfig: {
darkMode: false,
}
}
const rolesData = [
{
role: {
name: 'salesPerson'
}
];
const expectedUser = { id: 1 }
},
{
role: {
name: 'admin'
}
}
];
it('should fetch the user roles and then set token in the sessionStorage', async () => {
const expectedRoles = ['salesPerson', 'admin']
jest.spyOn(axios, 'get').mockResolvedValue({
data: { roles: rolesData, user: expectedUser }
@ -96,20 +109,6 @@ describe('session', () => {
});
it('should fetch the user roles and then set token in the localStorage', async () => {
const rolesData = [
{
role: {
name: 'salesPerson'
}
},
{
role: {
name: 'admin'
}
}
];
const expectedUser = { id: 1 }
const expectedRoles = ['salesPerson', 'admin']
jest.spyOn(axios, 'get').mockResolvedValue({
data: { roles: rolesData, user: expectedUser }

View File

@ -12,7 +12,7 @@ export function useRole() {
id: data.user.id,
name: data.user.name,
nickname: data.user.nickname,
language: data.user.lang || 'es',
lang: data.user.lang || 'es',
darkMode: data.user.userConfig.darkMode,
}
state.setUser(userData);

View File

@ -30,7 +30,7 @@ export function useSession() {
id: 0,
name: '',
nickname: '',
language: '',
lang: '',
darkMode: null,
});
}

View File

@ -4,7 +4,7 @@ const user = ref({
id: 0,
name: '',
nickname: '',
language: '',
lang: '',
darkMode: null,
});
@ -19,7 +19,7 @@ export function useState() {
id: user.value.id,
name: user.value.name,
nickname: user.value.nickname,
language: user.value.language,
lang: user.value.lang,
darkMode: user.value.darkMode,
};
});
@ -30,7 +30,7 @@ export function useState() {
id: data.id,
name: data.name,
nickname: data.nickname,
language: data.lang,
lang: data.lang,
darkMode: data.darkMode,
};
}

View File

@ -18,8 +18,17 @@ describe('Login', () => {
});
it('should successfully set the token into session', async () => {
const expectedUser = {
id: 999,
name: `T'Challa`,
nickname: 'Black Panther',
lang: 'en',
userConfig: {
darkMode: false,
}
}
jest.spyOn(axios, 'post').mockResolvedValue({ data: { token: 'token' } });
jest.spyOn(axios, 'get').mockResolvedValue({ data: { roles: [], user: { id: 1 } } });
jest.spyOn(axios, 'get').mockResolvedValue({ data: { roles: [], user: expectedUser } });
jest.spyOn(vm.quasar, 'notify')
expect(vm.session.getToken()).toEqual('');