generated from alphane/template
Initial commit
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
export { mockCurrentUserApi } from "./mock-current-user-api"
|
||||
export { mockHttpClient } from "./mock-http-client"
|
||||
@@ -0,0 +1,54 @@
|
||||
import { merge } from "lodash"
|
||||
|
||||
import { type Policy } from "@/api/base-api"
|
||||
import currentUserApi, { type UserAsShow } from "@/api/current-user-api"
|
||||
|
||||
export const DEFAULT_USER: UserAsShow = {
|
||||
id: 1,
|
||||
email: "default-mock-user@example.com",
|
||||
firstName: null,
|
||||
lastName: null,
|
||||
displayName: null,
|
||||
title: null,
|
||||
isActive: true,
|
||||
emailNotificationsEnabled: true,
|
||||
createdAt: "1970-01-01T00:00:00.000Z",
|
||||
updatedAt: "1970-01-01T00:00:00.000Z",
|
||||
}
|
||||
export const DEFAULT_POLICY: Policy = {
|
||||
show: true,
|
||||
create: false,
|
||||
update: true,
|
||||
destroy: false,
|
||||
}
|
||||
|
||||
/**
|
||||
* Usage:
|
||||
* At the top section of a test file import:
|
||||
* import { mockCurrentUserApi } from "@/tests/support"
|
||||
*
|
||||
* Then where you want to set the current user:
|
||||
* mockCurrentUserApi({ user, policy })
|
||||
*
|
||||
* Note that all values for user and policy are optional as minium safe defaults are provided.
|
||||
* Note that order of operations matters. This file must be imported before any file that imports currentUserApi.
|
||||
* As such this file has been imported and mocked in the pre-test run "web/tests/setup.ts" file.
|
||||
*
|
||||
* @param currentUserApiGetResponseWithDefaults - The response to set for the current user
|
||||
*/
|
||||
export function mockCurrentUserApi(
|
||||
currentUserApiGetResponseWithDefaults: { user?: UserAsShow; policy?: Policy } = {}
|
||||
) {
|
||||
vi.mock("@/api/current-user-api")
|
||||
|
||||
const userWithDefaults = merge({}, DEFAULT_USER, currentUserApiGetResponseWithDefaults.user)
|
||||
const policyWithDefaults = merge({}, DEFAULT_POLICY, currentUserApiGetResponseWithDefaults.policy)
|
||||
|
||||
const mockedCurrentUserApi = vi.mocked(currentUserApi, true)
|
||||
mockedCurrentUserApi.get.mockResolvedValue({
|
||||
user: userWithDefaults,
|
||||
policy: policyWithDefaults,
|
||||
})
|
||||
}
|
||||
|
||||
export default mockCurrentUserApi
|
||||
@@ -0,0 +1,40 @@
|
||||
import httpClient from "@/api/http-client"
|
||||
|
||||
vi.mock("@/api/http-client")
|
||||
|
||||
/**
|
||||
* Usage:
|
||||
* At the top section of a test file import:
|
||||
* import { mockHttpClient } from "@/tests/support"
|
||||
*
|
||||
* Then where you want to set the http client:
|
||||
* mockHttpClient()
|
||||
*
|
||||
* Note that order of operations matters. This file must be imported before any file that imports httpClient.
|
||||
* As such this file has been imported and mocked in the pre-test run "web/tests/setup.ts" file.
|
||||
*
|
||||
* @returns The mocked http client
|
||||
*/
|
||||
export function mockHttpClient() {
|
||||
const httpClientMock = vi.mocked(httpClient, true)
|
||||
|
||||
httpClientMock.get.mockResolvedValue({
|
||||
data: {},
|
||||
})
|
||||
httpClientMock.post.mockResolvedValue({
|
||||
data: {},
|
||||
})
|
||||
httpClientMock.put.mockResolvedValue({
|
||||
data: {},
|
||||
})
|
||||
httpClientMock.patch.mockResolvedValue({
|
||||
data: {},
|
||||
})
|
||||
httpClientMock.delete.mockResolvedValue({
|
||||
data: {},
|
||||
})
|
||||
|
||||
return httpClientMock
|
||||
}
|
||||
|
||||
export default mockHttpClient
|
||||
Reference in New Issue
Block a user