-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(content-type): enhance list command functionality (#129)
- Loading branch information
Showing
4 changed files
with
180 additions
and
11 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
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 |
---|---|---|
@@ -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 mockContentTypes = [ | ||
{ | ||
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: mockContentTypes | ||
}) | ||
|
||
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( | ||
mockContentTypes[0].sys.environment.sys.id | ||
) | ||
expect(log.mock.calls[0][0]).not.toContain( | ||
mockContentTypes[1].sys.environment.sys.id | ||
) | ||
expect(log.mock.calls[0][0]).not.toContain( | ||
mockContentTypes[2].sys.environment.sys.id | ||
) | ||
|
||
expect(log.mock.calls[1][0]).toContain(mockContentTypes[0].name) | ||
expect(log.mock.calls[1][0]).toContain(mockContentTypes[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( | ||
mockContentTypes[0].sys.environment.sys.id | ||
) | ||
expect(log.mock.calls[0][0]).toContain( | ||
mockContentTypes[1].sys.environment.sys.id | ||
) | ||
expect(log.mock.calls[0][0]).not.toContain( | ||
mockContentTypes[2].sys.environment.sys.id | ||
) | ||
|
||
expect(log.mock.calls[1][0]).toContain(mockContentTypes[1].name) | ||
expect(log.mock.calls[1][0]).toContain(mockContentTypes[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( | ||
mockContentTypes[0].sys.environment.sys.id | ||
) | ||
expect(log.mock.calls[0][0]).not.toContain( | ||
mockContentTypes[1].sys.environment.sys.id | ||
) | ||
expect(log.mock.calls[0][0]).toContain( | ||
mockContentTypes[2].sys.environment.sys.id | ||
) | ||
|
||
expect(log.mock.calls[1][0]).toContain(mockContentTypes[2].name) | ||
expect(log.mock.calls[1][0]).toContain(mockContentTypes[2].sys.id) | ||
}) |