Skip to content

Commit

Permalink
A little formatting + form test modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
emeraldsanto committed Mar 2, 2020
1 parent a7e9b21 commit 8592c58
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
7 changes: 3 additions & 4 deletions packages/core/src/components/Form.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import Router from 'next/router'
import React from 'react'

export default function({children, action, method, ...props}: {[index: string]: any}) {
const [i, setI] = React.useState(0)
Expand All @@ -22,10 +22,9 @@ export default function({children, action, method, ...props}: {[index: string]:
const form = event.target as HTMLFormElement
const data = new URLSearchParams()

for (const pair of new FormData(form).entries()) {
console.log(pair[0], pair[1])
for (const [key, value] of new FormData(form).entries()) {
// TODO: handle file types
data.append(pair[0], pair[1] as string)
data.append(key, value.toString())
}

const res = await fetch(action, {
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {NextApiRequest, NextApiResponse} from 'next'
import {PrismaClient} from '@prisma/client'
import prettyMs from 'pretty-ms'
import {NextApiRequest, NextApiResponse} from 'next'
import permit from 'permit-params'
import {isServer} from './utils'
import prettyMs from 'pretty-ms'
import {ControllerInput, ControllerInstance} from '../types/controller'
import {isServer} from './utils'

export {default as Form} from './components/Form'

Expand Down Expand Up @@ -38,7 +38,6 @@ export const harnessController = (Controller: ControllerInstance) => async (
res: NextApiResponse,
) => {
const startTime = new Date().getTime()
console.log('')
console.log(
`Started ${req.method} "${req.url}" for ${req.socket?.remoteAddress || 'unknown'} at ${new Date()}`,
)
Expand Down
30 changes: 19 additions & 11 deletions packages/core/test/components/Form.test.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
import React from 'react'
import {fireEvent, render, RenderResult, wait} from '@testing-library/react'
import Router from 'next/router'
import {render, wait, fireEvent} from '@testing-library/react'
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', () => {
const TestHarness = () => (
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 mockFetch = (res: any = {ok: true}) => {
;(global as any).fetch = jest.fn().mockImplementation(async () => res)
}

const submitForm = async (page: any) => {
const submitForm = async (page: RenderResult) => {
const form = await page.findByRole('form')
const button = await page.findByRole('button')
fireEvent.click(button, {target: form})
await wait()
}

beforeEach(() => mockFetch())

it('renders', async () => {
const page = await render(<TestHarness />)
expect(page.getByLabelText('Title')).toHaveValue('test')
})

describe('with redirect', () => {
beforeEach(() => {
beforeAll(() => {
const headers = {Location: 'location', 'x-as': 'xas'} as any
mockFetch({
ok: true,
Expand Down

0 comments on commit 8592c58

Please sign in to comment.