Skip to content

Commit

Permalink
chore: Use equality tester to simplify assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Oct 15, 2024
1 parent 129e8b0 commit 0b6b46e
Show file tree
Hide file tree
Showing 26 changed files with 80 additions and 107 deletions.
5 changes: 0 additions & 5 deletions test-utils/async-next-tick.ts

This file was deleted.

46 changes: 13 additions & 33 deletions test-utils/jest-extend-expect.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,17 @@
/* eslint-disable @typescript-eslint/no-empty-object-type */
/* eslint-disable @typescript-eslint/no-namespace */
export interface CustomMatchers<R = unknown> {
toContainId(id: string): R;
}
import { expect } from "@jest/globals";

// https://jestjs.io/docs/expect#expectaddequalitytesterstesters
function idChecker(a: unknown, b: unknown) {
const hasAId = typeof a === "object" && a !== null && "id" in a;
const hasBId = typeof b === "object" && b !== null && "id" in b;

declare global {
namespace jest {
interface Expect extends CustomMatchers {}
interface Matchers<R> extends CustomMatchers<R> {}
interface InverseAsymmetricMatchers extends CustomMatchers {}
if (hasAId && hasBId) {
return a.id === b.id;
} else if (hasAId === hasBId) {
return;
} else {
return false;
}
}

expect.extend({
toContainId<T extends { id: string }>(received?: T, expected?: string) {
if (!Array.isArray(received)) {
return { pass: false, message: () => "Expected an array" };
}

if (received.length === 0) {
return {
pass: false,
message: () => "Expected an array with at least one element",
};
}

const pass = received.some((entity) => entity.id === expected);

return {
pass,
message: () => {
return `List does not contain ${expected}`;
},
};
},
});
expect.addEqualityTesters([idChecker]);
5 changes: 4 additions & 1 deletion test-utils/jest-setup-after-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import "./jest-extend-expect";
import { createRestAPIClient } from "../src";
import { SessionPoolImpl } from "./pools";

jest.retryTimes(3);
if (process.env.CI) {
jest.retryTimes(3);
}

jest.setTimeout(1000 * 60);

globalThis.admin = createRestAPIClient({
Expand Down
2 changes: 1 addition & 1 deletion test-utils/pools/session-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class SessionPoolImpl implements Pool<Session> {
if (token == undefined) {
// eslint-disable-next-line no-console
console.warn(
`Session ${session.id} (${session.acct}) is already released`,
`Session ${session.id} (${session.account.acct}) is already released`,
);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions test-utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {

export interface Session {
readonly id: string;
readonly acct: string;
readonly account: mastodon.v1.Account;
readonly rest: mastodon.rest.Client;
readonly ws: mastodon.streaming.Client;
readonly [Symbol.asyncDispose]: () => Promise<void>;
Expand All @@ -32,7 +32,7 @@ export const createSession = async (

return Object.freeze({
id: account.id,
acct: account.acct,
account,
rest,
ws,
[Symbol.asyncDispose]: dispose,
Expand Down
18 changes: 8 additions & 10 deletions tests/rest/v1/accounts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ describe("account", () => {
.$select(bob.id)
.followers.list();

expect(followers).toContainId(alice.id);
expect(followers).toContainEqual(alice.account);
await alice.rest.v1.accounts.$select(bob.id).unfollow();
});

Expand All @@ -233,7 +233,7 @@ describe("account", () => {
.$select(alice.id)
.following.list();

expect(accounts).toContainId(bob.id);
expect(accounts).toContainEqual(bob.account);
await alice.rest.v1.accounts.$select(bob.id).unfollow();
});

Expand All @@ -244,7 +244,7 @@ describe("account", () => {
.$select(status.account.id)
.statuses.list();

expect(statuses).toContainId(status.id);
expect(statuses).toContainEqual(status);
});

it("searches", async () => {
Expand All @@ -253,7 +253,7 @@ describe("account", () => {
const accounts = await client.rest.v1.accounts.search.list({
q: me.username,
});
expect(accounts).toContainId(me.id);
expect(accounts).toContainEqual(me);
});

it("lists lists", async () => {
Expand All @@ -266,10 +266,8 @@ describe("account", () => {
await alice.rest.v1.lists.$select(list.id).accounts.create({
accountIds: [bob.id],
});
const accounts = await alice.rest.v1.accounts
.$select(bob.id)
.lists.list();
expect(accounts).toContainId(list.id);
const lists = await alice.rest.v1.accounts.$select(bob.id).lists.list();
expect(lists).toContainEqual(list);
} finally {
await alice.rest.v1.lists.$select(list.id).remove();
}
Expand All @@ -284,7 +282,7 @@ describe("account", () => {
const tags = await client.rest.v1.accounts
.$select(client.id)
.featuredTags.list();
expect(tags).toContainId(featuredTag.id);
expect(tags).toContainEqual(featuredTag);

await client.rest.v1.featuredTags.$select(featuredTag.id).remove();
});
Expand All @@ -308,7 +306,7 @@ describe("account", () => {
it("lookup", async () => {
await using client = await sessions.acquire();
const account = await client.rest.v1.accounts.lookup({
acct: client.acct,
acct: client.account.acct,
});
expect(account.id).toBe(client.id);
});
Expand Down
4 changes: 2 additions & 2 deletions tests/rest/v1/admin/canonical-email-blocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ describe("canonical-email-blocks", () => {
const result = await admin.v1.admin.canonicalEmailBlocks.test({
email: "test@example.com",
});
expect(result).toContainId(canonicalEmailBlock.id);
expect(result).toContainEqual(canonicalEmailBlock);

const canonicalEmailBlocks =
await admin.v1.admin.canonicalEmailBlocks.list();
expect(canonicalEmailBlocks).toContainId(canonicalEmailBlock.id);
expect(canonicalEmailBlocks).toContainEqual(canonicalEmailBlock);
} finally {
await admin.v1.admin.canonicalEmailBlocks
.$select(canonicalEmailBlock.id)
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/v1/admin/domain-allows.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ it("handles domain allows", async () => {
expect(domainAllow.domain).toMatch(/example.domain.to.allow.com/);

const list = await admin.v1.admin.domainAllows.list();
expect(list).toContainId(domainAllow.id);
expect(list).toContainEqual(domainAllow);
} finally {
await admin.v1.admin.domainAllows.$select(domainAllow.id).remove();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/v1/admin/domain-blocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ it("handles domain blocks", async () => {
expect(domainBlock.rejectMedia).toBe(false);

const list = await admin.v1.admin.domainBlocks.list();
expect(list).toContainId(domainBlock.id);
expect(list).toContainEqual(domainBlock);
} finally {
await admin.v1.admin.domainBlocks.$select(domainBlock.id).remove();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/v1/admin/email-domain-blocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ it("handle email domain block", async () => {
expect(emailDomainBlock.domain).toMatch(/example.domain.to.block.com/);

const list = await admin.v1.admin.emailDomainBlocks.list();
expect(list).toContainId(emailDomainBlock.id);
expect(list).toContainEqual(emailDomainBlock);
} finally {
await admin.v1.admin.emailDomainBlocks
.$select(emailDomainBlock.id)
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/v1/admin/ip-blocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ it("handles ip blocks", async () => {
expect(ipBlock.severity).toBe("sign_up_requires_approval");

const list = await admin.v1.admin.ipBlocks.list();
expect(list).toContainId(ipBlock.id);
expect(list).toContainEqual(ipBlock);
} finally {
await admin.v1.admin.ipBlocks.$select(ipBlock.id).remove();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/v1/blocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("blocks", () => {
try {
await alice.rest.v1.accounts.$select(bob.id).block();
const blocks = await alice.rest.v1.blocks.list();
expect(blocks).toContainId(bob.id);
expect(blocks).toContainEqual(bob.account);
} finally {
await alice.rest.v1.accounts.$select(bob.id).unblock();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/v1/bookmarks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("bookmarks", () => {
try {
await client.rest.v1.statuses.$select(status.id).bookmark();
const bookmarks = await client.rest.v1.bookmarks.list();
expect(bookmarks).toContainId(status.id);
expect(bookmarks).toContainEqual(status);
} finally {
await client.rest.v1.statuses.$select(status.id).unbookmark();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/rest/v1/conversations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("conversations", () => {
await bob.rest.v1.accounts.$select(alice.id).follow();

const status = await bob.rest.v1.statuses.create({
status: `@${alice.acct} Hi alice`,
status: `@${alice.account.acct} Hi alice`,
visibility: "direct",
});

Expand All @@ -25,7 +25,7 @@ describe("conversations", () => {
conversation = conversations.find(
(c) => c.lastStatus?.id === status.id,
);
expect(conversation?.accounts.map((a) => a.id)).toContain(bob.id);
expect(conversation?.accounts).toContainEqual(bob.account);
});

assert(conversation != undefined);
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/v1/endorsements.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ it("lists endorsements", async () => {
await alice.rest.v1.accounts.$select(bob.id).pin();
const endorsements = await alice.rest.v1.endorsements.list();

expect(endorsements).toContainId(bob.id);
expect(endorsements).toContainEqual(bob.account);
} finally {
await alice.rest.v1.accounts.$select(bob.id).unfollow();
await alice.rest.v1.accounts.$select(bob.id).unpin();
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/v1/favourites.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ it("list favourites", async () => {
try {
await client.rest.v1.statuses.$select(status.id).favourite();
const statuses = await client.rest.v1.favourites.list();
expect(statuses).toContainId(status.id);
expect(statuses).toContainEqual(status);
} finally {
await client.rest.v1.statuses.$select(status.id).remove();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/v1/filters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ it("lists filters", async () => {
expect(filter.context).toEqual(["home", "notifications"]);

const filters = await client.rest.v1.filters.list();
expect(filters).toContainId(filter.id);
expect(filters).toContainEqual(filter);
} finally {
await client.rest.v1.filters.$select(filter.id).remove();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/rest/v1/follow-requests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ it("authorize follow requests", async () => {
expect(relationship.requested).toBe(true);

const followRequests = await alice.rest.v1.followRequests.list();
expect(followRequests).toContainId(bob.id);
expect(followRequests).toContainEqual(bob.account);

await alice.rest.v1.followRequests.$select(bob.id).authorize();
[relationship] = await bob.rest.v1.accounts.relationships.fetch({
Expand Down Expand Up @@ -43,7 +43,7 @@ it("rejects follow requests", async () => {
expect(relationship.requested).toBe(true);

const followRequests = await alice.rest.v1.followRequests.list();
expect(followRequests).toContainId(bob.id);
expect(followRequests).toContainEqual(bob.account);

await alice.rest.v1.followRequests.$select(bob.id).reject();
[relationship] = await bob.rest.v1.accounts.relationships.fetch({
Expand Down
4 changes: 2 additions & 2 deletions tests/rest/v1/lists.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ it("mutates a list", async () => {
list = await alice.rest.v1.lists.$select(list.id).fetch();

const lists = await alice.rest.v1.lists.list();
expect(lists).toContainId(list.id);
expect(lists).toContainEqual(list);

await alice.rest.v1.lists
.$select(list.id)
.accounts.create({ accountIds: [bob.id] });

const accounts = await alice.rest.v1.lists.$select(list.id).accounts.list();
expect(accounts).toContainId(bob.id);
expect(accounts).toContainEqual(bob.account);
} finally {
await alice.rest.v1.lists
.$select(list.id)
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/v1/mutes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ it("lists mute", async () => {

try {
const mutes = await alice.rest.v1.mutes.list();
expect(mutes).toContainId(bob.id);
expect(mutes).toContainEqual(bob.account);
} finally {
await alice.rest.v1.accounts.$select(bob.id).unmute();
}
Expand Down
16 changes: 8 additions & 8 deletions tests/rest/v1/notifications.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ it("handles notifications", async () => {
await using alice = await sessions.acquire();
await using bob = await sessions.acquire();
const status = await bob.rest.v1.statuses.create({
status: `@${alice.acct} Hello`,
status: `@${alice.account.acct} Hello`,
});

try {
Expand All @@ -28,7 +28,7 @@ it("handles notifications", async () => {
await alice.rest.v1.notifications.$select(notification.id).dismiss();

notifications = await alice.rest.v1.notifications.list();
expect(notifications).not.toContainId(notification.id);
expect(notifications).not.toContainEqual(notification);
} finally {
await alice.rest.v1.notifications.clear();
await bob.rest.v1.statuses.$select(status.id).remove();
Expand All @@ -40,23 +40,23 @@ it("clear notifications", async () => {
await using bob = await sessions.acquire();

const s1 = await bob.rest.v1.statuses.create({
status: `@${alice.acct} Hello 1`,
status: `@${alice.account.acct} Hello 1`,
});
const s2 = await bob.rest.v1.statuses.create({
status: `@${alice.acct} Hello 2`,
status: `@${alice.account.acct} Hello 2`,
});
const s3 = await bob.rest.v1.statuses.create({
status: `@${alice.acct} Hello 3`,
status: `@${alice.account.acct} Hello 3`,
});

try {
let notifications = await alice.rest.v1.notifications.list();

await waitForExpect(async () => {
notifications = await alice.rest.v1.notifications.list();
expect(notifications.map((n) => n.status?.id)).toContain(s1.id);
expect(notifications.map((n) => n.status?.id)).toContain(s2.id);
expect(notifications.map((n) => n.status?.id)).toContain(s3.id);
expect(notifications.map((n) => n.status)).toContainEqual(s1);
expect(notifications.map((n) => n.status)).toContainEqual(s2);
expect(notifications.map((n) => n.status)).toContainEqual(s3);
});

expect(notifications.length >= 3).toBe(true);
Expand Down
Loading

0 comments on commit 0b6b46e

Please sign in to comment.