Skip to content

Commit 44ff75b

Browse files
test: improve Audio tests (#752)
1 parent 40bacb1 commit 44ff75b

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/quiz-question/audio/audio.test.tsx

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,10 @@ describe("<Audio />", () => {
2727

2828
// Check for custom controls
2929
expect(screen.getByRole("button", { name: "Play" })).toBeInTheDocument();
30-
expect(
31-
screen.getByRole("slider", { name: "Seek audio" }),
32-
).toBeInTheDocument();
33-
});
34-
35-
it("should toggle play/pause when button is clicked", async () => {
36-
const user = userEvent.setup();
37-
render(<Audio src="test-audio.mp3" aria-label="Audio player" />);
38-
39-
const playButton = screen.getByRole("button", { name: "Play" });
40-
expect(playButton).toBeInTheDocument();
41-
42-
await user.click(playButton);
43-
44-
// After clicking play, button should show pause
45-
expect(screen.getByRole("button", { name: "Pause" })).toBeInTheDocument();
30+
const slider = screen.getByRole("slider", { name: "Seek audio" });
31+
expect(slider).toBeInTheDocument();
32+
expect(slider).toHaveAttribute("type", "range");
33+
expect(slider).toHaveAttribute("min", "0");
4634
});
4735

4836
it("should render with audio segment when startTime is provided", () => {
@@ -87,6 +75,7 @@ describe("<Audio />", () => {
8775
// Segment duration should be 10 seconds (20 - 10)
8876
const slider = screen.getByRole("slider", { name: "Seek audio" });
8977
expect(slider).toHaveAttribute("max", "10");
78+
expect(slider).toHaveAttribute("min", "0");
9079
});
9180

9281
it("should set audio currentTime to startTime on load", () => {
@@ -120,8 +109,12 @@ describe("<Audio />", () => {
120109
expect(currentTime).toBe(15);
121110
});
122111

123-
it("should pause when pause button is clicked", async () => {
112+
it("should toggle play/pause when button is clicked", async () => {
124113
const user = userEvent.setup();
114+
const playMock = jest.spyOn(
115+
HTMLMediaElement.prototype,
116+
"play",
117+
) as jest.Mock;
125118
const pauseMock = jest.spyOn(
126119
HTMLMediaElement.prototype,
127120
"pause",
@@ -130,13 +123,21 @@ describe("<Audio />", () => {
130123
render(<Audio src="test-audio.mp3" aria-label="Audio player" />);
131124

132125
const playButton = screen.getByRole("button", { name: "Play" });
126+
expect(playButton).toBeInTheDocument();
127+
128+
// Click play
133129
await user.click(playButton);
130+
expect(playMock).toHaveBeenCalled();
134131

135-
// Now pause
132+
// After clicking play, button should show pause
136133
const pauseButton = screen.getByRole("button", { name: "Pause" });
137-
await user.click(pauseButton);
134+
expect(pauseButton).toBeInTheDocument();
138135

136+
// Click pause
137+
await user.click(pauseButton);
139138
expect(pauseMock).toHaveBeenCalled();
139+
140+
// Should be back to play
140141
expect(screen.getByRole("button", { name: "Play" })).toBeInTheDocument();
141142
});
142143

@@ -300,6 +301,7 @@ describe("<Audio />", () => {
300301

301302
// currentTime should be updated to startTime + 5 = 15
302303
expect(currentTime).toBe(15);
304+
expect(slider).toHaveValue("5");
303305
});
304306

305307
it("should format time correctly", () => {
@@ -363,6 +365,7 @@ describe("<Audio />", () => {
363365
// Segment duration should be total duration - startTime = 100 - 20 = 80
364366
const slider = screen.getByRole("slider", { name: "Seek audio" });
365367
expect(slider).toHaveAttribute("max", "80");
368+
expect(slider).toHaveAttribute("min", "0");
366369
});
367370

368371
it("should not reset when finishTime is not reached", () => {

0 commit comments

Comments
 (0)