diff --git a/apps/jsonthing-api/src/common/helpers/fixtures/doc.fixtures.ts b/apps/jsonthing-api/src/common/helpers/fixtures/doc.fixtures.ts new file mode 100644 index 0000000..f57f12a --- /dev/null +++ b/apps/jsonthing-api/src/common/helpers/fixtures/doc.fixtures.ts @@ -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, + ), +} diff --git a/apps/jsonthing-api/src/common/helpers/fixtures/index.ts b/apps/jsonthing-api/src/common/helpers/fixtures/index.ts new file mode 100644 index 0000000..3a51024 --- /dev/null +++ b/apps/jsonthing-api/src/common/helpers/fixtures/index.ts @@ -0,0 +1 @@ +export * from './doc.fixtures' diff --git a/apps/jsonthing-api/src/routes/docs/controllers/docs-aliases.controller.spec.ts b/apps/jsonthing-api/src/routes/docs/controllers/docs-aliases.controller.spec.ts index 019f80a..c5ec510 100644 --- a/apps/jsonthing-api/src/routes/docs/controllers/docs-aliases.controller.spec.ts +++ b/apps/jsonthing-api/src/routes/docs/controllers/docs-aliases.controller.spec.ts @@ -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' @@ -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) diff --git a/apps/jsonthing-api/src/routes/docs/controllers/docs.controller.spec.ts b/apps/jsonthing-api/src/routes/docs/controllers/docs.controller.spec.ts index d88a8d4..109332e 100644 --- a/apps/jsonthing-api/src/routes/docs/controllers/docs.controller.spec.ts +++ b/apps/jsonthing-api/src/routes/docs/controllers/docs.controller.spec.ts @@ -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' @@ -40,42 +44,25 @@ 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) }) @@ -83,17 +70,21 @@ describe('docsController', () => { 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, @@ -103,9 +94,7 @@ describe('docsController', () => { docId.toString(), ) - expect(docsService.getDocById).toHaveBeenCalledWith(docId) - - expect(result).toStrictEqual(getDocByIdResult) + expect(result).toStrictEqual(docFixture) }) }) @@ -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, @@ -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) diff --git a/apps/jsonthing-api/src/routes/docs/service/docs.service.spec.ts b/apps/jsonthing-api/src/routes/docs/service/docs.service.spec.ts index dbb5762..c958428 100644 --- a/apps/jsonthing-api/src/routes/docs/service/docs.service.spec.ts +++ b/apps/jsonthing-api/src/routes/docs/service/docs.service.spec.ts @@ -1,3 +1,7 @@ +import { + createDocPayloadFixture, + docFixture, +} from '@/common/helpers/fixtures' import { DEFAULT_DOC_CONTENT, DEFAULT_DOC_NAME, @@ -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, }) }) @@ -84,6 +80,12 @@ describe('docsService', () => { expect(result).toStrictEqual({ id: expect.stringMatching(OBJECT_ID_PATTERN), + contentText: JSON.stringify( + defaultDocValues.content, + null, + 2, + ), + ...defaultDocValues, }) }) @@ -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, @@ -132,9 +127,8 @@ describe('docsService', () => { const result = await docsService.getDocById(docId) expect(result).toStrictEqual({ + ...docFixture, id: docId.toString(), - - ...doc, }) }) @@ -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(), }), @@ -201,10 +187,9 @@ describe('docsService', () => { ) expect(result).toStrictEqual({ + ...docFixture, id: docId.toString(), - ...doc, - title: 'New Title', }) })