Skip to content

Commit

Permalink
fix(js): add client tests (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eunjae Lee authored Feb 1, 2022
1 parent 9c1d656 commit e3b7ed8
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 23 deletions.
17 changes: 0 additions & 17 deletions tests/CTS/client/search/basic.json

This file was deleted.

88 changes: 88 additions & 0 deletions tests/CTS/client/search/parameters.json
Original file line number Diff line number Diff line change
@@ -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`."
}
}
]
}
]
2 changes: 1 addition & 1 deletion tests/CTS/client/templates/javascript/suite.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function createClient(): {{client}} {
{{#blocks}}
describe('{{operationId}}', () => {
{{#tests}}
test('{{testName}}', async () => {
test('{{{testName}}}', async () => {
{{#autoCreateClient}}
const $client = createClient();
{{/autoCreateClient}}
Expand Down
132 changes: 127 additions & 5 deletions tests/output/javascript/tests/client/search.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/no-unused-vars */

// @ts-nocheck
import { SearchApi, EchoRequester } from '@algolia/client-search';

Expand All @@ -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(),
Expand All @@ -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`.'
);
});
});

0 comments on commit e3b7ed8

Please sign in to comment.