Skip to content

Commit

Permalink
feat: add test case for restart on prev feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed May 8, 2021
1 parent 924841a commit ad54e66
Show file tree
Hide file tree
Showing 3 changed files with 1,831 additions and 1,499 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ Support feature:
- `play prev audio`
- `play audio by custom play index`
- `update play index`
- `restart current song on play prev audio`
- [SortableJS methods](https://github.com/SortableJS/Sortable#methods)

```jsx
Expand Down
57 changes: 34 additions & 23 deletions __tests__/tests/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,40 @@ describe('<ReactJkMusicPlayer/>', () => {
expect(wrapper.state('soundValue')).toStrictEqual(0.5)
})

it('should reset audio current time to zero if prev audio button clicked', async () => {
const onPlayIndexChange = jest.fn()
const onAudioPlayTrackChange = jest.fn()
let audio
const wrapper = mount(
<ReactJkMusicPlayer
restartCurrentOnPrev
mode="full"
audioLists={[{ musicSrc: 'x' }, { musicSrc: 'y' }]}
onPlayIndexChange={onPlayIndexChange}
onAudioPlayTrackChange={onAudioPlayTrackChange}
getAudioInstance={(node) => (audio = node)}
/>,
)
audio.currentTime = 0

wrapper.find('.prev-audio').simulate('click')
await sleep(300)

expect(onPlayIndexChange).toHaveBeenCalledTimes(1)
expect(onAudioPlayTrackChange).toHaveBeenCalledTimes(1)

audio.currentTime = 20

wrapper.find('.prev-audio').simulate('click')
await sleep(300)

expect(audio.currentTime).toEqual(0)
expect(onPlayIndexChange).toHaveBeenCalledTimes(1)
expect(onAudioPlayTrackChange).toHaveBeenCalledTimes(1)

jest.clearAllMocks()
})

it("should don't play audio when audio seeked by slider bar if audio is paused", async () => {
const onAudioSeeked = jest.fn()
const onAudioPlay = jest.fn()
Expand Down Expand Up @@ -1858,27 +1892,4 @@ describe('<ReactJkMusicPlayer/>', () => {
expect(wrapper.state('isResetCoverRotate')).toBeFalsy()
})
})
it('should reset audio current time to zero if prev audio button clicked', async () => {
const onPlayIndexChange = jest.fn()
const wrapper = mount(
<ReactJkMusicPlayer
restartCurrentOnPrev
mode="full"
onPlayIndexChange={onPlayIndexChange}
/>,
)
window.HTMLMediaElement.prototype.currentTime = 0
wrapper.find('.prev-audio').simulate('click')
await sleep(100)

expect(onPlayIndexChange).toHaveBeenCalledTimes(1)

window.HTMLMediaElement.prototype.currentTime = 1
wrapper.find('.prev-audio').simulate('click')
await sleep(100)

expect(window.HTMLMediaElement.prototype.currentTime).toEqual(0)

expect(onPlayIndexChange).toHaveBeenCalledTimes(1)
})
})
Loading

0 comments on commit ad54e66

Please sign in to comment.