riot-web/test/theme-test.ts

118 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-09-28 12:42:40 +02:00
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { setTheme } from "../src/theme";
2022-12-12 12:24:14 +01:00
describe("theme", () => {
describe("setTheme", () => {
2022-09-28 12:42:40 +02:00
let lightTheme;
let darkTheme;
2022-09-28 15:25:16 +02:00
let spyQuerySelectorAll: jest.MockInstance<NodeListOf<Element>, [selectors: string]>;
2022-09-28 12:42:40 +02:00
beforeEach(() => {
const styles = [
{
attributes: {
2022-12-12 12:24:14 +01:00
"data-mx-theme": {
value: "light",
2022-09-28 12:42:40 +02:00
},
},
disabled: true,
2022-12-12 12:24:14 +01:00
href: "urlLight",
2022-09-28 12:42:40 +02:00
onload: () => void 0,
},
{
attributes: {
2022-12-12 12:24:14 +01:00
"data-mx-theme": {
value: "dark",
2022-09-28 12:42:40 +02:00
},
},
disabled: true,
2022-12-12 12:24:14 +01:00
href: "urlDark",
2022-09-28 12:42:40 +02:00
onload: () => void 0,
},
];
lightTheme = styles[0];
darkTheme = styles[1];
2022-12-12 12:24:14 +01:00
jest.spyOn(document.body, "style", "get").mockReturnValue([] as any);
spyQuerySelectorAll = jest.spyOn(document, "querySelectorAll").mockReturnValue(styles as any);
2022-09-28 12:42:40 +02:00
});
afterEach(() => {
jest.restoreAllMocks();
jest.useRealTimers();
});
2022-12-12 12:24:14 +01:00
it("should switch theme on onload call", async () => {
2022-09-28 12:42:40 +02:00
// When
2022-12-12 12:24:14 +01:00
await new Promise((resolve) => {
setTheme("light").then(resolve);
2022-09-28 12:42:40 +02:00
lightTheme.onload();
});
// Then
2022-12-12 12:24:14 +01:00
expect(spyQuerySelectorAll).toHaveBeenCalledWith("[data-mx-theme]");
2022-09-28 15:25:16 +02:00
expect(spyQuerySelectorAll).toBeCalledTimes(1);
2022-09-28 12:42:40 +02:00
expect(lightTheme.disabled).toBe(false);
expect(darkTheme.disabled).toBe(true);
});
2022-12-12 12:24:14 +01:00
it("should reject promise on onerror call", () => {
return expect(
new Promise((resolve) => {
setTheme("light").catch((e) => resolve(e));
lightTheme.onerror("call onerror");
}),
).resolves.toBe("call onerror");
2022-09-28 12:42:40 +02:00
});
2022-12-12 12:24:14 +01:00
it("should switch theme if CSS are preloaded", async () => {
2022-09-28 12:42:40 +02:00
// When
2022-12-12 12:24:14 +01:00
jest.spyOn(document, "styleSheets", "get").mockReturnValue([lightTheme] as any);
2022-09-28 12:42:40 +02:00
2022-12-12 12:24:14 +01:00
await setTheme("light");
2022-09-28 12:42:40 +02:00
// Then
expect(lightTheme.disabled).toBe(false);
expect(darkTheme.disabled).toBe(true);
});
2022-12-12 12:24:14 +01:00
it("should switch theme if CSS is loaded during pooling", async () => {
2022-09-28 12:42:40 +02:00
// When
jest.useFakeTimers();
2022-12-12 12:24:14 +01:00
await new Promise((resolve) => {
setTheme("light").then(resolve);
jest.spyOn(document, "styleSheets", "get").mockReturnValue([lightTheme] as any);
2022-09-28 12:42:40 +02:00
jest.advanceTimersByTime(200);
});
// Then
expect(lightTheme.disabled).toBe(false);
expect(darkTheme.disabled).toBe(true);
});
2022-12-12 12:24:14 +01:00
it("should reject promise if pooling maximum value is reached", () => {
2022-09-28 12:42:40 +02:00
jest.useFakeTimers();
2022-12-12 12:24:14 +01:00
return new Promise((resolve) => {
setTheme("light").catch(resolve);
2022-09-28 12:42:40 +02:00
jest.advanceTimersByTime(200 * 10);
});
});
});
});