-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e22805a
commit cb45ef8
Showing
1 changed file
with
63 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,77 @@ | ||
import React from 'react'; | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { render } from '@testing-library/react'; | ||
// import React from 'react'; | ||
import { | ||
renderHook, | ||
// act, | ||
} from '@testing-library/react-hooks'; | ||
// import { render } from '@testing-library/react'; | ||
|
||
import useVolume from '../lib/use-volume'; | ||
|
||
describe('useVolume:', () => { | ||
it('类型与默认值检查', () => { | ||
const { result } = renderHook(() => useVolume({ src: '', live: false, volume: 1, muted: false }, () => {})); | ||
const { result } = renderHook(() => useVolume(() => {})); | ||
|
||
expect(result.current.muted).toBe(false); | ||
expect(result.current.volume).toBe(1); | ||
|
||
expect(typeof result.current.onMutedClick).toBe('function'); | ||
expect(typeof result.current.changeVolume).toBe('function'); | ||
|
||
expect(typeof result.current.onVolumeChange).toBe('function'); | ||
}); | ||
|
||
it('onVolumeChange', () => { | ||
let videoEl = null; | ||
const { result } = renderHook(() => | ||
useVolume({ muted: false, volume: 1, onVolumeChange: () => {} }, () => videoEl), | ||
); | ||
|
||
// eslint-disable-next-line jsx-a11y/media-has-caption | ||
render(<video onVolumeChange={result.current.onVolumeChange} />); | ||
videoEl = document.querySelector('video'); | ||
|
||
expect(result.current.muted).toBe(false); | ||
expect(result.current.volume).toBe(1); | ||
|
||
act(() => { | ||
videoEl.volume = 0.8; | ||
}); | ||
expect(result.current.volume).toBe(0.8); | ||
expect(result.current.muted).toBe(false); | ||
|
||
act(() => { | ||
videoEl.volume = 0; | ||
}); | ||
expect(result.current.volume).toBe(0); | ||
expect(result.current.muted).toBe(true); | ||
}); | ||
|
||
it('onMutedClick/changeVolume', () => { | ||
let videoEl = null; | ||
const { result } = renderHook(() => | ||
useVolume({ muted: false, volume: 1, onVolumeChange: () => {} }, () => videoEl), | ||
); | ||
|
||
// eslint-disable-next-line jsx-a11y/media-has-caption | ||
render(<video onVolumeChange={result.current.onVolumeChange} />); | ||
videoEl = document.querySelector('video'); | ||
|
||
expect(result.current.volume).toBe(1); | ||
expect(result.current.muted).toBe(false); | ||
|
||
act(() => { | ||
result.current.onMutedClick(); | ||
}); | ||
expect(result.current.volume).toBe(1); | ||
expect(result.current.muted).toBe(true); | ||
|
||
act(() => { | ||
result.current.onMutedClick(); | ||
}); | ||
expect(result.current.volume).toBe(1); | ||
expect(result.current.muted).toBe(false); | ||
|
||
act(() => { | ||
result.current.changeVolume(0.8); | ||
}); | ||
expect(result.current.volume).toBe(0.8); | ||
expect(result.current.muted).toBe(false); | ||
|
||
act(() => { | ||
result.current.changeVolume(0); | ||
}); | ||
expect(result.current.volume).toBe(0); | ||
expect(result.current.muted).toBe(true); | ||
|
||
act(() => { | ||
result.current.changeVolume(0.8); | ||
}); | ||
expect(result.current.volume).toBe(0.8); | ||
expect(result.current.muted).toBe(false); | ||
|
||
act(() => { | ||
result.current.changeVolume(0); | ||
}); | ||
expect(result.current.volume).toBe(0); | ||
expect(result.current.muted).toBe(true); | ||
|
||
act(() => { | ||
result.current.onMutedClick(); | ||
}); | ||
expect(result.current.volume).toBe(1); | ||
expect(result.current.muted).toBe(false); | ||
}); | ||
// it('onMutedClick/changeVolume', () => { | ||
// let videoEl = null; | ||
// const { result } = renderHook(() => useVolume(() => videoEl, false)); | ||
|
||
// // eslint-disable-next-line jsx-a11y/media-has-caption | ||
// render(<video />); | ||
// videoEl = document.querySelector('video'); | ||
|
||
// expect(result.current.volume).toBe(1); | ||
// expect(result.current.muted).toBe(false); | ||
// expect(videoEl.muted).toBe(false); | ||
|
||
// act(() => { | ||
// result.current.onMutedClick(); | ||
// }); | ||
// expect(videoEl.muted).toBe(true); | ||
|
||
// expect(result.current.volume).toBe(1); | ||
// expect(result.current.muted).toBe(true); | ||
|
||
// act(() => { | ||
// result.current.onMutedClick(); | ||
// }); | ||
// expect(result.current.volume).toBe(1); | ||
// expect(result.current.muted).toBe(false); | ||
|
||
// act(() => { | ||
// result.current.changeVolume(0.8); | ||
// }); | ||
// expect(result.current.volume).toBe(0.8); | ||
// expect(result.current.muted).toBe(false); | ||
|
||
// act(() => { | ||
// result.current.changeVolume(0); | ||
// }); | ||
// expect(result.current.volume).toBe(0); | ||
// expect(result.current.muted).toBe(true); | ||
|
||
// act(() => { | ||
// result.current.changeVolume(0.8); | ||
// }); | ||
// expect(result.current.volume).toBe(0.8); | ||
// expect(result.current.muted).toBe(false); | ||
|
||
// act(() => { | ||
// result.current.changeVolume(0); | ||
// }); | ||
// expect(result.current.volume).toBe(0); | ||
// expect(result.current.muted).toBe(true); | ||
|
||
// act(() => { | ||
// result.current.onMutedClick(); | ||
// }); | ||
// expect(result.current.volume).toBe(1); | ||
// expect(result.current.muted).toBe(false); | ||
// }); | ||
}); |