Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(presenter): test header menu #399

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion presenter/scripts/tests/plugin.pinia.ts
Original file line number Diff line number Diff line change
@@ -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 }))
99 changes: 92 additions & 7 deletions presenter/src/components/menu/HeaderMenu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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!')
})
})
})
})
})
})