Skip to content

Commit

Permalink
test(content-type): add unit test for list cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
makinwab committed Mar 14, 2019
1 parent 71f0976 commit 0d402bd
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/cmds/content-type_cmds/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ async function ctList (argv) {

const space = await client.getSpace(spaceId)
const environment = await space.getEnvironment(environmentId)

log(highlightStyle(`Environment: "${environmentId}"`))

const result = await paginate({
client: environment,
method: 'getContentTypes',
Expand Down
138 changes: 138 additions & 0 deletions test/unit/cmds/content-type_cmds/list.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { handler } from '../../../../lib/cmds/content-type_cmds/list'
import { getContext } from '../../../../lib/context'
import { log } from '../../../../lib/utils/log'
import { createManagementClient } from '../../../../lib/utils/contentful-clients'

jest.mock('../../../../lib/context')
jest.mock('../../../../lib/utils/log')
jest.mock('../../../../lib/utils/contentful-clients')

const mockContentType = [
{
name: 'content type name',
sys: {
id: 'mockedMasterCT',
environment: {
sys: {
id: 'master'
}
}
}
},
{
name: 'content type name',
sys: {
id: 'mockedDevelopCT',
environment: {
sys: {
id: 'develop'
}
}
}
},
{
name: 'content type name',
sys: {
id: 'mockedTestCT',
environment: {
sys: {
id: 'test'
}
}
}
}
]

const getContentTypesSub = jest.fn().mockResolvedValue({
items: mockContentType
})

const fakeClient = {
getSpace: async () => ({
getEnvironment: async () => ({
getContentTypes: getContentTypesSub
})
})
}
createManagementClient.mockResolvedValue(fakeClient)

getContext.mockResolvedValue({
cmaToken: 'mockedToken',
activeSpaceId: 'someSpaceId'
})

afterEach(() => {
createManagementClient.mockClear()
getContentTypesSub.mockClear()
log.mockClear()
})

test('List content types from default environment, "master"', async () => {
await handler({})

expect(createManagementClient).toHaveBeenCalledTimes(1)
expect(getContentTypesSub).toHaveBeenCalledTimes(1)

expect(log.mock.calls[0][0]).toContain(
mockContentType[0].sys.environment.sys.id
)
expect(log.mock.calls[0][0]).not.toContain(
mockContentType[1].sys.environment.sys.id
)
expect(log.mock.calls[0][0]).not.toContain(
mockContentType[2].sys.environment.sys.id
)

expect(log.mock.calls[1][0]).toContain(mockContentType[0].name)
expect(log.mock.calls[1][0]).toContain(mockContentType[0].sys.id)
})

test('List content types based on active environment if available', async () => {
getContext.mockResolvedValue({
cmaToken: 'mockedToken',
activeSpaceId: 'someSpaceId',
activeEnvironmentId: 'develop'
})

await handler({})

expect(createManagementClient).toHaveBeenCalledTimes(1)
expect(getContentTypesSub).toHaveBeenCalledTimes(1)

expect(log.mock.calls[0][0]).not.toContain(
mockContentType[0].sys.environment.sys.id
)
expect(log.mock.calls[0][0]).toContain(
mockContentType[1].sys.environment.sys.id
)
expect(log.mock.calls[0][0]).not.toContain(
mockContentType[2].sys.environment.sys.id
)

expect(log.mock.calls[1][0]).toContain(mockContentType[1].name)
expect(log.mock.calls[1][0]).toContain(mockContentType[1].sys.id)
})

test('List content types based on environment passed if --environment-id option is used', async () => {
const stubArgv = {
environmentId: 'test'
}

await handler(stubArgv)

expect(createManagementClient).toHaveBeenCalledTimes(1)
expect(getContentTypesSub).toHaveBeenCalledTimes(1)

expect(log.mock.calls[0][0]).not.toContain(
mockContentType[0].sys.environment.sys.id
)
expect(log.mock.calls[0][0]).not.toContain(
mockContentType[1].sys.environment.sys.id
)
expect(log.mock.calls[0][0]).toContain(
mockContentType[2].sys.environment.sys.id
)

expect(log.mock.calls[1][0]).toContain(mockContentType[2].name)
expect(log.mock.calls[1][0]).toContain(mockContentType[2].sys.id)
})

0 comments on commit 0d402bd

Please sign in to comment.