Skip to content

Commit 0e1852f

Browse files
committed
fix: shouldResetPlayer
1 parent b5d3760 commit 0e1852f

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/YouTube.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ function filterResetOptions(opts) {
3434
return {
3535
...opts,
3636
playerVars: {
37+
...opts.playerVars,
3738
autoplay: 0,
3839
start: 0,
3940
end: 0,
40-
...opts.playerVars,
4141
},
4242
};
4343
}
@@ -52,7 +52,9 @@ function filterResetOptions(opts) {
5252
* @param {Object} props
5353
*/
5454
function shouldResetPlayer(prevProps, props) {
55-
return !isEqual(filterResetOptions(prevProps.opts), filterResetOptions(props.opts));
55+
return (
56+
prevProps.videoId !== props.videoId || !isEqual(filterResetOptions(prevProps.opts), filterResetOptions(props.opts))
57+
);
5658
}
5759

5860
/**

src/Youtube.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,43 @@ describe('YouTube', () => {
127127

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

0 commit comments

Comments
 (0)