Skip to content

Commit

Permalink
chore(tests): Add tests for accounts, bookmarks, etc
Browse files Browse the repository at this point in the history
add more tests
  • Loading branch information
neet committed Apr 8, 2023
1 parent fb23044 commit 71e11ca
Show file tree
Hide file tree
Showing 26 changed files with 331 additions and 189 deletions.
4 changes: 2 additions & 2 deletions src/mastodon/v1/aggregate-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class AggregateRepository {
readonly suggestions: SuggestionRepository;
readonly timelines: TimelineRepository;
readonly trends: TrendRepository;
readonly email: EmailRepository;
readonly emails: EmailRepository;
readonly tags: TagRepository;
readonly followedTags: FollowedTagRepository;

Expand Down Expand Up @@ -137,7 +137,7 @@ export class AggregateRepository {
this.suggestions = new SuggestionRepository(http, config, logger);
this.timelines = new TimelineRepository(http, config, logger);
this.trends = new TrendRepository(http, config, logger);
this.email = new EmailRepository(http, config, logger);
this.emails = new EmailRepository(http, config, logger);
this.tags = new TagRepository(http, config, logger);
this.followedTags = new FollowedTagRepository(http, config, logger);
}
Expand Down
26 changes: 0 additions & 26 deletions src/mastodon/v1/repositories/__tests__/email-repository.spec.ts

This file was deleted.

68 changes: 0 additions & 68 deletions src/mastodon/v1/repositories/__tests__/status-repository.spec.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/mastodon/v1/repositories/account-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export class AccountRepository {
* Add the given account to the user's featured profiles. (Featured profiles are currently shown on the user's own public profile.)
* @param id The id of the account in the database
* @return Relationship
* @see https://docs.joinmastodon.org/methods/accounts/
* @see https://docs.joinmastodon.org/methods/accounts#pin
*/
@version({ since: '2.5.0' })
pin(id: string): Promise<Relationship> {
Expand Down
4 changes: 3 additions & 1 deletion src/mastodon/v1/repositories/featured-tag-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export class FeaturedTagRepository implements Repository<FeaturedTag> {
*/
@version({ since: '3.0.0' })
create(params: CreateFeaturedTagParams): Promise<FeaturedTag> {
return this.http.post<FeaturedTag>('/api/v1/featured_tags', params);
return this.http.post<FeaturedTag>('/api/v1/featured_tags', params, {
headers: { 'Content-Type': 'multipart/form-data' },
});
}

/**
Expand Down
14 changes: 5 additions & 9 deletions src/mastodon/v1/repositories/list-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class ListRepository
* @param id ID of the list in the database
* @param params Parameters
* @return Array of Account
* @see https://docs.joinmastodon.org/methods/timelines/lists/
* @see https://docs.joinmastodon.org/methods/timelines/lists#accounts
*/
@version({ since: '2.1.0' })
listAccounts(
Expand All @@ -103,26 +103,22 @@ export class ListRepository
* @param id ID of the list in the database
* @param params Parameters
* @return N/A
* @see https://docs.joinmastodon.org/methods/timelines/lists/
* @see https://docs.joinmastodon.org/methods/timelines/lists#accounts-add
*/
@version({ since: '2.1.0' })
addAccount(id: string, params: AddListAccountsParams): Promise<void> {
return this.http.post<void>(`/api/v1/lists/${id}/accounts`, params, {
headers: { 'Content-Type': 'multipart/form-data' },
});
return this.http.post<void>(`/api/v1/lists/${id}/accounts`, params);
}

/**
* Remove accounts from the given list.
* @param id ID of the list in the database
* @param params Parameters
* @return N/A
* @see https://docs.joinmastodon.org/methods/timelines/lists/
* @see https://docs.joinmastodon.org/methods/timelines/lists#accounts-remove
*/
@version({ since: '2.1.0' })
removeAccount(id: string, params: RemoveListAccountsParams): Promise<void> {
return this.http.delete<void>(`/api/v1/lists/${id}/accounts`, params, {
headers: { 'Content-Type': 'multipart/form-data' },
});
return this.http.delete<void>(`/api/v1/lists/${id}/accounts`, params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class MediaAttachmentRepository
* @return Attachment
* @see https://docs.joinmastodon.org/methods/statuses/media/
*/
/* istanbul-ignore-next */
@deprecated('Use MastoClient.v2.media.create instead')
@version({ since: '0.0.0', until: '3.1.3' })
create(params: CreateMediaAttachmentParams): Promise<MediaAttachment> {
Expand Down
37 changes: 37 additions & 0 deletions test-utils/jest-extend-expect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable @typescript-eslint/no-empty-interface */
/* eslint-disable @typescript-eslint/no-namespace */
export interface CustomMatchers<R = unknown> {
toContainId(id: string): R;
}

declare global {
namespace jest {
interface Expect extends CustomMatchers {}
interface Matchers<R> extends CustomMatchers<R> {}
interface InverseAsymmetricMatchers extends CustomMatchers {}
}
}

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}`;
},
};
},
});
3 changes: 3 additions & 0 deletions test-utils/jest-setup-after-env.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './jest-extend-expect';

import { createClient } from '../src';
import { ClientPoolImpl } from './pools';

Expand All @@ -8,6 +10,7 @@ globalThis.admin = createClient({
version: __misc__.instance.version,
streamingApiUrl: __misc__.instance.urls.streamingApi,
accessToken: __misc__.adminToken.accessToken,
logLevel: 'debug',
});

globalThis.clients = new ClientPoolImpl();
41 changes: 23 additions & 18 deletions tests/v1/accounts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
import crypto from 'node:crypto';

describe('account', () => {
it('creates an account', () => {
return clients.use(async (client) => {
const username = crypto.randomBytes(8).toString('hex');
const email = `${username}@example.com`;

const token = await client.v1.accounts.create({
username,
email,
password: 'password',
agreement: true,
locale: 'en',
});

expect(token.accessToken).toEqual(expect.any(String));
});
});
it('verifies credential', () => {
return clients.use(async (alice) => {
const me = await alice.v1.accounts.verifyCredentials();
Expand Down Expand Up @@ -122,9 +140,7 @@ describe('account', () => {
await alice.v1.accounts.follow(bobId);
const followers = await alice.v1.accounts.listFollowers(bobId);

expect(followers).toEqual(
expect.arrayContaining([expect.objectContaining({ id: aliceId })]),
);
expect(followers).toContainId(aliceId);
await alice.v1.accounts.unfollow(bobId);
});
});
Expand All @@ -141,9 +157,7 @@ describe('account', () => {
.then((me) => me.id);
const accounts = await alice.v1.accounts.listFollowing(aliceId);

expect(accounts).toEqual(
expect.arrayContaining([expect.objectContaining({ id: bobId })]),
);
expect(accounts).toContainId(bobId);
await alice.v1.accounts.unfollow(bobId);
});
});
Expand All @@ -153,20 +167,15 @@ describe('account', () => {
const status = await client.v1.statuses.create({ status: 'Hello' });
const statuses = await client.v1.accounts.listStatuses(status.account.id);

expect(statuses).toEqual(
expect.arrayContaining([expect.objectContaining({ id: status.id })]),
);
expect(statuses).toContainId(status.id);
});
});

it('searches', () => {
return clients.use(async (client) => {
const me = await client.v1.accounts.verifyCredentials();
const accounts = await client.v1.accounts.search({ q: me.username });

expect(accounts).toEqual(
expect.arrayContaining([expect.objectContaining({ id: me.id })]),
);
expect(accounts).toContainId(me.id);
});
});

Expand All @@ -180,11 +189,7 @@ describe('account', () => {
});

const tags = await client.v1.accounts.listFeaturedTags(me.id);
expect(tags).toEqual(
expect.arrayContaining([
expect.objectContaining({ id: featuredTag.id }),
]),
);
expect(tags).toContainId(featuredTag.id);

await client.v1.featuredTags.remove(featuredTag.id);
});
Expand Down
19 changes: 7 additions & 12 deletions tests/v1/blocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ describe('blocks', () => {
.verifyCredentials()
.then((account) => account.id);

await alice.v1.accounts.block(bobId);
const blocks = await alice.v1.blocks.list();

expect(blocks).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: bobId,
}),
]),
);

await alice.v1.accounts.unblock(bobId);
try {
await alice.v1.accounts.block(bobId);
const blocks = await alice.v1.blocks.list();
expect(blocks).toContainId(bobId);
} finally {
await alice.v1.accounts.unblock(bobId);
}
});
});
});
18 changes: 7 additions & 11 deletions tests/v1/bookmarks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ describe('bookmarks', () => {
it('lists bookmarks', () => {
return clients.use(async (client) => {
const status = await client.v1.statuses.create({ status: 'status' });
await client.v1.statuses.bookmark(status.id);
const bookmarks = await client.v1.bookmarks.list();

expect(bookmarks).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: status.id,
}),
]),
);

await client.v1.statuses.unbookmark(status.id);
try {
await client.v1.statuses.bookmark(status.id);
const bookmarks = await client.v1.bookmarks.list();
expect(bookmarks).toContainId(status.id);
} finally {
await client.v1.statuses.unbookmark(status.id);
}
});
});
});
6 changes: 6 additions & 0 deletions tests/v1/directory.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
it('lists directory', () => {
return clients.use(async (client) => {
const directory = await client.v1.directory.list();
expect(directory).toEqual(expect.any(Array));
});
});
13 changes: 13 additions & 0 deletions tests/v1/domain-blocks.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
it('block a domain', () => {
return clients.use(async (client) => {
const domain = 'example.com';

await client.v1.domainBlocks.block(domain);
let domainBlocks = await client.v1.domainBlocks.list();
expect(domainBlocks).toEqual(expect.arrayContaining([domain]));

await client.v1.domainBlocks.unblock(domain);
domainBlocks = await client.v1.domainBlocks.list();
expect(domainBlocks).not.toEqual(expect.arrayContaining([domain]));
});
});

0 comments on commit 71e11ca

Please sign in to comment.