feat: tests

This commit is contained in:
2023-04-21 12:08:35 +02:00
parent 8cb9285462
commit d8c79256c9
140 changed files with 2100 additions and 2693 deletions

View File

@@ -28,11 +28,7 @@ import {expect} from 'chai';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import chaiSpies from 'chai-spies';
import {suite, test} from '@testdeck/mocha';
import {Client} from '../src/client.js';
import {ApiError, OutOfRangeError} from '../src/errors.js';
import {HttpClient} from '../src/http-client.js';
import {HttpClientResponse} from '../src/http-client-interface.js';
import {ApiError, OutOfRangeError, Client, HttpClient, HttpClientResponse} from '../src/index.js';
chai.should();
chai.use(chaiSpies);
@@ -84,21 +80,18 @@ async function invokeIndexRouteFails(): Promise<RecursivePartial<HttpClientRespo
};
}
@suite()
export class ClientSpec {
async after() {
describe('Client', function () {
afterEach(function () {
sandbox.restore();
}
});
@test
async construct() {
it('should construct', function () {
expect(() => {
return new Client(httpClient, 'http://localhost');
}).not.to.throw();
}
});
@test
async constructWithHeaders() {
it('should construct with headers', async function () {
sandbox.on(httpClient, 'request', invokeIndexRoute);
expect(httpClient.request).not.to.have.been.first.called();
@@ -115,10 +108,9 @@ export class ClientSpec {
method: indexRoute.method,
url: new URL('http://localhost' + indexRoute.getUrlPath()),
});
}
});
@test
async getThing() {
it('should get thing', async function () {
const message: SCMessage = {
audiences: ['employees'],
categories: ['news'],
@@ -174,10 +166,9 @@ export class ClientSpec {
method: searchRoute.method,
url: new URL('http://localhost' + searchRoute.getUrlPath()),
});
}
});
@test
async getThingFailsByEmptyResponse() {
it('should fail getThing by empty response', async function () {
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCSearchResponse>> => {
return {
body: {
@@ -202,10 +193,9 @@ export class ClientSpec {
const client = new Client(httpClient, 'http://localhost');
return client.getThing('bar').should.be.rejected;
}
});
@test
async getThingFailsByUid() {
it('should fail getThing by uid', async function () {
const message: SCMessage = {
audiences: ['employees'],
categories: ['news'],
@@ -244,10 +234,9 @@ export class ClientSpec {
const client = new Client(httpClient, 'http://localhost');
return client.getThing('bar').should.be.rejected;
}
});
@test
async handshake() {
it('should handshake', async function () {
sandbox.on(httpClient, 'request', invokeIndexRoute);
expect(httpClient.request).not.to.have.been.first.called();
@@ -263,10 +252,9 @@ export class ClientSpec {
method: indexRoute.method,
url: new URL('http://localhost' + indexRoute.getUrlPath()),
});
}
});
@test
async handshakeFails() {
it('should fail handshake', async function () {
sandbox.on(httpClient, 'request', invokeIndexRoute);
expect(httpClient.request).not.to.have.been.first.called();
@@ -274,10 +262,9 @@ export class ClientSpec {
const client = new Client(httpClient, 'http://localhost');
return client.handshake('bar.bar.dummy').should.be.rejectedWith(ApiError);
}
});
@test
async invokePlugin() {
it('should invoke plugin', async function () {
sandbox.on(
httpClient,
'request',
@@ -303,13 +290,12 @@ export class ClientSpec {
await client.invokePlugin('unsupportedPlugin').should.be.rejectedWith(ApiError, /.*supportedPlugin.*/gim);
// again with cached feature definitions
return client
await client
.invokePlugin('supportedPlugin')
.should.not.be.rejectedWith(ApiError, /.*supportedPlugin.*/gim);
}
});
@test
async invokePluginUnavailable() {
it('should invoke unavailable plugin', async function () {
sandbox.on(
httpClient,
'request',
@@ -350,10 +336,9 @@ export class ClientSpec {
);
// again with cached feature definitions
return client.invokePlugin('supportedPlugin').should.be.rejectedWith(ApiError, /.*supportedPlugin.*/gim);
}
});
@test
async invokeRoute() {
it('should invoke route', async function () {
sandbox.on(httpClient, 'request', invokeIndexRoute);
expect(httpClient.request).not.to.have.been.first.called();
@@ -369,10 +354,9 @@ export class ClientSpec {
method: indexRoute.method,
url: new URL('http://localhost' + indexRoute.getUrlPath()),
});
}
});
@test
async invokeRouteFails() {
it('should fail to invoke route', async function () {
sandbox.on(httpClient, 'request', invokeIndexRouteFails);
expect(httpClient.request).not.to.have.been.first.called();
@@ -380,10 +364,9 @@ export class ClientSpec {
const client = new Client(httpClient, 'http://localhost');
return client.invokeRoute(indexRoute).should.be.rejectedWith(ApiError);
}
});
@test
async multiSearch() {
it('should multi search', async function () {
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCMultiSearchResponse>> => {
return {
body: {
@@ -430,10 +413,9 @@ export class ClientSpec {
method: multiSearchRoute.method,
url: new URL('http://localhost' + multiSearchRoute.getUrlPath()),
});
}
});
@test
async multiSearchWithPreflight() {
it('should multi search with preflight', async function () {
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCMultiSearchResponse>> => {
return {
body: {
@@ -488,10 +470,9 @@ export class ClientSpec {
method: multiSearchRoute.method,
url: new URL('http://localhost' + multiSearchRoute.getUrlPath()),
});
}
});
@test
nextWindow() {
it('should next window', async function () {
let searchRequest: SCSearchRequest = {size: 30};
const searchResponse: SCSearchResponse = {
data: [],
@@ -515,10 +496,9 @@ export class ClientSpec {
expect(() => {
Client.nextWindow(searchRequest, searchResponse);
}).to.throw(OutOfRangeError);
}
});
@test
async search() {
it('should search', async function () {
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCSearchResponse>> => {
return {
body: {
@@ -551,10 +531,9 @@ export class ClientSpec {
method: searchRoute.method,
url: new URL('http://localhost' + searchRoute.getUrlPath()),
});
}
});
@test
async searchNext() {
it('should search next', async function () {
const searchResponse: SCSearchResponse = {
data: [],
facets: [],
@@ -589,10 +568,9 @@ export class ClientSpec {
method: searchRoute.method,
url: new URL('http://localhost' + searchRoute.getUrlPath()),
});
}
});
@test
async searchWithPreflight() {
it('should search with preflight', async function () {
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCSearchResponse>> => {
return {
body: {
@@ -633,5 +611,5 @@ export class ClientSpec {
method: searchRoute.method,
url: new URL('http://localhost' + searchRoute.getUrlPath()),
});
}
}
});
});