Skip to content

Commit d9f4426

Browse files
authored
T1308277 - Fix: Vitest, the "window.getComputedStyle" error is not defined when running tests (#31988)
1 parent bdabe78 commit d9f4426

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

packages/devextreme/js/__internal/ui/themes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ function readThemeMarker(): string | null {
5050
let result: string;
5151

5252
try {
53+
if (!window?.getComputedStyle) {
54+
return null;
55+
}
5356
result = window.getComputedStyle(element.get(0)).fontFamily;
5457
if (!result) {
5558
return null;

packages/devextreme/testing/tests/DevExpress.ui/themes.tests.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,3 +825,39 @@ QUnit.module('initialized method', (hooks) => {
825825
});
826826
});
827827
});
828+
829+
QUnit.module('readThemeMarker error handling', () => {
830+
test('readThemeMarker returns null when getComputedStyle throws an error', function(assert) {
831+
const done = assert.async();
832+
const originalGetComputedStyle = window.getComputedStyle;
833+
window.getComputedStyle = undefined;
834+
835+
try {
836+
themes.resetTheme();
837+
const value = themes.current();
838+
assert.strictEqual(value, null, 'current() returns null on getComputedStyle being undefined');
839+
} finally {
840+
window.getComputedStyle = originalGetComputedStyle;
841+
done();
842+
}
843+
});
844+
845+
test('waitForThemeLoad resolves even if getComputedStyle continuously throws', function(assert) {
846+
const done = assert.async();
847+
const originalGetComputedStyle = window.getComputedStyle;
848+
window.getComputedStyle = undefined;
849+
850+
const TEST_TIMEOUT = 30;
851+
themes.resetTheme();
852+
themes.setDefaultTimeout(TEST_TIMEOUT);
853+
854+
themes.ready(() => {
855+
assert.strictEqual(themes.current(), null, 'theme remains null after timeout');
856+
window.getComputedStyle = originalGetComputedStyle;
857+
themes.setDefaultTimeout(defaultTimeout);
858+
done();
859+
});
860+
861+
themes.waitForThemeLoad('some.nonexistent.theme');
862+
});
863+
});

0 commit comments

Comments
 (0)