element-web/test/theme-test.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-09-28 10:42:40 +00: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";
describe("theme", () => {
describe("setTheme", () => {
let lightTheme: HTMLStyleElement;
let darkTheme: HTMLStyleElement;
2022-09-28 10:42:40 +00:00
2022-09-28 13:25:16 +00:00
let spyQuerySelectorAll: jest.MockInstance<NodeListOf<Element>, [selectors: string]>;
2022-09-28 10:42:40 +00:00
beforeEach(() => {
const styles = [
{
dataset: {
mxTheme: "light",
2022-09-28 10:42:40 +00:00
},
disabled: true,
href: "urlLight",
onload: (): void => void 0,
} as unknown as HTMLStyleElement,
2022-09-28 10:42:40 +00:00
{
dataset: {
mxTheme: "dark",
2022-09-28 10:42:40 +00:00
},
disabled: true,
href: "urlDark",
onload: (): void => void 0,
} as unknown as HTMLStyleElement,
2022-09-28 10:42:40 +00:00
];
lightTheme = styles[0];
darkTheme = styles[1];
jest.spyOn(document.body, "style", "get").mockReturnValue([] as any);
2022-09-28 13:25:16 +00:00
spyQuerySelectorAll = jest.spyOn(document, "querySelectorAll").mockReturnValue(styles as any);
2022-09-28 10:42:40 +00:00
});
afterEach(() => {
jest.restoreAllMocks();
jest.useRealTimers();
});
it("should switch theme on onload call", async () => {
// When
await new Promise((resolve) => {
setTheme("light").then(resolve);
lightTheme.onload!({} as Event);
2022-09-28 10:42:40 +00:00
});
// Then
2022-09-28 13:25:16 +00:00
expect(spyQuerySelectorAll).toHaveBeenCalledWith("[data-mx-theme]");
expect(spyQuerySelectorAll).toBeCalledTimes(1);
2022-09-28 10:42:40 +00:00
expect(lightTheme.disabled).toBe(false);
expect(darkTheme.disabled).toBe(true);
});
it("should reject promise on onerror call", () => {
return expect(
new Promise((resolve) => {
setTheme("light").catch((e) => resolve(e));
lightTheme.onerror!("call onerror");
2022-09-28 10:42:40 +00:00
}),
).resolves.toBe("call onerror");
});
it("should switch theme if CSS are preloaded", async () => {
// When
jest.spyOn(document, "styleSheets", "get").mockReturnValue([lightTheme] as any);
await setTheme("light");
// Then
expect(lightTheme.disabled).toBe(false);
expect(darkTheme.disabled).toBe(true);
});
it("should switch theme if CSS is loaded during pooling", async () => {
// When
jest.useFakeTimers();
await new Promise((resolve) => {
setTheme("light").then(resolve);
jest.spyOn(document, "styleSheets", "get").mockReturnValue([lightTheme] as any);
jest.advanceTimersByTime(200);
});
// Then
expect(lightTheme.disabled).toBe(false);
expect(darkTheme.disabled).toBe(true);
});
it("should reject promise if pooling maximum value is reached", () => {
jest.useFakeTimers();
return new Promise((resolve) => {
setTheme("light").catch(resolve);
jest.advanceTimersByTime(200 * 10);
});
});
});
});