Skip to content

Commit 824ebec

Browse files
committed
fix: address review comments on test quality
- Mock FetchAdapter to assert correct URL for each environment - Use mockImplementation for getPackageBySubmissionId to return distinct objects per submission ID
1 parent d075af0 commit 824ebec

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/__tests__/EQP.test.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
22
import { EQP } from '../index';
3+
import { FetchAdapter } from '../FetchAdapter';
34
import { FileService } from '../services/FileService';
45
import { UserService } from '../services/UserService';
56
import { KeyService } from '../services/KeyService';
67
import { CallbackService } from '../services/CallbackService';
78
import { ReportService } from '../services/ReportService';
89
import { PackageService } from '../services/PackageService';
910

10-
// Mock global fetch to prevent real requests during EQP construction
11-
vi.stubGlobal('fetch', vi.fn());
11+
vi.mock('../FetchAdapter', () => ({
12+
FetchAdapter: vi.fn()
13+
}));
1214

1315
describe('EQP', () => {
1416
const defaultOptions = {
1517
appId: 'test-app-id',
1618
appSecret: 'test-app-secret'
1719
};
1820

21+
beforeEach(() => {
22+
vi.clearAllMocks();
23+
});
24+
1925
describe('environment URLs', () => {
2026
it('should use production URL by default', () => {
21-
const eqp = new EQP(defaultOptions);
27+
new EQP(defaultOptions);
2228

23-
expect(eqp).toBeDefined();
29+
expect(FetchAdapter).toHaveBeenCalledWith('https://commercedeveloper-api.adobe.com/rest/v1');
2430
});
2531

2632
it('should use production URL when environment is "production"', () => {
27-
const eqp = new EQP({ ...defaultOptions, environment: 'production' });
33+
new EQP({ ...defaultOptions, environment: 'production' });
2834

29-
expect(eqp).toBeDefined();
35+
expect(FetchAdapter).toHaveBeenCalledWith('https://commercedeveloper-api.adobe.com/rest/v1');
3036
});
3137

3238
it('should use sandbox URL when environment is "sandbox"', () => {
33-
const eqp = new EQP({ ...defaultOptions, environment: 'sandbox' });
39+
new EQP({ ...defaultOptions, environment: 'sandbox' });
3440

35-
expect(eqp).toBeDefined();
41+
expect(FetchAdapter).toHaveBeenCalledWith('https://commercedeveloper-sandbox-api.adobe.com/rest/v1');
3642
});
3743
});
3844

@@ -46,9 +52,9 @@ describe('EQP', () => {
4652
delete: vi.fn()
4753
};
4854

49-
const eqp = new EQP({ ...defaultOptions, adapter: customAdapter });
55+
new EQP({ ...defaultOptions, adapter: customAdapter });
5056

51-
expect(eqp).toBeDefined();
57+
expect(FetchAdapter).not.toHaveBeenCalled();
5258
});
5359
});
5460

src/__tests__/services/CallbackService.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('CallbackService', () => {
2323
updateUser: vi.fn().mockResolvedValue({ mage_id: 'MAG123' })
2424
},
2525
packageService: {
26-
getPackageBySubmissionId: vi.fn().mockResolvedValue({ submission_id: 'sub-1' })
26+
getPackageBySubmissionId: vi.fn().mockImplementation((id: string) => Promise.resolve({ submission_id: id }))
2727
},
2828
fileService: {
2929
getFile: vi.fn().mockResolvedValue({
@@ -92,7 +92,7 @@ describe('CallbackService', () => {
9292
expect(mockEqp.packageService.getPackageBySubmissionId).toHaveBeenCalledWith('sub-2');
9393
expect(result).toEqual({
9494
file: { file_upload_id: 'file-1', submission_ids: ['sub-1', 'sub-2'] },
95-
submissions: [{ submission_id: 'sub-1' }, { submission_id: 'sub-1' }],
95+
submissions: [{ submission_id: 'sub-1' }, { submission_id: 'sub-2' }],
9696
result: 'pass'
9797
});
9898
});

0 commit comments

Comments
 (0)