-
-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add core tests - Tests cover Form and Controller - You can check code coverage with "yarn test --coverage" * A little formatting + form test modifications * add back console log Co-authored-by: Yanick Bélanger <yanick.belanger@yahoo.com> Co-authored-by: Brandon Bayer <b@bayer.ws>
- Loading branch information
1 parent
8f6d0e0
commit 4113124
Showing
10 changed files
with
359 additions
and
57 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
require('@testing-library/jest-dom') |
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 |
---|---|---|
@@ -1,9 +1,2 @@ | ||
export * from './components' | ||
export * from './controller' | ||
|
||
export const sum = (a: number, b: number) => { | ||
if ('development' === process.env.NODE_ENV) { | ||
console.log('boop') | ||
} | ||
return a + b | ||
} |
This file was deleted.
Oops, something went wrong.
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,60 @@ | ||
import {fireEvent, render, RenderResult, wait} from '@testing-library/react' | ||
import Router from 'next/router' | ||
import React, {FC} from 'react' | ||
import Form from '../../src/components/Form' | ||
jest.mock('next/router') | ||
|
||
/** | ||
* Creates a mock implementation of fetch which will resolve asynchronously with the provided `res` param | ||
* @param res The response that will be sent back | ||
*/ | ||
function mockFetch(res: any = {ok: true}) { | ||
Object.assign(global, { | ||
fetch: jest.fn().mockImplementation(async () => res), | ||
}) | ||
} | ||
|
||
describe('Form', () => { | ||
beforeAll(() => { | ||
mockFetch() | ||
}) | ||
|
||
const TestHarness: FC = () => ( | ||
<Form> | ||
<label htmlFor="title">Title</label> | ||
<input id="title" name="title" type="text" defaultValue="test" /> | ||
<button type="submit">Submit</button>> | ||
</Form> | ||
) | ||
|
||
const submitForm = async (page: RenderResult) => { | ||
const form = await page.findByRole('form') | ||
const button = await page.findByRole('button') | ||
fireEvent.click(button, {target: form}) | ||
await wait() | ||
} | ||
|
||
it('renders', async () => { | ||
const page = await render(<TestHarness />) | ||
expect(page.getByLabelText('Title')).toHaveValue('test') | ||
}) | ||
|
||
describe('with redirect', () => { | ||
beforeAll(() => { | ||
const headers = {Location: 'location', 'x-as': 'xas'} as any | ||
mockFetch({ | ||
ok: true, | ||
headers: { | ||
get: jest.fn().mockImplementation(key => headers[key]), | ||
}, | ||
}) | ||
}) | ||
|
||
it('triggers routing', async () => { | ||
const page = await render(<TestHarness />) | ||
await submitForm(page) | ||
|
||
expect(Router.push).toBeCalledWith('location', 'xas') | ||
}) | ||
}) | ||
}) |
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,45 @@ | ||
import {Controller, harnessServerProps} from '../src/controller' | ||
|
||
const SimpleController = Controller(() => ({ | ||
name: 'SimpleController', | ||
|
||
async index() { | ||
return {status: 200, data: {message: 'indexed'}} | ||
}, | ||
|
||
async show() { | ||
return {status: 200, data: {message: 'shown'}} | ||
}, | ||
|
||
async create() { | ||
return { | ||
status: 201, | ||
data: {message: 'created'}, | ||
} | ||
}, | ||
|
||
async update() { | ||
return { | ||
status: 200, | ||
data: {message: 'updated'}, | ||
} | ||
}, | ||
|
||
async delete() { | ||
return { | ||
status: 204, | ||
data: {}, | ||
} | ||
}, | ||
})) | ||
|
||
const RedirectController = Controller(() => ({ | ||
name: 'RedirectController', | ||
|
||
async create() { | ||
return {redirect: {href: 'href', as: 'as'}} | ||
}, | ||
})) | ||
|
||
export const unstable_getSimpleServerProps = harnessServerProps(SimpleController) | ||
export const unstable_getRedirectServerProps = harnessServerProps(RedirectController) |
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,76 @@ | ||
import * as fixtures from './controller-fixtures' | ||
|
||
describe('controller', () => { | ||
const createContext = (opts: any = {}) => ({ | ||
query: {}, | ||
...opts, | ||
req: {method: 'GET', url: '/', socket: {remoteAddress: 'testAddress'}, ...opts.req}, | ||
res: {status: jest.fn(), ...opts.res}, | ||
}) | ||
|
||
const createSimpleRequest = (ctx: any) => fixtures.unstable_getSimpleServerProps(ctx) | ||
const createRedirectRequest = (ctx: any) => fixtures.unstable_getRedirectServerProps(ctx) | ||
|
||
it('simple index response', async () => { | ||
const ctx = createContext() | ||
const returning = (await createSimpleRequest(ctx)) as {props: any} | ||
expect(returning.props).toMatchObject({message: 'indexed'}) | ||
expect(ctx.res.status).toBeCalledWith(200) | ||
}) | ||
|
||
it('simple show response', async () => { | ||
const ctx = createContext({query: {id: 123}}) | ||
const returning = (await createSimpleRequest(ctx)) as {props: any} | ||
expect(returning.props).toMatchObject({message: 'shown'}) | ||
expect(ctx.res.status).toBeCalledWith(200) | ||
}) | ||
|
||
it('simple create response', async () => { | ||
const ctx = createContext({req: {method: 'POST'}}) | ||
const returning = (await createSimpleRequest(ctx)) as {props: any} | ||
expect(returning.props).toMatchObject({message: 'created'}) | ||
}) | ||
|
||
it('simple update response', async () => { | ||
const ctx = createContext({req: {method: 'PATCH'}}) | ||
const returning = (await createSimpleRequest(ctx)) as {props: any} | ||
expect(returning.props).toMatchObject({message: 'updated'}) | ||
}) | ||
|
||
it('simple delete response', async () => { | ||
const ctx = createContext({req: {method: 'DELETE'}}) | ||
await createSimpleRequest(ctx) | ||
expect(ctx.res.status).toBeCalledWith(204) | ||
}) | ||
|
||
it('simple head response', async () => { | ||
const ctx = createContext({ | ||
req: {method: 'HEAD'}, | ||
res: {status: jest.fn().mockImplementation(() => ctx.res), end: jest.fn()}, | ||
}) | ||
await createSimpleRequest(ctx) | ||
expect(ctx.res.status).toBeCalledWith(204) | ||
expect(ctx.res.end).toBeCalled() | ||
}) | ||
|
||
it('simple unknown response', async () => { | ||
const ctx = createContext({req: {method: 'UNKNOWN'}, res: {status: jest.fn(), end: jest.fn()}}) | ||
await createSimpleRequest(ctx) | ||
expect(ctx.res.status).toBeCalledWith(404) | ||
}) | ||
|
||
it('simple api response', async () => { | ||
const ctx = createContext({req: {url: '/api/simple'}, res: {status: jest.fn(), json: jest.fn()}}) | ||
await createSimpleRequest(ctx) | ||
expect(ctx.res.json).toBeCalledWith({message: 'indexed'}) | ||
expect(ctx.res.status).toBeCalledWith(200) | ||
}) | ||
|
||
it('redirect response', async () => { | ||
const ctx = createContext({req: {method: 'POST'}, res: {setHeader: jest.fn(), end: jest.fn()}}) | ||
await createRedirectRequest(ctx) | ||
|
||
expect(ctx.res.setHeader).toBeCalledWith('Location', 'href') | ||
expect(ctx.res.setHeader).toBeCalledWith('x-as', 'as') | ||
}) | ||
}) |
Oops, something went wrong.