From 8343271b1afba2a86e4001466f73aa61996d5986 Mon Sep 17 00:00:00 2001 From: carlosjr Date: Fri, 25 Mar 2022 14:37:14 +0100 Subject: [PATCH] minor test refactor for component instances --- .vscode/settings.json | 5 +- src/boot/i18n.js | 12 ++- src/components/UserPanel.vue | 103 ++++++++++----------- src/components/__tests__/UserPanel.spec.js | 61 +++++++++--- src/composables/useState.js | 6 +- 5 files changed, 113 insertions(+), 74 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 53e94ed89..3dcb9b1d6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,5 +11,8 @@ "fileMatch": ["cypress.json"], "url": "https://on.cypress.io/cypress.schema.json" } - ] + ], + "[javascript]": { + "editor.defaultFormatter": "vscode.typescript-language-features" + } } diff --git a/src/boot/i18n.js b/src/boot/i18n.js index aa450c279..e8f4203b9 100644 --- a/src/boot/i18n.js +++ b/src/boot/i18n.js @@ -2,12 +2,14 @@ import { boot } from 'quasar/wrappers'; import { createI18n } from 'vue-i18n'; import messages from 'src/i18n'; -export default boot(({ app }) => { - const i18n = createI18n({ - locale: 'en', - messages, - }); +const i18n = createI18n({ + locale: 'en', + messages, +}); +export default boot(({ app }) => { // Set i18n instance on app app.use(i18n); }); + +export { i18n }; \ No newline at end of file diff --git a/src/components/UserPanel.vue b/src/components/UserPanel.vue index 2e357698c..6accb30ec 100644 --- a/src/components/UserPanel.vue +++ b/src/components/UserPanel.vue @@ -1,3 +1,51 @@ + + - - diff --git a/src/components/__tests__/UserPanel.spec.js b/src/components/__tests__/UserPanel.spec.js index 1640a3a0e..542921ec6 100644 --- a/src/components/__tests__/UserPanel.spec.js +++ b/src/components/__tests__/UserPanel.spec.js @@ -1,11 +1,17 @@ import { describe, expect, it, jest } from '@jest/globals'; import { installQuasarPlugin } from '@quasar/quasar-app-extension-testing-unit-jest'; -import { mount } from '@vue/test-utils'; +import { mount, flushPromises } from '@vue/test-utils'; +import axios from 'axios'; import { i18n } from 'src/boot/i18n'; import UserPanel from '../UserPanel.vue'; +import { Notify } from 'quasar'; // Specify here Quasar config you'll need to test your component -installQuasarPlugin(); +installQuasarPlugin({ + plugins: { + Notify + } +}); const mockPush = jest.fn(); jest.mock('vue-router', () => ({ @@ -14,15 +20,48 @@ jest.mock('vue-router', () => ({ }), })); -describe('UserPanel', () => { - it('should have the function logout defined', () => { - const wrapper = mount(UserPanel, { - global: { - plugins: [i18n] - } - }); - const { vm } = wrapper; +function createWrapper(component) { + const wrapper = mount(component, { + global: { + plugins: [i18n] + } + }); + const vm = wrapper.vm; - expect(vm.logout).toBeDefined(); + return { vm, wrapper }; +} + +describe('UserPanel', () => { + describe('onMounted', () => { + it('should define the user into state', async () => { + const userMock = { + user: { + id: 1, + name: 'myName', + nickname: 'myNickName' + } + } + jest.spyOn(axios, 'get').mockResolvedValue(userMock); + const { vm } = createWrapper(UserPanel); + + await flushPromises() + + const expectedUser = expect.objectContaining(userMock.user) + expect(vm.state.getUser().value).toEqual(expectedUser); + }); + + it('should logout and notify the expected error', async () => { + jest.spyOn(axios, 'get').mockRejectedValue(new Error('error')); + + const { vm } = createWrapper(UserPanel); + + jest.spyOn(vm.quasar, 'notify'); + + await flushPromises() + + expect(vm.quasar.notify).toHaveBeenCalledWith(expect.objectContaining( + { 'type': 'negative' } + )); + }); }); }); diff --git a/src/composables/useState.js b/src/composables/useState.js index df121398e..2f182d651 100644 --- a/src/composables/useState.js +++ b/src/composables/useState.js @@ -2,7 +2,7 @@ import { ref, computed } from 'vue'; const user = ref({ id: 0, - username: '', + name: '', nickname: '', }); @@ -13,7 +13,7 @@ export function useState() { return computed(() => { return { id: user.value.id, - username: user.value.username, + name: user.value.name, nickname: user.value.nickname, }; }); @@ -22,7 +22,7 @@ export function useState() { function setUser(data) { user.value = { id: data.id, - username: data.username, + name: data.name, nickname: data.nickname, }; }