Skip to content

Commit fc423ea

Browse files
authoredMar 13, 2021
fix: shouldResetPlayer (#284)
1 parent d5f7908 commit fc423ea

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed
 

‎src/YouTube.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ function filterResetOptions(opts) {
5454
* @param {Object} props
5555
*/
5656
function shouldResetPlayer(prevProps, props) {
57-
return !isEqual(filterResetOptions(prevProps.opts), filterResetOptions(props.opts));
57+
return (
58+
prevProps.videoId !== props.videoId || !isEqual(filterResetOptions(prevProps.opts), filterResetOptions(props.opts))
59+
);
5860
}
5961

6062
/**

‎src/Youtube.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,43 @@ describe('YouTube', () => {
129129

130130
// player is destroyed & rebound, despite the changes
131131
expect(playerMock.destroy).toHaveBeenCalled();
132+
// and the video is updated
133+
expect(playerMock.loadVideoById).toHaveBeenCalled();
134+
});
135+
136+
it('should not create and bind a new YouTube player when only playerVars.autoplay, playerVars.start, or playerVars.end change', () => {
137+
const { rerender } = render(
138+
<YouTube
139+
videoId="XxVg_s8xAms"
140+
opts={{
141+
width: '480px',
142+
height: '360px',
143+
playerVars: {
144+
autoplay: 0,
145+
start: 0,
146+
end: 50,
147+
},
148+
}}
149+
/>,
150+
);
151+
152+
rerender(
153+
<YouTube
154+
videoId="XxVg_s8xAms"
155+
opts={{
156+
width: '480px',
157+
height: '360px',
158+
playerVars: {
159+
autoplay: 1, // changed, does not force destroy & rebind
160+
start: 10, // changed, does not force destroy & rebind
161+
end: 20, // changed, does not force destroy & rebind
162+
},
163+
}}
164+
/>,
165+
);
166+
167+
// player is destroyed & rebound, despite the changes
168+
expect(playerMock.destroy).not.toHaveBeenCalled();
132169
// instead only the video is updated
133170
expect(playerMock.loadVideoById).toHaveBeenCalled();
134171
});

0 commit comments

Comments
 (0)
Please sign in to comment.