|
| 1 | +import { jest } from '@jest/globals' |
| 2 | + |
| 3 | +import type { ArgumentsCamelCase, Argv } from 'yargs' |
| 4 | + |
| 5 | +import type { CommandArgs } from '../../../../commands/edge/channels/invites.js' |
| 6 | +import type { APICommand, APICommandFlags } from '../../../../lib/command/api-command.js' |
| 7 | +import { outputItemOrList, outputItemOrListBuilder } from '../../../../lib/command/listing-io.js' |
| 8 | +import { listTableFieldDefinitions, tableFieldDefinitions } from '../../../../lib/command/util/edge-invites-table.js' |
| 9 | +import { apiCommandMocks } from '../../../test-lib/api-command-mock.js' |
| 10 | +import { buildArgvMock, buildArgvMockStub } from '../../../test-lib/builder-mock.js' |
| 11 | +import { Invitation, InvitesEndpoint } from '../../../../lib/edge/endpoints/invites.js' |
| 12 | +import type { edgeCommand, EdgeCommand } from '../../../../lib/command/edge-command.js' |
| 13 | +import { buildListFunction } from '../../../../lib/command/util/edge-invites-util.js' |
| 14 | + |
| 15 | + |
| 16 | +const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../../../..') |
| 17 | + |
| 18 | +const edgeCommandMock = jest.fn<typeof edgeCommand>() |
| 19 | +jest.unstable_mockModule('../../../../lib/command/edge-command.js', () => ({ |
| 20 | + edgeCommand: edgeCommandMock, |
| 21 | +})) |
| 22 | + |
| 23 | +const outputItemOrListMock = jest.fn<typeof outputItemOrList<Invitation>>() |
| 24 | +const outputItemOrListBuilderMock = jest.fn<typeof outputItemOrListBuilder>() |
| 25 | +jest.unstable_mockModule('../../../../lib/command/listing-io.js', () => ({ |
| 26 | + outputItemOrList: outputItemOrListMock, |
| 27 | + outputItemOrListBuilder: outputItemOrListBuilderMock, |
| 28 | +})) |
| 29 | + |
| 30 | +const buildListFunctionMock = jest.fn<typeof buildListFunction>() |
| 31 | +jest.unstable_mockModule('../../../../lib/command/util/edge-invites-util.js', () => ({ |
| 32 | + buildListFunction: buildListFunctionMock, |
| 33 | +})) |
| 34 | + |
| 35 | + |
| 36 | +const { default: cmd } = await import('../../../../commands/edge/channels/invites.js') |
| 37 | + |
| 38 | + |
| 39 | +test('builder', async () => { |
| 40 | + const yargsMock = buildArgvMockStub<object>() |
| 41 | + const { |
| 42 | + yargsMock: apiCommandBuilderArgvMock, |
| 43 | + positionalMock, |
| 44 | + optionMock, |
| 45 | + exampleMock, |
| 46 | + argvMock, |
| 47 | + } = buildArgvMock<APICommandFlags, CommandArgs>() |
| 48 | + |
| 49 | + apiCommandBuilderMock.mockReturnValue(apiCommandBuilderArgvMock) |
| 50 | + outputItemOrListBuilderMock.mockReturnValue(argvMock) |
| 51 | + |
| 52 | + const builder = cmd.builder as (yargs: Argv<object>) => Argv<CommandArgs> |
| 53 | + |
| 54 | + expect(builder(yargsMock)).toBe(argvMock) |
| 55 | + |
| 56 | + expect(apiCommandBuilderMock).toHaveBeenCalledExactlyOnceWith(yargsMock) |
| 57 | + expect(outputItemOrListBuilderMock).toHaveBeenCalledExactlyOnceWith(apiCommandBuilderArgvMock) |
| 58 | + expect(positionalMock).toHaveBeenCalledTimes(1) |
| 59 | + expect(optionMock).toHaveBeenCalledTimes(1) |
| 60 | + expect(exampleMock).toHaveBeenCalledTimes(1) |
| 61 | +}) |
| 62 | + |
| 63 | +test('handler', async () => { |
| 64 | + const apiInvitesGetMock = jest.fn<InvitesEndpoint['get']>() |
| 65 | + const command = { profileName: 'default' } as APICommand |
| 66 | + const edgeCommand = { |
| 67 | + edgeClient: { |
| 68 | + invites: { |
| 69 | + get: apiInvitesGetMock, |
| 70 | + }, |
| 71 | + }, |
| 72 | + } as unknown as EdgeCommand |
| 73 | + |
| 74 | + apiCommandMock.mockResolvedValueOnce(command) |
| 75 | + edgeCommandMock.mockReturnValueOnce(edgeCommand) |
| 76 | + const listFunctionMock = jest.fn<Parameters<typeof outputItemOrList<Invitation>>[3]>() |
| 77 | + buildListFunctionMock.mockReturnValueOnce(listFunctionMock) |
| 78 | + |
| 79 | + const inputArgv = { |
| 80 | + profile: 'default', |
| 81 | + channel: 'cmd-line-channel-id', |
| 82 | + idOrIndex: 'cmd-line-id', |
| 83 | + } as ArgumentsCamelCase<CommandArgs> |
| 84 | + |
| 85 | + await expect(cmd.handler(inputArgv)).resolves.not.toThrow() |
| 86 | + |
| 87 | + expect(apiCommandMock).toHaveBeenCalledExactlyOnceWith(inputArgv) |
| 88 | + expect(edgeCommandMock).toHaveBeenCalledExactlyOnceWith(command) |
| 89 | + expect(buildListFunctionMock).toHaveBeenCalledExactlyOnceWith(edgeCommand, 'cmd-line-channel-id') |
| 90 | + expect(outputItemOrListMock).toHaveBeenCalledExactlyOnceWith( |
| 91 | + edgeCommand, |
| 92 | + expect.objectContaining({ |
| 93 | + primaryKeyName: 'id', |
| 94 | + listTableFieldDefinitions, |
| 95 | + tableFieldDefinitions, |
| 96 | + }), |
| 97 | + 'cmd-line-id', |
| 98 | + listFunctionMock, |
| 99 | + expect.any(Function), |
| 100 | + ) |
| 101 | + |
| 102 | + const getFunction = outputItemOrListMock.mock.calls[0][4] |
| 103 | + const invite = { id: 'invitation-id' } as Invitation |
| 104 | + apiInvitesGetMock.mockResolvedValueOnce(invite) |
| 105 | + |
| 106 | + expect(await getFunction('invitation-id')).toBe(invite) |
| 107 | +}) |
0 commit comments