forked from jaredpalmer/after.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add first jest unit tests and fix another bug in loadInitialProps (ja…
…redpalmer#178) * add first jest unit tests and fix bug in loadInitialProps * remove unused dep * fix typo for test Component * fix typo in mock NonDymamicImport * remove unnecessary cast in loadInitialProps * revert Async -> After name changes
- Loading branch information
1 parent
4bf6dfe
commit 3e4f2f4
Showing
17 changed files
with
4,517 additions
and
420 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ node_modules | |
*.log | ||
package-lock.json | ||
build | ||
.vscode | ||
.vscode | ||
*.map |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
|
||
export interface AsyncGetInitialPropsProps { | ||
stuff: string; | ||
} | ||
|
||
const api = () => Promise.resolve({stuff: 'async call'}) | ||
|
||
class AsyncGetInitialProps extends React.Component<AsyncGetInitialPropsProps> { | ||
static async getInitialProps({ req, res, match, history, location, ...ctx }) { | ||
return await api(); | ||
} | ||
|
||
render() { | ||
return <h1>AsyncGetInitialProps</h1> | ||
} | ||
} | ||
|
||
export default AsyncGetInitialProps; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
|
||
class Home extends React.Component { | ||
static displayName = 'Assessments'; | ||
|
||
static async getInitialProps({ req, res, match, history, location, ...ctx }) { | ||
return { stuff: 'home stuffs' }; | ||
} | ||
|
||
render() { | ||
return ( | ||
<h1>Home</h1> | ||
); | ||
} | ||
} | ||
|
||
export default Home; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import React from 'react'; | ||
|
||
export const NoGetInitialProps = () => <h1>No getInitialProps</h1> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react'; | ||
|
||
export class NonDefaultExport extends React.Component { | ||
static displayName = 'NonDefaultExport'; | ||
|
||
static async getInitialProps({ req, res, match, history, location, ...ctx }) { | ||
return { stuff: 'non default export' }; | ||
} | ||
|
||
render() { | ||
return ( | ||
<h1>Non Default Export</h1> | ||
); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
|
||
class NonDymamicImport extends React.Component { | ||
static displayName = 'NonDynamicExport'; | ||
|
||
static async getInitialProps({ req, res, match, history, location, ...ctx }) { | ||
return { stuff: 'non dynamic export' }; | ||
} | ||
|
||
render() { | ||
return ( | ||
<h1>Non Dynamic Import</h1> | ||
); | ||
} | ||
} | ||
|
||
export default NonDymamicImport; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { loadInitialProps } from '../loadInitialProps'; | ||
import routes from './routes'; | ||
import createMemoryHistory from 'history/createMemoryHistory'; | ||
import { History } from 'history' | ||
|
||
describe('loadInitialProps', () => { | ||
let history: History; | ||
|
||
beforeEach(() => { | ||
history = createMemoryHistory(); | ||
}) | ||
|
||
it('should find matched component and call getInitialProps', async () => { | ||
const url = '/' | ||
|
||
const matched = await loadInitialProps(routes, url, { history }); | ||
|
||
const expected = routes.find(r => r.path === url); | ||
|
||
expect(matched.match).toEqual(expected); | ||
|
||
expect(matched.data).toEqual({ stuff: 'home stuffs' }); | ||
}); | ||
|
||
|
||
it('should retrieve initial props from async call', async () => { | ||
const url = '/async-get-initial-props'; | ||
|
||
const matched = await loadInitialProps(routes, url, { history }); | ||
|
||
expect(matched.match.path).toBe(url); | ||
|
||
expect(matched.data).toEqual({ stuff: 'async call' }); | ||
}); | ||
|
||
it('should call getInitialProps for non dynamic import component', async () => { | ||
const url = '/non-dynamic-import'; | ||
|
||
const matched = await loadInitialProps(routes, url, { history }); | ||
|
||
expect(matched.match.path).toBe(url); | ||
|
||
expect(matched.data).toEqual({ stuff: 'non dynamic export' }); | ||
}); | ||
|
||
it('should call getInitialProps for non default export component', async () => { | ||
const url = '/non-default-export'; | ||
|
||
const matched = await loadInitialProps(routes, url, { history }); | ||
|
||
expect(matched.match.path).toBe(url); | ||
|
||
expect(matched.data).toEqual({ stuff: 'non default export' }); | ||
}); | ||
|
||
it('should load component with no getInitialProps', async () => { | ||
const url = '/no-get-initial-props'; | ||
|
||
const matched = await loadInitialProps(routes, url, { history }); | ||
|
||
expect(matched.match.path).toBe(url); | ||
|
||
expect(matched.data).toBeUndefined(); | ||
}); | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react'; | ||
|
||
import { asyncComponent } from '../asyncComponent'; | ||
import NonDymamicImport from './components/NonDynamicExport'; | ||
import { NoGetInitialProps } from './components/NoGetInitialProps'; | ||
|
||
const Placeholder = () => <div>...LOADING...</div> | ||
|
||
export default [ | ||
{ | ||
path: '/', | ||
exact: true, | ||
component: asyncComponent({ | ||
loader: () => import('./components/Home'), | ||
Placeholder | ||
}), | ||
}, | ||
{ | ||
path: '/async-get-initial-props', | ||
exact: true, | ||
component: asyncComponent({ | ||
loader: () => import('./components/AsyncGetInitialProps'), | ||
Placeholder | ||
}), | ||
}, | ||
{ | ||
path: '/non-dynamic-import', | ||
exact: true, | ||
component: NonDymamicImport | ||
}, | ||
{ | ||
path: '/non-default-export', | ||
exact: true, | ||
component: asyncComponent({ | ||
loader: () => import('./components/NoNDefaultExport').then((module) => module.NonDefaultExport), | ||
Placeholder | ||
}), | ||
}, | ||
{ | ||
path: '/no-get-initial-props', | ||
exact: true, | ||
component: NoGetInitialProps | ||
}, | ||
]; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
"config", | ||
"src/**/*.test.ts", | ||
"src/**/*.test.tsx", | ||
"src/**/*.story.tsx" | ||
"src/**/*.story.tsx", | ||
"src/test" | ||
] | ||
} | ||
} |
This file contains 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
Oops, something went wrong.