Skip to content

Commit

Permalink
Merge c30305a into e2ac078
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign committed Sep 27, 2020
2 parents e2ac078 + c30305a commit 5c7885b
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 41 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Changelog

## 0.2.0

### Breaking Changes

- Expose Typescript types

### New Features

- Migrate codebase to Typescript
- Replace query-string with node's querystring

## 0.1.0

Initial release
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ Next page tester will take care of:

## Options

| Property | Description | type | Default |
| ------------------ | ---------------------------------------------------------------------------------- | ------------------ | ------- |
| **route** | Next route (must start with `/`) | - | - |
| **pagesDirectory** | Absolute path of Next's `/pages` folder | - | - |
| **req** | Override default mocked [request object][req-docs]<br>(`getServerSideProps` only) | `res => res` | - |
| **res** | Override default mocked [response object][res-docs]<br>(`getServerSideProps` only) | `req => req` | - |
| **router** | Override default mocked [Next router object][next-docs-router] | `router => router` | - |
| Property | Description | type | Default |
| ------------------ | -------------------------------------------------------------------------------- | ------------------ | ------- |
| **route** | Next route (must start with `/`) | - | - |
| **pagesDirectory** | Absolute path of Next's `/pages` folder | - | - |
| **req** | Access default mocked [request object][req-docs]<br>(`getServerSideProps` only) | `res => res` | - |
| **res** | Access default mocked [response object][res-docs]<br>(`getServerSideProps` only) | `req => req` | - |
| **router** | Access default mocked [Next router object][next-docs-router] | `router => router` | - |

## Notes

Expand All @@ -61,6 +61,7 @@ It might be necessary to install `@types/react-dom` and `@types/webpack` when us
- Make available dynamic api routes under `/pages/api`
- Consider adding custom App and Document
- Consider adding a `getPage` factory
- Consider reusing Next.js code parts (not only types)

[ci]: https://travis-ci.com/toomuchdesign/next-page-tester
[ci-badge]: https://travis-ci.com/toomuchdesign/next-page-tester.svg?branch=master
Expand Down
23 changes: 2 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"clean": "rm -rf dist",
"compile": "npm run clean && tsc",
"test": "jest ./src",
"test:source": "npm t -- --coverage",
"test:source": "npm run test:ts && npm t -- --coverage",
"test:ts": "tsc --noEmit",
"prepare": "npm run test:source && npm run compile",
"preversion": "npm run prepare",
"version": "git add package.json",
Expand Down Expand Up @@ -61,7 +62,6 @@
"@types/express": "^4.17.8",
"@types/recursive-readdir": "^2.2.0",
"node-mocks-http": "^1.9.0",
"query-string": "^6.13.1",
"recursive-readdir": "^2.2.2"
},
"peerDependencies": {
Expand Down Expand Up @@ -89,7 +89,7 @@
"lint-staged": {
"**/*.{js,ts,json}": [
"prettier",
"npm t -- ."
"npm run test:source -- ."
],
"**/*.{md}": [
"prettier"
Expand Down
4 changes: 2 additions & 2 deletions src/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type Res = ReturnType<typeof createResponse>;
export type Options = {
route: string;
pagesDirectory: string;
req?: (req: Req) => { [name: string]: any };
res?: (res: Res) => { [name: string]: any };
req?: (req: Req) => Req;
res?: (res: Res) => Res;
router?: (router: NextRouter) => NextRouter;
};

Expand Down
11 changes: 5 additions & 6 deletions src/preparePage.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import React, { ReactNode } from 'react';
import queryString from 'query-string';
import { RouterContext } from 'next/dist/next-server/lib/router-context';
import type { NextRouter } from 'next/router';
import { parseRoute, removeFileExtension } from './utils';
import { parseRoute, removeFileExtension, parseQueryString } from './utils';
import type { Options, PageObject } from './commonTypes';

// https://github.com/vercel/next.js/issues/7479#issuecomment-659859682
function makeDefaultRouterMock(props = {}): NextRouter {
function makeDefaultRouterMock(props?: Partial<NextRouter>): NextRouter {
const routerMock = {
basePath: '',
pathname: '/',
route: '/',
asPath: '/',
query: {},
push: async () => true,
replace: async () => true,
push: /* istanbul ignore next */ async () => true,
replace: /* istanbul ignore next */ async () => true,
reload: () => {},
back: () => {},
prefetch: async () => {},
Expand Down Expand Up @@ -47,7 +46,7 @@ export default function preparePage({
makeDefaultRouterMock({
asPath: pathname + search + hash, // Includes querystring and anchor
pathname: removeFileExtension({ path: pagePath }), // Page component path without extension
query: { ...params, ...queryString.parse(search) }, // Route params + parsed querystring
query: { ...params, ...parseQueryString({ queryString: search }) }, // Route params + parsed querystring
route: removeFileExtension({ path: pagePath }), // Page component path without extension
})
),
Expand Down
13 changes: 11 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
const url = require('url');
import { URL } from 'url';
import querystring from 'querystring';

export function parseRoute({ route }: { route: string }) {
return url.parse(`http://test.com${route}`);
return new URL(`http://test.com${route}`);
}

export function parseQueryString({ queryString }: { queryString: string }) {
const qs = queryString.startsWith('?')
? queryString.substring(1)
: queryString;

return querystring.parse(qs);
}

export function sleep(ms: number) {
Expand Down

0 comments on commit 5c7885b

Please sign in to comment.