Skip to content

Instantly share code, notes, and snippets.

@ahayes91
Created June 13, 2021 00:37
Show Gist options
  • Save ahayes91/72e23f76ea795b4c0cd2dd56ad104c9b to your computer and use it in GitHub Desktop.
Save ahayes91/72e23f76ea795b4c0cd2dd56ad104c9b to your computer and use it in GitHub Desktop.
Snippet to show how you can emulate server-side logic in a Mock Service Worker handler
rest.post(`/assignment`, async (req, res, ctx) => {
// The newly created assignment in this test should have only 1 class and 3 students assigned
const numberOfClassesAssigned = req.body.assignments.length;
const numberOfStudentsAssigned = req.body.assignments[0].students.length;
if (numberOfClassesAssigned === 1 && numberOfStudentsAssigned === 3) {
return res(
ctx.status(200),
ctx.json({ assignments: [{ refId: 'mockRefId' }] }),
);
}
return res(ctx.status(404));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment