Skip to content

Commit

Permalink
chore: mock console.error in prepare zxing module unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sec-ant committed Dec 27, 2024
1 parent cf22ff0 commit 4819194
Showing 1 changed file with 120 additions and 112 deletions.
232 changes: 120 additions & 112 deletions tests/unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,172 +1,180 @@
import { expect, test } from "vitest";
import { afterAll, describe, expect, test, vi } from "vitest";
import {
prepareZXingModule as prepareZXingFullModule,
purgeZXingModule as purgeZXingFullModule,
} from "../src/full/index.js";
import { prepareZXingModule as prepareZXingReaderModule } from "../src/reader/index.js";

test("no module promise should be created without fireImmediately", () => {
const modulePromise = prepareZXingFullModule();
describe("prepare zxing module", () => {
const consoleMock = vi.spyOn(console, "error").mockImplementation(() => {});

expect(modulePromise).toBe(undefined);
});

test("module promise should be created with fireImmediately", () => {
const modulePromise = prepareZXingFullModule({
fireImmediately: true,
afterAll(() => {
consoleMock.mockReset();
});

modulePromise.catch(() => {});

expect(modulePromise).toBeInstanceOf(Promise);
});
test("no module promise should be created without fireImmediately", () => {
const modulePromise = prepareZXingFullModule();

test("module promise should be reused with no overrides", () => {
const modulePromise1 = prepareZXingFullModule({
fireImmediately: true,
expect(modulePromise).toBe(undefined);
});

modulePromise1.catch(() => {});
test("module promise should be created with fireImmediately", () => {
const modulePromise = prepareZXingFullModule({
fireImmediately: true,
});

modulePromise.catch(() => {});

const modulePromise2 = prepareZXingFullModule({
fireImmediately: true,
expect(modulePromise).toBeInstanceOf(Promise);
});

modulePromise2.catch(() => {});
test("module promise should be reused with no overrides", () => {
const modulePromise1 = prepareZXingFullModule({
fireImmediately: true,
});

expect(modulePromise1).toBe(modulePromise2);
});
modulePromise1.catch(() => {});

test("module promise should be reused with same overrides (Object.is)", () => {
const overrides = {};
const modulePromise2 = prepareZXingFullModule({
fireImmediately: true,
});

const modulePromise1 = prepareZXingFullModule({
overrides,
fireImmediately: true,
modulePromise2.catch(() => {});

expect(modulePromise1).toBe(modulePromise2);
});

modulePromise1.catch(() => {});
test("module promise should be reused with same overrides (Object.is)", () => {
const overrides = {};

const modulePromise2 = prepareZXingFullModule({
overrides,
fireImmediately: true,
});
const modulePromise1 = prepareZXingFullModule({
overrides,
fireImmediately: true,
});

modulePromise2.catch(() => {});
modulePromise1.catch(() => {});

expect(modulePromise1).toBe(modulePromise2);
});
const modulePromise2 = prepareZXingFullModule({
overrides,
fireImmediately: true,
});

test("module promise should be reused with same overrides (shallow)", () => {
const modulePromise1 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
modulePromise2.catch(() => {});

expect(modulePromise1).toBe(modulePromise2);
});

modulePromise1.catch(() => {});
test("module promise should be reused with same overrides (shallow)", () => {
const modulePromise1 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
});

const modulePromise2 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
});
modulePromise1.catch(() => {});

modulePromise2.catch(() => {});
const modulePromise2 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
});

expect(modulePromise1).toBe(modulePromise2);
});
modulePromise2.catch(() => {});

test("module promise shouldn't be reused with different overrides", () => {
const modulePromise1 = prepareZXingFullModule({
overrides: {
locateFile: (url) => url,
},
fireImmediately: true,
expect(modulePromise1).toBe(modulePromise2);
});

modulePromise1.catch(() => {});
test("module promise shouldn't be reused with different overrides", () => {
const modulePromise1 = prepareZXingFullModule({
overrides: {
locateFile: (url) => url,
},
fireImmediately: true,
});

const modulePromise2 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
});
modulePromise1.catch(() => {});

modulePromise2.catch(() => {});
const modulePromise2 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
});

const modulePromise3 = prepareZXingFullModule({
overrides: {
locateFile: (url) => url,
},
fireImmediately: true,
});
modulePromise2.catch(() => {});

modulePromise3.catch(() => {});
const modulePromise3 = prepareZXingFullModule({
overrides: {
locateFile: (url) => url,
},
fireImmediately: true,
});

expect(modulePromise1).not.toBe(modulePromise2);
expect(modulePromise1).not.toBe(modulePromise3);
});
modulePromise3.catch(() => {});

test("equalityFn should work", () => {
const modulePromise1 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
expect(modulePromise1).not.toBe(modulePromise2);
expect(modulePromise1).not.toBe(modulePromise3);
});

modulePromise1.catch(() => {});
test("equalityFn should work", () => {
const modulePromise1 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
});

const modulePromise2 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
equalityFn: Object.is,
});
modulePromise1.catch(() => {});

modulePromise2.catch(() => {});
const modulePromise2 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
equalityFn: Object.is,
});

expect(modulePromise1).not.toBe(modulePromise2);
});
modulePromise2.catch(() => {});

test("purgeZXingModule should work", () => {
const modulePromise1 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
expect(modulePromise1).not.toBe(modulePromise2);
});

modulePromise1.catch(() => {});
test("purgeZXingModule should work", () => {
const modulePromise1 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
});

purgeZXingFullModule();
modulePromise1.catch(() => {});

const modulePromise2 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
});
purgeZXingFullModule();

modulePromise2.catch(() => {});
const modulePromise2 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
});

expect(modulePromise1).not.toBe(modulePromise2);
});
modulePromise2.catch(() => {});

test("purgeZXingModule shouldn't affect each other", () => {
const modulePromise1 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
expect(modulePromise1).not.toBe(modulePromise2);
});

modulePromise1.catch(() => {});
test("purgeZXingModule shouldn't affect each other", () => {
const modulePromise1 = prepareZXingFullModule({
overrides: {},
fireImmediately: true,
});

const modulePromise2 = prepareZXingReaderModule({
overrides: {},
fireImmediately: true,
});
modulePromise1.catch(() => {});

modulePromise2.catch(() => {});
const modulePromise2 = prepareZXingReaderModule({
overrides: {},
fireImmediately: true,
});

purgeZXingFullModule();
modulePromise2.catch(() => {});

const modulePromise3 = prepareZXingReaderModule({
overrides: {},
fireImmediately: true,
});
purgeZXingFullModule();

modulePromise3.catch(() => {});
const modulePromise3 = prepareZXingReaderModule({
overrides: {},
fireImmediately: true,
});

expect(modulePromise2).toBe(modulePromise3);
modulePromise3.catch(() => {});

expect(modulePromise2).toBe(modulePromise3);
});
});

0 comments on commit 4819194

Please sign in to comment.