Skip to content

Commit

Permalink
fix: should not call onCoverClick if cover is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Sep 19, 2020
1 parent a18f1db commit 52ba3ae
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
44 changes: 44 additions & 0 deletions __tests__/tests/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,49 @@ describe('<ReactJkMusicPlayer/>', () => {
expect(onCoverClick).toHaveBeenCalledTimes(2)
})

it('should not call onCoverClick if cover is empty', () => {
const onCoverClick = jest.fn()
const wrapper = mount(
<ReactJkMusicPlayer
mode="mini"
audioLists={[
{
cover: '',
musicSrc: 'test.mp3',
},
]}
onCoverClick={onCoverClick}
/>,
)
wrapper.find('.music-player-controller').simulate('click')
expect(onCoverClick).not.toHaveBeenCalled()
wrapper.setProps({ mode: 'full' })
wrapper.find('.img-content').simulate('click')
expect(onCoverClick).not.toHaveBeenCalled()
})

it('should not call onCoverClick if showMiniModeCover is false in mini mode', () => {
const onCoverClick = jest.fn()
const wrapper = mount(
<ReactJkMusicPlayer
mode="mini"
showMiniModeCover={false}
audioLists={[
{
cover: 'xxx',
musicSrc: 'test.mp3',
},
]}
onCoverClick={onCoverClick}
/>,
)
wrapper.find('.music-player-controller').simulate('click')
expect(onCoverClick).not.toHaveBeenCalled()
wrapper.setProps({ mode: 'full' })
wrapper.find('.img-content').simulate('click')
expect(onCoverClick).toHaveBeenCalledTimes(1)
})

it('should trigger onPlayIndexChange hook when audio track list change', () => {
const onPlayIndexChange = jest.fn()
const wrapper = mount(
Expand Down Expand Up @@ -1305,6 +1348,7 @@ describe('<ReactJkMusicPlayer/>', () => {
drag={false}
onModeChange={onModeChange}
onCoverClick={onCoverClick}
audioLists={[{ cover: 'xx' }]}
/>,
)
expect(
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,12 @@ export default class ReactJkMusicPlayer extends PureComponent {
}

onCoverClick = (mode = MODE.FULL) => {
if (this.props.onCoverClick) {
const { showMiniModeCover } = this.props
const { cover } = this.state
if (!showMiniModeCover && mode === MODE.MINI) {
return
}
if (this.props.onCoverClick && cover) {
this.props.onCoverClick(
mode,
this.state.audioLists,
Expand Down

0 comments on commit 52ba3ae

Please sign in to comment.