Use testing library click

This commit is contained in:
Half-Shot
2024-12-05 11:29:15 +00:00
parent 923b548f98
commit 0adacd0d50

View File

@@ -13,6 +13,7 @@ import { afterEach } from "node:test";
import { deviceStub, MediaDevicesContext } from "./livekit/MediaDevicesContext"; import { deviceStub, MediaDevicesContext } from "./livekit/MediaDevicesContext";
import { useAudioContext } from "./useAudioContext"; import { useAudioContext } from "./useAudioContext";
import { soundEffectVolumeSetting } from "./settings/settings"; import { soundEffectVolumeSetting } from "./settings/settings";
import userEvent from "@testing-library/user-event";
const TestComponent: FC = () => { const TestComponent: FC = () => {
const audioCtx = useAudioContext({ const audioCtx = useAudioContext({
@@ -69,17 +70,19 @@ afterEach(() => {
}); });
test("can play a single sound", async () => { test("can play a single sound", async () => {
const user = userEvent.setup();
vitest.stubGlobal("AudioContext", MockAudioContext); vitest.stubGlobal("AudioContext", MockAudioContext);
const { findByText } = render(<TestComponent />); const { findByText } = render(<TestComponent />);
(await findByText("Valid sound")).click(); await user.click(await findByText("Valid sound"));
expect( expect(
MockAudioContext.testContext.createBufferSource, MockAudioContext.testContext.createBufferSource,
).toHaveBeenCalledOnce(); ).toHaveBeenCalledOnce();
}); });
test("will ignore sounds that are not registered", async () => { test("will ignore sounds that are not registered", async () => {
const user = userEvent.setup();
vitest.stubGlobal("AudioContext", MockAudioContext); vitest.stubGlobal("AudioContext", MockAudioContext);
const { findByText } = render(<TestComponent />); const { findByText } = render(<TestComponent />);
(await findByText("Invalid sound")).click(); await user.click(await findByText("Invalid sound"));
expect( expect(
MockAudioContext.testContext.createBufferSource, MockAudioContext.testContext.createBufferSource,
).not.toHaveBeenCalled(); ).not.toHaveBeenCalled();
@@ -112,11 +115,12 @@ test("will use the correct device", () => {
); );
}); });
test("will use the correct volume", async () => { test("will use the correct volume level", async () => {
const user = userEvent.setup();
vitest.stubGlobal("AudioContext", MockAudioContext); vitest.stubGlobal("AudioContext", MockAudioContext);
soundEffectVolumeSetting.setValue(0.33); soundEffectVolumeSetting.setValue(0.33);
const { findByText } = render(<TestComponent />); const { findByText } = render(<TestComponent />);
(await findByText("Valid sound")).click(); await user.click(await findByText("Valid sound"));
expect( expect(
MockAudioContext.testContext.gain.gain.setValueAtTime, MockAudioContext.testContext.gain.gain.setValueAtTime,
).toHaveBeenCalledWith(0.33, 0); ).toHaveBeenCalledWith(0.33, 0);