-
-
Notifications
You must be signed in to change notification settings - Fork 798
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
[legacy-framework] chore(core): Add core tests #16
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d4647ac
Add core tests
MrLeebo a7e9b21
Merge branch 'canary' into add-core-tests
MrLeebo 8592c58
A little formatting + form test modifications
emeraldsanto 3270c72
add back console log
flybayer 39fb911
Merge branch 'canary' into add-core-tests
flybayer 0c65a32
Merge branch 'canary' into add-core-tests
flybayer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -6,3 +6,4 @@ node_modules | |
reports | ||
*.log | ||
**/.env* | ||
coverage |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave this one in — it adds a new line between logs so they are easier to read
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, I thought it was useless. Why not simply use a newline character?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha, yes you are right — I just hadn't thought about that :)