-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tests): Improved mock clients
- Loading branch information
Showing
11 changed files
with
80 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,14 @@ | ||
import { BikeTagApiResponse, BikeTagClient } from 'biketag' | ||
import { getTagsResponse, getTagResponse } from './mock-data' | ||
import { Tag } from '#src/common/schema' | ||
|
||
/// *************************** Types *************************** /// | ||
|
||
export interface MockBikeTagClient extends BikeTagClient { | ||
mockResponses: { | ||
getTags: BikeTagApiResponse<Tag[]> | ||
uploadTagImage: BikeTagApiResponse<Tag> | ||
} | ||
} | ||
import * as mockData from './mock-data' | ||
import { exposeMocks } from '../helpers' | ||
|
||
/// *************************** Utils *************************** /// | ||
|
||
/** | ||
* Create mock BikeTag Client | ||
* Note: Alter mock responses using the `mockResponses` property | ||
*/ | ||
export function createMockClient(): MockBikeTagClient { | ||
return { | ||
mockResponses: { | ||
getTags: getTagsResponse, | ||
getTag: getTagResponse, | ||
}, | ||
getTags: jest.fn(function () { | ||
return getTagsResponse | ||
}), | ||
uploadTagImage: jest.fn(function () { | ||
return getTagResponse | ||
}), | ||
} as unknown as MockBikeTagClient | ||
export function createMockClient() { | ||
return exposeMocks({ | ||
getTags: jest.fn().mockResolvedValue(mockData.getTagsResponse), | ||
getTag: jest.fn().mockResolvedValue(mockData.getTagResponse), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/// *************************** Mock Helpers *************************** /// | ||
|
||
/** | ||
* Takes a mock object and exposes the jest function in `mock` property | ||
* @example | ||
* const myClient = exposeMocks({ | ||
* getTags: jest.fn(() => true) | ||
* }) | ||
* | ||
* // Use mock type to change response | ||
* myClient.mocks.getTags.mockReturnValue(false); | ||
* | ||
* // Directly call function | ||
* myClient.getTags(); | ||
*/ | ||
export function exposeMocks<T>( | ||
obj: T | ||
): T & { mocks: Record<keyof T, jest.Mock> } | ||
export function exposeMocks(obj: any): any { | ||
const keys = Object.keys(obj) | ||
|
||
obj.mocks = {} | ||
Object.defineProperties( | ||
obj.mocks, | ||
Object.fromEntries( | ||
keys.map((k) => [ | ||
k, | ||
{ get: () => obj[k], enumerable: true, configurable: true }, | ||
]) | ||
) | ||
) | ||
|
||
return obj | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,16 @@ | ||
import { ImgurApiResponse, ImgurClient } from 'imgur' | ||
import { | ||
getAlbumResponse, | ||
getImageResponse, | ||
updateImageResponse, | ||
uploadResponse, | ||
getTagsResponse, | ||
} from './mock-data' | ||
import { AlbumData, ImageData } from 'imgur/lib/common/types' | ||
|
||
/// *************************** Types *************************** /// | ||
|
||
export interface MockImgurClient extends ImgurClient { | ||
mockResponses: { | ||
getAlbum: ImgurApiResponse<AlbumData> | ||
getImage: ImgurApiResponse<ImageData> | ||
updateImage: ImgurApiResponse<boolean> | ||
upload: ImgurApiResponse<ImageData> | ||
} | ||
} | ||
import * as mockData from './mock-data' | ||
import { exposeMocks } from '../helpers' | ||
|
||
/// *************************** Utils *************************** /// | ||
|
||
/** | ||
* Create mock Imgur Client | ||
* Note: Alter mock responses using the `mockResponses` property | ||
*/ | ||
export function createMockClient(): MockImgurClient { | ||
const client = {} as MockImgurClient | ||
|
||
return Object.assign(client, { | ||
mockResponses: { | ||
getAlbum: getAlbumResponse, | ||
getImage: getImageResponse, | ||
updateImage: updateImageResponse, | ||
upload: uploadResponse, | ||
}, | ||
getAlbum: jest.fn(async function () { | ||
return client.mockResponses.getAlbum | ||
}), | ||
getImage: jest.fn(async function () { | ||
return client.mockResponses.getImage | ||
}), | ||
updateImage: jest.fn(async function () { | ||
return client.mockResponses.updateImage | ||
}), | ||
upload: jest.fn(async function () { | ||
return client.mockResponses.upload | ||
}), | ||
export function createMockClient() { | ||
return exposeMocks({ | ||
getAlbum: jest.fn().mockResolvedValue(mockData.getAlbumResponse), | ||
getImage: jest.fn().mockResolvedValue(mockData.getImageResponse), | ||
updateImage: jest.fn().mockResolvedValue(mockData.updateImageResponse), | ||
upload: jest.fn().mockResolvedValue(mockData.uploadResponse), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters