Skip to content

Commit

Permalink
Mock local storage when testing useLogoutUser.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTedder committed Nov 13, 2024
1 parent f3e2640 commit cf1a6fe
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions composables/api/useLogoutUser/useLogoutUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@ import { useCurrentUser, useSessionToken } from '#imports'
import type { UserViewModel } from 'lib/api/data-contracts'
import useLogoutUser from '.'

const token = 'auth-token'
let token = ''
const user: UserViewModel = {
id: '575888bd-9702-41a9-9b75-fc87d785c22a',
role: 'Confirmed',
username: 'test',
createdAt: '1984-01-01T00:00:00Z',
}

vi.mock('@vueuse/core', () => ({
useLocalStorage: () => ({
get value() {
return token
},
set value(val) {
token = val
},
}),
}))

afterEach(() => {
fetchMock.resetMocks()
vi.restoreAllMocks()
Expand All @@ -19,14 +30,14 @@ afterEach(() => {
describe('useLogoutUser', () => {
beforeEach(() => {
fetchMock.mockIf(/.*\/[Uu]ser\/me/, () => JSON.stringify(user))
useSessionToken().value = token
token = 'auth-token'
})

it('sets the `authToken` and `currentUser` back to the default values', async () => {
const authToken = useSessionToken()
const { data, refresh } = await useCurrentUser()

expect(authToken.value).toEqual(token)
expect(authToken.value).toEqual('auth-token')
expect(data.value).toEqual(user)

useLogoutUser()
Expand Down

0 comments on commit cf1a6fe

Please sign in to comment.