From 6043fc4553a78dab83b44645deebde9c9101f3aa Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Sun, 25 Aug 2019 22:55:07 +0900 Subject: [PATCH 1/2] refs #53 Add integration tests for mastodon --- test/integration/mastodon.spec.ts | 153 ++++++++++++++++++++++++++++++ test/unit/parser.spec.ts | 1 + 2 files changed, 154 insertions(+) create mode 100644 test/integration/mastodon.spec.ts diff --git a/test/integration/mastodon.spec.ts b/test/integration/mastodon.spec.ts new file mode 100644 index 000000000..b8ded6372 --- /dev/null +++ b/test/integration/mastodon.spec.ts @@ -0,0 +1,153 @@ +import Mastodon from '@/mastodon' +import { Account } from '@/entities/account' +import { Status } from '@/entities/status' +import { Application } from '@/entities/application' +import Response from '@/response' +import axios, { AxiosResponse } from 'axios' + +jest.mock('axios') + +const account: Account = { + id: '1', + username: 'h3poteto', + acct: 'h3poteto@pleroma.io', + display_name: 'h3poteto', + locked: false, + created_at: '2019-03-26T21:30:32', + followers_count: 10, + following_count: 10, + statuses_count: 100, + note: 'engineer', + url: 'https://pleroma.io', + avatar: '', + avatar_static: '', + header: '', + header_static: '', + emojis: [], + moved: null, + fields: null, + bot: false +} + +const status: Status = { + id: '1', + uri: 'http://example.com', + url: 'http://example.com', + account: account, + in_reply_to_id: null, + in_reply_to_account_id: null, + reblog: null, + content: 'hoge', + created_at: '2019-03-26T21:40:32', + emojis: [], + replies_count: 0, + reblogs_count: 0, + favourites_count: 0, + reblogged: null, + favourited: null, + muted: null, + sensitive: false, + spoiler_text: '', + visibility: 'public', + media_attachments: [], + mentions: [], + tags: [], + card: null, + poll: null, + application: { + name: 'Web' + } as Application, + language: null, + pinned: null +} +;(axios.CancelToken.source as any).mockImplementation(() => { + return { + token: 'cancelToken' + } +}) + +describe('get', () => { + const client = new Mastodon('testToken', 'https://pleroma.io/api/v1') + const mockResponse: AxiosResponse> = { + data: [status], + status: 200, + statusText: '200OK', + headers: [], + config: {} + } + it('should be responsed', async () => { + ;(axios.get as any).mockResolvedValue(mockResponse) + const response: Response> = await client.get>('/timelines/home') + expect(response.data).toEqual([status]) + }) +}) + +describe('put', () => { + const client = new Mastodon('testToken', 'https://pleroma.io/api/v1') + const mockResponse: AxiosResponse = { + data: account, + status: 200, + statusText: '200OK', + headers: [], + config: {} + } + it('should be responsed', async () => { + ;(axios.put as any).mockResolvedValue(mockResponse) + const response: Response = await client.put('/accounts/update_credentials', { + display_name: 'hoge' + }) + expect(response.data).toEqual(account) + }) +}) + +describe('patch', () => { + const client = new Mastodon('testToken', 'https://pleroma.io/api/v1') + const mockResponse: AxiosResponse = { + data: account, + status: 200, + statusText: '200OK', + headers: [], + config: {} + } + it('should be responsed', async () => { + ;(axios.patch as any).mockResolvedValue(mockResponse) + const response: Response = await client.patch('/accounts/update_credentials', { + display_name: 'hoge' + }) + expect(response.data).toEqual(account) + }) +}) + +describe('post', () => { + const client = new Mastodon('testToken', 'https://pleroma.io/api/v1') + const mockResponse: AxiosResponse = { + data: status, + status: 200, + statusText: '200OK', + headers: [], + config: {} + } + it('should be responsed', async () => { + ;(axios.post as any).mockResolvedValue(mockResponse) + const response: Response = await client.post('/statuses', { + status: 'hoge' + }) + expect(response.data).toEqual(status) + }) +}) + +describe('del', () => { + const client = new Mastodon('testToken', 'https://pleroma.io/api/v1') + const mockResponse: AxiosResponse<{}> = { + data: {}, + status: 200, + statusText: '200OK', + headers: [], + config: {} + } + it('should be responsed', async () => { + ;(axios.delete as any).mockResolvedValue(mockResponse) + const response: Response<{}> = await client.del<{}>('/statuses/12asdf34') + expect(response.data).toEqual({}) + }) +}) diff --git a/test/unit/parser.spec.ts b/test/unit/parser.spec.ts index 3c6df4aa2..fd6c13018 100644 --- a/test/unit/parser.spec.ts +++ b/test/unit/parser.spec.ts @@ -26,6 +26,7 @@ const account: Account = { fields: null, bot: false } + const status: Status = { id: '1', uri: 'http://example.com', From ad94313dfb4e10acf5bb8026fd459ffcb1495c55 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Mon, 26 Aug 2019 22:41:13 +0900 Subject: [PATCH 2/2] refs #53 Add integration tests for cancel, but it is skipped --- package.json | 3 ++- test/integration/cancel.spec.ts | 38 ++++++++++++++++++++++++++++++++ test/integration/cancelWorker.ts | 5 +++++ yarn.lock | 13 +++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 test/integration/cancel.spec.ts create mode 100644 test/integration/cancelWorker.ts diff --git a/package.json b/package.json index afa98e17c..bf402cab4 100644 --- a/package.json +++ b/package.json @@ -72,9 +72,10 @@ "eslint-plugin-prettier": "^3.1.0", "eslint-plugin-promise": "^4.1.1", "eslint-plugin-standard": "^4.0.0", + "jest": "^24.8.0", + "jest-worker": "^24.9.0", "lodash": "^4.17.14", "prettier": "^1.17.1", - "jest": "^24.8.0", "ts-jest": "^24.0.2", "tslint": "^5.10.0", "tslint-config-standard": "^7.0.0" diff --git a/test/integration/cancel.spec.ts b/test/integration/cancel.spec.ts new file mode 100644 index 000000000..6eed32f4d --- /dev/null +++ b/test/integration/cancel.spec.ts @@ -0,0 +1,38 @@ +import Mastodon from '@/mastodon' +import Worker from 'jest-worker' + +jest.mock('axios', () => { + const mockAxios = jest.requireActual('axios') + mockAxios.get = (_path: string) => { + return new Promise(resolve => { + setTimeout(() => { + console.log('hoge') + resolve({ + data: 'hoge', + status: 200, + statusText: '200OK', + headers: [], + config: {} + }) + }, 5000) + }) + } + return mockAxios +}) + +const worker = async (client: Mastodon) => { + const w: any = new Worker(require.resolve('./cancelWorker.ts')) + await w.cancel(client) +} + +// Could not use jest-worker under typescript. +// I'm waiting for resolve this issue. +// https://github.com/facebook/jest/issues/8872 +describe.skip('cancel', () => { + const client = new Mastodon('testToken', 'https://pleroma.io/api/v1') + it('should be raised', async () => { + const getPromise = client.get<{}>('/timelines/home') + worker(client) + await expect(getPromise).rejects.toThrow() + }) +}) diff --git a/test/integration/cancelWorker.ts b/test/integration/cancelWorker.ts new file mode 100644 index 000000000..6c6cf2393 --- /dev/null +++ b/test/integration/cancelWorker.ts @@ -0,0 +1,5 @@ +import Mastodon from '@/mastodon' + +export function cancel(client: Mastodon) { + return client.cancel() +} diff --git a/yarn.lock b/yarn.lock index 3b4512297..766c42a87 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2504,6 +2504,14 @@ jest-worker@^24.6.0: merge-stream "^1.0.1" supports-color "^6.1.0" +jest-worker@^24.9.0: + version "24.9.0" + resolved "https://registry.yarnpkg.com/jest-worker/-/jest-worker-24.9.0.tgz#5dbfdb5b2d322e98567898238a9697bcce67b3e5" + integrity sha512-51PE4haMSXcHohnSMdM42anbvZANYTqMrr52tVKPqqsPJMzoP6FYYDVqahX/HrAoKEKz3uUPzSvKs9A3qR4iVw== + dependencies: + merge-stream "^2.0.0" + supports-color "^6.1.0" + jest@^24.8.0: version "24.8.0" resolved "https://registry.yarnpkg.com/jest/-/jest-24.8.0.tgz#d5dff1984d0d1002196e9b7f12f75af1b2809081" @@ -2776,6 +2784,11 @@ merge-stream@^1.0.1: dependencies: readable-stream "^2.0.1" +merge-stream@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60" + integrity sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w== + micromatch@^3.1.10, micromatch@^3.1.4: version "3.1.10" resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-3.1.10.tgz#70859bc95c9840952f359a068a3fc49f9ecfac23"