Skip to content

Commit

Permalink
Merge pull request #64 from h3poteto/iss-53
Browse files Browse the repository at this point in the history
refs #53 Add integration tests
  • Loading branch information
h3poteto authored Aug 26, 2019
2 parents 6433b25 + ad94313 commit 41a6628
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
38 changes: 38 additions & 0 deletions test/integration/cancel.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
5 changes: 5 additions & 0 deletions test/integration/cancelWorker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Mastodon from '@/mastodon'

export function cancel(client: Mastodon) {
return client.cancel()
}
153 changes: 153 additions & 0 deletions test/integration/mastodon.spec.ts
Original file line number Diff line number Diff line change
@@ -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<Array<Status>> = {
data: [status],
status: 200,
statusText: '200OK',
headers: [],
config: {}
}
it('should be responsed', async () => {
;(axios.get as any).mockResolvedValue(mockResponse)
const response: Response<Array<Status>> = await client.get<Array<Status>>('/timelines/home')
expect(response.data).toEqual([status])
})
})

describe('put', () => {
const client = new Mastodon('testToken', 'https://pleroma.io/api/v1')
const mockResponse: AxiosResponse<Account> = {
data: account,
status: 200,
statusText: '200OK',
headers: [],
config: {}
}
it('should be responsed', async () => {
;(axios.put as any).mockResolvedValue(mockResponse)
const response: Response<Account> = await client.put<Account>('/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<Account> = {
data: account,
status: 200,
statusText: '200OK',
headers: [],
config: {}
}
it('should be responsed', async () => {
;(axios.patch as any).mockResolvedValue(mockResponse)
const response: Response<Account> = await client.patch<Account>('/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<Status> = {
data: status,
status: 200,
statusText: '200OK',
headers: [],
config: {}
}
it('should be responsed', async () => {
;(axios.post as any).mockResolvedValue(mockResponse)
const response: Response<Status> = await client.post<Status>('/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({})
})
})
1 change: 1 addition & 0 deletions test/unit/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const account: Account = {
fields: null,
bot: false
}

const status: Status = {
id: '1',
uri: 'http://example.com',
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 41a6628

Please sign in to comment.