Skip to content

Commit

Permalink
Merge pull request #1273 from canalplus/misc/global-scope-improvement
Browse files Browse the repository at this point in the history
Improve global scope situation with util and lint update
  • Loading branch information
peaBerberian committed Aug 31, 2023
2 parents 49f7219 + 1baba5d commit cb747b7
Show file tree
Hide file tree
Showing 33 changed files with 331 additions and 305 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ module.exports = {
"prefer-template": "off",
"no-restricted-properties": [
"error",
{
"object": "window",
"message": "`window` doesn't work in Node.JS and only works when JavaScript is running in the main thread. Please import `globalScope` instead.",
},
{
"object": "Object",
"property": "assign",
Expand Down
123 changes: 51 additions & 72 deletions src/compat/__tests__/browser_compatibility_types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import globalScope from "../global_scope";

// Needed for calling require (which itself is needed to mock properly) because
// it is not type-checked:
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
Expand All @@ -30,123 +32,100 @@ describe("compat - browser compatibility types", () => {
WebKitMediaSource? : unknown;
MSMediaSource? : unknown;
}
const win = window as IFakeWindow;
const gs = globalScope as IFakeWindow;
beforeEach(() => {
jest.resetModules();
});

it("should set the MediaSource to `undefined` when running nodejs", () => {
jest.mock("../is_node", () => ({ __esModule: true as const,
default: true }));

const origMediaSource = win.MediaSource;
const origMozMediaSource = win.MozMediaSource;
const origWebKitMediaSource = win.WebKitMediaSource;
const origMSMediaSource = win.MSMediaSource;

win.MediaSource = { a: 1 };
win.MozMediaSource = { a: 2 };
win.WebKitMediaSource = { a: 3 };
win.MSMediaSource = { a: 4 };

const { MediaSource_ } = jest.requireActual("../browser_compatibility_types");
expect(MediaSource_).toEqual(undefined);

win.MediaSource = origMediaSource;
win.MozMediaSource = origMozMediaSource;
win.WebKitMediaSource = origWebKitMediaSource;
win.MSMediaSource = origMSMediaSource;
});

it("should use the native MediaSource if defined", () => {
jest.mock("../is_node", () => ({ __esModule: true as const,
default: false }));

const origMediaSource = win.MediaSource;
const origMozMediaSource = win.MozMediaSource;
const origWebKitMediaSource = win.WebKitMediaSource;
const origMSMediaSource = win.MSMediaSource;
const origMediaSource = gs.MediaSource;
const origMozMediaSource = gs.MozMediaSource;
const origWebKitMediaSource = gs.WebKitMediaSource;
const origMSMediaSource = gs.MSMediaSource;

win.MediaSource = { a: 1 };
win.MozMediaSource = { a: 2 };
win.WebKitMediaSource = { a: 3 };
win.MSMediaSource = { a: 4 };
gs.MediaSource = { a: 1 };
gs.MozMediaSource = { a: 2 };
gs.WebKitMediaSource = { a: 3 };
gs.MSMediaSource = { a: 4 };

const { MediaSource_ } = jest.requireActual("../browser_compatibility_types");
expect(MediaSource_).toEqual({ a: 1 });

win.MediaSource = origMediaSource;
win.MozMediaSource = origMozMediaSource;
win.WebKitMediaSource = origWebKitMediaSource;
win.MSMediaSource = origMSMediaSource;
gs.MediaSource = origMediaSource;
gs.MozMediaSource = origMozMediaSource;
gs.WebKitMediaSource = origWebKitMediaSource;
gs.MSMediaSource = origMSMediaSource;
});

it("should use MozMediaSource if defined and MediaSource is not", () => {
jest.mock("../is_node", () => ({ __esModule: true as const,
default: false }));

const origMediaSource = win.MediaSource;
const origMozMediaSource = win.MozMediaSource;
const origWebKitMediaSource = win.WebKitMediaSource;
const origMSMediaSource = win.MSMediaSource;
const origMediaSource = gs.MediaSource;
const origMozMediaSource = gs.MozMediaSource;
const origWebKitMediaSource = gs.WebKitMediaSource;
const origMSMediaSource = gs.MSMediaSource;

win.MediaSource = undefined;
win.MozMediaSource = { a: 2 };
win.WebKitMediaSource = undefined;
win.MSMediaSource = undefined;
gs.MediaSource = undefined;
gs.MozMediaSource = { a: 2 };
gs.WebKitMediaSource = undefined;
gs.MSMediaSource = undefined;

const { MediaSource_ } = jest.requireActual("../browser_compatibility_types");
expect(MediaSource_).toEqual({ a: 2 });

win.MediaSource = origMediaSource;
win.MozMediaSource = origMozMediaSource;
win.WebKitMediaSource = origWebKitMediaSource;
win.MSMediaSource = origMSMediaSource;
gs.MediaSource = origMediaSource;
gs.MozMediaSource = origMozMediaSource;
gs.WebKitMediaSource = origWebKitMediaSource;
gs.MSMediaSource = origMSMediaSource;
});

it("should use WebKitMediaSource if defined and MediaSource is not", () => {
jest.mock("../is_node", () => ({ __esModule: true as const,
default: false }));

const origMediaSource = win.MediaSource;
const origMozMediaSource = win.MozMediaSource;
const origWebKitMediaSource = win.WebKitMediaSource;
const origMSMediaSource = win.MSMediaSource;
const origMediaSource = gs.MediaSource;
const origMozMediaSource = gs.MozMediaSource;
const origWebKitMediaSource = gs.WebKitMediaSource;
const origMSMediaSource = gs.MSMediaSource;

win.MediaSource = undefined;
win.MozMediaSource = undefined;
win.WebKitMediaSource = { a: 3 };
win.MSMediaSource = undefined;
gs.MediaSource = undefined;
gs.MozMediaSource = undefined;
gs.WebKitMediaSource = { a: 3 };
gs.MSMediaSource = undefined;

const { MediaSource_ } = jest.requireActual("../browser_compatibility_types");
expect(MediaSource_).toEqual({ a: 3 });

win.MediaSource = origMediaSource;
win.MozMediaSource = origMozMediaSource;
win.WebKitMediaSource = origWebKitMediaSource;
win.MSMediaSource = origMSMediaSource;
gs.MediaSource = origMediaSource;
gs.MozMediaSource = origMozMediaSource;
gs.WebKitMediaSource = origWebKitMediaSource;
gs.MSMediaSource = origMSMediaSource;
});

it("should use MSMediaSource if defined and MediaSource is not", () => {
jest.mock("../is_node", () => ({ __esModule: true as const,
default: false }));

const origMediaSource = win.MediaSource;
const origMozMediaSource = win.MozMediaSource;
const origWebKitMediaSource = win.WebKitMediaSource;
const origMSMediaSource = win.MSMediaSource;
const origMediaSource = gs.MediaSource;
const origMozMediaSource = gs.MozMediaSource;
const origWebKitMediaSource = gs.WebKitMediaSource;
const origMSMediaSource = gs.MSMediaSource;

win.MediaSource = undefined;
win.MozMediaSource = undefined;
win.WebKitMediaSource = undefined;
win.MSMediaSource = { a: 4 };
gs.MediaSource = undefined;
gs.MozMediaSource = undefined;
gs.WebKitMediaSource = undefined;
gs.MSMediaSource = { a: 4 };

const { MediaSource_ } = jest.requireActual("../browser_compatibility_types");
expect(MediaSource_).toEqual({ a: 4 });

win.MediaSource = origMediaSource;
win.MozMediaSource = origMozMediaSource;
win.WebKitMediaSource = origWebKitMediaSource;
win.MSMediaSource = origMSMediaSource;
gs.MediaSource = origMediaSource;
gs.MozMediaSource = origMozMediaSource;
gs.WebKitMediaSource = origWebKitMediaSource;
gs.MSMediaSource = origMSMediaSource;
});
});
36 changes: 20 additions & 16 deletions src/compat/__tests__/is_vtt_cue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import globalScope from "../global_scope";

/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-var-requires */
Expand All @@ -35,37 +37,39 @@ describe("Compat - isVTTCue", () => {
this.text = text;
}
}
const win = window as IFakeWindow;
const gs = globalScope as IFakeWindow;

it("should return true if the given cue is an instance of a vtt cue", () => {
const originalVTTCue = window.VTTCue;
win.VTTCue = MockVTTCue;
const originalVTTCue = globalScope.VTTCue;
gs.VTTCue = MockVTTCue;
const cue = new VTTCue(0, 10, "");
const isVTTCue = jest.requireActual("../is_vtt_cue").default;
expect(isVTTCue(cue)).toEqual(true);
window.VTTCue = originalVTTCue;
globalScope.VTTCue = originalVTTCue;
});

it("should return false if the given cue is not an instance of a vtt cue", () => {
const originalVTTCue = window.VTTCue;
win.VTTCue = MockVTTCue;
const originalVTTCue = globalScope.VTTCue;
gs.VTTCue = MockVTTCue;
const cue = {
startTime: 0,
endTime: 10,
text: "toto",
};
const isVTTCue = jest.requireActual("../is_vtt_cue").default;
expect(isVTTCue(cue)).toEqual(false);
window.VTTCue = originalVTTCue;
globalScope.VTTCue = originalVTTCue;
});

it("should return false in any case if window does not define a VTTCue", () => {
const originalVTTCue = window.VTTCue;
win.VTTCue = MockVTTCue;
const cue = new VTTCue(0, 10, "");
delete win.VTTCue;
const isVTTCue = jest.requireActual("../is_vtt_cue").default;
expect(isVTTCue(cue)).toEqual(false);
window.VTTCue = originalVTTCue;
});
it(
"should return false in any case if the global scope does not define a VTTCue",
() => {
const originalVTTCue = globalScope.VTTCue;
gs.VTTCue = MockVTTCue;
const cue = new VTTCue(0, 10, "");
delete gs.VTTCue;
const isVTTCue = jest.requireActual("../is_vtt_cue").default;
expect(isVTTCue(cue)).toEqual(false);
globalScope.VTTCue = originalVTTCue;
});
});
20 changes: 11 additions & 9 deletions src/compat/__tests__/make_vtt_cue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import globalScope from "../global_scope";

/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-var-requires */
Expand All @@ -33,23 +35,23 @@ describe("Compat - makeVTTCue", () => {
}
}

const win = window as {
const gs = globalScope as {
VTTCue? : unknown;
TextTrackCue? : unknown;
};

const ogVTTuCue = win.VTTCue;
const ogTextTrackCue = win.TextTrackCue;
const ogVTTuCue = gs.VTTCue;
const ogTextTrackCue = gs.TextTrackCue;
beforeEach(() => {
jest.resetModules();
win.VTTCue = ogVTTuCue;
win.TextTrackCue = ogTextTrackCue;
gs.VTTCue = ogVTTuCue;
gs.TextTrackCue = ogTextTrackCue;
});

it("should throw if nor VTTCue nor TextTrackCue is available", () => {
const mockLog = { warn: jest.fn() };
win.VTTCue = undefined;
win.TextTrackCue = undefined;
gs.VTTCue = undefined;
gs.TextTrackCue = undefined;
jest.mock("../../log", () => ({
__esModule: true as const,
default: mockLog,
Expand All @@ -70,7 +72,7 @@ describe("Compat - makeVTTCue", () => {

it("should warn and not create anything if start time is after end time", () => {
const mockLog = { warn: jest.fn() };
win.VTTCue = MockVTTCue;
gs.VTTCue = MockVTTCue;
jest.mock("../../log", () => ({
__esModule: true as const,
default: mockLog,
Expand All @@ -84,7 +86,7 @@ describe("Compat - makeVTTCue", () => {

it("should create a new VTT Cue in other cases", () => {
const mockLog = { warn: jest.fn() };
win.VTTCue = MockVTTCue;
gs.VTTCue = MockVTTCue;
jest.mock("../../log", () => ({
__esModule: true as const,
default: mockLog,
Expand Down
Loading

0 comments on commit cb747b7

Please sign in to comment.