From 154e0fe94a2567260647084cf76cb0db2c77b5d5 Mon Sep 17 00:00:00 2001
From: syuilo <Syuilotan@yahoo.co.jp>
Date: Fri, 25 Jun 2021 09:58:35 +0900
Subject: [PATCH] add tests

---
 test/api.ts | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 59 insertions(+), 2 deletions(-)

diff --git a/test/api.ts b/test/api.ts
index 8b27c54ed1..a7294a7161 100644
--- a/test/api.ts
+++ b/test/api.ts
@@ -38,12 +38,10 @@ describe('API', () => {
 
 		const res = await cli.request('i');
 
-		// validate response
 		expect(res).toEqual({
 			id: 'foo'
 		});
 
-		// validate fetch call
 		expect(getFetchCall(fetchMock.mock.calls[0])).toEqual({
 			url: 'https://misskey.test/api/i',
 			method: 'POST',
@@ -51,6 +49,65 @@ describe('API', () => {
 		});
 	});
 
+	test('with params', async () => {
+		fetchMock.resetMocks();
+		fetchMock.mockResponse(async (req) => {
+			const body = await req.json();
+			if (req.method == 'POST' && req.url == 'https://misskey.test/api/notes/show') {
+				if (body.i === 'TOKEN' && body.noteId === 'aaaaa') {
+					return JSON.stringify({ id: 'foo' });
+				} else {
+					return { status: 400 };
+				}
+			} else {
+				return { status: 404 };
+			}
+		});
+
+		const cli = new APIClient({
+			origin: 'https://misskey.test',
+			credential: 'TOKEN',
+		});
+
+		const res = await cli.request('notes/show', { noteId: 'aaaaa' });
+
+		expect(res).toEqual({
+			id: 'foo'
+		});
+
+		expect(getFetchCall(fetchMock.mock.calls[0])).toEqual({
+			url: 'https://misskey.test/api/notes/show',
+			method: 'POST',
+			body: { i: 'TOKEN', noteId: 'aaaaa' }
+		});
+	});
+
+	test('204 No Content で null が返る', async () => {
+		fetchMock.resetMocks();
+		fetchMock.mockResponse(async (req) => {
+			if (req.method == 'POST' && req.url == 'https://misskey.test/api/reset-password') {
+				return { status: 204 };
+			} else {
+				return { status: 404 };
+			}
+		});
+
+		const cli = new APIClient({
+			origin: 'https://misskey.test',
+			credential: 'TOKEN',
+		});
+
+		const res = await cli.request('reset-password', { token: 'aaa', password: 'aaa' });
+
+		expect(res).toEqual(null);
+
+		expect(getFetchCall(fetchMock.mock.calls[0])).toEqual({
+			url: 'https://misskey.test/api/reset-password',
+			method: 'POST',
+			body: { i: 'TOKEN', token: 'aaa', password: 'aaa' }
+		});
+	});
+
 	test('インスタンスの credential が指定されていても引数で credential が null ならば null としてリクエストされる', async () => {
 		fetchMock.resetMocks();
 		fetchMock.mockResponse(async (req) => {
-- 
GitLab