Skip to content

Commit

Permalink
chore(api): add doc fixtures helper (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
lharti authored Nov 12, 2024
1 parent 4e00036 commit e0e2229
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 93 deletions.
22 changes: 22 additions & 0 deletions apps/jsonthing-api/src/common/helpers/fixtures/doc.fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Doc } from '@/routes/docs/model'
import mongoose from 'mongoose'

export const createDocPayloadFixture = {
title: 'Sample Doc Title',

content: {
key: 'value',
},
}

export const docFixture: Doc = {
id: new mongoose.Types.ObjectId().toString(),

...createDocPayloadFixture,

contentText: JSON.stringify(
createDocPayloadFixture.content,
null,
2,
),
}
1 change: 1 addition & 0 deletions apps/jsonthing-api/src/common/helpers/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './doc.fixtures'
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { docFixture } from '@/common/helpers/fixtures'
import { DocsAliasesController } from '@/routes/docs/controllers/docs-aliases.controller'
import { DocsService } from '@/routes/docs/service'
import { Test, TestingModule } from '@nestjs/testing'
Expand Down Expand Up @@ -62,13 +63,7 @@ describe('docsAliasesController', () => {

const docId = new ObjectId()

const getDocByIdResult = {
id: docId.toString(),
title: 'Title',
content: {
value: 'doc-content',
},
}
const getDocByIdResult = docFixture

docsService.getDocById.mockResolvedValueOnce(getDocByIdResult)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
createDocPayloadFixture,
docFixture,
} from '@/common/helpers/fixtures'
import { DocsService } from '@/routes/docs/service'
import { Test, TestingModule } from '@nestjs/testing'
import mongoose from 'mongoose'
Expand Down Expand Up @@ -40,60 +44,47 @@ describe('docsController', () => {
it('should use docsService.createDoc', async () => {
expect.assertions(1)

const createDocPayload = {
title: 'Title',
content: {
value: 'doc-content',
},
}

await docsController.createDoc(createDocPayload)
await docsController.createDoc(createDocPayloadFixture)

expect(docsService.createDoc).toHaveBeenCalledWith(
createDocPayload,
createDocPayloadFixture,
)
})

it('should return docsService.createDoc result', async () => {
expect.assertions(1)

const createDocPayload = {
title: 'Title',
content: {
value: 'doc-content',
},
}

const createDocResult = {
id: new ObjectId().toString(),

...createDocPayload,
}
const createDocResult = docFixture

docsService.createDoc.mockResolvedValueOnce(
createDocResult,
)

const result =
await docsController.createDoc(createDocPayload)
const result = await docsController.createDoc(
createDocPayloadFixture,
)

expect(result).toStrictEqual(createDocResult)
})
})

describe('getDoc', () => {
it('should use docsService.getDocById', async () => {
expect.assertions(2)
expect.assertions(1)

const docId = new ObjectId()

const getDocByIdResult = {
id: docId.toString(),
title: 'Title',
content: {
value: 'doc-content',
},
}
await docsController.getDoc(docId.toString())

expect(docsService.getDocById).toHaveBeenCalledWith(docId)
})

it('should return docsService.getDocById result', async () => {
expect.assertions(1)

const docId = new ObjectId()

const getDocByIdResult = docFixture

docsService.getDocById.mockResolvedValueOnce(
getDocByIdResult,
Expand All @@ -103,9 +94,7 @@ describe('docsController', () => {
docId.toString(),
)

expect(docsService.getDocById).toHaveBeenCalledWith(docId)

expect(result).toStrictEqual(getDocByIdResult)
expect(result).toStrictEqual(docFixture)
})
})

Expand Down Expand Up @@ -137,21 +126,14 @@ describe('docsController', () => {
title: 'New Title',
}

const docId = new ObjectId()

const updateDocResult = {
id: docId.toString(),
content: {
value: 'doc-content',
},

...updatePayload,
}
const updateDocResult = docFixture

docsService.updateDoc.mockResolvedValueOnce(
updateDocResult,
)

const docId = new ObjectId()

const result = await docsController.updateDoc(
docId.toString(),
updatePayload,
Expand Down Expand Up @@ -183,13 +165,7 @@ describe('docsController', () => {

const docId = new ObjectId()

const getDocByIdResult = {
id: docId.toString(),
title: 'Title',
content: {
value: 'doc-content',
},
}
const getDocByIdResult = docFixture

docsService.getDocById.mockResolvedValueOnce(getDocByIdResult)

Expand Down
55 changes: 20 additions & 35 deletions apps/jsonthing-api/src/routes/docs/service/docs.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
createDocPayloadFixture,
docFixture,
} from '@/common/helpers/fixtures'
import {
DEFAULT_DOC_CONTENT,
DEFAULT_DOC_NAME,
Expand Down Expand Up @@ -53,21 +57,13 @@ describe('docsService', () => {
it('should create a new doc with payload data', async () => {
expect.assertions(1)

const random = Math.random().toString()
const createDocPayload = {
title: `${random}}-name`,
content: {
random: `${random}-content`,
},
}

const result =
await docsService.createDoc(createDocPayload)
const result = await docsService.createDoc(
createDocPayloadFixture,
)

expect(result).toStrictEqual({
...docFixture,
id: expect.stringMatching(OBJECT_ID_PATTERN),

...createDocPayload,
})
})

Expand All @@ -84,6 +80,12 @@ describe('docsService', () => {
expect(result).toStrictEqual({
id: expect.stringMatching(OBJECT_ID_PATTERN),

contentText: JSON.stringify(
defaultDocValues.content,
null,
2,
),

...defaultDocValues,
})
})
Expand All @@ -109,16 +111,9 @@ describe('docsService', () => {
it('should return a doc by id', async () => {
expect.assertions(1)

const doc = {
title: 'test',
content: {
test: 'test',
},
}

mockingoose(docsModel).toReturn(
(Query: unknown) => ({
...doc,
...docFixture,

// @ts-expect-error: trust me
_id: Query.getQuery()._id,
Expand All @@ -132,9 +127,8 @@ describe('docsService', () => {
const result = await docsService.getDocById(docId)

expect(result).toStrictEqual({
...docFixture,
id: docId.toString(),

...doc,
})
})

Expand Down Expand Up @@ -171,19 +165,11 @@ describe('docsService', () => {
it('should update a doc by id', async () => {
expect.assertions(1)

const doc = {
title: 'Old Title',
content: {
test: 'test',
},
}

mockingoose(docsModel).toReturn(
(Query: unknown) => ({
...doc,
// @ts-expect-error: trust me
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(Query: any) => ({
...docFixture,
...Query.getUpdate(),
// @ts-expect-error: trust me
...Query.getQuery(),
}),

Expand All @@ -201,10 +187,9 @@ describe('docsService', () => {
)

expect(result).toStrictEqual({
...docFixture,
id: docId.toString(),

...doc,

title: 'New Title',
})
})
Expand Down

0 comments on commit e0e2229

Please sign in to comment.