chore: format specs

This commit is contained in:
ThatOneCalculator 2023-09-01 16:47:20 -07:00
parent 63e740dccf
commit 788b979734
No known key found for this signature in database
GPG key ID: 8703CACD01000000
4 changed files with 584 additions and 565 deletions

View file

@ -1,27 +1,27 @@
import { detector } from '../../src/index' import { detector } from "../../src/index";
describe('detector', () => { describe("detector", () => {
describe('mastodon', () => { describe("mastodon", () => {
const url = 'https://fedibird.com' const url = "https://fedibird.com";
it('should be mastodon', async () => { it("should be mastodon", async () => {
const mastodon = await detector(url) const mastodon = await detector(url);
expect(mastodon).toEqual('mastodon') expect(mastodon).toEqual("mastodon");
}) });
}) });
describe('pleroma', () => { describe("pleroma", () => {
const url = 'https://pleroma.soykaf.com' const url = "https://pleroma.soykaf.com";
it('should be pleroma', async () => { it("should be pleroma", async () => {
const pleroma = await detector(url) const pleroma = await detector(url);
expect(pleroma).toEqual('pleroma') expect(pleroma).toEqual("pleroma");
}) });
}) });
describe('misskey', () => { describe("misskey", () => {
const url = 'https://misskey.io' const url = "https://misskey.io";
it('should be misskey', async () => { it("should be misskey", async () => {
const misskey = await detector(url) const misskey = await detector(url);
expect(misskey).toEqual('misskey') expect(misskey).toEqual("misskey");
}) });
}) });
}) });

View file

@ -1,29 +1,29 @@
import MisskeyEntity from '@/misskey/entity' import MisskeyEntity from "@/misskey/entity";
import MisskeyNotificationType from '@/misskey/notification' import MisskeyNotificationType from "@/misskey/notification";
import Misskey from '@/misskey' import Misskey from "@/misskey";
import MegalodonNotificationType from '@/notification' import MegalodonNotificationType from "@/notification";
import axios, { AxiosResponse } from 'axios' import axios, { AxiosResponse } from "axios";
jest.mock('axios') jest.mock("axios");
const user: MisskeyEntity.User = { const user: MisskeyEntity.User = {
id: '1', id: "1",
name: 'test_user', name: "test_user",
username: 'TestUser', username: "TestUser",
host: 'misskey.io', host: "misskey.io",
avatarUrl: 'https://example.com/icon.png', avatarUrl: "https://example.com/icon.png",
avatarColor: '#000000', avatarColor: "#000000",
emojis: [] emojis: [],
} };
const note: MisskeyEntity.Note = { const note: MisskeyEntity.Note = {
id: '1', id: "1",
createdAt: '2021-02-01T01:49:29', createdAt: "2021-02-01T01:49:29",
userId: '1', userId: "1",
user: user, user: user,
text: 'hogehoge', text: "hogehoge",
cw: null, cw: null,
visibility: 'public', visibility: "public",
renoteCount: 0, renoteCount: 0,
repliesCount: 0, repliesCount: 0,
reactions: {}, reactions: {},
@ -31,174 +31,177 @@ const note: MisskeyEntity.Note = {
fileIds: [], fileIds: [],
files: [], files: [],
replyId: null, replyId: null,
renoteId: null renoteId: null,
} };
const follow: MisskeyEntity.Notification = { const follow: MisskeyEntity.Notification = {
id: '1', id: "1",
createdAt: '2021-02-01T01:49:29', createdAt: "2021-02-01T01:49:29",
userId: user.id, userId: user.id,
user: user, user: user,
type: MisskeyNotificationType.Follow type: MisskeyNotificationType.Follow,
} };
const mention: MisskeyEntity.Notification = { const mention: MisskeyEntity.Notification = {
id: '1', id: "1",
createdAt: '2021-02-01T01:49:29', createdAt: "2021-02-01T01:49:29",
userId: user.id, userId: user.id,
user: user, user: user,
type: MisskeyNotificationType.Mention, type: MisskeyNotificationType.Mention,
note: note note: note,
} };
const reply: MisskeyEntity.Notification = { const reply: MisskeyEntity.Notification = {
id: '1', id: "1",
createdAt: '2021-02-01T01:49:29', createdAt: "2021-02-01T01:49:29",
userId: user.id, userId: user.id,
user: user, user: user,
type: MisskeyNotificationType.Reply, type: MisskeyNotificationType.Reply,
note: note note: note,
} };
const renote: MisskeyEntity.Notification = { const renote: MisskeyEntity.Notification = {
id: '1', id: "1",
createdAt: '2021-02-01T01:49:29', createdAt: "2021-02-01T01:49:29",
userId: user.id, userId: user.id,
user: user, user: user,
type: MisskeyNotificationType.Renote, type: MisskeyNotificationType.Renote,
note: note note: note,
} };
const quote: MisskeyEntity.Notification = { const quote: MisskeyEntity.Notification = {
id: '1', id: "1",
createdAt: '2021-02-01T01:49:29', createdAt: "2021-02-01T01:49:29",
userId: user.id, userId: user.id,
user: user, user: user,
type: MisskeyNotificationType.Quote, type: MisskeyNotificationType.Quote,
note: note note: note,
} };
const reaction: MisskeyEntity.Notification = { const reaction: MisskeyEntity.Notification = {
id: '1', id: "1",
createdAt: '2021-02-01T01:49:29', createdAt: "2021-02-01T01:49:29",
userId: user.id, userId: user.id,
user: user, user: user,
type: MisskeyNotificationType.Reaction, type: MisskeyNotificationType.Reaction,
note: note, note: note,
reaction: '♥' reaction: "♥",
} };
const pollVote: MisskeyEntity.Notification = { const pollVote: MisskeyEntity.Notification = {
id: '1', id: "1",
createdAt: '2021-02-01T01:49:29', createdAt: "2021-02-01T01:49:29",
userId: user.id, userId: user.id,
user: user, user: user,
type: MisskeyNotificationType.PollEnded, type: MisskeyNotificationType.PollEnded,
note: note note: note,
} };
const receiveFollowRequest: MisskeyEntity.Notification = { const receiveFollowRequest: MisskeyEntity.Notification = {
id: '1', id: "1",
createdAt: '2021-02-01T01:49:29', createdAt: "2021-02-01T01:49:29",
userId: user.id, userId: user.id,
user: user, user: user,
type: MisskeyNotificationType.ReceiveFollowRequest type: MisskeyNotificationType.ReceiveFollowRequest,
} };
const followRequestAccepted: MisskeyEntity.Notification = { const followRequestAccepted: MisskeyEntity.Notification = {
id: '1', id: "1",
createdAt: '2021-02-01T01:49:29', createdAt: "2021-02-01T01:49:29",
userId: user.id, userId: user.id,
user: user, user: user,
type: MisskeyNotificationType.FollowRequestAccepted type: MisskeyNotificationType.FollowRequestAccepted,
} };
const groupInvited: MisskeyEntity.Notification = { const groupInvited: MisskeyEntity.Notification = {
id: '1', id: "1",
createdAt: '2021-02-01T01:49:29', createdAt: "2021-02-01T01:49:29",
userId: user.id, userId: user.id,
user: user, user: user,
type: MisskeyNotificationType.GroupInvited type: MisskeyNotificationType.GroupInvited,
} };
(axios.CancelToken.source as any).mockImplementation(() => {
;(axios.CancelToken.source as any).mockImplementation(() => {
return { return {
token: { token: {
throwIfRequested: () => {}, throwIfRequested: () => {},
promise: { promise: {
then: () => {}, then: () => {},
catch: () => {} catch: () => {},
} },
} },
} };
}) });
describe('getNotifications', () => { describe("getNotifications", () => {
const client = new Misskey('http://localhost', 'sample token') const client = new Misskey("http://localhost", "sample token");
const cases: Array<{ event: MisskeyEntity.Notification; expected: Entity.NotificationType; title: string }> = [ const cases: Array<{
event: MisskeyEntity.Notification;
expected: Entity.NotificationType;
title: string;
}> = [
{ {
event: follow, event: follow,
expected: MegalodonNotificationType.Follow, expected: MegalodonNotificationType.Follow,
title: 'follow' title: "follow",
}, },
{ {
event: mention, event: mention,
expected: MegalodonNotificationType.Mention, expected: MegalodonNotificationType.Mention,
title: 'mention' title: "mention",
}, },
{ {
event: reply, event: reply,
expected: MegalodonNotificationType.Mention, expected: MegalodonNotificationType.Mention,
title: 'reply' title: "reply",
}, },
{ {
event: renote, event: renote,
expected: MegalodonNotificationType.Reblog, expected: MegalodonNotificationType.Reblog,
title: 'renote' title: "renote",
}, },
{ {
event: quote, event: quote,
expected: MegalodonNotificationType.Reblog, expected: MegalodonNotificationType.Reblog,
title: 'quote' title: "quote",
}, },
{ {
event: reaction, event: reaction,
expected: MegalodonNotificationType.Reaction, expected: MegalodonNotificationType.Reaction,
title: 'reaction' title: "reaction",
}, },
{ {
event: pollVote, event: pollVote,
expected: MegalodonNotificationType.Poll, expected: MegalodonNotificationType.Poll,
title: 'pollVote' title: "pollVote",
}, },
{ {
event: receiveFollowRequest, event: receiveFollowRequest,
expected: MegalodonNotificationType.FollowRequest, expected: MegalodonNotificationType.FollowRequest,
title: 'receiveFollowRequest' title: "receiveFollowRequest",
}, },
{ {
event: followRequestAccepted, event: followRequestAccepted,
expected: MegalodonNotificationType.Follow, expected: MegalodonNotificationType.Follow,
title: 'followRequestAccepted' title: "followRequestAccepted",
}, },
{ {
event: groupInvited, event: groupInvited,
expected: MisskeyNotificationType.GroupInvited, expected: MisskeyNotificationType.GroupInvited,
title: 'groupInvited' title: "groupInvited",
} },
] ];
cases.forEach(c => { cases.forEach((c) => {
it(`should be ${c.title} event`, async () => { it(`should be ${c.title} event`, async () => {
const mockResponse: AxiosResponse<Array<MisskeyEntity.Notification>> = { const mockResponse: AxiosResponse<Array<MisskeyEntity.Notification>> = {
data: [c.event], data: [c.event],
status: 200, status: 200,
statusText: '200OK', statusText: "200OK",
headers: {}, headers: {},
config: {} config: {},
} };
;(axios.post as any).mockResolvedValue(mockResponse) (axios.post as any).mockResolvedValue(mockResponse);
const res = await client.getNotifications() const res = await client.getNotifications();
expect(res.data[0].type).toEqual(c.expected) expect(res.data[0].type).toEqual(c.expected);
}) });
}) });
}) });

View file

@ -1,196 +1,208 @@
import MisskeyAPI from '@/misskey/api_client' import MisskeyAPI from "@/misskey/api_client";
import MegalodonEntity from '@/entity' import MegalodonEntity from "@/entity";
import MisskeyEntity from '@/misskey/entity' import MisskeyEntity from "@/misskey/entity";
import MegalodonNotificationType from '@/notification' import MegalodonNotificationType from "@/notification";
import MisskeyNotificationType from '@/misskey/notification' import MisskeyNotificationType from "@/misskey/notification";
const user: MisskeyEntity.User = { const user: MisskeyEntity.User = {
id: '1', id: "1",
name: 'test_user', name: "test_user",
username: 'TestUser', username: "TestUser",
host: 'misskey.io', host: "misskey.io",
avatarUrl: 'https://example.com/icon.png', avatarUrl: "https://example.com/icon.png",
avatarColor: '#000000', avatarColor: "#000000",
emojis: [] emojis: [],
} };
const converter: MisskeyAPI.Converter = new MisskeyAPI.Converter("https://example.com") const converter: MisskeyAPI.Converter = new MisskeyAPI.Converter(
"https://example.com",
);
describe('api_client', () => { describe("api_client", () => {
describe('notification', () => { describe("notification", () => {
describe('encode', () => { describe("encode", () => {
it('megalodon notification type should be encoded to misskey notification type', () => { it("megalodon notification type should be encoded to misskey notification type", () => {
const cases: Array<{ src: MegalodonEntity.NotificationType; dist: MisskeyEntity.NotificationType }> = [ const cases: Array<{
src: MegalodonEntity.NotificationType;
dist: MisskeyEntity.NotificationType;
}> = [
{ {
src: MegalodonNotificationType.Follow, src: MegalodonNotificationType.Follow,
dist: MisskeyNotificationType.Follow dist: MisskeyNotificationType.Follow,
}, },
{ {
src: MegalodonNotificationType.Mention, src: MegalodonNotificationType.Mention,
dist: MisskeyNotificationType.Reply dist: MisskeyNotificationType.Reply,
}, },
{ {
src: MegalodonNotificationType.Favourite, src: MegalodonNotificationType.Favourite,
dist: MisskeyNotificationType.Reaction dist: MisskeyNotificationType.Reaction,
}, },
{ {
src: MegalodonNotificationType.Reaction, src: MegalodonNotificationType.Reaction,
dist: MisskeyNotificationType.Reaction dist: MisskeyNotificationType.Reaction,
}, },
{ {
src: MegalodonNotificationType.Reblog, src: MegalodonNotificationType.Reblog,
dist: MisskeyNotificationType.Renote dist: MisskeyNotificationType.Renote,
}, },
{ {
src: MegalodonNotificationType.Poll, src: MegalodonNotificationType.Poll,
dist: MisskeyNotificationType.PollEnded dist: MisskeyNotificationType.PollEnded,
}, },
{ {
src: MegalodonNotificationType.FollowRequest, src: MegalodonNotificationType.FollowRequest,
dist: MisskeyNotificationType.ReceiveFollowRequest dist: MisskeyNotificationType.ReceiveFollowRequest,
} },
] ];
cases.forEach(c => { cases.forEach((c) => {
expect(converter.encodeNotificationType(c.src)).toEqual(c.dist) expect(converter.encodeNotificationType(c.src)).toEqual(c.dist);
}) });
}) });
}) });
describe('decode', () => { describe("decode", () => {
it('misskey notification type should be decoded to megalodon notification type', () => { it("misskey notification type should be decoded to megalodon notification type", () => {
const cases: Array<{ src: MisskeyEntity.NotificationType; dist: MegalodonEntity.NotificationType }> = [ const cases: Array<{
src: MisskeyEntity.NotificationType;
dist: MegalodonEntity.NotificationType;
}> = [
{ {
src: MisskeyNotificationType.Follow, src: MisskeyNotificationType.Follow,
dist: MegalodonNotificationType.Follow dist: MegalodonNotificationType.Follow,
}, },
{ {
src: MisskeyNotificationType.Mention, src: MisskeyNotificationType.Mention,
dist: MegalodonNotificationType.Mention dist: MegalodonNotificationType.Mention,
}, },
{ {
src: MisskeyNotificationType.Reply, src: MisskeyNotificationType.Reply,
dist: MegalodonNotificationType.Mention dist: MegalodonNotificationType.Mention,
}, },
{ {
src: MisskeyNotificationType.Renote, src: MisskeyNotificationType.Renote,
dist: MegalodonNotificationType.Reblog dist: MegalodonNotificationType.Reblog,
}, },
{ {
src: MisskeyNotificationType.Quote, src: MisskeyNotificationType.Quote,
dist: MegalodonNotificationType.Reblog dist: MegalodonNotificationType.Reblog,
}, },
{ {
src: MisskeyNotificationType.Reaction, src: MisskeyNotificationType.Reaction,
dist: MegalodonNotificationType.Reaction dist: MegalodonNotificationType.Reaction,
}, },
{ {
src: MisskeyNotificationType.PollEnded, src: MisskeyNotificationType.PollEnded,
dist: MegalodonNotificationType.Poll dist: MegalodonNotificationType.Poll,
}, },
{ {
src: MisskeyNotificationType.ReceiveFollowRequest, src: MisskeyNotificationType.ReceiveFollowRequest,
dist: MegalodonNotificationType.FollowRequest dist: MegalodonNotificationType.FollowRequest,
}, },
{ {
src: MisskeyNotificationType.FollowRequestAccepted, src: MisskeyNotificationType.FollowRequestAccepted,
dist: MegalodonNotificationType.Follow dist: MegalodonNotificationType.Follow,
} },
] ];
cases.forEach(c => { cases.forEach((c) => {
expect(converter.decodeNotificationType(c.src)).toEqual(c.dist) expect(converter.decodeNotificationType(c.src)).toEqual(c.dist);
}) });
}) });
}) });
}) });
describe('reactions', () => { describe("reactions", () => {
it('should be mapped', () => { it("should be mapped", () => {
const misskeyReactions = [ const misskeyReactions = [
{ {
id: '1', id: "1",
createdAt: '2020-04-21T13:04:13.968Z', createdAt: "2020-04-21T13:04:13.968Z",
user: { user: {
id: '81u70uwsja', id: "81u70uwsja",
name: 'h3poteto', name: "h3poteto",
username: 'h3poteto', username: "h3poteto",
host: null, host: null,
avatarUrl: 'https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png', avatarUrl:
avatarColor: 'rgb(146,189,195)', "https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png",
emojis: [] avatarColor: "rgb(146,189,195)",
emojis: [],
}, },
type: '❤' type: "❤",
}, },
{ {
id: '2', id: "2",
createdAt: '2020-04-21T13:04:13.968Z', createdAt: "2020-04-21T13:04:13.968Z",
user: { user: {
id: '81u70uwsja', id: "81u70uwsja",
name: 'h3poteto', name: "h3poteto",
username: 'h3poteto', username: "h3poteto",
host: null, host: null,
avatarUrl: 'https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png', avatarUrl:
avatarColor: 'rgb(146,189,195)', "https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png",
emojis: [] avatarColor: "rgb(146,189,195)",
emojis: [],
}, },
type: '❤' type: "❤",
}, },
{ {
id: '3', id: "3",
createdAt: '2020-04-21T13:04:13.968Z', createdAt: "2020-04-21T13:04:13.968Z",
user: { user: {
id: '81u70uwsja', id: "81u70uwsja",
name: 'h3poteto', name: "h3poteto",
username: 'h3poteto', username: "h3poteto",
host: null, host: null,
avatarUrl: 'https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png', avatarUrl:
avatarColor: 'rgb(146,189,195)', "https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png",
emojis: [] avatarColor: "rgb(146,189,195)",
emojis: [],
}, },
type: '☺' type: "☺",
}, },
{ {
id: '4', id: "4",
createdAt: '2020-04-21T13:04:13.968Z', createdAt: "2020-04-21T13:04:13.968Z",
user: { user: {
id: '81u70uwsja', id: "81u70uwsja",
name: 'h3poteto', name: "h3poteto",
username: 'h3poteto', username: "h3poteto",
host: null, host: null,
avatarUrl: 'https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png', avatarUrl:
avatarColor: 'rgb(146,189,195)', "https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png",
emojis: [] avatarColor: "rgb(146,189,195)",
emojis: [],
}, },
type: '❤' type: "❤",
} },
] ];
const reactions = converter.reactions(misskeyReactions) const reactions = converter.reactions(misskeyReactions);
expect(reactions).toEqual([ expect(reactions).toEqual([
{ {
count: 3, count: 3,
me: false, me: false,
name: '❤' name: "❤",
}, },
{ {
count: 1, count: 1,
me: false, me: false,
name: '☺' name: "☺",
} },
]) ]);
}) });
}) });
describe('status', () => { describe("status", () => {
describe('plain content', () => { describe("plain content", () => {
it('should be exported plain content and html content', () => { it("should be exported plain content and html content", () => {
const plainContent = 'hoge\nfuga\nfuga' const plainContent = "hoge\nfuga\nfuga";
const content = 'hoge<br>fuga<br>fuga' const content = "hoge<br>fuga<br>fuga";
const note: MisskeyEntity.Note = { const note: MisskeyEntity.Note = {
id: '1', id: "1",
createdAt: '2021-02-01T01:49:29', createdAt: "2021-02-01T01:49:29",
userId: '1', userId: "1",
user: user, user: user,
text: plainContent, text: plainContent,
cw: null, cw: null,
visibility: 'public', visibility: "public",
renoteCount: 0, renoteCount: 0,
repliesCount: 0, repliesCount: 0,
reactions: {}, reactions: {},
@ -198,23 +210,23 @@ describe('api_client', () => {
fileIds: [], fileIds: [],
files: [], files: [],
replyId: null, replyId: null,
renoteId: null renoteId: null,
} };
const megalodonStatus = converter.note(note, user.host || 'misskey.io') const megalodonStatus = converter.note(note, user.host || "misskey.io");
expect(megalodonStatus.plain_content).toEqual(plainContent) expect(megalodonStatus.plain_content).toEqual(plainContent);
expect(megalodonStatus.content).toEqual(content) expect(megalodonStatus.content).toEqual(content);
}) });
it('html tags should be escaped', () => { it("html tags should be escaped", () => {
const plainContent = '<p>hoge\nfuga\nfuga<p>' const plainContent = "<p>hoge\nfuga\nfuga<p>";
const content = '&lt;p&gt;hoge<br>fuga<br>fuga&lt;p&gt;' const content = "&lt;p&gt;hoge<br>fuga<br>fuga&lt;p&gt;";
const note: MisskeyEntity.Note = { const note: MisskeyEntity.Note = {
id: '1', id: "1",
createdAt: '2021-02-01T01:49:29', createdAt: "2021-02-01T01:49:29",
userId: '1', userId: "1",
user: user, user: user,
text: plainContent, text: plainContent,
cw: null, cw: null,
visibility: 'public', visibility: "public",
renoteCount: 0, renoteCount: 0,
repliesCount: 0, repliesCount: 0,
reactions: {}, reactions: {},
@ -222,12 +234,12 @@ describe('api_client', () => {
fileIds: [], fileIds: [],
files: [], files: [],
replyId: null, replyId: null,
renoteId: null renoteId: null,
} };
const megalodonStatus = converter.note(note, user.host || 'misskey.io') const megalodonStatus = converter.note(note, user.host || "misskey.io");
expect(megalodonStatus.plain_content).toEqual(plainContent) expect(megalodonStatus.plain_content).toEqual(plainContent);
expect(megalodonStatus.content).toEqual(content) expect(megalodonStatus.content).toEqual(content);
}) });
}) });
}) });
}) });

View file

@ -1,39 +1,39 @@
import { Parser } from '@/parser' import { Parser } from "@/parser";
import Entity from '@/entity' import Entity from "@/entity";
const account: Entity.Account = { const account: Entity.Account = {
id: '1', id: "1",
username: 'h3poteto', username: "h3poteto",
acct: 'h3poteto@pleroma.io', acct: "h3poteto@pleroma.io",
display_name: 'h3poteto', display_name: "h3poteto",
locked: false, locked: false,
created_at: '2019-03-26T21:30:32', created_at: "2019-03-26T21:30:32",
followers_count: 10, followers_count: 10,
following_count: 10, following_count: 10,
statuses_count: 100, statuses_count: 100,
note: 'engineer', note: "engineer",
url: 'https://pleroma.io', url: "https://pleroma.io",
avatar: '', avatar: "",
avatar_static: '', avatar_static: "",
header: '', header: "",
header_static: '', header_static: "",
emojis: [], emojis: [],
moved: null, moved: null,
fields: [], fields: [],
bot: false bot: false,
} };
const status: Entity.Status = { const status: Entity.Status = {
id: '1', id: "1",
uri: 'http://example.com', uri: "http://example.com",
url: 'http://example.com', url: "http://example.com",
account: account, account: account,
in_reply_to_id: null, in_reply_to_id: null,
in_reply_to_account_id: null, in_reply_to_account_id: null,
reblog: null, reblog: null,
content: 'hoge', content: "hoge",
plain_content: 'hoge', plain_content: "hoge",
created_at: '2019-03-26T21:40:32', created_at: "2019-03-26T21:40:32",
emojis: [], emojis: [],
replies_count: 0, replies_count: 0,
reblogs_count: 0, reblogs_count: 0,
@ -42,111 +42,115 @@ const status: Entity.Status = {
favourited: null, favourited: null,
muted: null, muted: null,
sensitive: false, sensitive: false,
spoiler_text: '', spoiler_text: "",
visibility: 'public', visibility: "public",
media_attachments: [], media_attachments: [],
mentions: [], mentions: [],
tags: [], tags: [],
card: null, card: null,
poll: null, poll: null,
application: { application: {
name: 'Web' name: "Web",
} as Entity.Application, } as Entity.Application,
language: null, language: null,
pinned: null, pinned: null,
reactions: [], reactions: [],
bookmarked: false, bookmarked: false,
quote: null quote: null,
} };
const notification: Entity.Notification = { const notification: Entity.Notification = {
id: '1', id: "1",
account: account, account: account,
status: status, status: status,
type: 'favourite', type: "favourite",
created_at: '2019-04-01T17:01:32' created_at: "2019-04-01T17:01:32",
} };
const conversation: Entity.Conversation = { const conversation: Entity.Conversation = {
id: '1', id: "1",
accounts: [account], accounts: [account],
last_status: status, last_status: status,
unread: true unread: true,
} };
describe('Parser', () => { describe("Parser", () => {
let parser: Parser let parser: Parser;
beforeEach(() => { beforeEach(() => {
parser = new Parser() parser = new Parser();
}) });
describe('parse', () => { describe("parse", () => {
describe('message is heartbeat', () => { describe("message is heartbeat", () => {
const message: string = ':thump\n' const message: string = ":thump\n";
it('should be called', () => { it("should be called", () => {
const spy = jest.fn() const spy = jest.fn();
parser.on('heartbeat', spy) parser.on("heartbeat", spy);
parser.parse(message) parser.parse(message);
expect(spy).toHaveBeenLastCalledWith({}) expect(spy).toHaveBeenLastCalledWith({});
}) });
}) });
describe('message is not json', () => { describe("message is not json", () => {
describe('event is delete', () => { describe("event is delete", () => {
const message = `event: delete\ndata: 12asdf34\n\n` const message = `event: delete\ndata: 12asdf34\n\n`;
it('should be called', () => { it("should be called", () => {
const spy = jest.fn() const spy = jest.fn();
parser.once('delete', spy) parser.once("delete", spy);
parser.parse(message) parser.parse(message);
expect(spy).toHaveBeenCalledWith('12asdf34') expect(spy).toHaveBeenCalledWith("12asdf34");
}) });
}) });
describe('event is not delete', () => { describe("event is not delete", () => {
const message = `event: event\ndata: 12asdf34\n\n` const message = `event: event\ndata: 12asdf34\n\n`;
it('should be error', () => { it("should be error", () => {
const error = jest.fn() const error = jest.fn();
const deleted = jest.fn() const deleted = jest.fn();
parser.once('error', error) parser.once("error", error);
parser.once('delete', deleted) parser.once("delete", deleted);
parser.parse(message) parser.parse(message);
expect(error).toHaveBeenCalled() expect(error).toHaveBeenCalled();
expect(deleted).not.toHaveBeenCalled() expect(deleted).not.toHaveBeenCalled();
}) });
}) });
}) });
describe('message is json', () => { describe("message is json", () => {
describe('event is update', () => { describe("event is update", () => {
const message = `event: update\ndata: ${JSON.stringify(status)}\n\n` const message = `event: update\ndata: ${JSON.stringify(status)}\n\n`;
it('should be called', () => { it("should be called", () => {
const spy = jest.fn() const spy = jest.fn();
parser.once('update', spy) parser.once("update", spy);
parser.parse(message) parser.parse(message);
expect(spy).toHaveBeenCalledWith(status) expect(spy).toHaveBeenCalledWith(status);
}) });
}) });
describe('event is notification', () => { describe("event is notification", () => {
const message = `event: notification\ndata: ${JSON.stringify(notification)}\n\n` const message = `event: notification\ndata: ${JSON.stringify(
it('should be called', () => { notification,
const spy = jest.fn() )}\n\n`;
parser.once('notification', spy) it("should be called", () => {
parser.parse(message) const spy = jest.fn();
expect(spy).toHaveBeenCalledWith(notification) parser.once("notification", spy);
}) parser.parse(message);
}) expect(spy).toHaveBeenCalledWith(notification);
});
});
describe('event is conversation', () => { describe("event is conversation", () => {
const message = `event: conversation\ndata: ${JSON.stringify(conversation)}\n\n` const message = `event: conversation\ndata: ${JSON.stringify(
it('should be called', () => { conversation,
const spy = jest.fn() )}\n\n`;
parser.once('conversation', spy) it("should be called", () => {
parser.parse(message) const spy = jest.fn();
expect(spy).toHaveBeenCalledWith(conversation) parser.once("conversation", spy);
}) parser.parse(message);
}) expect(spy).toHaveBeenCalledWith(conversation);
}) });
}) });
}) });
});
});