Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test for json status params #9

Merged
merged 3 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pages/form-component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test('form component re-renders page on submit', async () => {
message: 'hi from post',
});

await sleep(3);
await sleep(100);

// page should update with a new time from the get handler
await expect(page).not.toMatchText('#time', firstRequestTime);
Expand Down
7 changes: 4 additions & 3 deletions src/__utils__/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ async function request(

return {
status: response.status,
statusText: response.statusText,
headers: response.headers,
body: await response.json(),
};
}

export async function next(getServerSideProps: GetServerSideProps) {
let lastRequest = Date.now();
let lastRequest;

const server = http.createServer((req, res) => {
lastRequest = Date.now();
Expand All @@ -77,10 +78,10 @@ export async function next(getServerSideProps: GetServerSideProps) {
});

const interval = setInterval(() => {
if (Date.now() - lastRequest < 50) return;
if (!lastRequest || Date.now() - lastRequest < 250) return;
clearInterval(interval);
server.close();
}, 3);
}, 25);

const url = await listen(server);

Expand Down
15 changes: 14 additions & 1 deletion src/responses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ test('can set response headers', async () => {
handle({
async get({ req: { method }, res }) {
res.setHeader('session', 'one');
// note: this requires node > 16.4
return json({ method }, { headers: { 'alt-syntax': 'two' } });
},
}),
Expand All @@ -74,6 +73,20 @@ test('can set response headers', async () => {
expect(response.headers.get('alt-syntax')).toEqual('two');
});

test('can set response code and status message', async () => {
const fetch = await next(
handle({
async get({ req: { method }, res }) {
return json({ method }, { status: 418, statusText: `I\'m a teapot` });
},
}),
);

const response = await fetch('/');
expect(response.status).toEqual(418);
expect(response.statusText).toEqual(`I'm a teapot`);
});

test('can get request cookies', async () => {
const fetch = await next(
handle({
Expand Down