Skip to content

Commit c7fb361

Browse files
salmanmkcCopilot
andauthored
test: add multi-token usage tests for getOctokit
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 74a7682 commit c7fb361

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

__test__/async-function.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,82 @@ describe('callAsyncFunction', () => {
2020
expect(result).toEqual('secondary-client')
2121
})
2222

23+
test('getOctokit creates client independent from github', async () => {
24+
const github = {rest: {issues: 'primary'}}
25+
const getOctokit = jest.fn().mockReturnValue({rest: {issues: 'secondary'}})
26+
27+
const result = await callAsyncFunction(
28+
{github, getOctokit} as any,
29+
`
30+
const secondary = getOctokit('other-token')
31+
return {
32+
primary: github.rest.issues,
33+
secondary: secondary.rest.issues,
34+
different: github !== secondary
35+
}
36+
`
37+
)
38+
39+
expect(result).toEqual({
40+
primary: 'primary',
41+
secondary: 'secondary',
42+
different: true
43+
})
44+
expect(getOctokit).toHaveBeenCalledWith('other-token')
45+
})
46+
47+
test('getOctokit passes options through', async () => {
48+
const getOctokit = jest.fn().mockReturnValue('client-with-opts')
49+
50+
const result = await callAsyncFunction(
51+
{getOctokit} as any,
52+
`return getOctokit('my-token', { baseUrl: 'https://ghes.example.com/api/v3' })`
53+
)
54+
55+
expect(getOctokit).toHaveBeenCalledWith('my-token', {
56+
baseUrl: 'https://ghes.example.com/api/v3'
57+
})
58+
expect(result).toEqual('client-with-opts')
59+
})
60+
61+
test('getOctokit supports plugins', async () => {
62+
const getOctokit = jest.fn().mockReturnValue('client-with-plugins')
63+
64+
const result = await callAsyncFunction(
65+
{getOctokit} as any,
66+
`return getOctokit('my-token', { previews: ['v3'] }, 'pluginA', 'pluginB')`
67+
)
68+
69+
expect(getOctokit).toHaveBeenCalledWith(
70+
'my-token',
71+
{previews: ['v3']},
72+
'pluginA',
73+
'pluginB'
74+
)
75+
expect(result).toEqual('client-with-plugins')
76+
})
77+
78+
test('multiple getOctokit calls produce independent clients', async () => {
79+
const getOctokit = jest
80+
.fn()
81+
.mockReturnValueOnce({id: 'client-a'})
82+
.mockReturnValueOnce({id: 'client-b'})
83+
84+
const result = await callAsyncFunction(
85+
{getOctokit} as any,
86+
`
87+
const a = getOctokit('token-a')
88+
const b = getOctokit('token-b')
89+
return { a: a.id, b: b.id, different: a !== b }
90+
`
91+
)
92+
93+
expect(getOctokit).toHaveBeenCalledTimes(2)
94+
expect(getOctokit).toHaveBeenNthCalledWith(1, 'token-a')
95+
expect(getOctokit).toHaveBeenNthCalledWith(2, 'token-b')
96+
expect(result).toEqual({a: 'client-a', b: 'client-b', different: true})
97+
})
98+
2399
test('throws on ReferenceError', async () => {
24100
expect.assertions(1)
25101

0 commit comments

Comments
 (0)