Skip to content

Commit 2b3590d

Browse files
adding tests to cover new config functionality
1 parent bee94ee commit 2b3590d

File tree

5 files changed

+177
-5
lines changed

5 files changed

+177
-5
lines changed

src/requests/countAll.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ export const countAll = async (request: CountAllRequest, client: IDataverseClien
1313

1414
const response = await retrieveAllRequest(request, client);
1515

16-
return response ? (response.value ? response.value.length : 0) : 0;
16+
return response.value.length;
1717
};

src/utils/Config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const mergeApiConfigs = (apiConfig: ApiConfig | undefined, apiType: ApiType, int
4949
const mergeSearchApiOptions = (options: SearchApiOptions | undefined, internalApiConfig: InternalApiConfig): void => {
5050
if (!options) return;
5151

52-
if (internalApiConfig.escapeSpecialCharacters != null) {
52+
if (options.escapeSpecialCharacters != null) {
5353
ErrorHelper.boolParameterCheck(options.escapeSpecialCharacters, FUNCTION_NAME, `config.searchApi.options.escapeSpecialCharacters`);
5454
internalApiConfig.escapeSpecialCharacters = options.escapeSpecialCharacters;
5555
}
@@ -95,12 +95,12 @@ export class ConfigurationUtility {
9595
internalConfig.maxPageSize = config.maxPageSize;
9696
}
9797

98-
if (config?.returnRepresentation) {
98+
if (config?.returnRepresentation != null) {
9999
ErrorHelper.boolParameterCheck(config.returnRepresentation, FUNCTION_NAME, "config.returnRepresentation");
100100
internalConfig.returnRepresentation = config.returnRepresentation;
101101
}
102102

103-
if (config?.useEntityNames) {
103+
if (config?.useEntityNames != null) {
104104
ErrorHelper.boolParameterCheck(config.useEntityNames, FUNCTION_NAME, "config.useEntityNames");
105105
internalConfig.useEntityNames = config.useEntityNames;
106106
}

tests/common.spec.ts

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import nock from "nock";
33
import * as mocks from "./stubs";
44

55
import * as RequestClient from "../src/client/RequestClient";
6-
import { InternalConfig } from "../src/utils/Config";
6+
import { ConfigurationUtility, InternalConfig } from "../src/utils/Config";
77
import * as Core from "../src/types";
88
import * as Regex from "../src/helpers/Regex";
99
import * as RequestUtility from "../src/client/request";
@@ -285,3 +285,113 @@ describe("Composers.composeHeaders -", () => {
285285
expect(result).to.deep.equal({ "custom-header": "10", something: "else", john: "doe" });
286286
});
287287
});
288+
289+
describe("Config.merge -", () => {
290+
const defaultConfig: InternalConfig = ConfigurationUtility.default();
291+
292+
it("returnRepresentation = true -> false", () => {
293+
const internalConfig: InternalConfig = {
294+
...defaultConfig,
295+
returnRepresentation: true,
296+
};
297+
298+
ConfigurationUtility.merge(internalConfig, {
299+
returnRepresentation: false,
300+
});
301+
302+
expect(internalConfig.returnRepresentation).to.be.false;
303+
});
304+
305+
it("searchApiOptions. escapeSpecialCharacters = null -> true", () => {
306+
const internalConfig: InternalConfig = {
307+
...defaultConfig,
308+
};
309+
310+
ConfigurationUtility.merge(internalConfig, {
311+
searchApi: {
312+
options: {
313+
escapeSpecialCharacters: true,
314+
},
315+
},
316+
});
317+
318+
expect(internalConfig.searchApi.escapeSpecialCharacters).to.be.true;
319+
});
320+
321+
it("searchApiOptions. escapeSpecialCharacters = true -> false", () => {
322+
const internalConfig: InternalConfig = {
323+
...defaultConfig,
324+
searchApi: {
325+
url: "",
326+
escapeSpecialCharacters: true,
327+
},
328+
};
329+
330+
ConfigurationUtility.merge(internalConfig, {
331+
searchApi: {
332+
options: {
333+
escapeSpecialCharacters: false,
334+
},
335+
},
336+
});
337+
338+
expect(internalConfig.searchApi.escapeSpecialCharacters).to.be.false;
339+
});
340+
341+
it("searchApiOptions. escapeSpecialCharacters = false -> true", () => {
342+
const internalConfig: InternalConfig = {
343+
...defaultConfig,
344+
searchApi: {
345+
url: "",
346+
escapeSpecialCharacters: false,
347+
},
348+
};
349+
350+
ConfigurationUtility.merge(internalConfig, {
351+
searchApi: {
352+
options: {
353+
escapeSpecialCharacters: true,
354+
},
355+
},
356+
});
357+
358+
expect(internalConfig.searchApi.escapeSpecialCharacters).to.be.true;
359+
});
360+
361+
it("searchApiOptions. empty options does not overwrite current options", () => {
362+
const internalConfig: InternalConfig = {
363+
...defaultConfig,
364+
searchApi: {
365+
url: "",
366+
escapeSpecialCharacters: true,
367+
},
368+
};
369+
370+
ConfigurationUtility.merge(internalConfig, {
371+
searchApi: {
372+
options: {},
373+
},
374+
});
375+
376+
expect(internalConfig.searchApi.escapeSpecialCharacters).to.be.true;
377+
});
378+
379+
it("searchApiOptions. options = undefined", () => {
380+
const internalConfig: InternalConfig = {
381+
...defaultConfig,
382+
searchApi: {
383+
url: "",
384+
escapeSpecialCharacters: false,
385+
},
386+
};
387+
388+
ConfigurationUtility.merge(internalConfig, {
389+
searchApi: {
390+
version: "2.0"
391+
},
392+
});
393+
394+
expect(internalConfig.searchApi.escapeSpecialCharacters).to.be.false;
395+
expect(internalConfig.searchApi.version).to.be.eq("2.0");
396+
});
397+
});

tests/requests.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { expect } from "chai";
2+
import { IDataverseClient } from "../src/client/dataverse";
3+
import { Config } from "../src/dynamics-web-api";
4+
import { countAll } from "../src/requests";
5+
import { InternalRequest, WebApiResponse } from "../src/types";
6+
import { ConfigurationUtility } from "../src/utils/Config";
7+
import { makeRequest } from "../src/client/RequestClient";
8+
9+
const defaultClient: IDataverseClient = {
10+
config: ConfigurationUtility.default(),
11+
isBatch: false,
12+
batchRequestId: null,
13+
makeRequest: async (_request: InternalRequest) => undefined,
14+
setConfig: (_config: Config): void => {},
15+
};
16+
17+
describe("countAll", () => {
18+
it("returns number of items in an array", async () => {
19+
const testClient = {
20+
...defaultClient,
21+
makeRequest: async (_request: InternalRequest) => {
22+
return { data: { value: ["something", "test"] } } as WebApiResponse;
23+
},
24+
};
25+
26+
const response = await countAll(
27+
{
28+
collection: "test",
29+
},
30+
testClient,
31+
);
32+
33+
expect(response).to.equal(2);
34+
});
35+
});

tests/searchApi.convertQuery.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { convertEntitiesProperty, convertQuery, convertSearchQuery } from "../sr
55
import { InternalApiConfig } from "../src/utils/Config";
66

77
describe("convertEntitiesProperty", () => {
8+
it("returns undefined if entities is undefined", () => {
9+
const convertedEntities = convertEntitiesProperty(undefined, "1.0");
10+
expect(convertedEntities).to.be.undefined;
11+
});
12+
813
describe("v1.0 <- v2.0", () => {
914
describe("string", () => {
1015
const entities = JSON.stringify([
@@ -247,6 +252,28 @@ describe("convertQuery", () => {
247252
});
248253
});
249254

255+
describe("returnTotalRecordCount, searchMode, searchType 2", () => {
256+
const searchQuery: Query = {
257+
search: "test",
258+
returnTotalRecordCount: true,
259+
searchMode: "any",
260+
searchType: "simple",
261+
};
262+
263+
it("converts the query correctly", () => {
264+
convertQuery(searchQuery, "2.0");
265+
266+
expect(searchQuery).to.deep.equal({
267+
search: "test",
268+
count: true,
269+
options: JSON.stringify({
270+
searchmode: "any",
271+
querytype: "simple",
272+
}),
273+
});
274+
});
275+
});
276+
250277
describe("existing options - returnTotalRecordCount, searchMode, searchType", () => {
251278
const searchQuery: Query = {
252279
search: "test",

0 commit comments

Comments
 (0)