generated from alphane/template
Initial commit
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
import { mockHttpClient } from "@/tests/support/mock-http-client"
|
||||
import { mockCurrentUserApi } from "@/tests/support/mock-current-user-api"
|
||||
|
||||
beforeEach(() => {
|
||||
mockHttpClient()
|
||||
mockCurrentUserApi()
|
||||
})
|
||||
@@ -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
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"extends": "./../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": "./../",
|
||||
"types": ["vitest/globals"],
|
||||
"paths": {
|
||||
"@/tests/*": ["./tests/*"],
|
||||
"@/*": ["./src/*"]
|
||||
}
|
||||
},
|
||||
"include": ["../src/**/*", "../tests/**/*", ""]
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { type RouteLocationNormalized } from "vue-router"
|
||||
|
||||
import authorizationGuard from "@/utils/authorization-guards/authorization-guard"
|
||||
|
||||
describe("web/src/utils/authorization-guards/authorization-guard.ts", () => {
|
||||
describe("authorizationGuard", () => {
|
||||
test("when there are no guards, returns true", async () => {
|
||||
const to = {
|
||||
meta: {},
|
||||
} as unknown as RouteLocationNormalized
|
||||
|
||||
const result = await authorizationGuard(to)
|
||||
|
||||
expect(result).toBe(true)
|
||||
})
|
||||
|
||||
test("when all guards return true, returns true", async () => {
|
||||
const to = {
|
||||
meta: {
|
||||
guards: [() => true, () => true, () => true],
|
||||
},
|
||||
} as unknown as RouteLocationNormalized
|
||||
|
||||
const result = await authorizationGuard(to)
|
||||
|
||||
expect(result).toBe(true)
|
||||
})
|
||||
|
||||
test("when any guard returns false, returns false", async () => {
|
||||
const to = {
|
||||
meta: {
|
||||
guards: [() => true, () => false, () => true],
|
||||
},
|
||||
} as unknown as RouteLocationNormalized
|
||||
|
||||
const result = await authorizationGuard(to)
|
||||
|
||||
expect(result).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user