Skip to content

Commit

Permalink
Also port common-features to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
volkanceylan committed Dec 20, 2024
1 parent a7a0e0e commit 7e5f110
Show file tree
Hide file tree
Showing 25 changed files with 136 additions and 176 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,10 @@ jobs:
- name: pnpm install
uses: ./.github/actions/pnpm-install

- name: sleekgrid build
run: pnpm build
working-directory: "./packages/sleekgrid"

- name: sleekgrid tests
run: pnpm test
working-directory: "./packages/sleekgrid"

- name: corelib build
run: pnpm build
working-directory: "./packages/corelib"

- name: corelib tests
run: pnpm test
working-directory: "./packages/corelib"
Expand Down
7 changes: 4 additions & 3 deletions common-features/src/demo.basicsamples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
"scripts": {
"build": "node ./tsbuild.js",
"build:watch": "node ./tsbuild.js --watch",
"jest": "node ../../../node_modules/jest/bin/jest.js",
"test": "node ./tsbuild.js && pnpm jest --coverage",
"tsc": "tsc"
"coverage": "pnpm run test --coverage",
"test": "pnpm build && pnpm vitest run",
"tsc": "tsc",
"vitest": "node ../../../node_modules/vitest/dist/cli.js"
},
"type": "module"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@ import { InlineActionGrid } from "../../Modules/Grids/InlineActionButtons/Inline
import { Fluent, confirmDialog } from "@serenity-is/corelib";
import { CustomerService } from "@serenity-is/demo.northwind";

const orderLoadEntitySpy = jest.fn();
const orderLoadEntitySpy = vi.fn();

jest.mock("@serenity-is/demo.northwind", () => {
const northwindActual = jest.requireActual("@serenity-is/demo.northwind");
vi.mock(import("@serenity-is/corelib"), async (importOriginal) => {
const actual = await importOriginal();
return {
...northwindActual,
OrderDialog: class extends northwindActual.OrderDialog {
...actual,
confirmDialog: vi.fn().mockImplementation((msg, onYes) => onYes())
}
});

vi.mock(import("@serenity-is/demo.northwind"), async (importOriginal) => {
const actual = await importOriginal();
return {
...actual,
OrderDialog: class extends actual.OrderDialog {
constructor(opt?: any) {
super(opt);
this.loadEntityAndOpenDialog = orderLoadEntitySpy;
}
}
} as any
}
});

jest.mock("@serenity-is/corelib", () => {
const corelibActual = jest.requireActual("@serenity-is/corelib");
return {
...corelibActual,
confirmDialog: jest.fn().mockImplementation((msg, onYes) => onYes())
}
});

beforeAll(() => {
mockAdmin();
Expand All @@ -33,7 +34,7 @@ beforeAll(() => {
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
Fluent(document.body).empty();
});

Expand All @@ -55,7 +56,7 @@ function createInlineActionGrid(): InlineActionGrid {
describe("Inline Action Buttons", () => {
it("should call edit item when clicking view-details action", () => {
const grid = createInlineActionGrid();
const editSpy = (grid as any).editItem = jest.fn();
const editSpy = (grid as any).editItem = vi.fn();

var actions = grid.element.findAll<HTMLAnchorElement>(".inline-action[data-action=view-details]");
expect(actions.length).toBe(2);
Expand Down Expand Up @@ -83,7 +84,7 @@ describe("Inline Action Buttons", () => {
var actions = grid.element.findAll<HTMLAnchorElement>(".inline-action[data-action=delete-row]");
expect(actions.length).toBe(2);

const deleteSpy = jest.spyOn(CustomerService, "Delete").mockImplementation();
const deleteSpy = vi.spyOn(CustomerService, "Delete").mockImplementation();

actions[1].click();
expect(confirmDialog).toHaveBeenCalledTimes(1);
Expand Down
2 changes: 1 addition & 1 deletion common-features/src/demo.basicsamples/test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"outDir": "../out/test",
"typeRoots": null,
"types": [
"jest"
"vitest/globals"
]
},
"include": [
Expand Down
5 changes: 5 additions & 0 deletions common-features/src/demo.basicsamples/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import vitestDefaults from "test-utils/vitest-defaults";

export default {
...vitestDefaults({})
}
5 changes: 0 additions & 5 deletions common-features/src/demo.northwind/jest.config.js

This file was deleted.

7 changes: 4 additions & 3 deletions common-features/src/demo.northwind/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
"scripts": {
"build": "node ./tsbuild.js",
"build:watch": "node ./tsbuild.js --watch",
"coverage": "pnpm run test --coverage",
"dts": "pnpm dts-bundle-generator Modules/index.ts -o dist/index.d.ts --no-banner --disable-symlinks-following --inline-declare-global",
"jest": "node ../../../node_modules/jest/bin/jest.js",
"test": "node ./tsbuild.js && pnpm jest --coverage",
"tsc": "tsc"
"test": "pnpm build && pnpm vitest run",
"tsc": "tsc",
"vitest": "node ../../../node_modules/vitest/dist/cli.js"
},
"type": "module",
"types": "dist/index.d.ts"
Expand Down
2 changes: 1 addition & 1 deletion common-features/src/demo.northwind/test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"outDir": "../out/test",
"typeRoots": null,
"types": [
"jest"
"vitest/globals"
]
},
"include": [
Expand Down
5 changes: 5 additions & 0 deletions common-features/src/demo.northwind/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import vitestDefaults from "test-utils/vitest-defaults";

export default {
...vitestDefaults({})
}
5 changes: 0 additions & 5 deletions common-features/src/extensions/jest.config.js

This file was deleted.

7 changes: 4 additions & 3 deletions common-features/src/extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"scripts": {
"build": "node ./tsbuild.js",
"build:watch": "node ./tsbuild.js --watch",
"coverage": "pnpm run test --coverage",
"dts": "pnpm dts-bundle-generator Modules/index.ts -o dist/index.d.ts --no-banner --disable-symlinks-following --inline-declare-global",
"jest": "node ../../../node_modules/jest/bin/jest.js",
"test": "node ./tsbuild.js && pnpm jest --coverage",
"tsc": "tsc"
"test": "pnpm build && pnpm vitest run",
"tsc": "tsc",
"vitest": "node ../../../node_modules/vitest/dist/cli.js"
},
"main": "dist/index.js",
"private": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { GridEditorBase } from "../../Modules/GridEditor/GridEditorBase";
import { mockFetch, unmockFetch } from "test-utils";
import { GridEditorBase } from "../../Modules/GridEditor/GridEditorBase";

beforeAll(() => {
beforeEach(() => {
vi.useFakeTimers();
mockFetch({ "*": () => ({}) });
});

afterEach(() => {
vi.clearAllTimers();
unmockFetch();
});

Expand Down
2 changes: 1 addition & 1 deletion common-features/src/extensions/test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"outDir": "../out/test",
"typeRoots": null,
"types": [
"jest"
"vitest/globals"
]
},
"include": [
Expand Down
14 changes: 7 additions & 7 deletions common-features/src/extensions/test/widgets/promptdialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ function clickOkButton(dlg: PromptDialog) {
}

beforeEach(() => {
jest.useFakeTimers();
vi.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
jest.restoreAllMocks();
vi.useRealTimers();
vi.restoreAllMocks();
});

describe("PromptDialog", () => {
Expand All @@ -33,11 +33,11 @@ describe("PromptDialog", () => {
}
});
try {
const closeSpy = jest.spyOn(Dialog.prototype, "close");
const closeSpy = vi.spyOn(Dialog.prototype, "close");
dlg.dialogOpen();
expect(closeSpy).not.toHaveBeenCalled();
clickOkButton(dlg);
await jest.runOnlyPendingTimersAsync();
await vi.runOnlyPendingTimersAsync();
expect(closeSpy).toHaveBeenCalledTimes(1);
}
finally {
Expand All @@ -53,11 +53,11 @@ describe("PromptDialog", () => {
}
});
try {
const closeSpy = jest.spyOn(Dialog.prototype, "close");
const closeSpy = vi.spyOn(Dialog.prototype, "close");
dlg.dialogOpen();
expect(closeSpy).not.toHaveBeenCalled();
clickOkButton(dlg);
await jest.runOnlyPendingTimersAsync();
await vi.runOnlyPendingTimersAsync();
expect(closeSpy).not.toHaveBeenCalled();
}
finally {
Expand Down
5 changes: 5 additions & 0 deletions common-features/src/extensions/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import vitestDefaults from "test-utils/vitest-defaults";

export default {
...vitestDefaults({})
}
4 changes: 2 additions & 2 deletions packages/corelib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
"repository": "https://github.com/serenity-is/serenity/packages/corelib",
"scripts": {
"build": "node build/build",
"coverage": "vitest run --coverage",
"coverage": "pnpm run test --coverage",
"doc": "typedoc --plugin typedoc-plugin-markdown --exclude **/lib/**/* --excludePrivate --readme none --githubPages false --hidePageHeader --sourceLinkTemplate https://github.com/serenity-is/serenity/blob/master/{path}#L{line} --out /serenity-is/SerenityIs/SerenityIs.Web/Docs/api/js/corelib --json ./out/typedoc.json src",
"dts": "tsc --build && dts-bundle-generator --project ./src/tsconfig.dts.json --disable-symlinks-following --export-referenced-types=false --inline-declare-externals --no-banner out/index.d.ts -o dist/index.d.ts",
"prepublishOnly": "node build/build && pnpm coverage && pnpm dts",
"test": "vitest run",
"test": "pnpm build && vitest run",
"tsc": "tsc --build"
},
"type": "module"
Expand Down
41 changes: 41 additions & 0 deletions packages/corelib/src/base/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,45 @@ export function getReturnUrl(opt?: {
returnUrl = Config.defaultReturnUrl(opt?.purpose);

return returnUrl;
}

export function cssEscape(selector: string) {
if (typeof CSS !== 'undefined' && typeof CSS.escape === "function")
return CSS.escape(selector);

var string = String(selector);
var length = string.length;
var index = -1;
var codeUnit: number;
var result = '';
var firstCodeUnit = string.charCodeAt(0);

if (length == 1 && firstCodeUnit == 0x002D)
return '\\' + string;

while (++index < length) {
codeUnit = string.charCodeAt(index);
if (codeUnit == 0x0000) {
result += '\uFFFD';
continue;
}

if ((codeUnit >= 0x0001 && codeUnit <= 0x001F) || codeUnit == 0x007F ||
(index == 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
(index == 1 && codeUnit >= 0x0030 && codeUnit <= 0x0039 && firstCodeUnit == 0x002D)
) {
result += '\\' + codeUnit.toString(16) + ' ';
continue;
}

if (codeUnit >= 0x0080 || codeUnit == 0x002D || codeUnit == 0x005F || codeUnit >= 0x0030 && codeUnit <= 0x0039 ||
codeUnit >= 0x0041 && codeUnit <= 0x005A || codeUnit >= 0x0061 && codeUnit <= 0x007A
) {
result += string.charAt(index);
continue;
}

result += '\\' + string.charAt(index);
}
return result;
}
13 changes: 7 additions & 6 deletions packages/corelib/src/base/validator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { Config } from "./config";
import { Fluent } from "./fluent";
import { parseDate, parseDecimal, parseInteger, stringFormat } from "./formatting";
import { cssEscape } from "./html";
import { localText } from "./localtext";
import { isArrayLike } from "./system";

Expand Down Expand Up @@ -700,7 +701,7 @@ export class Validator {
if (element instanceof HTMLInputElement) {
if (element.type === "radio" || element.type === "checkbox") {
if (element.name && element.form) {
var values = Array.from(element.form.querySelectorAll<HTMLInputElement>(`input[name=${CSS.escape(element.name)}]`))
var values = Array.from(element.form.querySelectorAll<HTMLInputElement>(`input[name=${cssEscape(element.name)}]`))
.map(el => el.checked ? null : el.value);

if (values.length > 1)
Expand Down Expand Up @@ -1297,13 +1298,13 @@ export class Validator {

// If the element is not a child of an associated label, then it's necessary
// to explicitly apply aria-describedby
} else if (!error.closest("label[for='" + CSS.escape(elementID) + "']")) {
} else if (!error.closest("label[for='" + cssEscape(elementID) + "']")) {
var errorID = error.getAttribute("id");

// Respect existing non-error aria-describedby
if (!describedBy) {
describedBy = errorID;
} else if (!describedBy.match(new RegExp("\\b" + CSS.escape(errorID) + "\\b"))) {
} else if (!describedBy.match(new RegExp("\\b" + cssEscape(errorID) + "\\b"))) {

// Add to end of list if not already present
describedBy += " " + errorID;
Expand Down Expand Up @@ -1336,13 +1337,13 @@ export class Validator {
}

errorsFor(element: ValidatableElement) {
var name = CSS.escape(this.idOrName(element)),
var name = cssEscape(this.idOrName(element)),
describer = element.getAttribute("aria-describedby"),
selector = "label[for='" + name + "'], label[for='" + name + "'] *";

// 'aria-describedby' should directly reference the error element
if (describer) {
selector = selector + ", #" + CSS.escape(describer)
selector = selector + ", #" + cssEscape(describer)
.replace(/\s+/g, ", #");
}

Expand All @@ -1367,7 +1368,7 @@ export class Validator {
}

findByName(name: string): ValidatableElement[] {
return Array.from(this.currentForm.querySelectorAll<ValidatableElement>("[name='" + CSS.escape(name) + "']"));
return Array.from(this.currentForm.querySelectorAll<ValidatableElement>("[name='" + cssEscape(name) + "']"));
}

dependTypes = {
Expand Down
Loading

0 comments on commit 7e5f110

Please sign in to comment.