Skip to content

Commit

Permalink
Merge pull request #5 from fumeapp/test-helpers
Browse files Browse the repository at this point in the history
💚 cleaning up and passing types with helpers
  • Loading branch information
acidjazz authored Aug 21, 2024
2 parents f377518 + b700ea6 commit 34f3d88
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
9 changes: 5 additions & 4 deletions test/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { $fetch } from '@nuxt/test-utils/e2e'
import type { MetapiResponse } from '~/types/metapi'
import type { User, UserSession } from '~/types/models'
import { createUser } from '~~/server/utils/user'

Expand Down Expand Up @@ -42,10 +43,10 @@ async function actingAs(email: string) {
const user = await userFromEmail(email)
const { data } = await $fetch('/api/test/session', { method: 'POST', body: { id: user?.session?.id.toString(), hash: user?.session?.hash } })
user.cookie = data.cookie[1].split(';')[0] as string
const get = (url: string) => $fetch(url, { headers: { cookie: user.cookie as string } })
const post = (url: string, params: object) => $fetch(url, { method: 'POST', body: params, headers: { cookie: user.cookie as string } })
const put = (url: string, params: object) => $fetch(url, { method: 'PUT', body: params, headers: { cookie: user.cookie as string } })
return { get, post, put }
const get = <T>(url: string) => $fetch<MetapiResponse<T>>(url, { headers: { cookie: user.cookie as string } })
const post = <T>(url: string, params: object) => $fetch<MetapiResponse<T>>(url, { method: 'POST', body: params, headers: { cookie: user.cookie as string } })
const put = <T>(url: string, params: object) => $fetch<MetapiResponse<T>>(url, { method: 'PUT', body: params, headers: { cookie: user.cookie as string } })
return { get, post, put, user }
}

export {
Expand Down
17 changes: 8 additions & 9 deletions test/pen.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { describe, expect, it } from 'vitest'
import { setup } from '@nuxt/test-utils'
import { actingAs, userFromEmail } from './auth'
import { actingAs } from './auth'
import { setupConfig } from './config'
import { penColors } from '~/utils/shared'
import type { MetapiResponse } from '~/types/metapi'
import type { Pen } from '~/types/models'

describe('/api/pen', async () => {
Expand All @@ -12,28 +11,28 @@ describe('/api/pen', async () => {
const pens: Pen[] = []

it('post /api/pen - create a pen', async () => {
const { post } = await actingAs('[email protected]')
const { data: pen } = await post('/api/pen', { color: penColors[0] }) as MetapiResponse<Pen>
const { post, user } = await actingAs('[email protected]')
const { data: pen } = await post<Pen>('/api/pen', { color: penColors[0] })
expect(pen.color).toBe(penColors[0])
expect(pen.userId).toBe(user.session.id.toString())
pens.push(pen)
expect(pen.userId).toBe((await userFromEmail('[email protected]')).session.id.toString())
})

it(' get /api/pen - list all pens', async () => {
it('get /api/pen - list all pens', async () => {
const { get } = await actingAs('[email protected]')
const response = await get('/api/pen') as MetapiResponse<Pen[]>
const response = await get<Pen[]>('/api/pen')
expect(response.data[0]).toStrictEqual(pens[0])
})

it ('get /api/pen/:id - get a pen', async () => {
const { get } = await actingAs('[email protected]')
const response = await get(`/api/pen/${pens[0]?.id}`) as MetapiResponse<Pen>
const response = await get<Pen>(`/api/pen/${pens[0]?.id}`)
expect(response.data).toStrictEqual(pens[0])
})

it ('put /api/pen/:id - update a pen', async () => {
const { put } = await actingAs('[email protected]')
const { data: pen } = await put(`/api/pen/${pens[0]?.id}`, { color: penColors[1] }) as MetapiResponse<Pen>
const { data: pen } = await put<Pen>(`/api/pen/${pens[0]?.id}`, { color: penColors[1] })
expect(pen.color).toBe(penColors[1])
})
})
27 changes: 11 additions & 16 deletions test/user.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { $fetch, setup } from '@nuxt/test-utils/e2e'
import { actingAs, users } from './auth'
import { actingAs } from './auth'
import { setupConfig } from './config'
import type { MetapiResponse } from '~/types/metapi'
import type { User } from '~/types/models'
Expand All @@ -9,33 +9,28 @@ describe('/api/me and /api/user', async () => {
await setup(setupConfig())
it('/api/me should 401', async () => {
try { await $fetch('/api/me') }
catch (error: any) {
expect(error.response.status).toBe(401)
}
catch (error: any) { expect(error.response.status).toBe(401) }
})

it('get /api/me - current user session', async () => {
const { get } = await actingAs('[email protected]')
const response = await get('/api/me') as MetapiResponse<User>
expect(response.data.email).toEqual(users[0]?.session.email)
const { get, user } = await actingAs('[email protected]')
const response = await get<User>('/api/me')
expect(response.data.email).toEqual(user.session.email)
})

it ('get /api/user isAdmin: false - 404', async () => {
try {
await (await actingAs('[email protected]')).get('/api/user')
}
catch (error: any) {
expect(error.response.status).toBe(404)
}
try { await (await actingAs('[email protected]')).get('/api/user') }
catch (error: any) { expect(error.response.status).toBe(404) }
})

it ('get /api/user GET', async () => {
const response = await (await actingAs('[email protected]')).get('/api/user') as MetapiResponse<User[]>
const response = await (await actingAs('[email protected]')).get<User[]>('/api/user')
expect(response.data.length).toBe(2)
})

it ('get /api/user/:id', async () => {
const response = await (await actingAs('[email protected]')).get(`/api/user/${users[0]?.session.id}`) as MetapiResponse<User>
expect(response.data.id).toBe(users[0]?.session.id.toString())
const { user, get } = await actingAs('[email protected]')
const response = await get<User>(`/api/user/${user.session.id}`)
expect(response.data.id).toBe(user.session.id.toString())
})
})
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default defineVitestConfig({
},
},
},
reporters: ['verbose'],
coverage: {
reporter: ['text', 'json-summary', 'json'],
reportOnFailure: true,
Expand Down

0 comments on commit 34f3d88

Please sign in to comment.