Skip to content

Commit

Permalink
test: WIP - first entity type test refactor - content-types
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed Jun 27, 2024
1 parent 5b5332d commit 6086602
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cloneMock } from '../mocks/entities'
import { cloneMock, contentTypeMock } from '../mocks/entities'
import setupMakeRequest from '../mocks/makeRequest'
import { wrapContentType, wrapContentTypeCollection } from '../../../lib/entities/content-type'
import {
Expand All @@ -17,15 +17,17 @@ import {
isUpdatedTest,
omitAndDeleteFieldTest,
} from '../test-creators/instance-entity-methods'
import { describe, test } from 'mocha'
import { describe, test } from 'vitest'

function setup(promise) {
return {
makeRequest: setupMakeRequest(promise),
entityMock: cloneMock('contentType'),
entityMock: cloneMock('contentType') as typeof contentTypeMock,
}
}

export type ContentTypeSetupType = typeof setup

describe('Entity ContentType', () => {
test('ContentType is wrapped', async () => {
return entityWrappedTest(setup, {
Expand Down Expand Up @@ -58,7 +60,7 @@ describe('Entity ContentType', () => {
})
})

test('ContentType delete fails', async () => {
test.only('ContentType delete fails', async () => {
return failingActionTest(setup, {
wrapperMethod: wrapContentType,
actionMethod: 'delete',
Expand Down
182 changes: 182 additions & 0 deletions test/unit/test-creators/instance-entity-methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import { ContentTypeSetupType } from '../entities/content-type.test'
import { cloneMock, errorMock, mockCollection } from '../mocks/entities'
import { expect, vi } from 'vitest'

export function entityWrappedTest(setup, { wrapperMethod }) {
const { makeRequest, entityMock } = setup()
const entity = wrapperMethod(makeRequest, entityMock)
expect(entity.toPlainObject()).eql(entityMock)
}

export function entityCollectionWrappedTest(setup, { wrapperMethod }) {
const { makeRequest, entityMock } = setup()
const collection = mockCollection(entityMock)
const entity = wrapperMethod(makeRequest, collection)
expect(entity.toPlainObject()).eql(collection)
}

export async function entityPatchTest(setup, { wrapperMethod }) {
const { makeRequest, entityMock } = setup()
const entity = wrapperMethod(makeRequest, entityMock)
const response = await entity.patch([])
expect(response.toPlainObject, 'response is wrapped').to.be.ok
}

export async function entityUpdateTest(setup, { wrapperMethod }) {
await entityActionTest(setup, { wrapperMethod, actionMethod: 'update' })
}

export async function entityCollectionActionTest(setup, { wrapperMethod, actionMethod }) {
const { makeRequest, entityMock } = setup(Promise.resolve({ items: [] }))
const entity = wrapperMethod(makeRequest, entityMock)
return entity[actionMethod]().then((response) => {
expect(response.toPlainObject, 'response is wrapped').to.be.ok
})
}

export async function entityActionTest(
setup,
{ wrapperMethod, actionMethod },
checkResponse = true
) {
const { makeRequest, entityMock } = setup()
const entity = wrapperMethod(makeRequest, entityMock)
if (checkResponse) {
const response = await entity[actionMethod]()
expect(response.toPlainObject, 'response is wrapped').to.be.ok
} else {
expect(await entity[actionMethod]()).to.not.throw
}
}

export async function entityDeleteTest(setup, { wrapperMethod }) {
const { makeRequest, entityMock } = setup()
const entity = wrapperMethod(makeRequest, entityMock)
expect(await entity.delete()).to.not.throw
}

export async function entityPublishTest(setup, { wrapperMethod }) {
await entityActionTest(setup, { wrapperMethod, actionMethod: 'publish' })
}

export async function failingActionTest(setup: ContentTypeSetupType, { wrapperMethod, actionMethod }) {
const error = cloneMock('error') as typeof errorMock
console.dir({ actionMethod, error, type: typeof error }, { depth: null })

const errorToBeThrown = error // new Error("mocked-error")
// console.log({ errorToBeThrown })

const { makeRequest, entityMock } = setup(
Promise.reject({})
)
const entity = wrapperMethod(makeRequest, entityMock)
// const res = await entity[actionMethod]()
// console.dir(
// {
// res,
// entity,
// keys: Object.keys(entity),
// method: entity[actionMethod],
// typeMethod: typeof entity[actionMethod],
// },
// { depth: null }
// )
// expect(res).toStrictEqual(error)
await expect(entity[actionMethod]).rejects.toBe(errorToBeThrown)
// try {
// const res = await entity[actionMethod]()
// console.log({res})
// } catch (cathErr) {
// console.log({cathErr})
// }
}

export async function failingVersionActionTest(setup, { wrapperMethod, actionMethod }) {
const error = cloneMock('error')
const { makeRequest, entityMock } = setup(Promise.resolve(error))
entityMock.sys.version = 2
const entity = wrapperMethod(makeRequest, entityMock)
expect(await entity[actionMethod]()).toStrictEqual(error)
}

export async function isPublishedTest(setup, { wrapperMethod }) {
const { makeRequest, entityMock } = setup()
const unpublishedEntity = wrapperMethod(makeRequest, entityMock)
expect(unpublishedEntity.isPublished(), 'entity initially unpublished').to.be.false
entityMock.sys.publishedVersion = 2
const publishedEntity = wrapperMethod(makeRequest, entityMock)
expect(publishedEntity.isPublished(), 'entity is now published').to.be.true
}

export async function isUpdatedTest(setup, { wrapperMethod }) {
const { makeRequest, entityMock } = setup()
const unpublishedEntity = wrapperMethod(makeRequest, entityMock)
expect(unpublishedEntity.isUpdated(), 'entity not published').to.be.false
entityMock.sys.publishedVersion = 2
entityMock.sys.version = 3
const unchangedEntity = wrapperMethod(makeRequest, entityMock)
expect(unchangedEntity.isUpdated(), 'entity initially unchanged').to.be.false
entityMock.sys.version = 5
const changedEntity = wrapperMethod(makeRequest, entityMock)
expect(changedEntity.isUpdated(), 'entity is now updated').to.be.true
}

export async function isDraftTest(setup, { wrapperMethod }) {
const { makeRequest, entityMock } = setup()
const unpublishedEntity = wrapperMethod(makeRequest, entityMock)
expect(unpublishedEntity.isDraft(), 'entity is in draft mode').to.be.true
entityMock.sys.publishedVersion = 2
entityMock.sys.version = 3
const unchangedEntity = wrapperMethod(makeRequest, entityMock)
expect(unchangedEntity.isDraft(), 'entity is now published').to.be.false
}

export async function isArchivedTest(setup, { wrapperMethod }) {
const { makeRequest, entityMock } = setup()
const unarchivedEntity = wrapperMethod(makeRequest, entityMock)
expect(unarchivedEntity.isArchived(), 'entity initially unarchived').to.be.false
entityMock.sys.archivedVersion = 2
const archivedEntity = wrapperMethod(makeRequest, entityMock)
expect(archivedEntity.isArchived(), 'entity is now archived').to.be.true
}

export async function omitAndDeleteFieldTest(setup: ContentTypeSetupType, { wrapperMethod }) {
const setupData = setup(Promise.reject(errorMock))
setupData.entityMock.fields = [
{
id: 'title',
name: 'Title',
value: 'myTitle',
omitted: false,
deleted: false,
},
]
/* Since this method calls update() 2x, first call needs to return a properly wrapped entity. */
const makeRequestSpy = vi.spyOn(setupData, 'entityMock')
// const plainEntity = wrapperMethod(setupData.makeRequest, setupData.entityMock)

const entity = wrapperMethod(setupData.makeRequest, setupData.entityMock)
return entity.omitAndDeleteField('title').then((response) => {
expect(response.toPlainObject, 'response is wrapped').to.be.ok
expect(
makeRequestSpy.mock.calls[0][0].payload.fields.find((field) => field.id === 'title').omitted
).equals(true, 'omitted was set to true in the first update')
expect(
makeRequestSpy.mock.calls[1][0].payload.fields.find((field) => field.id === 'title').deleted
).equals(true, 'deleted was set to true in the first update')
})
}

export async function failingOmitAndDeleteFieldTest(setup, { wrapperMethod }) {
const { makeRequest, entityMock } = setup()
const entity = wrapperMethod(makeRequest, entityMock)
return entity.omitAndDeleteField('doesntExist').catch((r) => {
expect(r, "throws an error when field doesn't exist").to.be.ok
})
}

export async function entityReferenceCollectionTest(setup, { wrapperMethod }) {
const { makeRequest, entityMock } = setup()
const entity = wrapperMethod(makeRequest, entityMock)
expect(entity.includes).not.to.be.empty
}

0 comments on commit 6086602

Please sign in to comment.