Skip to content

Commit ed062ee

Browse files
📝 CodeRabbit Chat: Update tests: ESM Plyr mock, constructor/options and lifecycle
1 parent d753806 commit ed062ee

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

tests/Plyr.lifecycle.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as React from "react";
2+
import { render } from "@testing-library/react";
3+
import { Plyr, PlyrInstance } from "../src/index";
4+
5+
// Localized mock for this suite
6+
const destroy = jest.fn();
7+
const ctor = jest.fn().mockImplementation(() => ({ destroy, playing: false }));
8+
jest.mock("plyr", () => ({ __esModule: true, default: ctor }));
9+
10+
describe("<Plyr /> lifecycle", () => {
11+
it("calls destroy on unmount", () => {
12+
const ref = React.createRef<{ plyr: PlyrInstance }>();
13+
const { unmount } = render(<Plyr ref={ref} source={null} />);
14+
expect(ref.current?.plyr).toBeDefined();
15+
unmount();
16+
expect(destroy).toHaveBeenCalledTimes(1);
17+
});
18+
19+
it("does not create a new instance on rerender with same props", () => {
20+
const ref = React.createRef<{ plyr: PlyrInstance }>();
21+
const { rerender } = render(<Plyr ref={ref} source={null} />);
22+
expect(ctor).toHaveBeenCalledTimes(1);
23+
const inst = ref.current?.plyr;
24+
rerender(<Plyr ref={ref} source={null} />);
25+
expect(ref.current?.plyr).toBe(inst);
26+
expect(ctor).toHaveBeenCalledTimes(1);
27+
});
28+
});

tests/Plyr.test.tsx

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,94 @@ describe("<Plyr />", () => {
6666
expect((ref.current?.plyr as PlyrInstance).playing).toBe(false);
6767
});
6868
});
69+
70+
describe("<Plyr /> – constructor and options behavior", () => {
71+
const destroySpy = jest.fn();
72+
const setSourceSpy = jest.fn();
73+
74+
const mockCtor = jest.fn().mockImplementation(() => ({
75+
destroy: destroySpy,
76+
playing: false,
77+
set source(val) {
78+
setSourceSpy(val);
79+
(this as any)._source = val;
80+
},
81+
get source() {
82+
return (this as any)._source;
83+
},
84+
}));
85+
86+
beforeAll(() => {
87+
jest.resetModules();
88+
jest.doMock("plyr", () => ({ __esModule: true, default: mockCtor }));
89+
});
90+
91+
afterAll(() => {
92+
jest.dontMock("plyr");
93+
jest.clearAllMocks();
94+
});
95+
96+
beforeEach(() => {
97+
destroySpy.mockClear();
98+
setSourceSpy.mockClear();
99+
mockCtor.mockClear();
100+
});
101+
102+
it("constructs Plyr with '.plyr-react' selector and empty options by default", () => {
103+
const React = require("react");
104+
const { render } = require("@testing-library/react");
105+
const { Plyr } = require("../src/index");
106+
107+
render(<Plyr source={null} />);
108+
expect(mockCtor).toHaveBeenCalledTimes(1);
109+
expect(mockCtor).toHaveBeenLastCalledWith(".plyr-react", {});
110+
});
111+
112+
it("applies initial source on mount when provided", () => {
113+
const React = require("react");
114+
const { render } = require("@testing-library/react");
115+
const { Plyr } = require("../src/index");
116+
117+
const initialSource = { type: "video", sources: [{ src: "init.mp4", type: "video/mp4" }] };
118+
render(<Plyr source={initialSource} />);
119+
expect(setSourceSpy).toHaveBeenCalledWith(initialSource);
120+
});
121+
122+
it("passes provided options to Plyr constructor", () => {
123+
const React = require("react");
124+
const { render } = require("@testing-library/react");
125+
const { Plyr } = require("../src/index");
126+
127+
const opts = { controls: ["play", "progress"], autoplay: true };
128+
render(<Plyr source={null} options={opts} />);
129+
expect(mockCtor).toHaveBeenCalledTimes(1);
130+
expect(mockCtor).toHaveBeenLastCalledWith(".plyr-react", opts);
131+
});
132+
133+
it("re-instantiates when options change and destroys previous instance", () => {
134+
const React = require("react");
135+
const { render } = require("@testing-library/react");
136+
const { Plyr } = require("../src/index");
137+
138+
const { rerender } = render(<Plyr source={null} options={{ clickToPlay: true }} />);
139+
expect(mockCtor).toHaveBeenCalledTimes(1);
140+
141+
rerender(<Plyr source={null} options={{ clickToPlay: false }} />);
142+
expect(mockCtor).toHaveBeenCalledTimes(2);
143+
expect(destroySpy).toHaveBeenCalled();
144+
});
145+
146+
it("renders default classes when className prop is absent", () => {
147+
const React = require("react");
148+
const { render } = require("@testing-library/react");
149+
const { Plyr } = require("../src/index");
150+
151+
const { container } = render(<Plyr source={null} />);
152+
const video = container.querySelector("video") as HTMLElement | null;
153+
expect(video).toBeTruthy();
154+
if (video) {
155+
const classes = Array.from(video.classList);
156+
expect(classes).toEqual(expect.arrayContaining(["plyr-react", "plyr"]));
157+
}
158+
});
159+
});

0 commit comments

Comments
 (0)