From f5fdced5e4bd2d9ba3843e70aae5a28596d1c938 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Tue, 26 Mar 2024 11:54:03 +0100 Subject: [PATCH 1/2] feat(presenter): test header menu --- presenter/scripts/tests/plugin.pinia.ts | 2 +- .../src/components/menu/HeaderMenu.test.ts | 99 +++++++++++++++++-- presenter/vitest.config.ts | 4 +- 3 files changed, 95 insertions(+), 10 deletions(-) diff --git a/presenter/scripts/tests/plugin.pinia.ts b/presenter/scripts/tests/plugin.pinia.ts index bc3bfc4db8..6eb1afd90f 100644 --- a/presenter/scripts/tests/plugin.pinia.ts +++ b/presenter/scripts/tests/plugin.pinia.ts @@ -1,4 +1,4 @@ import { createTestingPinia } from '@pinia/testing' import { config } from '@vue/test-utils' -config.global.plugins.push(createTestingPinia()) +config.global.plugins.push(createTestingPinia({ stubActions: false })) diff --git a/presenter/src/components/menu/HeaderMenu.test.ts b/presenter/src/components/menu/HeaderMenu.test.ts index e8c8695bf7..53c0caef5c 100644 --- a/presenter/src/components/menu/HeaderMenu.test.ts +++ b/presenter/src/components/menu/HeaderMenu.test.ts @@ -5,6 +5,7 @@ import { Component, h } from 'vue' import { VApp } from 'vuetify/components' import { AUTH } from '#src/env' +import { useAuthStore } from '#stores/authStore' import { authService } from '#tests/mock.authService' import HeaderMenu from './HeaderMenu.vue' @@ -80,20 +81,104 @@ describe('HeaderMenu', () => { }) }) - /* describe('sign up button', () => { + const spy = vi.spyOn(authService, 'signUp') + beforeEach(() => { vi.clearAllMocks() - wrapper.find('button.sign-up').trigger('click') }) - // how to redirect correctly (navigate vs recirect)? - it('redirects to sign up url', () => { - expect(true).toBe(true) + describe('without error', () => { + beforeEach(async () => { + await wrapper.find('button.sign-up').trigger('click') + }) + + it('call auth service signUp', () => { + expect(spy).toBeCalled() + }) + }) + + describe('with error', () => { + const consoleSpy = vi.spyOn(console, 'log') + + beforeEach(async () => { + spy.mockRejectedValue('Oh no!') + await wrapper.find('button.sign-up').trigger('click') + }) + + it('logs the error', () => { + expect(consoleSpy).toBeCalledWith('auth error', 'Oh no!') + }) }) }) - describe('logged in', () => {}) - */ + describe('logged in', () => { + const authStore = useAuthStore() + + beforeEach(() => { + authStore.save({ + access_token: 'access_token', + profile: { + aud: 'aud', + sub: 'sub', + exp: 1, + iat: 1, + iss: 'iss', + }, + token_type: 'token_type', + session_state: null, + state: null, + expires_in: 0, + expired: false, + scopes: ['email'], + toStorageString: () => 'toStorageString', + }) + }) + + it('has a sign out button', () => { + expect(wrapper.find('button.sign-out').exists()).toBe(true) + }) + + it('has no sign in/up button', () => { + expect(wrapper.find('button.sign-up').exists()).toBe(false) + expect(wrapper.find('button.sign-in').exists()).toBe(false) + }) + + describe('click sign out', () => { + const authSpy = vi.spyOn(authService, 'signOut') + const storeSpy = vi.spyOn(authStore, 'clear') + + beforeEach(() => { + vi.clearAllMocks() + }) + + describe('without error', () => { + beforeEach(async () => { + await wrapper.find('button.sign-out').trigger('click') + }) + + it('calls auth service sign out', () => { + expect(authSpy).toBeCalled() + }) + + it('clears the store', () => { + expect(storeSpy).toBeCalled() + }) + }) + + describe('with error', () => { + const consoleSpy = vi.spyOn(console, 'log') + + beforeEach(async () => { + authSpy.mockRejectedValue('Error!') + await wrapper.find('button.sign-out').trigger('click') + }) + + it('logs the error', () => { + expect(consoleSpy).toBeCalledWith('auth error', 'Error!') + }) + }) + }) + }) }) }) diff --git a/presenter/vitest.config.ts b/presenter/vitest.config.ts index 3f932b437b..1653a292d8 100644 --- a/presenter/vitest.config.ts +++ b/presenter/vitest.config.ts @@ -26,10 +26,10 @@ export default mergeConfig( '**/*{.,-}stories.?(c|m)[jt]s?(x)', ], thresholds: { - lines: 98, + lines: 99, // functions: 73, // has problems see https://github.com/vitest-dev/vitest/issues/3607 branches: 98, - statements: 98, + statements: 99, }, }, }, From 0b5b5cb55b6e6679d011a8e37aff4f00d1ead041 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Wed, 27 Mar 2024 08:32:49 +0100 Subject: [PATCH 2/2] fix coverage --- presenter/vitest.config.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/presenter/vitest.config.ts b/presenter/vitest.config.ts index 1653a292d8..3f932b437b 100644 --- a/presenter/vitest.config.ts +++ b/presenter/vitest.config.ts @@ -26,10 +26,10 @@ export default mergeConfig( '**/*{.,-}stories.?(c|m)[jt]s?(x)', ], thresholds: { - lines: 99, + lines: 98, // functions: 73, // has problems see https://github.com/vitest-dev/vitest/issues/3607 branches: 98, - statements: 99, + statements: 98, }, }, },