toggle.test.ts

 1import {
 2    NO_ACTIVE_ERROR,
 3    NO_INACTIVE_OR_BASE_ERROR,
 4    toggleable,
 5} from "./toggle"
 6import { describe, it, expect } from "vitest"
 7
 8describe("toggleable", () => {
 9    it("creates a Toggleable<Element> with base properties and states", () => {
10        const result = toggleable({
11            base: { background: "#000000", color: "#CCCCCC" },
12            state: {
13                active: { color: "#FFFFFF" },
14            },
15        })
16
17        expect(result).toEqual({
18            inactive: { background: "#000000", color: "#CCCCCC" },
19            active: { background: "#000000", color: "#FFFFFF" },
20        })
21    })
22
23    it("creates a Toggleable<Element> with no base properties", () => {
24        const result = toggleable({
25            state: {
26                inactive: { background: "#000000", color: "#CCCCCC" },
27                active: { background: "#000000", color: "#FFFFFF" },
28            },
29        })
30
31        expect(result).toEqual({
32            inactive: { background: "#000000", color: "#CCCCCC" },
33            active: { background: "#000000", color: "#FFFFFF" },
34        })
35    })
36
37    it("throws error when both inactive and base are missing", () => {
38        const state = {
39            active: { background: "#000000", color: "#FFFFFF" },
40        }
41
42        expect(() => toggleable({ state })).toThrow(NO_INACTIVE_OR_BASE_ERROR)
43    })
44
45    it("throws error when no active state is present", () => {
46        const state = {
47            inactive: { background: "#000000", color: "#CCCCCC" },
48        }
49
50        expect(() => toggleable({ state })).toThrow(NO_ACTIVE_ERROR)
51    })
52})