Zoupers
6 months ago
5 changed files with 140 additions and 64 deletions
@ -0,0 +1,51 @@
|
||||
import type { Config } from "jest"; |
||||
// import webpackConfig from "./webpack/webpack.dev";
|
||||
|
||||
export default { |
||||
// testEnvironment: "<rootDir>/test/testEnvironmentJsdom.ts",
|
||||
testEnvironment: "jsdom", |
||||
testEnvironmentOptions: { |
||||
// html: fs.readFileSync(path.join(__dirname, "test/index.html")),
|
||||
}, |
||||
testMatch: ["<rootDir>/src/**/*.test.js"], |
||||
collectCoverage: false, |
||||
collectCoverageFrom: ["src/**/*.js", "!src/**/*.test.js"], |
||||
coverageDirectory: "coverage", |
||||
coverageReporters: ["html", "json-summary", "cobertura"], |
||||
|
||||
reporters: [ |
||||
"default", |
||||
[ |
||||
"jest-html-reporters", |
||||
{ |
||||
publicPath: "./html-report", |
||||
openReport: true, |
||||
}, |
||||
], |
||||
], |
||||
|
||||
transform: { |
||||
"^.+\\.jsx?$": "babel-jest", |
||||
"^.+\\.tsx?$": "babel-jest", |
||||
}, |
||||
|
||||
preset: "jest-puppeteer", |
||||
verbose: true, |
||||
watchman: true, |
||||
injectGlobals: true, |
||||
globals: { |
||||
"ts-jest": { |
||||
tsconfig: "tsconfig.json", |
||||
}, |
||||
}, |
||||
setupFilesAfterEnv: [ |
||||
"<rootDir>/src/bundle.js", |
||||
"<rootDir>/src/core/platform/web/config.js", |
||||
"<rootDir>/../demo/i18n/i18n.cn.js", |
||||
"<rootDir>/test/utils.js", |
||||
"<rootDir>/test/prepare.js", |
||||
], |
||||
moduleNameMapper: { |
||||
"@/(.*)$": "<rootDir>/src/$1", |
||||
}, |
||||
} as Config; |
@ -1,45 +1,71 @@
|
||||
const onetimeAfterEach = [] |
||||
afterEach(() => { |
||||
while(onetimeAfterEach.length > 0) { |
||||
onetimeAfterEach.shift()() |
||||
} |
||||
}); |
||||
|
||||
function registeCloser(...f) { |
||||
onetimeAfterEach.unshift(...f) |
||||
}; |
||||
|
||||
function sleep(t) { |
||||
return new Promise(resolve => setTimeout(() => { |
||||
resolve() |
||||
}, t)) |
||||
}; |
||||
|
||||
function createWidget(widgetJson) { |
||||
var widget = BI.createWidget(BI.extend(widgetJson, { |
||||
root: true |
||||
})); |
||||
widget.element.appendTo("body"); |
||||
registeCloser(() => { |
||||
widget.destroy() |
||||
}); |
||||
return widget; |
||||
}; |
||||
|
||||
/** |
||||
* 模拟一次输入框的keydown事件 |
||||
*/ |
||||
function triggerKeyDown (element, value, keyCode, callback) { |
||||
// keydown
|
||||
var e = BI.$.Event("keydown"); |
||||
e.keyCode = keyCode; |
||||
element.trigger(e); |
||||
|
||||
// input
|
||||
BI.isNotNull(value) && element.val(value); |
||||
var e1 = BI.$.Event("input"); |
||||
e1.originalEvent = {}; |
||||
e1.keyCode = keyCode; |
||||
element.trigger(e1); |
||||
|
||||
// keyup 至少等300ms后触发
|
||||
var e2 = BI.$.Event("keyup"); |
||||
e2.keyCode = keyCode; |
||||
element.trigger(e2); |
||||
BI.delay(function () { |
||||
callback(); |
||||
}, 300); |
||||
}; |
||||
|
||||
/** |
||||
* 模拟一次鼠标hover |
||||
*/ |
||||
function triggerMouseover(element, callback) { |
||||
// keydown
|
||||
var e = BI.$.Event("mouseover"); |
||||
element.trigger(e); |
||||
callback && callback(); |
||||
}; |
||||
|
||||
BI.Test = {}; |
||||
BI.extend(BI.Test, { |
||||
createWidget: function (widgetJson) { |
||||
var widget = BI.createWidget(BI.extend(widgetJson, { |
||||
root: true |
||||
})); |
||||
widget.element.appendTo("body"); |
||||
return widget; |
||||
}, |
||||
|
||||
/** |
||||
* 模拟一次输入框的keydown事件 |
||||
*/ |
||||
triggerKeyDown: function (element, value, keyCode, callback) { |
||||
// keydown
|
||||
var e = BI.$.Event("keydown"); |
||||
e.keyCode = keyCode; |
||||
element.trigger(e); |
||||
|
||||
// input
|
||||
BI.isNotNull(value) && element.val(value); |
||||
var e1 = BI.$.Event("input"); |
||||
e1.originalEvent = {}; |
||||
e1.keyCode = keyCode; |
||||
element.trigger(e1); |
||||
|
||||
// keyup 至少等300ms后触发
|
||||
var e2 = BI.$.Event("keyup"); |
||||
e2.keyCode = keyCode; |
||||
element.trigger(e2); |
||||
BI.delay(function () { |
||||
callback(); |
||||
}, 300); |
||||
}, |
||||
|
||||
/** |
||||
* 模拟一次鼠标hover |
||||
*/ |
||||
triggerMouseover: function (element, callback) { |
||||
// keydown
|
||||
var e = BI.$.Event("mouseover"); |
||||
element.trigger(e); |
||||
callback && callback(); |
||||
} |
||||
registeCloser, |
||||
sleep, |
||||
createWidget, |
||||
triggerKeyDown, |
||||
triggerMouseover |
||||
}) |
Loading…
Reference in new issue