From e3b7ed8f47fa96c6558270e223256c468f05f97d Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Tue, 1 Feb 2022 11:14:37 +0100 Subject: [PATCH] fix(js): add client tests (#110) --- tests/CTS/client/search/basic.json | 17 --- tests/CTS/client/search/parameters.json | 88 ++++++++++++ .../templates/javascript/suite.mustache | 2 +- .../javascript/tests/client/search.test.ts | 132 +++++++++++++++++- 4 files changed, 216 insertions(+), 23 deletions(-) delete mode 100644 tests/CTS/client/search/basic.json create mode 100644 tests/CTS/client/search/parameters.json diff --git a/tests/CTS/client/search/basic.json b/tests/CTS/client/search/basic.json deleted file mode 100644 index 79a8e709d2..0000000000 --- a/tests/CTS/client/search/basic.json +++ /dev/null @@ -1,17 +0,0 @@ -[ - { - "testName": "client throws with invalid parameters", - "autoCreateClient": false, - "steps": [ - { - "type": "createClient", - "parameters": { - "apiKey": "blah" - }, - "expected": { - "error": "`appId` is missing." - } - } - ] - } -] diff --git a/tests/CTS/client/search/parameters.json b/tests/CTS/client/search/parameters.json new file mode 100644 index 0000000000..f80fe060e5 --- /dev/null +++ b/tests/CTS/client/search/parameters.json @@ -0,0 +1,88 @@ +[ + { + "testName": "constructor throws with invalid parameters", + "autoCreateClient": false, + "steps": [ + { + "type": "createClient", + "expected": { + "error": "`appId` is missing." + } + }, + { + "type": "createClient", + "parameters": { + "apiKey": "my-api-key" + }, + "expected": { + "error": "`appId` is missing." + } + }, + { + "type": "createClient", + "parameters": { + "appId": "my-app-id" + }, + "expected": { + "error": "`apiKey` is missing." + } + } + ] + }, + { + "testName": "`addApiKey` throws with invalid parameters", + "steps": [ + { + "type": "method", + "object": "$client", + "path": "addApiKey", + "expected": { + "error": "Parameter `apiKey` is required when calling `addApiKey`." + } + }, + { + "type": "method", + "object": "$client", + "path": "addApiKey", + "parameters": [{}], + "expected": { + "error": "Parameter `apiKey.acl` is required when calling `addApiKey`." + } + } + ] + }, + { + "testName": "`addOrUpdateObject` throws with invalid parameters", + "steps": [ + { + "type": "method", + "object": "$client", + "path": "addOrUpdateObject", + "parameters": [{ "objectID": "my-object-id", "body": {} }], + "expected": { + "error": "Parameter `indexName` is required when calling `addOrUpdateObject`." + } + }, + { + "type": "method", + "object": "$client", + "path": "addOrUpdateObject", + "parameters": [{ "indexName": "my-index-name", "body": {} }], + "expected": { + "error": "Parameter `objectID` is required when calling `addOrUpdateObject`." + } + }, + { + "type": "method", + "object": "$client", + "path": "addOrUpdateObject", + "parameters": [ + { "indexName": "my-index-name", "objectID": "my-object-id" } + ], + "expected": { + "error": "Parameter `body` is required when calling `addOrUpdateObject`." + } + } + ] + } +] diff --git a/tests/CTS/client/templates/javascript/suite.mustache b/tests/CTS/client/templates/javascript/suite.mustache index db0d85b295..d863e23a70 100644 --- a/tests/CTS/client/templates/javascript/suite.mustache +++ b/tests/CTS/client/templates/javascript/suite.mustache @@ -14,7 +14,7 @@ function createClient(): {{client}} { {{#blocks}} describe('{{operationId}}', () => { {{#tests}} - test('{{testName}}', async () => { + test('{{{testName}}}', async () => { {{#autoCreateClient}} const $client = createClient(); {{/autoCreateClient}} diff --git a/tests/output/javascript/tests/client/search.test.ts b/tests/output/javascript/tests/client/search.test.ts index 0c69c11479..6001f04cbf 100644 --- a/tests/output/javascript/tests/client/search.test.ts +++ b/tests/output/javascript/tests/client/search.test.ts @@ -1,5 +1,3 @@ -/* eslint-disable @typescript-eslint/no-unused-vars */ - // @ts-nocheck import { SearchApi, EchoRequester } from '@algolia/client-search'; @@ -10,14 +8,14 @@ function createClient(): SearchApi { return new SearchApi(appId, apiKey, { requester: new EchoRequester() }); } -describe('basic', () => { - test('client throws with invalid parameters', async () => { +describe('parameters', () => { + test('constructor throws with invalid parameters', async () => { let actual; await expect( new Promise((resolve, reject) => { const $client = new SearchApi( '', - 'blah', + '', { requester: new EchoRequester(), @@ -32,5 +30,129 @@ describe('basic', () => { } }) ).rejects.toThrow('`appId` is missing.'); + + await expect( + new Promise((resolve, reject) => { + const $client = new SearchApi( + '', + 'my-api-key', + + { + requester: new EchoRequester(), + } + ); + actual = $client; + + if (actual instanceof Promise) { + actual.then(resolve).catch(reject); + } else { + resolve(); + } + }) + ).rejects.toThrow('`appId` is missing.'); + + await expect( + new Promise((resolve, reject) => { + const $client = new SearchApi( + 'my-app-id', + '', + + { + requester: new EchoRequester(), + } + ); + actual = $client; + + if (actual instanceof Promise) { + actual.then(resolve).catch(reject); + } else { + resolve(); + } + }) + ).rejects.toThrow('`apiKey` is missing.'); + }); + + test('`addApiKey` throws with invalid parameters', async () => { + const $client = createClient(); + + let actual; + await expect( + new Promise((resolve, reject) => { + actual = $client.addApiKey(); + if (actual instanceof Promise) { + actual.then(resolve).catch(reject); + } else { + resolve(); + } + }) + ).rejects.toThrow( + 'Parameter `apiKey` is required when calling `addApiKey`.' + ); + + await expect( + new Promise((resolve, reject) => { + actual = $client.addApiKey({}); + if (actual instanceof Promise) { + actual.then(resolve).catch(reject); + } else { + resolve(); + } + }) + ).rejects.toThrow( + 'Parameter `apiKey.acl` is required when calling `addApiKey`.' + ); + }); + + test('`addOrUpdateObject` throws with invalid parameters', async () => { + const $client = createClient(); + + let actual; + await expect( + new Promise((resolve, reject) => { + actual = $client.addOrUpdateObject({ + objectID: 'my-object-id', + body: {}, + }); + if (actual instanceof Promise) { + actual.then(resolve).catch(reject); + } else { + resolve(); + } + }) + ).rejects.toThrow( + 'Parameter `indexName` is required when calling `addOrUpdateObject`.' + ); + + await expect( + new Promise((resolve, reject) => { + actual = $client.addOrUpdateObject({ + indexName: 'my-index-name', + body: {}, + }); + if (actual instanceof Promise) { + actual.then(resolve).catch(reject); + } else { + resolve(); + } + }) + ).rejects.toThrow( + 'Parameter `objectID` is required when calling `addOrUpdateObject`.' + ); + + await expect( + new Promise((resolve, reject) => { + actual = $client.addOrUpdateObject({ + indexName: 'my-index-name', + objectID: 'my-object-id', + }); + if (actual instanceof Promise) { + actual.then(resolve).catch(reject); + } else { + resolve(); + } + }) + ).rejects.toThrow( + 'Parameter `body` is required when calling `addOrUpdateObject`.' + ); }); });