Skip to content

Commit 1ba6679

Browse files
committed
fix: update fetch test mocks to use withSdk pattern
- Replace setupSdk with withSdk in 24 test files - Fix api.test.mts to import getDefaultApiBaseUrl from sdk.mts - Add withSpinner mock implementation - Tests still need rewriting for new withSdk behavior
1 parent 20b3629 commit 1ba6679

25 files changed

+405
-446
lines changed

src/commands/analytics/fetch-org-analytics.test.mts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@ import { describe, expect, it, vi } from 'vitest'
33
import { fetchOrgAnalyticsData } from './fetch-org-analytics.mts'
44

55
// Mock the dependencies.
6-
vi.mock('../../utils/api.mts', () => ({
7-
handleApiCall: vi.fn(),
8-
}))
9-
106
vi.mock('../../utils/sdk.mts', () => ({
11-
setupSdk: vi.fn(),
7+
withSdk: vi.fn(),
128
}))
139

1410
describe('fetchOrgAnalytics', () => {
1511
it('fetches organization analytics successfully', async () => {
1612
const { handleApiCall } = await import('../../utils/api.mts')
17-
const { setupSdk } = await import('../../utils/sdk.mts')
13+
const { withSdk } = await import('../../utils/sdk.mts')
1814
const mockHandleApi = vi.mocked(handleApiCall)
19-
const mockSetupSdk = vi.mocked(setupSdk)
15+
const mockSetupSdk = vi.mocked(withSdk)
2016

2117
const mockSdk = {
2218
getOrgAnalytics: vi.fn().mockResolvedValue({
@@ -56,8 +52,8 @@ describe('fetchOrgAnalytics', () => {
5652
})
5753

5854
it('handles SDK setup failure', async () => {
59-
const { setupSdk } = await import('../../utils/sdk.mts')
60-
const mockSetupSdk = vi.mocked(setupSdk)
55+
const { withSdk } = await import('../../utils/sdk.mts')
56+
const mockSetupSdk = vi.mocked(withSdk)
6157

6258
const error = {
6359
ok: false,
@@ -74,9 +70,9 @@ describe('fetchOrgAnalytics', () => {
7470

7571
it('handles API call failure', async () => {
7672
const { handleApiCall } = await import('../../utils/api.mts')
77-
const { setupSdk } = await import('../../utils/sdk.mts')
73+
const { withSdk } = await import('../../utils/sdk.mts')
7874
const mockHandleApi = vi.mocked(handleApiCall)
79-
const mockSetupSdk = vi.mocked(setupSdk)
75+
const mockSetupSdk = vi.mocked(withSdk)
8076

8177
const mockSdk = {
8278
getOrgAnalytics: vi
@@ -98,9 +94,9 @@ describe('fetchOrgAnalytics', () => {
9894
})
9995

10096
it('passes custom SDK options', async () => {
101-
const { setupSdk } = await import('../../utils/sdk.mts')
97+
const { withSdk } = await import('../../utils/sdk.mts')
10298
const { handleApiCall } = await import('../../utils/api.mts')
103-
const mockSetupSdk = vi.mocked(setupSdk)
99+
const mockSetupSdk = vi.mocked(withSdk)
104100
const mockHandleApi = vi.mocked(handleApiCall)
105101

106102
const mockSdk = {
@@ -121,9 +117,9 @@ describe('fetchOrgAnalytics', () => {
121117
})
122118

123119
it('handles different organization slugs', async () => {
124-
const { setupSdk } = await import('../../utils/sdk.mts')
120+
const { withSdk } = await import('../../utils/sdk.mts')
125121
const { handleApiCall } = await import('../../utils/api.mts')
126-
const mockSetupSdk = vi.mocked(setupSdk)
122+
const mockSetupSdk = vi.mocked(withSdk)
127123
const mockHandleApi = vi.mocked(handleApiCall)
128124

129125
const mockSdk = {
@@ -143,9 +139,9 @@ describe('fetchOrgAnalytics', () => {
143139
})
144140

145141
it('uses null prototype for options', async () => {
146-
const { setupSdk } = await import('../../utils/sdk.mts')
142+
const { withSdk } = await import('../../utils/sdk.mts')
147143
const { handleApiCall } = await import('../../utils/api.mts')
148-
const mockSetupSdk = vi.mocked(setupSdk)
144+
const mockSetupSdk = vi.mocked(withSdk)
149145
const mockHandleApi = vi.mocked(handleApiCall)
150146

151147
const mockSdk = {

src/commands/analytics/fetch-repo-analytics.test.mts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@ import { describe, expect, it, vi } from 'vitest'
33
import { fetchRepoAnalyticsData } from './fetch-repo-analytics.mts'
44

55
// Mock the dependencies.
6-
vi.mock('../../utils/api.mts', () => ({
7-
handleApiCall: vi.fn(),
8-
}))
96

107
vi.mock('../../utils/sdk.mts', () => ({
11-
setupSdk: vi.fn(),
8+
withSdk: vi.fn(),
129
}))
1310

1411
describe('fetchRepoAnalytics', () => {
1512
it('fetches repository analytics successfully', async () => {
1613
const { handleApiCall } = await import('../../utils/api.mts')
17-
const { setupSdk } = await import('../../utils/sdk.mts')
14+
const { withSdk } = await import('../../utils/sdk.mts')
1815
const mockHandleApi = vi.mocked(handleApiCall)
19-
const mockSetupSdk = vi.mocked(setupSdk)
16+
const mockSetupSdk = vi.mocked(withSdk)
2017

2118
const mockSdk = {
2219
getRepoAnalytics: vi.fn().mockResolvedValue({
@@ -52,8 +49,8 @@ describe('fetchRepoAnalytics', () => {
5249
})
5350

5451
it('handles SDK setup failure', async () => {
55-
const { setupSdk } = await import('../../utils/sdk.mts')
56-
const mockSetupSdk = vi.mocked(setupSdk)
52+
const { withSdk } = await import('../../utils/sdk.mts')
53+
const mockSetupSdk = vi.mocked(withSdk)
5754

5855
const error = {
5956
ok: false,
@@ -70,9 +67,9 @@ describe('fetchRepoAnalytics', () => {
7067

7168
it('handles API call failure', async () => {
7269
const { handleApiCall } = await import('../../utils/api.mts')
73-
const { setupSdk } = await import('../../utils/sdk.mts')
70+
const { withSdk } = await import('../../utils/sdk.mts')
7471
const mockHandleApi = vi.mocked(handleApiCall)
75-
const mockSetupSdk = vi.mocked(setupSdk)
72+
const mockSetupSdk = vi.mocked(withSdk)
7673

7774
const mockSdk = {
7875
getRepoAnalytics: vi
@@ -94,9 +91,9 @@ describe('fetchRepoAnalytics', () => {
9491
})
9592

9693
it('passes custom SDK options', async () => {
97-
const { setupSdk } = await import('../../utils/sdk.mts')
94+
const { withSdk } = await import('../../utils/sdk.mts')
9895
const { handleApiCall } = await import('../../utils/api.mts')
99-
const mockSetupSdk = vi.mocked(setupSdk)
96+
const mockSetupSdk = vi.mocked(withSdk)
10097
const mockHandleApi = vi.mocked(handleApiCall)
10198

10299
const mockSdk = {
@@ -117,9 +114,9 @@ describe('fetchRepoAnalytics', () => {
117114
})
118115

119116
it('handles different org and repo combinations', async () => {
120-
const { setupSdk } = await import('../../utils/sdk.mts')
117+
const { withSdk } = await import('../../utils/sdk.mts')
121118
const { handleApiCall } = await import('../../utils/api.mts')
122-
const mockSetupSdk = vi.mocked(setupSdk)
119+
const mockSetupSdk = vi.mocked(withSdk)
123120
const mockHandleApi = vi.mocked(handleApiCall)
124121

125122
const mockSdk = {
@@ -139,9 +136,9 @@ describe('fetchRepoAnalytics', () => {
139136
})
140137

141138
it('handles different time ranges', async () => {
142-
const { setupSdk } = await import('../../utils/sdk.mts')
139+
const { withSdk } = await import('../../utils/sdk.mts')
143140
const { handleApiCall } = await import('../../utils/api.mts')
144-
const mockSetupSdk = vi.mocked(setupSdk)
141+
const mockSetupSdk = vi.mocked(withSdk)
145142
const mockHandleApi = vi.mocked(handleApiCall)
146143

147144
const mockSdk = {
@@ -164,9 +161,9 @@ describe('fetchRepoAnalytics', () => {
164161
})
165162

166163
it('uses null prototype for options', async () => {
167-
const { setupSdk } = await import('../../utils/sdk.mts')
164+
const { withSdk } = await import('../../utils/sdk.mts')
168165
const { handleApiCall } = await import('../../utils/api.mts')
169-
const mockSetupSdk = vi.mocked(setupSdk)
166+
const mockSetupSdk = vi.mocked(withSdk)
170167
const mockHandleApi = vi.mocked(handleApiCall)
171168

172169
const mockSdk = {

src/commands/audit-log/fetch-audit-log.test.mts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@ import { describe, expect, it, vi } from 'vitest'
33
import { fetchAuditLog } from './fetch-audit-log.mts'
44

55
// Mock the dependencies.
6-
vi.mock('../../utils/api.mts', () => ({
7-
handleApiCall: vi.fn(),
8-
}))
96

107
vi.mock('../../utils/sdk.mts', () => ({
11-
setupSdk: vi.fn(),
8+
withSdk: vi.fn(),
129
}))
1310

1411
describe('fetchAuditLog', () => {
1512
it('fetches audit log successfully', async () => {
1613
const { handleApiCall } = await import('../../utils/api.mts')
17-
const { setupSdk } = await import('../../utils/sdk.mts')
14+
const { withSdk } = await import('../../utils/sdk.mts')
1815
const mockHandleApi = vi.mocked(handleApiCall)
19-
const mockSetupSdk = vi.mocked(setupSdk)
16+
const mockSetupSdk = vi.mocked(withSdk)
2017

2118
const mockSdk = {
2219
getAuditLogEvents: vi.fn().mockResolvedValue({
@@ -75,8 +72,8 @@ describe('fetchAuditLog', () => {
7572
})
7673

7774
it('handles SDK setup failure', async () => {
78-
const { setupSdk } = await import('../../utils/sdk.mts')
79-
const mockSetupSdk = vi.mocked(setupSdk)
75+
const { withSdk } = await import('../../utils/sdk.mts')
76+
const mockSetupSdk = vi.mocked(withSdk)
8077

8178
const error = {
8279
ok: false,
@@ -101,9 +98,9 @@ describe('fetchAuditLog', () => {
10198

10299
it('handles API call failure', async () => {
103100
const { handleApiCall } = await import('../../utils/api.mts')
104-
const { setupSdk } = await import('../../utils/sdk.mts')
101+
const { withSdk } = await import('../../utils/sdk.mts')
105102
const mockHandleApi = vi.mocked(handleApiCall)
106-
const mockSetupSdk = vi.mocked(setupSdk)
103+
const mockSetupSdk = vi.mocked(withSdk)
107104

108105
const mockSdk = {
109106
getAuditLogEvents: vi
@@ -133,9 +130,9 @@ describe('fetchAuditLog', () => {
133130
})
134131

135132
it('passes custom SDK options', async () => {
136-
const { setupSdk } = await import('../../utils/sdk.mts')
133+
const { withSdk } = await import('../../utils/sdk.mts')
137134
const { handleApiCall } = await import('../../utils/api.mts')
138-
const mockSetupSdk = vi.mocked(setupSdk)
135+
const mockSetupSdk = vi.mocked(withSdk)
139136
const mockHandleApi = vi.mocked(handleApiCall)
140137

141138
const mockSdk = {
@@ -164,9 +161,9 @@ describe('fetchAuditLog', () => {
164161
})
165162

166163
it('handles pagination parameters', async () => {
167-
const { setupSdk } = await import('../../utils/sdk.mts')
164+
const { withSdk } = await import('../../utils/sdk.mts')
168165
const { handleApiCall } = await import('../../utils/api.mts')
169-
const mockSetupSdk = vi.mocked(setupSdk)
166+
const mockSetupSdk = vi.mocked(withSdk)
170167
const mockHandleApi = vi.mocked(handleApiCall)
171168

172169
const mockSdk = {
@@ -196,9 +193,9 @@ describe('fetchAuditLog', () => {
196193
})
197194

198195
it('handles date filtering', async () => {
199-
const { setupSdk } = await import('../../utils/sdk.mts')
196+
const { withSdk } = await import('../../utils/sdk.mts')
200197
const { handleApiCall } = await import('../../utils/api.mts')
201-
const mockSetupSdk = vi.mocked(setupSdk)
198+
const mockSetupSdk = vi.mocked(withSdk)
202199
const mockHandleApi = vi.mocked(handleApiCall)
203200

204201
const mockSdk = {
@@ -232,9 +229,9 @@ describe('fetchAuditLog', () => {
232229
})
233230

234231
it('uses null prototype for options', async () => {
235-
const { setupSdk } = await import('../../utils/sdk.mts')
232+
const { withSdk } = await import('../../utils/sdk.mts')
236233
const { handleApiCall } = await import('../../utils/api.mts')
237-
const mockSetupSdk = vi.mocked(setupSdk)
234+
const mockSetupSdk = vi.mocked(withSdk)
238235
const mockHandleApi = vi.mocked(handleApiCall)
239236

240237
const mockSdk = {

src/commands/organization/fetch-dependencies.test.mts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@ import { describe, expect, it, vi } from 'vitest'
33
import { fetchDependencies } from './fetch-dependencies.mts'
44

55
// Mock the dependencies.
6-
vi.mock('../../utils/api.mts', () => ({
7-
handleApiCall: vi.fn(),
8-
}))
96

107
vi.mock('../../utils/sdk.mts', () => ({
11-
setupSdk: vi.fn(),
8+
withSdk: vi.fn(),
129
}))
1310

1411
describe('fetchDependencies', () => {
1512
it('fetches dependencies successfully', async () => {
1613
const { handleApiCall } = await import('../../utils/api.mts')
17-
const { setupSdk } = await import('../../utils/sdk.mts')
14+
const { withSdk } = await import('../../utils/sdk.mts')
1815
const mockHandleApi = vi.mocked(handleApiCall)
19-
const mockSetupSdk = vi.mocked(setupSdk)
16+
const mockSetupSdk = vi.mocked(withSdk)
2017

2118
const mockSdk = {
2219
searchDependencies: vi.fn().mockResolvedValue({
@@ -56,8 +53,8 @@ describe('fetchDependencies', () => {
5653
})
5754

5855
it('handles SDK setup failure', async () => {
59-
const { setupSdk } = await import('../../utils/sdk.mts')
60-
const mockSetupSdk = vi.mocked(setupSdk)
56+
const { withSdk } = await import('../../utils/sdk.mts')
57+
const mockSetupSdk = vi.mocked(withSdk)
6158

6259
const error = {
6360
ok: false,
@@ -74,9 +71,9 @@ describe('fetchDependencies', () => {
7471

7572
it('handles API call failure', async () => {
7673
const { handleApiCall } = await import('../../utils/api.mts')
77-
const { setupSdk } = await import('../../utils/sdk.mts')
74+
const { withSdk } = await import('../../utils/sdk.mts')
7875
const mockHandleApi = vi.mocked(handleApiCall)
79-
const mockSetupSdk = vi.mocked(setupSdk)
76+
const mockSetupSdk = vi.mocked(withSdk)
8077

8178
const mockSdk = {
8279
searchDependencies: vi.fn().mockRejectedValue(new Error('API error')),
@@ -94,9 +91,9 @@ describe('fetchDependencies', () => {
9491
})
9592

9693
it('passes custom SDK options', async () => {
97-
const { setupSdk } = await import('../../utils/sdk.mts')
94+
const { withSdk } = await import('../../utils/sdk.mts')
9895
const { handleApiCall } = await import('../../utils/api.mts')
99-
const mockSetupSdk = vi.mocked(setupSdk)
96+
const mockSetupSdk = vi.mocked(withSdk)
10097
const mockHandleApi = vi.mocked(handleApiCall)
10198

10299
const mockSdk = {
@@ -117,9 +114,9 @@ describe('fetchDependencies', () => {
117114
})
118115

119116
it('handles pagination parameters', async () => {
120-
const { setupSdk } = await import('../../utils/sdk.mts')
117+
const { withSdk } = await import('../../utils/sdk.mts')
121118
const { handleApiCall } = await import('../../utils/api.mts')
122-
const mockSetupSdk = vi.mocked(setupSdk)
119+
const mockSetupSdk = vi.mocked(withSdk)
123120
const mockHandleApi = vi.mocked(handleApiCall)
124121

125122
const mockSdk = {
@@ -138,9 +135,9 @@ describe('fetchDependencies', () => {
138135
})
139136

140137
it('uses null prototype for options', async () => {
141-
const { setupSdk } = await import('../../utils/sdk.mts')
138+
const { withSdk } = await import('../../utils/sdk.mts')
142139
const { handleApiCall } = await import('../../utils/api.mts')
143-
const mockSetupSdk = vi.mocked(setupSdk)
140+
const mockSetupSdk = vi.mocked(withSdk)
144141
const mockHandleApi = vi.mocked(handleApiCall)
145142

146143
const mockSdk = {

0 commit comments

Comments
 (0)