Skip to content

Commit 48a592f

Browse files
committed
refactor: convert edge:channels:invites command to yargs
1 parent 9571a1d commit 48a592f

File tree

20 files changed

+600
-203
lines changed

20 files changed

+600
-203
lines changed

packages/edge/src/commands/edge/channels/invites.ts

Lines changed: 0 additions & 93 deletions
This file was deleted.

packages/edge/src/lib/edge-client.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/edge/src/lib/edge-command.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

packages/edge/src/lib/endpoints/invites.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { jest } from '@jest/globals'
2+
3+
import type { APICommand } from '../../../lib/command/api-command.js'
4+
import type { EdgeClient, newEdgeClient } from '../../../lib/edge/edge-client.js'
5+
import type { InvitesEndpoint } from '../../../lib/edge/endpoints/invites.js'
6+
7+
8+
const newEdgeClientMock = jest.fn<typeof newEdgeClient>()
9+
jest.unstable_mockModule('../../../lib/edge/edge-client.js', () => ({
10+
newEdgeClient: newEdgeClientMock,
11+
}))
12+
13+
14+
const { edgeCommand } = await import('../../../lib/command/edge-command.js')
15+
16+
test('edgeCommand', () => {
17+
const edgeClientMock = { invites: {} as InvitesEndpoint } as EdgeClient
18+
newEdgeClientMock.mockReturnValueOnce(edgeClientMock)
19+
const parent = { profileName: 'default' } as APICommand
20+
21+
expect(edgeCommand(parent)).toStrictEqual({ profileName: 'default', edgeClient: edgeClientMock })
22+
})
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { jest } from '@jest/globals'
2+
3+
import type { EdgeCommand } from '../../../../lib/command/edge-command.js'
4+
import type { buildListFunction } from '../../../../lib/command/util/edge-invites-util.js'
5+
import { ChooseFunction, createChooseFn } from '../../../../lib/command/util/util-util.js'
6+
import type { Invitation } from '../../../../lib/edge/endpoints/invites.js'
7+
8+
9+
const buildListFunctionMock = jest.fn<typeof buildListFunction>()
10+
jest.unstable_mockModule('../../../../lib/command/util/edge-invites-util.js', () => ({
11+
buildListFunction: buildListFunctionMock,
12+
}))
13+
14+
const createChooseFnMock = jest.fn<typeof createChooseFn<Invitation>>()
15+
jest.unstable_mockModule('../../../../lib/command/util/util-util.js', () => ({
16+
createChooseFn: createChooseFnMock,
17+
}))
18+
19+
20+
const { chooseInviteFn } = await import('../../../../lib/command/util/edge-invites-choose.js')
21+
22+
23+
describe('chooseInviteFn', () => {
24+
const command = {} as EdgeCommand
25+
26+
const chooseInvite = jest.fn<ChooseFunction<Invitation>>()
27+
createChooseFnMock.mockReturnValue(chooseInvite)
28+
29+
const listFunctionMock = jest.fn<ReturnType<typeof buildListFunction>>()
30+
buildListFunctionMock.mockReturnValue(listFunctionMock)
31+
32+
it('allows all without channel id', async () => {
33+
expect(chooseInviteFn(command)).toBe(chooseInvite)
34+
35+
expect(buildListFunctionMock).toHaveBeenCalledExactlyOnceWith(command, undefined)
36+
expect(createChooseFnMock).toHaveBeenCalledExactlyOnceWith(
37+
expect.objectContaining({ itemName: 'invitation' }),
38+
listFunctionMock,
39+
)
40+
})
41+
42+
it('limits by channel when specified', async () => {
43+
expect(chooseInviteFn(command, { channelId: 'specified-channel-id' })).toBe(chooseInvite)
44+
45+
expect(buildListFunctionMock).toHaveBeenCalledExactlyOnceWith(command, 'specified-channel-id')
46+
expect(createChooseFnMock).toHaveBeenCalledExactlyOnceWith(
47+
expect.objectContaining({ itemName: 'invitation' }),
48+
listFunctionMock,
49+
)
50+
})
51+
})

0 commit comments

Comments
 (0)