Skip to content

Commit

Permalink
refactor(tests): Improved mock clients
Browse files Browse the repository at this point in the history
  • Loading branch information
nonara committed Jan 10, 2022
1 parent 6d63bc9 commit 927f465
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 107 deletions.
33 changes: 7 additions & 26 deletions test/src/mock/biketag/client.ts
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),
})
}
34 changes: 34 additions & 0 deletions test/src/mock/helpers.ts
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
}
51 changes: 8 additions & 43 deletions test/src/mock/imgur/client.ts
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),
})
}
10 changes: 5 additions & 5 deletions test/tests/imgur/deleteTag.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MockBikeTag, MockImgur, MockTag } from '#test-src'
import * as imgurModule from '#src/imgur'
import { HttpStatusCode } from '#src/common/enums'
import * as imgurModule from '#src/imgur'

/// *************************** Config *************************** ///

Expand All @@ -9,11 +9,11 @@ const imgurDeleteTagMethod = 'biketag.images.deleteTag'
/// *************************** Tests *************************** ///

describe(imgurDeleteTagMethod, () => {
const client = MockImgur.createMockClient()
const bikeTagClientMock = MockBikeTag.createMockClient()
const mockClient = MockImgur.createMockClient()
const mockBikeTagClient = MockBikeTag.createMockClient()
const deleteTag = imgurModule.deleteTag.bind(
{ getTags: bikeTagClientMock.getTags },
client
mockBikeTagClient,
<any>mockClient
)

test(`${imgurDeleteTagMethod} method requires ImgurHash from payload`, () => {
Expand Down
12 changes: 6 additions & 6 deletions test/tests/imgur/deleteTags.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as deleteTagsModule from '#src/imgur'
import * as imgurModule from '#src/imgur'
import { HttpStatusCode } from '#src/common/enums'
import { MockBikeTag, MockImgur, MockTag } from '#test-src'

Expand All @@ -9,11 +9,11 @@ const imgurDeleteTagsMethod = 'biketag.images.deleteTags'
/// *************************** Tests *************************** ///

describe(imgurDeleteTagsMethod, () => {
const client = MockImgur.createMockClient()
const bikeTagClientMock = MockBikeTag.createMockClient()
const deleteTags = deleteTagsModule.getTags.bind(
{ getTags: bikeTagClientMock.getTags },
client
const mockClient = MockImgur.createMockClient()
const mockBikeTagClient = MockBikeTag.createMockClient()
const deleteTags = imgurModule.deleteTags.bind(
mockBikeTagClient,
<any>mockClient
)

test(`${imgurDeleteTagsMethod} method requires ImgurHash from payload`, () => {
Expand Down
8 changes: 4 additions & 4 deletions test/tests/imgur/getPlayers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { HttpStatusCode } from '#src/common/enums'
/// *************************** Test *************************** ///

describe(imgurGetPlayersMethod, () => {
const client = MockImgur.createMockClient()
const bikeTagClientMock = MockBikeTag.createMockClient()
const mockClient = MockImgur.createMockClient()
const mockBikeTagClient = MockBikeTag.createMockClient()
const getPlayers = imgurModule.getPlayers.bind(
{ getTags: bikeTagClientMock.getTags },
client
mockBikeTagClient,
<any>mockClient
)

test(`${imgurGetPlayersMethod} method requires ImgurHash from payload`, () => {
Expand Down
7 changes: 4 additions & 3 deletions test/tests/imgur/getTags.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as imgurModule from '#src/imgur'
import { isTag } from '#src/common/schema'
import { HttpStatusCode } from '#src/common/enums'
import { MockImgur, MockTag } from '#test-src'
import { MockBikeTag, MockImgur, MockTag } from '#test-src'

/// *************************** Config *************************** ///

Expand All @@ -10,8 +10,9 @@ const imgurGetTagsMethod = 'biketag.images.getTags'
/// *************************** Tests *************************** ///

describe(imgurGetTagsMethod, () => {
const client = MockImgur.createMockClient()
const getTags = imgurModule.getTags.bind(undefined, client)
const mockClient = MockImgur.createMockClient()
const mockBikeTagClient = MockBikeTag.createMockClient()
const getTags = imgurModule.getTags.bind(mockBikeTagClient, <any>mockClient)

test(`${imgurGetTagsMethod} method requires ImgurHash from payload`, () => {
return expect(getTags(<any>{})).rejects.toThrow()
Expand Down
11 changes: 3 additions & 8 deletions test/tests/imgur/queueTag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,15 @@ const imgurQueueTagMethod = 'biketag.images.queueTag'
/// *************************** Test *************************** ///

describe(imgurQueueTagMethod, () => {
const client = MockImgur.createMockClient()
const queueTag = imgurModule.queueTag.bind(undefined, client)
const mockClient = MockImgur.createMockClient()
const mockBikeTagClient = MockBikeTag.createMockClient()
const queueTag = imgurModule.queueTag.bind(mockBikeTagClient, <any>mockClient)

test(`${imgurQueueTagMethod} method requires ImgurHash from payload`, () => {
return expect(queueTag(<any>{})).rejects.toThrow()
})

test(`${imgurQueueTagMethod} method requires one of [hashes, tagnumbers, slugs] from payload`, () => {
const client = MockImgur.createMockClient()
const bikeTagClientMock = MockBikeTag.createMockClient()
const queueTag = imgurModule.queueTag.bind(
{ getTags: bikeTagClientMock.getTags },
client
)
const queuedTag = queueTag({} as queueTagPayload)

expect(isTag(queuedTag)).toBeTruthy()
Expand Down
11 changes: 4 additions & 7 deletions test/tests/imgur/updateTag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ const imgurUpdateTagsMethod = 'biketag.images.updateTags'
/// *************************** Tests *************************** ///

describe(imgurUpdateTagsMethod, () => {
const client = MockImgur.createMockClient()
const bikeTagClientMock = MockBikeTag.createMockClient()
const mockClient = MockImgur.createMockClient()
const mockBikeTagClient = MockBikeTag.createMockClient()
const updateTag = imgurModule.updateTag.bind(
{
getTags: bikeTagClientMock.getTags,
uploadTagImage: bikeTagClientMock.uploadTagImage,
},
client
mockBikeTagClient,
<any>mockClient
)

test(`${imgurUpdateTagsMethod} method requires ImgurHash from payload`, () => {
Expand Down
8 changes: 4 additions & 4 deletions test/tests/imgur/uploadTagImage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ const img2 = {
/// *************************** Tests *************************** ///

describe(imgurUploadTagImageMethod, () => {
const client = MockImgur.createMockClient()
const bikeTagClientMock = MockBikeTag.createMockClient()
const mockClient = MockImgur.createMockClient()
const mockBikeTagClient = MockBikeTag.createMockClient()
const uploadTagImage = imgurModule.uploadTagImage.bind(
{ getTags: bikeTagClientMock.getTags },
client
mockBikeTagClient,
<any>mockClient
)

test(`${imgurUploadTagImageMethod} method requires ImgurHash from payload`, () => {
Expand Down
2 changes: 1 addition & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"compilerOptions": {
"noEmit": true,
"lib": ["esnext", "dom"],
"target": "es2015",
"target": "ES2019",
"module": "CommonJS",
"moduleResolution": "node",
"strictBindCallApply": true,
Expand Down

0 comments on commit 927f465

Please sign in to comment.