|
16 | 16 |
|
17 | 17 | import { renderHook } from '@testing-library/react-hooks'; |
18 | 18 |
|
19 | | -import { useMediaCapabilities } from './'; |
20 | | - |
21 | 19 | const mediaConfig = { |
22 | | - type: 'file', |
23 | | - audio: { |
24 | | - contentType: 'audio/mp3', |
25 | | - channels: 2, |
26 | | - bitrate: 132700, |
27 | | - samplerate: 5200 |
28 | | - } |
| 20 | + type: 'file', |
| 21 | + audio: { |
| 22 | + contentType: 'audio/mp3', |
| 23 | + channels: 2, |
| 24 | + bitrate: 132700, |
| 25 | + samplerate: 5200 |
| 26 | + } |
29 | 27 | }; |
30 | 28 |
|
31 | 29 | const mediaCapabilitiesMapper = { |
32 | | - 'audio/mp3': { |
33 | | - powerEfficient: true, |
34 | | - smooth: true, |
35 | | - supported: true |
36 | | - } |
37 | | -} |
| 30 | + 'audio/mp3': { |
| 31 | + powerEfficient: true, |
| 32 | + smooth: true, |
| 33 | + supported: true |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +afterEach(function() { |
| 38 | + // Reload hook for every test |
| 39 | + jest.resetModules(); |
| 40 | +}); |
38 | 41 |
|
39 | 42 | describe('useMediaCapabilities', () => { |
40 | | - test('should return supported flag on unsupported platforms', () => { |
41 | | - const { result } = renderHook(() => useMediaCapabilities(mediaConfig)); |
| 43 | + const navigator = window.navigator; |
42 | 44 |
|
43 | | - expect(result.current.mediaCapabilities).toEqual({ hasMediaConfig: true, supported: false }); |
44 | | - }); |
| 45 | + afterEach(() => { |
| 46 | + if (!window.navigator) window.navigator = navigator; |
| 47 | + }); |
45 | 48 |
|
46 | | - test('should return supported and hasMediaConfig flags on unsupported platforms and no config given', () => { |
47 | | - const { result } = renderHook(() => useMediaCapabilities()); |
48 | | - |
49 | | - expect(result.current.mediaCapabilities).toEqual({ hasMediaConfig: false, supported: false }); |
50 | | - }); |
| 49 | + test('should return supported flag on unsupported platforms', () => { |
| 50 | + const { useMediaCapabilities } = require('./'); |
| 51 | + const { result } = renderHook(() => useMediaCapabilities(mediaConfig)); |
51 | 52 |
|
52 | | - test('should return initialMediaCapabilities for unsupported', () => { |
53 | | - const initialMediaCapabilities = { |
54 | | - supported: true, |
55 | | - smooth: false, |
56 | | - powerEfficient: true |
57 | | - }; |
| 53 | + expect(result.current.mediaCapabilities).toEqual({hasMediaConfig: true, supported: false}); |
| 54 | + }); |
58 | 55 |
|
59 | | - const { result } = renderHook(() => useMediaCapabilities(mediaConfig, initialMediaCapabilities)); |
| 56 | + test('should return supported and hasMediaConfig flags on unsupported platforms and no config given', () => { |
| 57 | + const { useMediaCapabilities } = require('./'); |
| 58 | + const { result } = renderHook(() => useMediaCapabilities()); |
| 59 | + |
| 60 | + expect(result.current.mediaCapabilities).toEqual({hasMediaConfig: false, supported: false}); |
| 61 | + }); |
60 | 62 |
|
61 | | - expect(result.current.mediaCapabilities.supported).toBe(true); |
62 | | - expect(result.current.mediaCapabilities.smooth).toEqual(false); |
63 | | - expect(result.current.mediaCapabilities.powerEfficient).toEqual(true); |
64 | | - }); |
| 63 | + test('should return initialMediaCapabilities for unsupported', () => { |
| 64 | + const initialMediaCapabilities = { |
| 65 | + supported: true, |
| 66 | + smooth: false, |
| 67 | + powerEfficient: true |
| 68 | + }; |
| 69 | + const { useMediaCapabilities } = require('./'); |
| 70 | + const { result } = renderHook(() => useMediaCapabilities(mediaConfig, initialMediaCapabilities)); |
| 71 | + |
| 72 | + expect(result.current.mediaCapabilities.supported).toBe(true); |
| 73 | + expect(result.current.mediaCapabilities.smooth).toEqual(false); |
| 74 | + expect(result.current.mediaCapabilities.powerEfficient).toEqual(true); |
| 75 | + }); |
65 | 76 |
|
66 | | - test('should return hasMediaConfig flag when no config given', () => { |
67 | | - Object.defineProperty(window.navigator, 'mediaCapabilities', { |
68 | | - value: true, |
69 | | - configurable: true, |
70 | | - writable: true |
71 | | - }); |
72 | | - |
73 | | - const { result } = renderHook(() => useMediaCapabilities()); |
74 | | - |
75 | | - expect(result.current.mediaCapabilities).toEqual({ hasMediaConfig: false, supported: true }); |
| 77 | + test('should return hasMediaConfig flag when no config given', () => { |
| 78 | + Object.defineProperty(window.navigator, 'mediaCapabilities', { |
| 79 | + value: true, |
| 80 | + configurable: true, |
| 81 | + writable: true |
76 | 82 | }); |
| 83 | + const { useMediaCapabilities } = require('./'); |
| 84 | + const { result } = renderHook(() => useMediaCapabilities()); |
77 | 85 |
|
78 | | - test('should return MediaDecodingConfiguration for given media configuration', () => { |
79 | | - Object.defineProperty(window.navigator, 'mediaCapabilities', { |
80 | | - value: { |
81 | | - decodingInfo: (mediaConfig) => mediaCapabilitiesMapper[mediaConfig.audio.contentType] |
82 | | - }, |
83 | | - configurable: true, |
84 | | - writable: true |
85 | | - }); |
86 | | - |
87 | | - const { result } = renderHook(() => useMediaCapabilities(mediaConfig)); |
| 86 | + expect(result.current.mediaCapabilities).toEqual({hasMediaConfig: false, supported: true}); |
| 87 | + }); |
| 88 | + |
| 89 | + test('should return MediaDecodingConfiguration for given media configuration', () => { |
| 90 | + Object.defineProperty(window.navigator, 'mediaCapabilities', { |
| 91 | + value: { |
| 92 | + decodingInfo: mediaConfig => mediaCapabilitiesMapper[mediaConfig.audio.contentType] |
| 93 | + }, |
| 94 | + configurable: true, |
| 95 | + writable: true |
| 96 | + }); |
| 97 | + const { useMediaCapabilities } = require('./'); |
| 98 | + const { result } = renderHook(() => useMediaCapabilities(mediaConfig)); |
88 | 99 |
|
89 | | - expect(result.current.mediaCapabilities).toEqual({ |
90 | | - powerEfficient: true, |
91 | | - smooth: true, |
92 | | - supported: true |
93 | | - }); |
| 100 | + expect(result.current.mediaCapabilities).toEqual({ |
| 101 | + powerEfficient: true, |
| 102 | + smooth: true, |
| 103 | + supported: true |
94 | 104 | }); |
| 105 | + }); |
95 | 106 | }); |
0 commit comments