Skip to content

Commit

Permalink
Fetch all measurements that belong to a single measurement category
Browse files Browse the repository at this point in the history
The measurement detail page would only show the first 20 measurements
instead of all measurements.

See: #782
  • Loading branch information
StijnKing authored and rolandgeider committed May 1, 2024
1 parent 53ed9ba commit 1130eaa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
43 changes: 42 additions & 1 deletion src/services/measurements.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import { MeasurementCategory } from "components/Measurements/models/Category";
import { MeasurementEntry } from "components/Measurements/models/Entry";
import { getMeasurementCategories } from "services/measurements";
import { getMeasurementCategories, getMeasurementCategory } from "services/measurements";

jest.mock("axios");

Expand Down Expand Up @@ -53,4 +53,45 @@ describe('measurement service tests', () => {
])
]);
});

test('GET measurement category', async () => {
const measurementResponse = {
"id": 1,
"name": "Weight",
"unit": "kg"
};

const measurementEntryResponse = {
count: 2,
next: null,
previous: null,
results: [
{
"id": 1,
"category": 1,
"value": 80,
"date": "2021-01-01",
"notes": ""
}
]
};

// @ts-ignore
axios.get.mockImplementation((url: string) => {
if (url.includes("measurement-category/1")) {
return Promise.resolve({ data: measurementResponse });
} else if (url.includes("measurement/?category=1")) {
return Promise.resolve({ data: measurementEntryResponse });
}
});

const result = await getMeasurementCategory(1);
expect(axios.get).toHaveBeenCalledTimes(2);

expect(result).toStrictEqual(
new MeasurementCategory(1, "Weight", "kg", [
new MeasurementEntry(1, 1, new Date("2021-01-01"), 80, "")
])
);
});
});
17 changes: 11 additions & 6 deletions src/services/measurements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,18 @@ export const getMeasurementCategory = async (id: number): Promise<MeasurementCat
);

const category = new MeasurementCategoryAdapter().fromJson(receivedCategories);

const { data: receivedEntries } = await axios.get<ResponseType<ApiMeasurementEntryType>>(
makeUrl(API_MEASUREMENTS_ENTRY_PATH, { query: { category: id.toString() } }),
{ headers: makeHeader(), }
);
const adapter = new MeasurementEntryAdapter();
category.entries = receivedEntries.results.map(l => adapter.fromJson(l));
const measurements: MeasurementEntry[] = [];
const url = makeUrl(API_MEASUREMENTS_ENTRY_PATH, { query: { category: category.id } });

// Collect all pages of entries
for await (const page of fetchPaginated(url, makeHeader())) {
for (const entries of page) {
measurements.push(adapter.fromJson(entries));
}
}

category.entries = measurements;

return category;
};
Expand Down

0 comments on commit 1130eaa

Please sign in to comment.