front unit tests amends for darkMode and setUser modifications
gitea/salix-front/pipeline/head This commit looks good Details

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('UserPanel onMounted()', () => {
describe('onMounted', () => { it('should define the user into state', async () => {
it.only('should define the user into state', async () => { const userMock = {
const userMock = { user: {
user: { id: 1,
id: 1, name: 'myName',
name: 'myName', nickname: 'myNickName',
nickname: 'myNickName' lang: 'en',
}, userConfig: {
roles: [] darkMode: 'false'
} }
jest.spyOn(axios, 'get').mockResolvedValue({ data: userMock }); },
const { vm } = createWrapper(UserPanel); 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) await flushPromises()
expect(vm.state.getUser().value).toEqual(expectedUser);
});
it('should logout and notify the expected error', async () => { expect(vm.state.getUser().value).toEqual(expectedUserData);
jest.spyOn(axios, 'get').mockRejectedValue(new Error('error')); });
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( await flushPromises()
{ 'type': 'negative' }
)); 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'] const expectedRoles = ['salesPerson', 'admin']
jest.spyOn(axios, 'get').mockResolvedValue({ jest.spyOn(axios, 'get').mockResolvedValue({
data: { roles: rolesData, user: expectedUser } data: { roles: rolesData, user: fetchedUser }
}); });
jest.spyOn(role.state, 'setUser'); jest.spyOn(role.state, 'setUser');

View File

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

View File

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

View File

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

View File

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

View File

@ -18,8 +18,17 @@ describe('Login', () => {
}); });
it('should successfully set the token into session', async () => { 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, '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') jest.spyOn(vm.quasar, 'notify')
expect(vm.session.getToken()).toEqual(''); expect(vm.session.getToken()).toEqual('');