diff --git a/packages/demo/i18n/i18n.cn.js b/packages/demo/i18n/i18n.cn.js index 879cf1c49..b0ef4f39b 100644 --- a/packages/demo/i18n/i18n.cn.js +++ b/packages/demo/i18n/i18n.cn.js @@ -1,4 +1,4 @@ -window.BI = {}; +window.BI = window.BI || {}; window.BI.i18n = { 'BI-Multi_Date_Quarter_End': '季度末', 'BI-Multi_Date_Month_Begin': '月初', diff --git a/packages/fineui/jest.config.ts b/packages/fineui/jest.config.ts new file mode 100644 index 000000000..d3b870b84 --- /dev/null +++ b/packages/fineui/jest.config.ts @@ -0,0 +1,51 @@ +import type { Config } from "jest"; +// import webpackConfig from "./webpack/webpack.dev"; + +export default { + // testEnvironment: "/test/testEnvironmentJsdom.ts", + testEnvironment: "jsdom", + testEnvironmentOptions: { + // html: fs.readFileSync(path.join(__dirname, "test/index.html")), + }, + testMatch: ["/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: [ + "/src/bundle.js", + "/src/core/platform/web/config.js", + "/../demo/i18n/i18n.cn.js", + "/test/utils.js", + "/test/prepare.js", + ], + moduleNameMapper: { + "@/(.*)$": "/src/$1", + }, +} as Config; diff --git a/packages/fineui/src/widget/searchmultitextvaluecombo/__test__/multitextvalue.combo.search.test.js b/packages/fineui/src/widget/searchmultitextvaluecombo/__test__/multitextvalue.combo.search.test.js index 7474e3f39..3db9ee7d1 100644 --- a/packages/fineui/src/widget/searchmultitextvaluecombo/__test__/multitextvalue.combo.search.test.js +++ b/packages/fineui/src/widget/searchmultitextvaluecombo/__test__/multitextvalue.combo.search.test.js @@ -69,33 +69,32 @@ describe("multitextvaluecombo", () => { /** * test_author_windy **/ - it("点选选值", done => { + it("点选选值", async () => { var widget = BI.Test.createWidget({ type: "bi.search_multi_text_value_combo", width: 220, items: items }); + // 组件创建需要时间... + await BI.Test.sleep(300); widget.element.find(".bi-multi-select-trigger").click(); + await BI.Test.sleep(300); // 为什么要delay 300呢,因为按钮有debounce - BI.delay(function () { - // 点选1、2、3 - BI.each(itemSelectorGetter([1,2,3]), function (idx, selector) { - widget.element.find(selector).click(); - }); - // 点全选 - widget.element.find(".bi-multi-select-popup-view .bi-label:contains(全选)").click(); - // 取消勾选1、2、3 - BI.delay(function () { - BI.each(itemSelectorGetter([1,2,3]), function (idx, selector) { - widget.element.find(selector).click(); - }); - var value = widget.getValue(); - expect(value.type).toBe(2); - expect(value.value).toEqual([0, 1, 2]); - widget.destroy(); - done(); - }, 300); - }, 300); + // 点选1、2、3 + BI.each(itemSelectorGetter([1,2,3]), function (idx, selector) { + widget.element.find(selector).click(); + }); + // 点全选 + widget.element.find(".bi-multi-select-popup-view .bi-label:contains(全选)").click(); + await BI.Test.sleep(300); + // 取消勾选1、2、3 + BI.each(itemSelectorGetter([1,2,3]), function (idx, selector) { + widget.element.find(selector).click(); + }); + await BI.Test.sleep(300); + var value = widget.getValue(); + expect(value.type).toBe(2); + expect(value.value).toEqual([0, 1, 2]); }); /** diff --git a/packages/fineui/test/utils.js b/packages/fineui/test/utils.js index 0aa209d0a..5ce97a378 100644 --- a/packages/fineui/test/utils.js +++ b/packages/fineui/test/utils.js @@ -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 }) \ No newline at end of file diff --git a/packages/fineui/tsconfig.json b/packages/fineui/tsconfig.json index 26425a5c4..bf8eef10f 100644 --- a/packages/fineui/tsconfig.json +++ b/packages/fineui/tsconfig.json @@ -22,7 +22,7 @@ "noFallthroughCasesInSwitch": true, "emitDeclarationOnly": true, "paths": { - "@": ["src"] + "@/*": ["src/*"] } }, "include": ["typescript/*.ts", "typescript/**/*.ts", "types/*.d.ts", "src/*.js", "src/**/*.js"],