Skip to content

Commit

Permalink
fix: reset volume to 0.1 if last is mute when toggle mute button
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Jul 2, 2020
1 parent aeb1630 commit 0dc4be5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
56 changes: 47 additions & 9 deletions __tests__/tests/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import CircleProcessBar from '../../src/components/CircleProcessBar'
import Load from '../../src/components/Load'
import { SPACE_BAR_KEYCODE } from '../../src/config/keycode'
import { sleep } from '../utils'
import { MEDIA_QUERY } from '../../src/config/mediaQuery'

describe('<ReactJkMusicPlayer/>', () => {
it('should render a <ReactJkMusicPlayer/> components', () => {
Expand Down Expand Up @@ -1180,7 +1181,6 @@ describe('<ReactJkMusicPlayer/>', () => {
expect(wrapper.state().name).toEqual('b')
})


it('should get secondary audio info by play index', () => {
const wrapper = mount(
<ReactJkMusicPlayer
Expand Down Expand Up @@ -1249,13 +1249,11 @@ describe('<ReactJkMusicPlayer/>', () => {
})

it('should not throw error when unmount player', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
const wrapper = mount(
<ReactJkMusicPlayer
mode="full"
audioLists={[
{ musicSrc: 'a', cover: 'a', name: 'a' },
]}
audioLists={[{ musicSrc: 'a', cover: 'a', name: 'a' }]}
autoPlay={false}
/>,
)
Expand All @@ -1264,13 +1262,11 @@ describe('<ReactJkMusicPlayer/>', () => {
})

it('should not throw error when unmount player after destroyed', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
const wrapper = mount(
<ReactJkMusicPlayer
mode="full"
audioLists={[
{ musicSrc: 'a', cover: 'a', name: 'a' },
]}
audioLists={[{ musicSrc: 'a', cover: 'a', name: 'a' }]}
autoPlay={false}
showDestroy
/>,
Expand All @@ -1279,4 +1275,46 @@ describe('<ReactJkMusicPlayer/>', () => {
wrapper.unmount()
expect(errorSpy).not.toHaveBeenCalled()
})

it('should reset volume if current is mute and mute icon clicked', () => {
const wrapper = mount(
<ReactJkMusicPlayer
mode="full"
audioLists={[{ musicSrc: 'a', cover: 'a', name: 'a' }]}
autoPlay={false}
/>,
)
wrapper.setState({ currentAudioVolume: 0 })
wrapper.find('.sounds-icon').simulate('click')
expect(wrapper.state().currentAudioVolume).not.toEqual(0)
})

it('should set theme to dark if media dark theme matches', () => {
window.matchMedia = jest.fn().mockImplementation((query) => {
return {
matches: query === MEDIA_QUERY.DARK_THEME,
addListener: jest.fn(),
}
})
const wrapper = mount(<ReactJkMusicPlayer theme="auto" autoPlay={false} />)
expect(wrapper.state().theme).toEqual('dark')
})

it('should set theme to light if media dark theme not matches', () => {
window.matchMedia = jest.fn().mockImplementation((query) => {
return {
matches: query === MEDIA_QUERY.LIGHT_THEME,
addListener: jest.fn(),
}
})
const wrapper = mount(<ReactJkMusicPlayer theme="auto" autoPlay={false} />)
expect(wrapper.state().theme).toEqual('light')
})

it('should update auto theme props', () => {
const wrapper = mount(<ReactJkMusicPlayer autoPlay={false} />)

wrapper.setProps({ theme: 'auto' })
expect(wrapper.state().theme).toEqual('auto')
})
})
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ export default class ReactJkMusicPlayer extends PureComponent {

onResetVolume = () => {
const { currentAudioVolume } = this.state
this.setAudioVolume(currentAudioVolume)
this.setAudioVolume(currentAudioVolume || 0.1)
}
setAudioVolume = (value) => {
this.audio.volume = value
Expand Down Expand Up @@ -1611,6 +1611,7 @@ export default class ReactJkMusicPlayer extends PureComponent {

addMatchMediaListener = (query, handler) => {
const media = window.matchMedia(query)
handler(media)
if ('addEventListener' in media) {
media.addEventListener('change', handler)
} else {
Expand Down

0 comments on commit 0dc4be5

Please sign in to comment.