-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from h3poteto/iss-53
refs #53 Add integration tests
- Loading branch information
Showing
6 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters