Skip to content

Commit

Permalink
fixup! feat(components): initial mutations over time component
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKellerer committed Jul 19, 2024
1 parent 861b990 commit 8bbcbd8
Showing 1 changed file with 35 additions and 52 deletions.
87 changes: 35 additions & 52 deletions components/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type AssertionError } from 'node:assert';

import { http } from 'msw';
import { type DefaultBodyType, http, type StrictRequest } from 'msw';
import { setupServer } from 'msw/node';
import { afterAll, afterEach, beforeAll, expect } from 'vitest';

Expand Down Expand Up @@ -52,33 +52,7 @@ export const lapisRequestMocks = {
};
}[],
) => {
testServer.use(
http.post(aggregatedEndpoint(DUMMY_LAPIS_URL), async ({ request }) => {
const actualBody = await request.json();

const errors = [];
for (const { body, response } of expectedRequests) {
try {
expect(actualBody, 'Request body did not match').to.deep.equal(body);
} catch (error) {
errors.push(error);
continue;
}
return new Response(JSON.stringify(response), {
status: 200,
});
}

return new Response(
JSON.stringify({
error: errors.map((error) => getError(error as AssertionError)),
}),
{
status: 400,
},
);
}),
);
testServer.use(http.post(aggregatedEndpoint(DUMMY_LAPIS_URL), resolver(expectedRequests)));
},
multipleMutations: (
expectedRequests: {
Expand All @@ -88,34 +62,43 @@ export const lapisRequestMocks = {
sequenceType: 'nucleotide' | 'amino acid',
) => {
testServer.use(
http.post(substitutionsOrDeletionsEndpoint(DUMMY_LAPIS_URL, sequenceType), async ({ request }) => {
const actualBody = await request.json();
http.post(substitutionsOrDeletionsEndpoint(DUMMY_LAPIS_URL, sequenceType), resolver(expectedRequests)),
);
},
};

const errors = [];
for (const { body, response } of expectedRequests) {
try {
expect(actualBody, 'Request body did not match').to.deep.equal(body);
} catch (error) {
errors.push(error);
continue;
}
return new Response(JSON.stringify(response), {
status: 200,
});
}
function resolver(
expectedRequests: {
body: unknown;
response: unknown;
}[],
) {
return async ({ request }: { request: StrictRequest<DefaultBodyType> }) => {
const actualBody = await request.json();

return new Response(
JSON.stringify({
error: errors.map((error) => getError(error as AssertionError)),
}),
{
status: 400,
},
);
const errors = [];
for (const { body, response } of expectedRequests) {
try {
expect(actualBody, 'Request body did not match').to.deep.equal(body);
} catch (error) {
errors.push(error);
continue;
}
return new Response(JSON.stringify(response), {
status: 200,
});
}

return new Response(
JSON.stringify({
error: errors.map((error) => getError(error as AssertionError)),
}),
{
status: 400,
},
);
},
};
};
}

beforeAll(() => testServer.listen({ onUnhandledRequest: 'warn' }));

Expand Down

0 comments on commit 8bbcbd8

Please sign in to comment.