[CRUD] Add basic tests#4709
Merged
apedroferreira merged 9 commits intomui:masterfrom Mar 13, 2025
apedroferreira:crud-tests
Merged
[CRUD] Add basic tests#4709apedroferreira merged 9 commits intomui:masterfrom apedroferreira:crud-tests
apedroferreira merged 9 commits intomui:masterfrom
apedroferreira:crud-tests
Conversation
Netlify deploy preview |
Janpot
reviewed
Mar 11, 2025
| ], | ||
| getMany: async ({ paginationModel, filterModel, sortModel }) => { | ||
| getMany: ({ paginationModel, filterModel, sortModel }) => { | ||
| return new Promise((resolve) => { |
Member
There was a problem hiding this comment.
Using an all encompassing promise constructor with callback code inside is an anti-pattern. This should really be written as
getMany: async ({ paginationModel, filterModel, sortModel }) => {
await new Promise((resolve) => {
setTimeout(resolve, 750);
});
let processedNotes = [...notesStore];
// Apply filters (demo only)
if (filterModel?.items?.length) {
filterModel.items.forEach(({ field, value, operator }) => {
if (!field || value == null) {
return;
}
processedNotes = processedNotes.filter((note) => {
const noteValue = note[field];
switch (operator) {
case 'contains':
return String(noteValue)
.toLowerCase()
.includes(String(value).toLowerCase());
case 'equals':
return noteValue === value;
case 'startsWith':
return String(noteValue)
.toLowerCase()
.startsWith(String(value).toLowerCase());
case 'endsWith':
return String(noteValue)
.toLowerCase()
.endsWith(String(value).toLowerCase());
case '>':
return noteValue > value;
case '<':
return noteValue < value;
default:
return true;
}
});
});
}
// Apply sorting
if (sortModel?.length) {
processedNotes.sort((a, b) => {
for (const { field, sort } of sortModel) {
if (a[field] < b[field]) {
return sort === 'asc' ? -1 : 1;
}
if (a[field] > b[field]) {
return sort === 'asc' ? 1 : -1;
}
}
return 0;
});
}
// Apply pagination
const start = paginationModel.page * paginationModel.pageSize;
const end = start + paginationModel.pageSize;
const paginatedNotes = processedNotes.slice(start, end);
return {
items: paginatedNotes,
itemCount: processedNotes.length,
};
},everywhere. In most cases, code inside of a promise constructure should be strictly reduced to the callback calling function in practice, then be composed as promises.
Collaborator
Author
There was a problem hiding this comment.
Ok I can change that so that not everything is wrapped in the Promise, using await just for the timeout sounds good.
Janpot
approved these changes
Mar 13, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add tests for CRUD component.
Covers: