diff options
| author | marihachi <marihachi0620@gmail.com> | 2021-05-16 11:11:05 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-16 11:11:05 +0900 |
| commit | 460e23c2e74964c478cb89da4a80161b17772f6c (patch) | |
| tree | 29b45ecdbb8329097bd73fa230570a99d9211c54 /test/api.ts | |
| parent | Rename render acct (#5) (diff) | |
| download | misskey-460e23c2e74964c478cb89da4a80161b17772f6c.tar.gz misskey-460e23c2e74964c478cb89da4a80161b17772f6c.tar.bz2 misskey-460e23c2e74964c478cb89da4a80161b17772f6c.zip | |
fix fetch mock (#9)
Diffstat (limited to '')
| -rw-r--r-- | test/api.ts | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/test/api.ts b/test/api.ts index cbb8e5736e..499c133cad 100644 --- a/test/api.ts +++ b/test/api.ts @@ -1,29 +1,47 @@ -import fetchMock from 'fetch-mock-jest'; import { request } from '../src/api'; +import { enableFetchMocks } from 'jest-fetch-mock'; + +enableFetchMocks(); + +function getFetchCall(call: any[]) { + const { body, method } = call[1]; + if (body != null && typeof body != 'string') { + throw new Error('invalid body'); + } + return { + url: call[0], + method: method, + body: JSON.parse(body as any) + }; +} describe('API', () => { test('success', async () => { - fetchMock - .post('https://misskey.test/api/i', (url, options) => { - if (typeof options.body.i === 'string') { - return { - body: { - id: 'foo' - } - }; + fetchMock.resetMocks(); + fetchMock.mockResponse(async (req) => { + const body = await req.json(); + if (req.method == 'POST' && req.url == 'https://misskey.test/api/i') { + if (typeof body.i != 'string') { + return { status: 400 }; } - return 400; - }); + return JSON.stringify({ id: 'foo' }); + } else { + return { status: 404 }; + } + }); const res = await request('https://misskey.test', 'i', {}, 'TOKEN'); + // validate response expect(res).toEqual({ id: 'foo' }); - expect(fetchMock).toHaveLastFetched({ + // validate fetch call + expect(getFetchCall(fetchMock.mock.calls[0])).toEqual({ url: 'https://misskey.test/api/i', + method: 'POST', body: { i: 'TOKEN' } - }, 'post'); + }); }); }); |