Skip to content

Commit

Permalink
fix: throw error when unmount player after destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Jun 23, 2020
1 parent 77c6e09 commit 5327c31
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
32 changes: 32 additions & 0 deletions __tests__/tests/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1247,4 +1247,36 @@ describe('<ReactJkMusicPlayer/>', () => {
expect(onPlayIndexChange).not.toHaveBeenCalled()
expect(wrapper.state().name).toEqual('b')
})

it('should not throw error when unmount player', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const wrapper = mount(
<ReactJkMusicPlayer
mode="full"
audioLists={[
{ musicSrc: 'a', cover: 'a', name: 'a' },
]}
autoPlay={false}
/>,
)
wrapper.unmount()
expect(errorSpy).not.toHaveBeenCalled()
})

it('should not throw error when unmount player after destroyed', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const wrapper = mount(
<ReactJkMusicPlayer
mode="full"
audioLists={[
{ musicSrc: 'a', cover: 'a', name: 'a' },
]}
autoPlay={false}
showDestroy
/>,
)
wrapper.find('.destroy-btn').simulate('click')
wrapper.unmount()
expect(errorSpy).not.toHaveBeenCalled()
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-jinke-music-player",
"version": "4.15.2",
"version": "4.15.3",
"description": "Maybe the best beautiful HTML5 responsive player component for react",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @version 4.15.2
* @version 4.15.3
* @name react-jinke-music-player
* @description Maybe the best beautiful HTML5 responsive player component for react :)
* @author Jinke.Li <[email protected]>
Expand Down Expand Up @@ -1867,7 +1867,9 @@ export default class ReactJkMusicPlayer extends PureComponent {
}
}
unBindKeyDownEvents = () => {
this.player.current.removeEventListener('keydown', this.onKeyDown, false)
if (this.player.current) {
this.player.current.removeEventListener('keydown', this.onKeyDown, false)
}
}
onKeyDown = (e) => {
const { spaceBar } = this.props
Expand Down

0 comments on commit 5327c31

Please sign in to comment.