Skip to content

Commit 1dd1651

Browse files
Add logic to map rowCount from raw axios response if present, update tests to ensure this behavior
1 parent 892f9ef commit 1dd1651

2 files changed

Lines changed: 64 additions & 4 deletions

File tree

src/utilities/service-utils.test.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ describe("ServiceUtils", () => {
369369
expect(result.resultObjects).toBeUndefined();
370370
});
371371

372-
test("it returns rowCount equal to the resultObject list length", () => {
372+
test("when response.data.rowCount is undefined, it returns rowCount equal to the resultObject list length", () => {
373373
// Arrange
374374
const resultObject: StubResourceRecord[] = Factory.buildList(
375375
FactoryType.StubResourceRecord,
@@ -380,6 +380,7 @@ describe("ServiceUtils", () => {
380380
{
381381
data: {
382382
resultObject: resultObject,
383+
rowCount: undefined, // This is the important setup
383384
},
384385
}
385386
);
@@ -394,6 +395,61 @@ describe("ServiceUtils", () => {
394395
expect(result.rowCount).toBe(resultObject.length);
395396
});
396397

398+
test("when response.data.rowCount is null, it returns rowCount equal to the resultObject list length", () => {
399+
// Arrange
400+
const resultObject: StubResourceRecord[] = Factory.buildList(
401+
FactoryType.StubResourceRecord,
402+
2
403+
);
404+
const axiosResponse = Factory.build<AxiosResponse>(
405+
AndcultureCodeFactoryType.AxiosResponse,
406+
{
407+
data: {
408+
resultObject: resultObject,
409+
rowCount: null, // This is the important setup
410+
},
411+
}
412+
);
413+
414+
// Act
415+
const result = ServiceUtils.mapPagedAxiosResponse(
416+
StubResourceRecord,
417+
axiosResponse
418+
);
419+
420+
// Assert
421+
expect(result.rowCount).toBe(resultObject.length);
422+
});
423+
424+
test("when response.data.rowCount has a value, it returns the mapped rowCount from the original response", () => {
425+
// Arrange
426+
const resultObject: StubResourceRecord[] = Factory.buildList(
427+
FactoryType.StubResourceRecord,
428+
2
429+
);
430+
const rowCount = faker.random.number({
431+
min: resultObject.length + 1,
432+
}); // This is the important setup (should be different from resultObject.length)
433+
const axiosResponse = Factory.build<AxiosResponse>(
434+
AndcultureCodeFactoryType.AxiosResponse,
435+
{
436+
data: {
437+
resultObject: resultObject,
438+
rowCount: rowCount,
439+
},
440+
}
441+
);
442+
443+
// Act
444+
const result = ServiceUtils.mapPagedAxiosResponse(
445+
StubResourceRecord,
446+
axiosResponse
447+
);
448+
449+
// Assert
450+
expect(result.rowCount).toBe(rowCount);
451+
});
452+
397453
test("it returns the mapped status from the original response", () => {
398454
// Arrange
399455
const axiosResponse = Factory.build<AxiosResponse>(

src/utilities/service-utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ResultRecord } from "../view-models/result-record";
77
import { PagedResult } from "../interfaces/paged-result";
88
import { HttpHeader } from "../enumerations/http-header";
99
import { ContentType } from "../enumerations/content-type";
10+
import { CollectionUtils } from "utilities/collection-utils";
1011

1112
// -----------------------------------------------------------------------------------------
1213
// #region Variables
@@ -128,12 +129,15 @@ const _mapPagedAxiosResponse = <TRecord>(
128129
// Ensure result data is wrapped within records
129130
let resultObjects;
130131
let rowCount = 0;
131-
if (data?.resultObject?.length != null && data.resultObject.length > 0) {
132-
resultObjects = data.resultObject.map((r: any) => new recordType(r));
132+
if (CollectionUtils.hasValues(data?.resultObject)) {
133+
resultObjects = data.resultObject!.map((r: any) => new recordType(r));
134+
135+
// For now, record rowCount as the number of resultObjects we got back. We'll check the
136+
// response for a rowCount of the total query set if the value was returned.
133137
rowCount = resultObjects.length;
134138
}
135139

136-
if (data.rowCount != null) {
140+
if (data?.rowCount != null) {
137141
rowCount = data.rowCount;
138142
}
139143

0 commit comments

Comments
 (0)