diff --git a/jest.config.js b/jest.config.js index a5237ce..9cff1c8 100644 --- a/jest.config.js +++ b/jest.config.js @@ -5,5 +5,5 @@ module.exports = { moduleNameMapper: { '^hls.js$': '/test/mocks/hls.mock.ts', }, - testMatch: ['/test/**/*.test.tsx'], + testMatch: ['/test/**/*.spec.tsx'], } diff --git a/test/ReactHlsPlayer.test.tsx b/test/ReactHlsPlayer.spec.tsx similarity index 100% rename from test/ReactHlsPlayer.test.tsx rename to test/ReactHlsPlayer.spec.tsx diff --git a/test/mocks/hls.mock.ts b/test/mocks/hls.mock.ts index 43c1631..6d05372 100644 --- a/test/mocks/hls.mock.ts +++ b/test/mocks/hls.mock.ts @@ -1,96 +1,34 @@ -import { EventEmitter } from 'eventemitter3'; -import { HlsConfig, ManifestParsedData, HlsEventEmitter } from 'hls.js'; - -const Events = { - MEDIA_ATTACHED: 'hlsMediaAttached', - MANIFEST_PARSED: 'hlsManifestParsed', - ERROR: 'hlsError', -}; - -const ErrorTypes = { - NETWORK_ERROR: 'networkError', - MEDIA_ERROR: 'mediaError', - KEY_SYSTEM_ERROR: 'keySystemError', - MUX_ERROR: 'muxError', - OTHER_ERROR: 'otherError', -}; - -const ErrorDetails = { - KEY_SYSTEM_NO_KEYS: 'keySystemNoKeys', - KEY_SYSTEM_NO_ACCESS: 'keySystemNoAccess', - KEY_SYSTEM_NO_SESSION: 'keySystemNoSession', - KEY_SYSTEM_NO_CONFIGURED_LICENSE: 'keySystemNoConfiguredLicense', - KEY_SYSTEM_LICENSE_REQUEST_FAILED: 'keySystemLicenseRequestFailed', - KEY_SYSTEM_SERVER_CERTIFICATE_REQUEST_FAILED: - 'keySystemServerCertificateRequestFailed', - KEY_SYSTEM_SERVER_CERTIFICATE_UPDATE_FAILED: - 'keySystemServerCertificateUpdateFailed', - KEY_SYSTEM_SESSION_UPDATE_FAILED: 'keySystemSessionUpdateFailed', - KEY_SYSTEM_STATUS_OUTPUT_RESTRICTED: 'keySystemStatusOutputRestricted', - KEY_SYSTEM_STATUS_INTERNAL_ERROR: 'keySystemStatusInternalError', - MANIFEST_LOAD_ERROR: 'manifestLoadError', - MANIFEST_LOAD_TIMEOUT: 'manifestLoadTimeOut', - MANIFEST_PARSING_ERROR: 'manifestParsingError', - MANIFEST_INCOMPATIBLE_CODECS_ERROR: 'manifestIncompatibleCodecsError', - LEVEL_EMPTY_ERROR: 'levelEmptyError', - LEVEL_LOAD_ERROR: 'levelLoadError', - LEVEL_LOAD_TIMEOUT: 'levelLoadTimeOut', - LEVEL_PARSING_ERROR: 'levelParsingError', - LEVEL_SWITCH_ERROR: 'levelSwitchError', - AUDIO_TRACK_LOAD_ERROR: 'audioTrackLoadError', - AUDIO_TRACK_LOAD_TIMEOUT: 'audioTrackLoadTimeOut', - SUBTITLE_LOAD_ERROR: 'subtitleTrackLoadError', - SUBTITLE_TRACK_LOAD_TIMEOUT: 'subtitleTrackLoadTimeOut', - FRAG_LOAD_ERROR: 'fragLoadError', - FRAG_LOAD_TIMEOUT: 'fragLoadTimeOut', - FRAG_DECRYPT_ERROR: 'fragDecryptError', - FRAG_PARSING_ERROR: 'fragParsingError', - FRAG_GAP: 'fragGap', - REMUX_ALLOC_ERROR: 'remuxAllocError', - KEY_LOAD_ERROR: 'keyLoadError', - KEY_LOAD_TIMEOUT: 'keyLoadTimeOut', - BUFFER_ADD_CODEC_ERROR: 'bufferAddCodecError', - BUFFER_INCOMPATIBLE_CODECS_ERROR: 'bufferIncompatibleCodecsError', - BUFFER_APPEND_ERROR: 'bufferAppendError', - BUFFER_APPENDING_ERROR: 'bufferAppendingError', - BUFFER_STALLED_ERROR: 'bufferStalledError', - BUFFER_FULL_ERROR: 'bufferFullError', - BUFFER_SEEK_OVER_HOLE: 'bufferSeekOverHole', - BUFFER_NUDGE_ON_STALL: 'bufferNudgeOnStall', - INTERNAL_EXCEPTION: 'internalException', - INTERNAL_ABORTED: 'aborted', - UNKNOWN: 'unknown', -}; +import { EventEmitter } from 'eventemitter3' +import { HlsConfig, ManifestParsedData, HlsEventEmitter } from 'hls.js' +import { ErrorDetails, ErrorTypes, Events } from './hlsVars.mock' class MockHls extends EventEmitter implements HlsEventEmitter { - static isSupported = jest.fn().mockReturnValue(true); - static Events = Events; - static ErrorTypes = ErrorTypes; - static ErrorDetails = ErrorDetails; - - attachMedia: jest.Mock; - loadSource: jest.Mock; - startLoad: jest.Mock; - recoverMediaError: jest.Mock; - destroy: jest.Mock; - - constructor(config?: HlsConfig) { - super(); - - this.attachMedia = jest - .fn() - .mockImplementation((media: HTMLMediaElement) => { - this.emit(Events.MEDIA_ATTACHED, { media }); - }); - - this.loadSource = jest.fn().mockImplementation((src: string) => { - this.emit(Events.MANIFEST_PARSED, {} as ManifestParsedData); - }); - - this.startLoad = jest.fn(); - this.recoverMediaError = jest.fn(); - this.destroy = jest.fn(); - } + static isSupported = jest.fn().mockReturnValue(true) + static Events = Events + static ErrorTypes = ErrorTypes + static ErrorDetails = ErrorDetails + + attachMedia: jest.Mock + loadSource: jest.Mock + startLoad: jest.Mock + recoverMediaError: jest.Mock + destroy: jest.Mock + + constructor(config?: HlsConfig) { + super() + + this.attachMedia = jest.fn().mockImplementation((media: HTMLMediaElement) => { + this.emit(Events.MEDIA_ATTACHED, { media }) + }) + + this.loadSource = jest.fn().mockImplementation((src: string) => { + this.emit(Events.MANIFEST_PARSED, {} as ManifestParsedData) + }) + + this.startLoad = jest.fn() + this.recoverMediaError = jest.fn() + this.destroy = jest.fn() + } } -export default MockHls; +export default MockHls diff --git a/test/mocks/hlsVars.mock.ts b/test/mocks/hlsVars.mock.ts new file mode 100644 index 0000000..798e546 --- /dev/null +++ b/test/mocks/hlsVars.mock.ts @@ -0,0 +1,58 @@ +export const Events = { + MEDIA_ATTACHED: 'hlsMediaAttached', + MANIFEST_PARSED: 'hlsManifestParsed', + ERROR: 'hlsError', +} + +export const ErrorTypes = { + NETWORK_ERROR: 'networkError', + MEDIA_ERROR: 'mediaError', + KEY_SYSTEM_ERROR: 'keySystemError', + MUX_ERROR: 'muxError', + OTHER_ERROR: 'otherError', +} + +export const ErrorDetails = { + KEY_SYSTEM_NO_KEYS: 'keySystemNoKeys', + KEY_SYSTEM_NO_ACCESS: 'keySystemNoAccess', + KEY_SYSTEM_NO_SESSION: 'keySystemNoSession', + KEY_SYSTEM_NO_CONFIGURED_LICENSE: 'keySystemNoConfiguredLicense', + KEY_SYSTEM_LICENSE_REQUEST_FAILED: 'keySystemLicenseRequestFailed', + KEY_SYSTEM_SERVER_CERTIFICATE_REQUEST_FAILED: 'keySystemServerCertificateRequestFailed', + KEY_SYSTEM_SERVER_CERTIFICATE_UPDATE_FAILED: 'keySystemServerCertificateUpdateFailed', + KEY_SYSTEM_SESSION_UPDATE_FAILED: 'keySystemSessionUpdateFailed', + KEY_SYSTEM_STATUS_OUTPUT_RESTRICTED: 'keySystemStatusOutputRestricted', + KEY_SYSTEM_STATUS_INTERNAL_ERROR: 'keySystemStatusInternalError', + MANIFEST_LOAD_ERROR: 'manifestLoadError', + MANIFEST_LOAD_TIMEOUT: 'manifestLoadTimeOut', + MANIFEST_PARSING_ERROR: 'manifestParsingError', + MANIFEST_INCOMPATIBLE_CODECS_ERROR: 'manifestIncompatibleCodecsError', + LEVEL_EMPTY_ERROR: 'levelEmptyError', + LEVEL_LOAD_ERROR: 'levelLoadError', + LEVEL_LOAD_TIMEOUT: 'levelLoadTimeOut', + LEVEL_PARSING_ERROR: 'levelParsingError', + LEVEL_SWITCH_ERROR: 'levelSwitchError', + AUDIO_TRACK_LOAD_ERROR: 'audioTrackLoadError', + AUDIO_TRACK_LOAD_TIMEOUT: 'audioTrackLoadTimeOut', + SUBTITLE_LOAD_ERROR: 'subtitleTrackLoadError', + SUBTITLE_TRACK_LOAD_TIMEOUT: 'subtitleTrackLoadTimeOut', + FRAG_LOAD_ERROR: 'fragLoadError', + FRAG_LOAD_TIMEOUT: 'fragLoadTimeOut', + FRAG_DECRYPT_ERROR: 'fragDecryptError', + FRAG_PARSING_ERROR: 'fragParsingError', + FRAG_GAP: 'fragGap', + REMUX_ALLOC_ERROR: 'remuxAllocError', + KEY_LOAD_ERROR: 'keyLoadError', + KEY_LOAD_TIMEOUT: 'keyLoadTimeOut', + BUFFER_ADD_CODEC_ERROR: 'bufferAddCodecError', + BUFFER_INCOMPATIBLE_CODECS_ERROR: 'bufferIncompatibleCodecsError', + BUFFER_APPEND_ERROR: 'bufferAppendError', + BUFFER_APPENDING_ERROR: 'bufferAppendingError', + BUFFER_STALLED_ERROR: 'bufferStalledError', + BUFFER_FULL_ERROR: 'bufferFullError', + BUFFER_SEEK_OVER_HOLE: 'bufferSeekOverHole', + BUFFER_NUDGE_ON_STALL: 'bufferNudgeOnStall', + INTERNAL_EXCEPTION: 'internalException', + INTERNAL_ABORTED: 'aborted', + UNKNOWN: 'unknown', +}