Skip to content

Commit

Permalink
tests: Don't actually call vi.mock for the logger
Browse files Browse the repository at this point in the history
  • Loading branch information
peaBerberian committed Dec 31, 2024
1 parent d921358 commit f7d5638
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 330 deletions.
21 changes: 6 additions & 15 deletions src/compat/__tests__/clear_element_src.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
import { describe, beforeEach, it, expect, vi, afterEach } from "vitest";
import log from "../../log";
import arrayFindIndex from "../../utils/array_find_index";
import type { IMediaElement } from "../browser_compatibility_types";
import clearElementSrc from "../clear_element_src";

const mocks = vi.hoisted(() => {
return {
logWarn: vi.fn(),
};
});

vi.mock("../../log", () => {
return {
default: {
warn: mocks.logWarn,
},
};
const logWarn = vi.spyOn(log, "warn").mockImplementation(() => {
/* noop */
});

describe("Compat - clearElementSrc", () => {
beforeEach(() => {
vi.resetModules();
});
afterEach(() => {
mocks.logWarn.mockReset();
logWarn.mockClear();
});

it("should empty the src and remove the Attribute for a given Element", () => {
Expand Down Expand Up @@ -119,8 +110,8 @@ describe("Compat - clearElementSrc", () => {
expect(spyRemoveAttribute).toHaveBeenCalledWith("src");
expect(spyHasChildNodes).toHaveBeenCalledTimes(1);
expect(spyRemoveChild).toHaveBeenCalledTimes(2);
expect(mocks.logWarn).toHaveBeenCalledTimes(2);
expect(mocks.logWarn).toHaveBeenCalledWith(
expect(logWarn).toHaveBeenCalledTimes(2);
expect(logWarn).toHaveBeenCalledWith(
"Compat: Could not remove text track child from element.",
);
});
Expand Down
25 changes: 8 additions & 17 deletions src/compat/__tests__/make_vtt_cue.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import { describe, beforeEach, it, expect, vi, afterEach } from "vitest";
import log from "../../log";
import globalScope from "../../utils/global_scope";
import makeCue from "../make_vtt_cue";

const mocks = vi.hoisted(() => {
return {
logWarn: vi.fn(),
};
});

vi.mock("../../log", () => {
return {
default: {
warn: mocks.logWarn,
},
};
const logWarn = vi.spyOn(log, "warn").mockImplementation(() => {
/* noop */
});

describe("Compat - makeVTTCue", () => {
afterEach(() => {
mocks.logWarn.mockReset();
logWarn.mockClear();
});
class MockVTTCue {
public startTime: number;
Expand Down Expand Up @@ -57,21 +48,21 @@ describe("Compat - makeVTTCue", () => {
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toEqual("VTT cues not supported in your target");
expect(result).toBe(undefined);
expect(mocks.logWarn).not.toHaveBeenCalled();
expect(logWarn).not.toHaveBeenCalled();
});

it("should warn and not create anything if start time is after end time", () => {
gs.VTTCue = MockVTTCue;
const result = makeCue(12, 10, "toto");
expect(result).toBeNull();
expect(mocks.logWarn).toHaveBeenCalledTimes(1);
expect(mocks.logWarn).toHaveBeenCalledWith("Compat: Invalid cue times: 12 - 10");
expect(logWarn).toHaveBeenCalledTimes(1);
expect(logWarn).toHaveBeenCalledWith("Compat: Invalid cue times: 12 - 10");
});

it("should create a new VTT Cue in other cases", () => {
gs.VTTCue = MockVTTCue;
const result = makeCue(10, 12, "toto");
expect(result).toEqual(new MockVTTCue(10, 12, "toto"));
expect(mocks.logWarn).not.toHaveBeenCalled();
expect(logWarn).not.toHaveBeenCalled();
});
});
26 changes: 8 additions & 18 deletions src/compat/__tests__/remove_cue.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import { describe, beforeEach, afterEach, it, expect, vi } from "vitest";
import log from "../../log";
import arrayFindIndex from "../../utils/array_find_index";
import EnvDetector, { mockEnvironment, resetEnvironment } from "../env_detector";
import removeCue from "../remove_cue";

const mocks = vi.hoisted(() => {
return {
logWarn: vi.fn(),
};
const logWarn = vi.spyOn(log, "warn").mockImplementation(() => {
/* noop */
});
vi.mock("../../log", () => ({
default: {
warn: mocks.logWarn,
},
}));

describe("compat - removeCue", () => {
beforeEach(() => {
vi.resetModules();
});
afterEach(() => {
resetEnvironment();
mocks.logWarn.mockReset();
logWarn.mockClear();
});

it("should remove cue from track if not on firefox", () => {
Expand Down Expand Up @@ -169,10 +163,8 @@ describe("compat - removeCue", () => {

expect(fakeTrack.cues?.length).toBe(1);
expect(fakeTrack.mode).toBe("showing");
expect(mocks.logWarn).toHaveBeenCalledTimes(1);
expect(mocks.logWarn).toHaveBeenCalledWith(
"Compat: Could not remove cue from text track.",
);
expect(logWarn).toHaveBeenCalledTimes(1);
expect(logWarn).toHaveBeenCalledWith("Compat: Could not remove cue from text track.");
expect(mockRemoveCue).toHaveBeenCalledTimes(1);
expect(mockRemoveCue).toHaveBeenLastCalledWith(fakeCue);
});
Expand All @@ -194,10 +186,8 @@ describe("compat - removeCue", () => {

expect(fakeTrack.cues?.length).toBe(1);
expect(fakeTrack.mode).toBe("showing");
expect(mocks.logWarn).toHaveBeenCalledTimes(1);
expect(mocks.logWarn).toHaveBeenCalledWith(
"Compat: Could not remove cue from text track.",
);
expect(logWarn).toHaveBeenCalledTimes(1);
expect(logWarn).toHaveBeenCalledWith("Compat: Could not remove cue from text track.");
expect(mockRemoveCue).toHaveBeenCalledTimes(1);
expect(mockRemoveCue).toHaveBeenLastCalledWith({ id: "1" });
});
Expand Down
26 changes: 9 additions & 17 deletions src/core/adaptive/__tests__/buffer_based_chooser.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
import { describe, beforeEach, it, expect, vi, afterEach } from "vitest";
import log from "../../../log";
import BufferBasedChooser from "../buffer_based_chooser";
import { ScoreConfidenceLevel } from "../utils/representation_score_calculator";

const mocks = vi.hoisted(() => {
return {
logDebug: vi.fn(),
logInfo: vi.fn(),
};
const logDebug = vi.spyOn(log, "debug").mockImplementation(() => {
/* noop */
});

vi.mock("../../../log", () => {
return {
default: {
debug: mocks.logDebug,
info: mocks.logInfo,
},
};
const logInfo = vi.spyOn(log, "info").mockImplementation(() => {
/* noop */
});

describe("BufferBasedChooser", () => {
beforeEach(() => {
vi.resetModules();
});
afterEach(() => {
mocks.logDebug.mockReset();
mocks.logInfo.mockReset();
logDebug.mockClear();
logInfo.mockClear();
});

it("should return the first bitrate if the current bitrate is undefined", () => {
Expand Down Expand Up @@ -91,8 +83,8 @@ describe("BufferBasedChooser", () => {
currentScore: undefined,
});
expect(bbc.getLastEstimate()).toEqual(10);
expect(mocks.logInfo).toHaveBeenCalledTimes(1);
expect(mocks.logInfo).toHaveBeenCalledWith(
expect(logInfo).toHaveBeenCalledTimes(1);
expect(logInfo).toHaveBeenCalledWith(
"ABR: Current Bitrate not found in the calculated levels",
);
});
Expand Down
48 changes: 22 additions & 26 deletions src/main_thread/api/__tests__/option_utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, afterEach, it, expect, vi } from "vitest";
import config from "../../../config";
import log from "../../../log";
import type {
IAudioTrackSwitchingMode,
IKeySystemOption,
Expand All @@ -14,26 +15,21 @@ import {

const mocks = vi.hoisted(() => {
return {
logWarn: vi.fn(),
warnOnce: vi.fn(),
};
});
vi.mock("../../../log", () => {
return {
default: {
warn: mocks.logWarn,
},
};
});
vi.mock("../../../utils/languages");
vi.mock("../../../utils/warn_once", () => {
return { default: mocks.warnOnce };
});
const logWarn = vi.spyOn(log, "warn").mockImplementation(() => {
/* noop */
});

describe("API - parseConstructorOptions", () => {
afterEach(() => {
vi.resetModules();
mocks.logWarn.mockReset();
logWarn.mockClear();
mocks.warnOnce.mockReset();
});

Expand Down Expand Up @@ -254,7 +250,7 @@ describe("API - parseConstructorOptions", () => {
describe("API - parseLoadVideoOptions", () => {
afterEach(() => {
vi.resetModules();
mocks.logWarn.mockReset();
logWarn.mockClear();
mocks.warnOnce.mockReset();
});

Expand Down Expand Up @@ -659,7 +655,7 @@ describe("API - parseLoadVideoOptions", () => {
});

it("should set an 'undefined' defaultAudioTrackSwitchingMode mode when the parameter is invalid or not specified", () => {
mocks.logWarn.mockReturnValue(undefined);
logWarn.mockReturnValue(undefined);
expect(
parseLoadVideoOptions({
defaultAudioTrackSwitchingMode: "foo-bar" as unknown as IAudioTrackSwitchingMode,
Expand All @@ -672,16 +668,16 @@ describe("API - parseLoadVideoOptions", () => {
transport: "bar",
defaultAudioTrackSwitchingMode: undefined,
});
expect(mocks.logWarn).toHaveBeenCalledTimes(1);
expect(mocks.logWarn).toHaveBeenCalledWith(
expect(logWarn).toHaveBeenCalledTimes(1);
expect(logWarn).toHaveBeenCalledWith(
"The `defaultAudioTrackSwitchingMode` loadVideo option must match one of " +
`the following strategy name:
- \`seamless\`
- \`direct\`
- \`reload\``,
);
mocks.logWarn.mockReset();
mocks.logWarn.mockReturnValue(undefined);
logWarn.mockClear();
logWarn.mockReturnValue(undefined);

expect(
parseLoadVideoOptions({
Expand All @@ -694,7 +690,7 @@ describe("API - parseLoadVideoOptions", () => {
transport: "bar",
defaultAudioTrackSwitchingMode: undefined,
});
expect(mocks.logWarn).not.toHaveBeenCalled();
expect(logWarn).not.toHaveBeenCalled();
});

it("should authorize setting a valid onCodecSwitch option", () => {
Expand Down Expand Up @@ -726,7 +722,7 @@ describe("API - parseLoadVideoOptions", () => {
});

it("should set a 'continue' onCodecSwitch when the parameter is invalid or not specified", () => {
mocks.logWarn.mockReturnValue(undefined);
logWarn.mockReturnValue(undefined);
expect(
parseLoadVideoOptions({
onCodecSwitch: "foo-bar" as unknown as "continue",
Expand All @@ -739,16 +735,16 @@ describe("API - parseLoadVideoOptions", () => {
transport: "bar",
onCodecSwitch: "continue",
});
expect(mocks.logWarn).toHaveBeenCalledTimes(1);
expect(mocks.logWarn).toHaveBeenCalledWith(
expect(logWarn).toHaveBeenCalledTimes(1);
expect(logWarn).toHaveBeenCalledWith(
"The `onCodecSwitch` loadVideo option must match one " +
`of the following string:
- \`continue\`
- \`reload\`
If badly set, continue will be used as default`,
);
mocks.logWarn.mockReset();
mocks.logWarn.mockReturnValue(undefined);
logWarn.mockClear();
logWarn.mockReturnValue(undefined);

expect(
parseLoadVideoOptions({
Expand All @@ -761,7 +757,7 @@ If badly set, continue will be used as default`,
transport: "bar",
onCodecSwitch: "continue",
});
expect(mocks.logWarn).not.toHaveBeenCalled();
expect(logWarn).not.toHaveBeenCalled();
});

it("should authorize setting a valid enableFastSwitching option", () => {
Expand Down Expand Up @@ -1020,15 +1016,15 @@ If badly set, continue will be used as default`,
});

it("should warn when setting a textTrackElement with a `native` textTrackMode", () => {
mocks.logWarn.mockReturnValue(undefined);
logWarn.mockReturnValue(undefined);
const textTrackElement = document.createElement("div");

parseLoadVideoOptions({
textTrackMode: "native",
url: "foo",
transport: "bar",
});
expect(mocks.logWarn).not.toHaveBeenCalled();
expect(logWarn).not.toHaveBeenCalled();

expect(
parseLoadVideoOptions({
Expand All @@ -1044,8 +1040,8 @@ If badly set, continue will be used as default`,
textTrackMode: "native",
});

expect(mocks.logWarn).toHaveBeenCalledTimes(1);
expect(mocks.logWarn).toHaveBeenCalledWith(
expect(logWarn).toHaveBeenCalledTimes(1);
expect(logWarn).toHaveBeenCalledWith(
"API: You have set a textTrackElement " +
'without being in an "html" textTrackMode. It will be ignored.',
);
Expand Down
Loading

0 comments on commit f7d5638

Please sign in to comment.