2021-05-22 22:19:39 -05:00
|
|
|
import { APIClient, isAPIError } from '../src/api';
|
2021-05-15 21:11:05 -05: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-13 23:49:40 -05:00
|
|
|
|
|
|
|
describe('API', () => {
|
|
|
|
test('success', async () => {
|
2021-05-15 21:11:05 -05:00
|
|
|
fetchMock.resetMocks();
|
|
|
|
fetchMock.mockResponse(async (req) => {
|
|
|
|
const body = await req.json();
|
|
|
|
if (req.method == 'POST' && req.url == 'https://misskey.test/api/i') {
|
2021-05-22 22:15:28 -05:00
|
|
|
if (body.i === 'TOKEN') {
|
|
|
|
return JSON.stringify({ id: 'foo' });
|
|
|
|
} else {
|
2021-05-15 21:11:05 -05:00
|
|
|
return { status: 400 };
|
2021-05-13 23:49:40 -05:00
|
|
|
}
|
2021-05-15 21:11:05 -05:00
|
|
|
} else {
|
|
|
|
return { status: 404 };
|
|
|
|
}
|
|
|
|
});
|
2021-05-13 23:49:40 -05:00
|
|
|
|
2021-05-22 22:15:28 -05:00
|
|
|
const cli = new APIClient({
|
|
|
|
origin: 'https://misskey.test',
|
|
|
|
credential: 'TOKEN',
|
|
|
|
});
|
|
|
|
|
|
|
|
const res = await cli.request('i');
|
2021-05-13 23:49:40 -05:00
|
|
|
|
2021-05-15 21:11:05 -05:00
|
|
|
// validate response
|
2021-05-13 23:49:40 -05:00
|
|
|
expect(res).toEqual({
|
|
|
|
id: 'foo'
|
|
|
|
});
|
|
|
|
|
2021-05-15 21:11:05 -05:00
|
|
|
// validate fetch call
|
|
|
|
expect(getFetchCall(fetchMock.mock.calls[0])).toEqual({
|
2021-05-13 23:49:40 -05:00
|
|
|
url: 'https://misskey.test/api/i',
|
2021-05-15 21:11:05 -05:00
|
|
|
method: 'POST',
|
2021-05-13 23:49:40 -05:00
|
|
|
body: { i: 'TOKEN' }
|
2021-05-15 21:11:05 -05:00
|
|
|
});
|
2021-05-13 23:49:40 -05:00
|
|
|
});
|
2021-05-17 09:55:13 -05:00
|
|
|
|
2021-05-19 00:01:04 -05:00
|
|
|
test('error', async () => {
|
|
|
|
fetchMock.resetMocks();
|
|
|
|
fetchMock.mockResponse(async (req) => {
|
|
|
|
return {
|
|
|
|
status: 500,
|
|
|
|
body: JSON.stringify({
|
2021-05-22 22:19:39 -05:00
|
|
|
error: {
|
|
|
|
message: 'Internal error occurred. Please contact us if the error persists.',
|
|
|
|
code: 'INTERNAL_ERROR',
|
|
|
|
id: '5d37dbcb-891e-41ca-a3d6-e690c97775ac',
|
|
|
|
kind: 'server',
|
|
|
|
},
|
2021-05-19 00:01:04 -05:00
|
|
|
})
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2021-05-22 22:15:28 -05:00
|
|
|
const cli = new APIClient({
|
|
|
|
origin: 'https://misskey.test',
|
|
|
|
credential: 'TOKEN',
|
|
|
|
});
|
|
|
|
|
|
|
|
await cli.request('i');
|
2021-05-19 00:01:04 -05:00
|
|
|
} catch (e) {
|
2021-05-22 22:19:39 -05:00
|
|
|
expect(isAPIError(e)).toEqual(true);
|
2021-05-19 00:01:04 -05:00
|
|
|
expect(e.id).toEqual('5d37dbcb-891e-41ca-a3d6-e690c97775ac');
|
|
|
|
}
|
|
|
|
});
|
2021-05-17 09:55:13 -05:00
|
|
|
|
|
|
|
// TODO: ネットワークエラーのテスト
|
2021-05-22 22:15:28 -05:00
|
|
|
|
|
|
|
// TODO: JSON以外が返ってきた場合のハンドリング
|
2021-05-13 23:49:40 -05:00
|
|
|
});
|