Skip to content

Commit 7891203

Browse files
author
OlgaLarina
committed
use getComputedCssVariableValues - update unit tests
1 parent 89e1045 commit 7891203

4 files changed

Lines changed: 41 additions & 27 deletions

File tree

packages/survey-creator-core/tests/creator-theme/register-creator-theme.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ test("Creator theme: update editors after theme switching", (): any => {
4242

4343
expect(themeName.value).toEqual("default");
4444
expect(colorPalette.value).toEqual("light");
45-
expect(surfaceBackgroundColor.value).toEqual("#EDF9F7");
46-
expect(primaryBackgroundColor.value).toEqual("#19B394");
45+
expect(surfaceBackgroundColor.value).toEqual("rgba(237, 249, 247, 1)");
46+
expect(primaryBackgroundColor.value).toEqual("rgba(25, 179, 148, 1)");
4747

4848
themeName.value = "contrast";
4949
expect(themeName.value).toEqual("contrast");
@@ -54,8 +54,8 @@ test("Creator theme: update editors after theme switching", (): any => {
5454
themeName.value = "default";
5555
expect(themeName.value).toEqual("default");
5656
expect(colorPalette.value).toEqual("light");
57-
expect(surfaceBackgroundColor.value).toEqual("#EDF9F7");
58-
expect(primaryBackgroundColor.value).toEqual("#19B394");
57+
expect(surfaceBackgroundColor.value).toEqual("rgba(237, 249, 247, 1)");
58+
expect(primaryBackgroundColor.value).toEqual("rgba(25, 179, 148, 1)");
5959
});
6060

6161
test("Creator theme: update editors after palette switching", (): any => {
@@ -77,6 +77,6 @@ test("Creator theme: update editors after palette switching", (): any => {
7777
colorPalette.value = "light";
7878
expect(themeName.value).toEqual("default");
7979
expect(colorPalette.value).toEqual("light");
80-
expect(surfaceBackgroundColor.value).toEqual("#EDF9F7");
81-
expect(primaryBackgroundColor.value).toEqual("#19B394");
80+
expect(surfaceBackgroundColor.value).toEqual("rgba(237, 249, 247, 1)");
81+
expect(primaryBackgroundColor.value).toEqual("rgba(25, 179, 148, 1)");
8282
});

packages/survey-creator-core/tests/tabs/theme-header-model.tests.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ export * from "../../src/components/tabs/theme-custom-questions/shadow-effects";
1414
export * from "../../src/property-grid/theme-settings";
1515
export * from "../../src/property-grid/header-settings";
1616
import { ContrastLight, DefaultDark, DefaultLight } from "./test-themes";
17-
import { mockGetRGBaColorIdentity, restoreGetRGBaColorMock } from "./theme-test-mocks";
17+
import { mockDomWindowGetComputedStyleFromInlineStyles, mockGetRGBaColorIdentity, restoreGetRGBaColorMock } from "./theme-test-mocks";
18+
import type { MockInstance } from "vitest";
1819

1920
import SurveyThemes from "survey-core/themes";
2021
registerSurveyTheme(SurveyThemes);
2122

2223
const cssVariables = DefaultTheme.cssVariables;
24+
let computedStyleSpy: MockInstance | undefined;
2325
beforeEach(() => {
2426
mockGetRGBaColorIdentity();
27+
computedStyleSpy = mockDomWindowGetComputedStyleFromInlineStyles();
2528

2629
Themes["default-light"] = DefaultLight;
2730
Themes["contrast-light"] = ContrastLight;
@@ -31,6 +34,8 @@ beforeEach(() => {
3134
});
3235

3336
afterEach(() => {
37+
computedStyleSpy?.mockRestore();
38+
computedStyleSpy = undefined;
3439
restoreGetRGBaColorMock();
3540
});
3641

packages/survey-creator-core/tests/tabs/theme-model.tests.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { ThemeModel, getThemeChanges } from "../../src/components/tabs/theme-mod
1111
import { registerSurveyTheme } from "../../src/components/tabs/theme-model";
1212
import SurveyThemes from "survey-core/themes";
1313
import { ContrastLight, DefaultDark, DefaultLight } from "./test-themes";
14-
import { mockGetRGBaColorIdentity, restoreGetRGBaColorMock } from "./theme-test-mocks";
14+
import { mockDomWindowGetComputedStyleFromInlineStyles, mockGetRGBaColorIdentity, restoreGetRGBaColorMock } from "./theme-test-mocks";
15+
import type { MockInstance } from "vitest";
1516
registerSurveyTheme(SurveyThemes);
1617
import "survey-core/survey.i18n";
1718

@@ -76,16 +77,19 @@ const themeFromFile = {
7677
};
7778

7879
const cssVariables = DefaultTheme.cssVariables;
80+
let getComputedStyleMock: MockInstance | undefined;
7981
beforeEach(() => {
8082
mockGetRGBaColorIdentity();
83+
getComputedStyleMock = mockDomWindowGetComputedStyleFromInlineStyles();
8184
Themes["default-light"] = DefaultLight;
8285
Themes["contrast-light"] = ContrastLight;
8386
Themes["default-dark"] = DefaultDark;
8487
ThemeModel.DefaultTheme = Themes["default-light"];
85-
DefaultTheme.cssVariables = {} as any;
8688
});
8789

8890
afterEach(() => {
91+
getComputedStyleMock?.mockRestore();
92+
getComputedStyleMock = undefined;
8993
restoreGetRGBaColorMock();
9094
DefaultTheme.cssVariables = cssVariables;
9195
});

packages/survey-creator-core/tests/tabs/theme-test-mocks.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DomWindowHelper } from "survey-core";
1+
import { DomDocumentHelper } from "survey-core";
22
import { vi, type MockInstance } from "vitest";
33
import * as colorUtils from "../../src/utils/color-utils";
44

@@ -26,23 +26,28 @@ const defaultComputedCssVariables: Record<string, string> = {
2626

2727
export function mockDomWindowGetComputedStyleFromInlineStyles(values: Record<string, string> = {}): MockInstance {
2828
const mergedDefaults = { ...defaultComputedCssVariables, ...values };
29-
return vi.spyOn(DomWindowHelper, "getWindow").mockReturnValue({
30-
...window,
31-
getComputedStyle: (el: any) => {
32-
const style = el?.style;
33-
return {
34-
getPropertyValue: (property: string) => {
35-
if (style) {
36-
const v = style.getPropertyValue?.(property);
37-
if (typeof v === "string" && v.length > 0) return v;
38-
// Fallback for non-custom properties in JSDOM mocks
39-
const v2 = style[property];
40-
if (typeof v2 === "string" && v2.length > 0) return v2;
29+
return vi.spyOn(DomDocumentHelper, "getComputedStyle").mockImplementation((el: any) => {
30+
const style = el?.style;
31+
return {
32+
getPropertyValue: (property: string) => {
33+
if (style) {
34+
const v = style.getPropertyValue?.(property);
35+
if (typeof v === "string" && v.length > 0) {
36+
const varRefMatch = v.match(/^var\((--[^)]+)\)$/);
37+
if (varRefMatch) {
38+
const varName = varRefMatch[1];
39+
const cssVarValue = style.getPropertyValue?.(varName) || mergedDefaults[varName];
40+
return cssVarValue || "";
41+
}
42+
return v;
4143
}
42-
if (mergedDefaults[property] !== undefined) return mergedDefaults[property];
43-
return "";
44+
// Fallback for non-custom properties in JSDOM mocks
45+
const v2 = style[property];
46+
if (typeof v2 === "string" && v2.length > 0) return v2;
4447
}
45-
} as any;
46-
}
47-
} as any);
48+
if (mergedDefaults[property] !== undefined) return mergedDefaults[property];
49+
return "";
50+
}
51+
} as any;
52+
});
4853
}

0 commit comments

Comments
 (0)