Skip to content

Commit

Permalink
Merge pull request #92 from eliotjordan/91-hls-fix
Browse files Browse the repository at this point in the history
Fix HLS format detection
  • Loading branch information
stephenwf authored Aug 24, 2023
2 parents ca50e15 + 0051081 commit 3fdceb1
Show file tree
Hide file tree
Showing 9 changed files with 12,409 additions and 177 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ dist-umd
dist-var
types
.vitest-result.json
.tool-versions

5 changes: 5 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ruby 3.1.0
nodejs 16.15.0
java zulu-8.58.0.13
python 3.9.1
yarn 1.22.10
2 changes: 1 addition & 1 deletion __tests__/__snapshots__/time-plan.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Time plan > Create valid timeplans > Short range contains valid canvas indexes 1`] = `
{
Expand Down
45 changes: 45 additions & 0 deletions __tests__/can-play-hls.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { beforeEach, describe, expect, vitest, test } from 'vitest'
import { canPlayHls } from '../src/helpers/can-play-hls'

describe('canPlayHls', () => {
describe('with a browser that natively supports HLS', () => {
beforeEach(() => {
window.HTMLMediaElement.prototype.canPlayType = vitest.fn((_type) => {
return "probably"
})
})

test('returns true', () => {
expect(canPlayHls()).toBeTruthy()
})
})

describe('with a browser that does not natively support HLS', () => {
beforeEach(() => {
window.HTMLMediaElement.prototype.canPlayType = vitest.fn((_type) => {
return ""
})
})

test('returns false', () => {
expect(canPlayHls()).toBeFalsy()
})
})

describe('with a browser that does not natively support HLS but is using HLS.js', () => {
beforeEach(() => {
class Hls {
isSupported() { return true }
}

window.HTMLMediaElement.prototype.canPlayType = vitest.fn((_type) => {
return ""
})
window.Hls = new Hls
})

test('returns true', () => {
expect(canPlayHls()).toBeTruthy()
})
})
})
36 changes: 36 additions & 0 deletions __tests__/hls-format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { beforeEach, describe, expect, vitest, test } from 'vitest';
import { HlsFormat } from '../src/media-formats/hls-format';

describe('HlsFormat', () => {
class HlsClassMock {
loadSource() {}
attachMedia() {}
}

afterEach(() => {
vitest.unstubAllGlobals()
})

describe('when hls.js is included', () => {
test('hls attaches the media element', () => {
vitest.stubGlobal('Hls', HlsClassMock);
const attachMediaSpy = vitest.spyOn(HlsClassMock.prototype, 'attachMedia');
const loadSourceSpy = vitest.spyOn(HlsClassMock.prototype, 'loadSource');
const format = new HlsFormat();
format.attachTo();
expect(loadSourceSpy).toHaveBeenCalled();
expect(attachMediaSpy).toHaveBeenCalled();
});
});

describe('when hls.js is not included', () => {
test('hls attaches the media element', () => {
const attachMediaSpy = vitest.spyOn(HlsClassMock.prototype, 'attachMedia');
const loadSourceSpy = vitest.spyOn(HlsClassMock.prototype, 'loadSource');
const format = new HlsFormat();
format.attachTo();
expect(loadSourceSpy).not.toHaveBeenCalled();
expect(attachMediaSpy).not.toHaveBeenCalled();
});
});
});
Loading

0 comments on commit 3fdceb1

Please sign in to comment.