Tweak Jest setup file

This commit is contained in:
Pig Fang 2019-11-26 11:44:44 +08:00
parent a43e8036c5
commit 383e20acbd
4 changed files with 26 additions and 11 deletions

View File

@ -194,7 +194,7 @@
"\\.(png|jpg)$": "<rootDir>/resources/assets/tests/__mocks__/file.ts" "\\.(png|jpg)$": "<rootDir>/resources/assets/tests/__mocks__/file.ts"
}, },
"setupFilesAfterEnv": [ "setupFilesAfterEnv": [
"<rootDir>/resources/assets/tests/setup.js" "<rootDir>/resources/assets/tests/setup.ts"
], ],
"coveragePathIgnorePatterns": [ "coveragePathIgnorePatterns": [
"/node_modules/", "/node_modules/",

View File

@ -2,10 +2,9 @@ import Vue from 'vue'
export function trans(key: string, parameters = Object.create(null)): string { export function trans(key: string, parameters = Object.create(null)): string {
const segments = key.split('.') const segments = key.split('.')
let temp = ( let temp = (blessing.i18n) as {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition [k: string]: string | { [k: string]: string }
blessing.i18n || Object.create(null) }
) as { [k: string]: string | { [k: string]: string } }
let result = '' let result = ''
for (const segment of segments) { for (const segment of segments) {

View File

@ -8,6 +8,9 @@ interface Window {
trans(key: string, parameters: object): string trans(key: string, parameters: object): string
blessing: { blessing: {
base_url: string
site_name: string
version: string
i18n: object i18n: object
extra: object extra: object
} }

View File

@ -13,25 +13,36 @@ window.blessing = {
site_name: 'Blessing Skin', site_name: 'Blessing Skin',
version: '4.0.0', version: '4.0.0',
extra: {}, extra: {},
i18n: {},
} }
window.Headers = class extends Map { class Headers extends Map {
constructor(headers = {}) { constructor(headers = {}) {
// @ts-ignore
super(Object.entries(headers)) super(Object.entries(headers))
} }
} }
class Request {
public url: string
window.Request = class { public headers: Headers
constructor(url, init) {
constructor(url: string, init: RequestInit) {
this.url = url this.url = url
Object.assign(this, init) Object.assign(this, init)
this.headers = new Map(Object.entries(init.headers || {})) // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
this.headers = new Headers(Object.entries(init.headers || {}))
} }
} }
Object.assign(window, { Headers, Request })
const noop = () => undefined const noop = () => undefined
// eslint-disable-next-line no-console Object.assign(console, {
Object.keys(console).forEach(method => (console[method] = noop)) log: noop,
info: noop,
warn: noop,
error: noop,
})
Vue.prototype.$t = key => key Vue.prototype.$t = key => key
@ -56,12 +67,14 @@ Vue.use(Button)
Vue.use(Input) Vue.use(Input)
Vue.use(Radio) Vue.use(Radio)
Vue.use(Switch) Vue.use(Switch)
// @ts-ignore
Vue.prototype.$message = { Vue.prototype.$message = {
info: jest.fn(), info: jest.fn(),
success: jest.fn(), success: jest.fn(),
warning: jest.fn(), warning: jest.fn(),
error: jest.fn(), error: jest.fn(),
} }
// @ts-ignore
Vue.prototype.$msgbox = jest.fn() Vue.prototype.$msgbox = jest.fn()
Vue.prototype.$alert = jest.fn() Vue.prototype.$alert = jest.fn()
Vue.prototype.$confirm = jest.fn() Vue.prototype.$confirm = jest.fn()