Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
volkanceylan committed Dec 20, 2024
1 parent 8331258 commit a7a0e0e
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 20 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ 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
1 change: 0 additions & 1 deletion packages/corelib/src/ui/editors/editorwidget.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getElementReadOnly, setElementReadOnly } from "../../base";
import { Decorators } from "../../types/decorators";
import { Widget, WidgetProps } from "../widgets/widget";
import { EditorUtils } from "./editorutils";

export type EditorProps<T> = WidgetProps<T> & {
initialValue?: any;
Expand Down
5 changes: 4 additions & 1 deletion packages/corelib/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"extends": "./src/tsconfig.json",
"compilerOptions": {
"composite": false,
"emitDeclarationOnly": false,
"noEmit": true,
"rootDir": "./src",
"tsBuildInfoFile": "./out/tsconfig.test.tsbuildinfo",
"types": [
"vitest/globals"
],
"outDir": "./out/test",
"noImplicitAny": false
},
"include": [
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/entitydialogutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class EntityDialogWrapper<TDialog extends EntityDialog<any, any>> {
throw "Delete button not found in the dialog!";
if (button.hasClass("disabled"))
throw "Delete button is disabled!";
const spy = jest.spyOn(window, "confirm").mockReturnValue(true);
const spy = ((globalThis as any).jest || (globalThis as any).vitest).spyOn(window, "confirm").mockReturnValue(true);
button.click();
spy.mockRestore();
return waitForAjaxRequests();
Expand Down
8 changes: 5 additions & 3 deletions packages/test-utils/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ScriptData, scriptDataHooks } from "@serenity-is/corelib";
import { inject } from "vitest";

let orgFetchScriptData: any;

Expand All @@ -10,7 +11,8 @@ export function mockDynamicData() {
orgFetchScriptData = scriptDataHooks.fetchScriptData ?? null;
scriptDataHooks.fetchScriptData = <TData>(name: string) => {
try {
return jest.requireActual("dynamic-data/" + name + ".json");
// @ts-ignore
return JSON.parse(inject("dynamic-data/" + name.split("?")[0] + ".json") || "null");
}
catch (e) {
console.warn("Failed to load mock dynamic data for: " + name);
Expand Down Expand Up @@ -38,13 +40,13 @@ export type MockFetchInfo = {
}

var orgFetch: any;
var fetchSpy: jest.Mock<Promise<any>, [url: string, init: RequestInit], any> & { requests: MockFetchInfo[] }
var fetchSpy: any;
var fetchMap: Record<string, (info: MockFetchInfo) => any> = {};

export function mockFetch(map?: { [urlOrService: string]: ((info: MockFetchInfo) => any) }) {
if (!fetchSpy) {
orgFetch = (window as any).fetch;
fetchSpy = (window as any).fetch = jest.fn(async (url: string, init: RequestInit) => {
fetchSpy = (window as any).fetch = ((globalThis as any).vitest !== "undefined" ? (globalThis as any).vitest.fn : (globalThis as any).jest.fn as any)(async (url: string, init: RequestInit) => {
var callback = fetchMap[url] ?? fetchMap["*"];
if (!callback) {
console.error(`Mock fetch is not configured for URL: (${url})!`);
Expand Down
8 changes: 4 additions & 4 deletions packages/test-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"compilerOptions": {
"noEmit": true,
"target": "ES2015",
"module": "ESNext",
"moduleResolution": "bundler",
"lib": [
"es2015",
"dom"
],
"outDir": "./out",
"types": [
"jest"
]
"outDir": "./out"
},
"include": [
"."
Expand Down
4 changes: 4 additions & 0 deletions packages/test-utils/vitest-defaults.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function vitestDefaults(opt?: {
projectRoot?: string,
dynamicData?: boolean,
}): any;
73 changes: 73 additions & 0 deletions packages/test-utils/vitest-defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { execSync } from "child_process";
import { existsSync, readdirSync, readFileSync } from "fs";
import { basename, join, resolve } from "path";
import { fileURLToPath } from 'url';
import { defineConfig, } from "vitest/config";

const testUtils = resolve(join(fileURLToPath(new URL('.', import.meta.url)), './'));
const serenityRoot = resolve(join(testUtils, "../../"));

export default (opt) => {

if ((opt?.dynamicData ?? true) &&
!existsSync(`${testUtils}/dynamic-data/Columns.Administration.Language.json`) &&
!existsSync(resolve(`./dynamic-data/Columns.Administration.Language.json`))) {
if (resolve("./").indexOf('Serene.Web') >= 0 || !tryProject(`${serenityRoot}/..`, "StartSharp"))
tryProject(`${serenityRoot}/serene`, "Serene");
}

const provide = {};
if (opt?.dynamicData ?? true) {
for (var folder of [join(testUtils, "dynamic-data"), join(opt?.projectRoot ?? resolve("./"), "dynamic-data")]) {
if (existsSync(folder)) {
for (var file of readdirSync(folder)) {
if (file.endsWith(".json")) {
provide["dynamic-data/" + basename(file)] = readFileSync(join(folder, file), "utf8");
}
}
}
}
}

return defineConfig({
test: {
environment: "jsdom",
globals: true,
alias: [
{ find: "@serenity-is/corelib", replacement: `${testUtils}/../corelib/dist/index.js` },
{ find: "jsx-dom/min/jsx-dev-runtime", replacement: "jsx-dom/jsx-runtime.js" },
{ find: "jsx-dom/jsx-dev-runtime", replacement: "jsx-dom/jsx-runtime.js" }
],
provide,
setupFiles: [
"test-utils/vitest-setup.js"
]
}
});
}

function tryProject(root, name) {
const target = "net8.0";
const folder = `${root}/src/${name}.Web`;
const csproj = `${folder}/${name}.Web.csproj`;
if (!existsSync(csproj))
return false;

const debugDll = `${folder}/bin/Debug/${target}/${name}.Web.dll`;
const releaseDll = `${folder}/bin/Release/${target}/${name}.Web.dll`;

let debugExists = existsSync(debugDll);
let releaseExists = !debugExists && existsSync(releaseDll);
if (!debugExists && !releaseExists)
execSync(`dotnet build ${csproj}`, { timeout: 60000 });

debugExists = existsSync(debugDll);
releaseExists = !debugExists && existsSync(releaseDll);
if (debugExists || releaseExists)
execSync(`dotnet ${debugExists ? debugDll : releaseDll} dynamic-data`, {
timeout: 60000,
cwd: resolve(".").indexOf(name + ".Web") >= 0 ? resolve("./") : testUtils
});

return true;
}
90 changes: 90 additions & 0 deletions packages/test-utils/vitest-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
function addCSSEscape(window) {
if ((!window.CSS || !window.CSS.escape)) {
// https://drafts.csswg.org/cssom/#serialize-an-identifier
var cssEscape = function (value) {
if (arguments.length == 0) {
throw new TypeError('`CSS.escape` requires an argument.');
}
var string = String(value);
var length = string.length;
var index = -1;
var codeUnit;
var result = '';
var firstCodeUnit = string.charCodeAt(0);

if (
// If the character is the first character and is a `-` (U+002D), and
// there is no second character, […]
length == 1 &&
firstCodeUnit == 0x002D
) {
return '\\' + string;
}

while (++index < length) {
codeUnit = string.charCodeAt(index);
// Note: there’s no need to special-case astral symbols, surrogate
// pairs, or lone surrogates.

// If the character is NULL (U+0000), then the REPLACEMENT CHARACTER
// (U+FFFD).
if (codeUnit == 0x0000) {
result += '\uFFFD';
continue;
}

if (
// If the character is in the range [\1-\1F] (U+0001 to U+001F) or is
// U+007F, […]
(codeUnit >= 0x0001 && codeUnit <= 0x001F) || codeUnit == 0x007F ||
// If the character is the first character and is in the range [0-9]
// (U+0030 to U+0039), […]
(index == 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
// If the character is the second character and is in the range [0-9]
// (U+0030 to U+0039) and the first character is a `-` (U+002D), […]
(
index == 1 &&
codeUnit >= 0x0030 && codeUnit <= 0x0039 &&
firstCodeUnit == 0x002D
)
) {
// https://drafts.csswg.org/cssom/#escape-a-character-as-code-point
result += '\\' + codeUnit.toString(16) + ' ';
continue;
}

// If the character is not handled by one of the above rules and is
// greater than or equal to U+0080, is `-` (U+002D) or `_` (U+005F), or
// is in one of the ranges [0-9] (U+0030 to U+0039), [A-Z] (U+0041 to
// U+005A), or [a-z] (U+0061 to U+007A), […]
if (
codeUnit >= 0x0080 ||
codeUnit == 0x002D ||
codeUnit == 0x005F ||
codeUnit >= 0x0030 && codeUnit <= 0x0039 ||
codeUnit >= 0x0041 && codeUnit <= 0x005A ||
codeUnit >= 0x0061 && codeUnit <= 0x007A
) {
// the character itself
result += string.charAt(index);
continue;
}

// Otherwise, the escaped character.
// https://drafts.csswg.org/cssom/#escape-a-character
result += '\\' + string.charAt(index);
}
return result;
};

if (!window.CSS) {
window.CSS = {};
}

window.CSS.escape = cssEscape;
}
}

addCSSEscape(window);

export {}
5 changes: 0 additions & 5 deletions serene/src/Serene.Web/jest.config.js

This file was deleted.

6 changes: 3 additions & 3 deletions serene/src/Serene.Web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
},
"scripts": {
"build": "npm run tsbuild",
"jest": "node ../../../node_modules/jest/bin/jest.js",
"preinstall": "dotnet build -target:RestoreNodeTypes",
"test": "node ./tsbuild.js && npm run jest --coverage",
"test": "node ./tsbuild.js && npm run vitest run --coverage",
"tsbuild": "node ./tsbuild.js",
"tsbuild:watch": "node ./tsbuild.js --watch"
"tsbuild:watch": "node ./tsbuild.js --watch",
"vitest": "node ../../../node_modules/vitest/dist/cli.js"
},
"private": true,
"type": "module"
Expand Down
5 changes: 3 additions & 2 deletions serene/src/Serene.Web/test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
"outDir": "../out/test",
"typeRoots": [
"../node_modules/@types",
"../../../../node_modules/@types"
"../../../../node_modules/@types",
"../../../../node_modules",
],
"types": [
"jest"
"vitest/globals"
]
},
"include": [
Expand Down
5 changes: 5 additions & 0 deletions serene/src/Serene.Web/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({})
}

0 comments on commit a7a0e0e

Please sign in to comment.