1
0
mirror of https://github.com/MisskeyIO/misskey synced 2024-12-17 16:18:23 +09:00
MisskeyIO/test/api.ts

52 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-05-14 13:49:40 +09:00
import { request } from '../src/api';
2021-05-16 11:11:05 +09:00
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)
};
}
2021-05-14 13:49:40 +09:00
describe('API', () => {
test('success', async () => {
2021-05-16 11:11:05 +09:00
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 };
2021-05-14 13:49:40 +09:00
}
2021-05-16 11:11:05 +09:00
return JSON.stringify({ id: 'foo' });
} else {
return { status: 404 };
}
});
2021-05-14 13:49:40 +09:00
const res = await request('https://misskey.test', 'i', {}, 'TOKEN');
2021-05-16 11:11:05 +09:00
// validate response
2021-05-14 13:49:40 +09:00
expect(res).toEqual({
id: 'foo'
});
2021-05-16 11:11:05 +09:00
// validate fetch call
expect(getFetchCall(fetchMock.mock.calls[0])).toEqual({
2021-05-14 13:49:40 +09:00
url: 'https://misskey.test/api/i',
2021-05-16 11:11:05 +09:00
method: 'POST',
2021-05-14 13:49:40 +09:00
body: { i: 'TOKEN' }
2021-05-16 11:11:05 +09:00
});
2021-05-14 13:49:40 +09:00
});
2021-05-17 23:55:13 +09:00
// TODO: APIエラーのテスト
// TODO: ネットワークエラーのテスト
2021-05-14 13:49:40 +09:00
});