From 93478a911f2e30ae6a36c469a88124783bc73cbf Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Sun, 6 Oct 2024 20:34:06 -0400 Subject: [PATCH] add Structure test 'toggle fullscreen mode' --- tests/unit/Structure.test.ts | 39 +++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/tests/unit/Structure.test.ts b/tests/unit/Structure.test.ts index 82b2d6b..f05d6d3 100644 --- a/tests/unit/Structure.test.ts +++ b/tests/unit/Structure.test.ts @@ -1,20 +1,23 @@ import { Structure } from '$lib' import { structures } from '$site' import { tick } from 'svelte' -import { describe, expect, test, vi } from 'vitest' +import { beforeEach, describe, expect, test, vi } from 'vitest' import { doc_query } from '.' const structure = structures[0] describe(`Structure`, () => { - test(`open control panel when clicking toggle button`, async () => { - const struct = new Structure({ + let struct: Structure + + beforeEach(() => { + struct = new Structure({ target: document.body, props: { structure }, }) + }) - const dialog = doc_query(`dialog`) - expect(dialog.open).toBe(false) + test(`open control panel when clicking toggle button`, async () => { + expect(struct.controls_open).toBe(false) doc_query(`button.controls-toggle`).click() await tick() @@ -42,4 +45,30 @@ describe(`Structure`, () => { // @ts-expect-error - function is mocked window.URL.createObjectURL.mockRestore() }) + + test(`toggle fullscreen mode`, async () => { + const requestFullscreenMock = vi.fn().mockResolvedValue(undefined) + const exitFullscreenMock = vi.fn() + + struct.wrapper = { requestFullscreen: requestFullscreenMock } + document.exitFullscreen = exitFullscreenMock + + await struct.toggle_fullscreen() + expect(requestFullscreenMock).toHaveBeenCalledOnce() + + // Simulate fullscreen mode + Object.defineProperty(document, `fullscreenElement`, { + value: struct.wrapper, + configurable: true, + }) + + await struct.toggle_fullscreen() + expect(exitFullscreenMock).toHaveBeenCalledOnce() + + // Reset fullscreenElement + Object.defineProperty(document, `fullscreenElement`, { + value: null, + configurable: true, + }) + }) })