From 88317e7606074ec6b8098bb72326ef1b63fb5ed3 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Thu, 4 Sep 2025 14:02:08 -0600 Subject: [PATCH] Allow testing page in both light and dark mode in a single test --- src/Api/PendingAwaitablePage.php | 11 ++++++ tests/Browser/Visit/SingleUrl.php | 66 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/src/Api/PendingAwaitablePage.php b/src/Api/PendingAwaitablePage.php index f6f09806..8d48214e 100644 --- a/src/Api/PendingAwaitablePage.php +++ b/src/Api/PendingAwaitablePage.php @@ -72,6 +72,17 @@ public function inLightMode(): self ]); } + /** + * Allows you to run all tests in both color modes. + */ + public function inLightAndDarkMode(): ArrayablePendingAwaitablePage + { + return new ArrayablePendingAwaitablePage([ + $this->inLightMode(), + $this->inDarkMode(), + ]); + } + /** * Allows you to set a different physical location for the browser. */ diff --git a/tests/Browser/Visit/SingleUrl.php b/tests/Browser/Visit/SingleUrl.php index 0f160719..f4ef48b0 100644 --- a/tests/Browser/Visit/SingleUrl.php +++ b/tests/Browser/Visit/SingleUrl.php @@ -73,6 +73,72 @@ $page->assertScreenshotMatches(); })->with(ColorScheme::cases())->skipOnCI(); +it('may visit a page in both light and dark modes', function (): void { + Route::get('/', fn (): string => ' + + + Example test page + + + +
+

Light and Dark Mode Test

+

This is a test for both light and dark modes.

+
This is only visible in dark mode
+
This is only visible in light mode
+
+ + + '); + + $pages = visit('/') + ->inLightAndDarkMode() + ->assertNoAccessibilityIssues(level: 3); + + $lightModePage = $pages[0]; + $darkModePage = $pages[1]; + + $lightModeColorScheme = $lightModePage->script('window.matchMedia("(prefers-color-scheme: light)").matches'); + $darkModeColorScheme = $darkModePage->script('window.matchMedia("(prefers-color-scheme: dark)").matches'); + + expect($lightModeColorScheme)->toBeTrue() + ->and($darkModeColorScheme)->toBeTrue(); + + $lightModePage->assertSee('This is only visible in light mode') + ->assertDontSee('This is only visible in dark mode'); + $darkModePage->assertSee('This is only visible in dark mode') + ->assertDontSee('This is only visible in light mode'); +}); + it('may visit a page with custom locale and timezone', function (): void { Route::get('/', fn (): string => '