-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from fumeapp/test-helpers
💚 cleaning up and passing types with helpers
- Loading branch information
Showing
4 changed files
with
25 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () => { | ||
|
@@ -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]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
@@ -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()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters