import React from "react"
import { render, screen, act } from "@testing-library/react"
import { ExtensionStateContextProvider, useExtensionState } from "../ExtensionStateContext"
// Test component that consumes the context
const TestComponent = () => {
const { allowedCommands, setAllowedCommands, soundEnabled } = useExtensionState()
return (
{JSON.stringify(allowedCommands)}
{JSON.stringify(soundEnabled)}
)
}
describe("ExtensionStateContext", () => {
it("initializes with empty allowedCommands array", () => {
render(
,
)
expect(JSON.parse(screen.getByTestId("allowed-commands").textContent!)).toEqual([])
})
it("initializes with soundEnabled set to false", () => {
render(
,
)
expect(JSON.parse(screen.getByTestId("sound-enabled").textContent!)).toBe(false)
})
it("updates allowedCommands through setAllowedCommands", () => {
render(
,
)
act(() => {
screen.getByTestId("update-button").click()
})
expect(JSON.parse(screen.getByTestId("allowed-commands").textContent!)).toEqual(["npm install", "git status"])
})
it("throws error when used outside provider", () => {
// Suppress console.error for this test since we expect an error
const consoleSpy = jest.spyOn(console, "error")
consoleSpy.mockImplementation(() => {})
expect(() => {
render()
}).toThrow("useExtensionState must be used within an ExtensionStateContextProvider")
consoleSpy.mockRestore()
})
})