Skip to content

Commit

Permalink
test auth page, add pinia testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogge committed Mar 14, 2024
1 parent 85b352f commit 8c4fbe9
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 16 deletions.
42 changes: 42 additions & 0 deletions presenter/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions presenter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"devDependencies": {
"@eslint-community/eslint-plugin-eslint-comments": "^4.1.0",
"@intlify/eslint-plugin-vue-i18n": "^2.0.0",
"@pinia/testing": "^0.1.3",
"@storybook/addon-essentials": "^7.6.17",
"@storybook/addon-interactions": "^7.6.17",
"@storybook/addon-links": "^7.6.17",
Expand Down
5 changes: 2 additions & 3 deletions presenter/scripts/tests/plugin.pinia.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createTestingPinia } from '@pinia/testing'
import { config } from '@vue/test-utils'

import pinia from '#plugins/pinia'

config.global.plugins.push(pinia)
config.global.plugins.push(createTestingPinia())
109 changes: 100 additions & 9 deletions presenter/src/pages/auth/Page.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,114 @@
import { mount } from '@vue/test-utils'
import { describe, it, expect } from 'vitest'
import { navigate } from 'vike/client/router'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { Component, h } from 'vue'
import { VApp } from 'vuetify/components'

import { useAuthStore } from '#stores/authStore'
import { authService } from '#tests/mock.authService'

import AuthPage from './+Page.vue'
import { title } from './+title'

vi.mock('vike/client/router')
vi.mocked(navigate).mockResolvedValue()

describe('AuthPage', () => {
const wrapper = mount(VApp, {
slots: {
default: h(AuthPage as Component),
},
const Wrapper = () => {
return mount(VApp, {
slots: {
default: h(AuthPage as Component),
},
})
}

const authServiceSpy = vi.spyOn(authService, 'signInCallback')
const authStore = useAuthStore()

let wrapper: ReturnType<typeof Wrapper>

describe('signin callback without error', () => {
beforeEach(() => {
vi.clearAllMocks()
authServiceSpy.mockResolvedValue({
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',
})
wrapper = Wrapper()
})

it('title returns correct title', () => {
expect(title).toBe('DreamMall | Authentifizierung')
})

it('renders', () => {
expect(wrapper.element).toMatchSnapshot()
})

it('calls authservie signin callback', () => {
expect(authServiceSpy).toBeCalled()
})

it('updates the store', () => {
expect(authStore.save).toBeCalledWith(
expect.objectContaining({
access_token: 'access_token',
profile: {
aud: 'aud',
sub: 'sub',
exp: 1,
iat: 1,
iss: 'iss',
},
token_type: 'token_type',
}),
)
})

it('navigates to /', () => {
expect(navigate).toBeCalledWith('/')
})
})

it('title returns correct title', () => {
expect(title).toBe('DreamMall | Authentifizierung')
describe('no user returned', () => {
beforeEach(() => {
vi.clearAllMocks()
authServiceSpy.mockResolvedValue()
})

it('throws an error', () => {
try {
wrapper = Wrapper()
} catch (err) {
expect(err).toBe('Could not Sign In')
}
})
})

it('renders', () => {
expect(wrapper.element).toMatchSnapshot()
describe('signin callback with error', () => {
const consoleSpy = vi.spyOn(global.console, 'log')

beforeEach(() => {
vi.clearAllMocks()
authServiceSpy.mockRejectedValue('Ouch!')
wrapper = Wrapper()
})

it('logs the error on console', () => {
expect(consoleSpy).toBeCalledWith('auth error', 'Ouch!')
})
})
})
8 changes: 4 additions & 4 deletions presenter/src/pages/auth/__snapshots__/Page.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`AuthPage > renders 1`] = `
exports[`AuthPage > signin callback without error > renders 1`] = `
<div
class="v-application v-theme--light v-layout v-layout--full-height v-locale--is-ltr"
>
Expand All @@ -12,7 +12,7 @@ exports[`AuthPage > renders 1`] = `
class="v-main main-layout"
data-v-22c0849a=""
data-v-75d0be1e=""
style="--v-layout-left: 0px; --v-layout-right: 0px; --v-layout-top: 64px; --v-layout-bottom: 0px;"
style="--v-layout-left: 0px; --v-layout-right: 0px; --v-layout-top: 64px; --v-layout-bottom: 0px; transition: none !important;"
>
<div
Expand All @@ -23,7 +23,7 @@ exports[`AuthPage > renders 1`] = `
<header
class="v-toolbar v-toolbar--flat v-toolbar--density-default v-theme--light v-locale--is-ltr v-app-bar"
data-v-5e2a2776=""
style="top: 0px; z-index: 1008; transform: translateY(0%); position: fixed; background-color: #f5f5f5; color: #000; caret-color: #000; left: 0px; width: calc(100% - 0px - 0px);"
style="top: 0px; z-index: 1008; transform: translateY(0%); position: fixed; transition: none !important; background-color: #f5f5f5; color: #000; caret-color: #000; left: 0px; width: calc(100% - 0px - 0px);"
>
<!---->
Expand Down Expand Up @@ -448,7 +448,7 @@ exports[`AuthPage > renders 1`] = `
<nav
class="v-navigation-drawer v-navigation-drawer--right v-navigation-drawer--temporary v-theme--light v-navigation-drawer--mobile nav-drawer d-flex d-md-none"
data-v-5e2a2776=""
style="background-color: #f5f5f5; color: #000; caret-color: #000; right: 0px; z-index: 1006; transform: translateX(110%); position: fixed; height: calc(100% - 64px - 0px); top: 64px; bottom: 0px; width: 256px;"
style="background-color: #f5f5f5; color: #000; caret-color: #000; right: 0px; z-index: 1006; transform: translateX(110%); position: fixed; transition: none !important; height: calc(100% - 64px - 0px); top: 64px; bottom: 0px; width: 256px;"
>
<!---->
<!---->
Expand Down

0 comments on commit 8c4fbe9

Please sign in to comment.