Skip to content

Commit

Permalink
Migrate to Vitest v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Jul 15, 2024
1 parent 0268722 commit 08c2903
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 64 deletions.
18 changes: 0 additions & 18 deletions test/helpers.ts

This file was deleted.

37 changes: 22 additions & 15 deletions test/vitest/delegated.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ import {
createPermissionStore,
createPermissions,
} from "fake-permissions";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { mockFn, type Mocked } from "../helpers.js";
import {
afterEach,
beforeEach,
describe,
expect,
it,
vi,
type Mock,
} from "vitest";

describe("Delegated permissions", () => {
const permissionA: PermissionDescriptor = {
Expand All @@ -30,9 +37,9 @@ describe("Delegated permissions", () => {
let statusA: PermissionStatus;
let statusB: PermissionStatus;
let statusC: PermissionStatus;
let listenerA: Mocked;
let listenerB: Mocked;
let listenerC: Mocked;
let listenerA: Mock;
let listenerB: Mock;
let listenerC: Mock;

beforeEach(() => {
permissionStoreA = createPermissionStore({
Expand Down Expand Up @@ -104,9 +111,9 @@ describe("Delegated permissions", () => {
statusB = await permissions.query(permissionB);
statusC = await permissions.query(permissionC);

listenerA = mockFn();
listenerB = mockFn();
listenerC = mockFn();
listenerA = vi.fn();
listenerB = vi.fn();
listenerC = vi.fn();

statusA.addEventListener("change", listenerA);
statusB.addEventListener("change", listenerB);
Expand Down Expand Up @@ -207,9 +214,9 @@ describe("Delegated permissions", () => {

describe("when a change event listener is added by setting onchange", () => {
beforeEach(() => {
listenerA = mockFn();
listenerB = mockFn();
listenerC = mockFn();
listenerA = vi.fn();
listenerB = vi.fn();
listenerC = vi.fn();

statusA.onchange = listenerA;
statusB.onchange = listenerB;
Expand Down Expand Up @@ -245,7 +252,7 @@ describe("Delegated permissions", () => {

describe("when a non-change event listener is added", () => {
beforeEach(() => {
listenerA = mockFn();
listenerA = vi.fn();
statusA.addEventListener("event-a", listenerA);
});

Expand Down Expand Up @@ -282,9 +289,9 @@ describe("Delegated permissions", () => {
statusB = await permissions.query(permissionB);
statusC = await permissions.query(permissionC);

listenerA = mockFn();
listenerB = mockFn();
listenerC = mockFn();
listenerA = vi.fn();
listenerB = vi.fn();
listenerC = vi.fn();

statusA.addEventListener("change", listenerA);
statusB.addEventListener("change", listenerB);
Expand Down
43 changes: 20 additions & 23 deletions test/vitest/permission-status.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import {
expect,
it,
vi,
type Mock,
type MockInstance,
} from "vitest";
import { mockFn, type Mocked } from "../helpers.js";

describe("PermissionStatus", () => {
const permissionA: PermissionDescriptor = {
Expand All @@ -28,10 +28,7 @@ describe("PermissionStatus", () => {
};

let permissionStore: PermissionStore;
let subscribe: MockInstance<
Parameters<PermissionStore["subscribe"]>,
ReturnType<PermissionStore["subscribe"]>
>;
let subscribe: MockInstance<PermissionStore["subscribe"]>;

let user: User;
let permissions: Permissions;
Expand Down Expand Up @@ -103,12 +100,12 @@ describe("PermissionStatus", () => {
});

describe("when a change event listener is added by setting onchange", () => {
let listenerA: Mocked;
let listenerB: Mocked;
let listenerA: Mock;
let listenerB: Mock;

beforeEach(() => {
listenerA = mockFn();
listenerB = mockFn();
listenerA = vi.fn();
listenerB = vi.fn();

statusA.onchange = listenerA;
statusB.onchange = listenerA;
Expand Down Expand Up @@ -167,10 +164,10 @@ describe("PermissionStatus", () => {
});

describe("when a change event listener is added by calling addEventListener()", () => {
let listener: Mocked;
let listener: Mock;

beforeEach(() => {
listener = mockFn();
listener = vi.fn();
statusA.addEventListener("change", listener);
statusB.addEventListener("change", listener);
statusC.addEventListener("change", listener);
Expand Down Expand Up @@ -240,10 +237,10 @@ describe("PermissionStatus", () => {
});

describe("when an object-based change event listener is added by calling addEventListener()", () => {
let listener: { handleEvent: Mocked };
let listener: { handleEvent: Mock };

beforeEach(() => {
listener = { handleEvent: mockFn() };
listener = { handleEvent: vi.fn() };
statusA.addEventListener("change", listener);
statusB.addEventListener("change", listener);
statusC.addEventListener("change", listener);
Expand Down Expand Up @@ -313,10 +310,10 @@ describe("PermissionStatus", () => {
});

describe('when a change event listener is added by calling addEventListener() with the "once" option', () => {
let listener: Mocked;
let listener: Mock;

beforeEach(() => {
listener = mockFn();
listener = vi.fn();
statusA.addEventListener("change", listener, { once: true });
statusB.addEventListener("change", listener, { once: true });
statusC.addEventListener("change", listener, { once: true });
Expand Down Expand Up @@ -386,11 +383,11 @@ describe("PermissionStatus", () => {
});

describe('when a change event listener is added by calling addEventListener() with the "signal" option', () => {
let listener: Mocked;
let listener: Mock;
let controller: AbortController;

beforeEach(() => {
listener = mockFn();
listener = vi.fn();
controller = new AbortController();
const { signal } = controller;

Expand Down Expand Up @@ -437,10 +434,10 @@ describe("PermissionStatus", () => {
});

describe('when a change event listener is added by calling addEventListener() in the "capture" phase', () => {
let listener: Mocked;
let listener: Mock;

beforeEach(() => {
listener = mockFn();
listener = vi.fn();
statusA.addEventListener("change", listener, true);
statusB.addEventListener("change", listener, true);
statusC.addEventListener("change", listener, true);
Expand Down Expand Up @@ -526,10 +523,10 @@ describe("PermissionStatus", () => {
});

describe("when removeEventListener() is called with a change event listener that is not registered", () => {
let listener: Mocked;
let listener: Mock;

beforeEach(() => {
listener = mockFn();
listener = vi.fn();
});

it("has no effect", () => {
Expand All @@ -540,10 +537,10 @@ describe("PermissionStatus", () => {
});

describe("when a non-change event listener is added", () => {
let listener: Mocked;
let listener: Mock;

beforeEach(() => {
listener = mockFn();
listener = vi.fn();
statusA.addEventListener("event-a", listener);
});

Expand Down
15 changes: 11 additions & 4 deletions test/vitest/permission-store.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { PermissionStore, createPermissionStore } from "fake-permissions";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { mockFn, type Mocked } from "../helpers.js";
import {
afterEach,
beforeEach,
describe,
expect,
it,
vi,
type Mock,
} from "vitest";

describe("PermissionStore()", () => {
const geolocation: PermissionDescriptor = { name: "geolocation" };
Expand Down Expand Up @@ -138,10 +145,10 @@ describe("PermissionStore()", () => {
});

describe("subscribe()", () => {
let subscriber: Mocked;
let subscriber: Mock;

beforeEach(() => {
permissionStore.subscribe((subscriber = mockFn()));
permissionStore.subscribe((subscriber = vi.fn()));
});

afterEach(() => {
Expand Down
7 changes: 3 additions & 4 deletions test/vitest/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
createPermissionStore,
createUser,
} from "fake-permissions";
import { beforeEach, describe, expect, it } from "vitest";
import { mockFn, type Mocked } from "../helpers.js";
import { beforeEach, describe, expect, it, vi, type Mock } from "vitest";

describe("User", () => {
const permissionA: PermissionDescriptor = {
Expand Down Expand Up @@ -60,10 +59,10 @@ describe("User", () => {
});

describe("when a permission request callback is configured", () => {
let handlePermissionRequest: Mocked<HandlePermissionRequest>;
let handlePermissionRequest: Mock<HandlePermissionRequest>;

beforeEach(() => {
handlePermissionRequest = mockFn(
handlePermissionRequest = vi.fn(
// eslint-disable-next-line @typescript-eslint/no-misused-promises
(async () => "granted") as HandlePermissionRequest,
);
Expand Down

0 comments on commit 08c2903

Please sign in to comment.