Skip to content

Commit

Permalink
test: add e2e tests for color mode setter & inverter
Browse files Browse the repository at this point in the history
  • Loading branch information
DavieReid authored and joshwooding committed Sep 11, 2024
1 parent 1d85767 commit f4fe967
Showing 1 changed file with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {
useAriaAnnouncer,
useDensity,
useTheme,
ModeValues,
} from "@salt-ds/core";
import { WindowProvider } from "@salt-ds/window";
import { mount } from "cypress/react18";
import { type ReactNode, useCallback, useState } from "react";
import { createPortal } from "react-dom";
import { Mode } from "fs";

Check failure on line 14 in packages/core/src/__tests__/__e2e__/salt-provider/SaltProvider.cy.tsx

View workflow job for this annotation

GitHub Actions / lint

lint/style/useNodejsImportProtocol

A Node.js builtin module should be imported with the node: protocol.

Check failure on line 14 in packages/core/src/__tests__/__e2e__/salt-provider/SaltProvider.cy.tsx

View workflow job for this annotation

GitHub Actions / lint

lint/style/useImportType

All these imports are only used as types.

const TestComponent = ({
id = "test-1",
Expand Down Expand Up @@ -316,6 +318,93 @@ describe("Given a SaltProvider", () => {

cy.get("@consoleSpy").should("not.have.been.called");
});

describe("when the mode is set", () => {
const ThemeToggle = () => {
const { setMode } = useTheme();

const handleClick = () => {
setMode((prevState) =>
prevState === ModeValues[0] ? ModeValues[1] : ModeValues[0]
);
};
return <button onClick={handleClick}>Set Mode</button>;
};

it("should update the mode", () => {
mount(
<SaltProvider>
<ThemeToggle />
<TestComponent />
</SaltProvider>
);
cy.get("#test-1").should("exist").and("have.attr", "data-mode", "light");
cy.findByRole("button").realClick();
cy.get("#test-1").should("have.attr", "data-mode", "dark");
cy.findByRole("button").realClick();
cy.get("#test-1").should("have.attr", "data-mode", "light");
});
});

describe("when the mode is controlled by consumers", () => {
const ControlledModeSaltProvider = ({
children,
}: {
children: ReactNode;
}) => {
const [mode, setMode] = useState<Mode>(ModeValues[0]);

const handleClick = () => {
setMode((prevState) =>
prevState === ModeValues[0] ? ModeValues[1] : ModeValues[0]
);
};
return (
<SaltProvider mode={mode}>

Check failure on line 363 in packages/core/src/__tests__/__e2e__/salt-provider/SaltProvider.cy.tsx

View workflow job for this annotation

GitHub Actions / type-checks

Type 'Mode' is not assignable to type '"dark" | "light" | undefined'.
<button onClick={handleClick}>Set Mode</button>
{children}
</SaltProvider>
);
};

it("should update the mode", () => {
mount(
<ControlledModeSaltProvider>
<TestComponent />
</ControlledModeSaltProvider>
);
cy.get("#test-1").should("exist").and("have.attr", "data-mode", "light");
cy.findByRole("button").realClick();
cy.get("#test-1").should("have.attr", "data-mode", "dark");
cy.findByRole("button").realClick();
cy.get("#test-1").should("have.attr", "data-mode", "light");
});
});

describe("when the mode is inverted", () => {
const ThemeToggle = () => {
const { invertMode } = useTheme();

const handleClick = () => {
invertMode();
};
return <button onClick={handleClick}>Invert Mode</button>;
};

it("should update the mode", () => {
mount(
<SaltProvider>
<ThemeToggle />
<TestComponent />
</SaltProvider>
);
cy.get("#test-1").should("exist").and("have.attr", "data-mode", "light");
cy.findByRole("button").realClick();
cy.get("#test-1").should("have.attr", "data-mode", "dark");
cy.findByRole("button").realClick();
cy.get("#test-1").should("have.attr", "data-mode", "light");
});
});
});

describe("Given a SaltProviderNext", () => {
Expand Down

0 comments on commit f4fe967

Please sign in to comment.