From d4647ac5af422f5cb475add4a9fff6e9acae3aa7 Mon Sep 17 00:00:00 2001 From: Jeremy Liberman Date: Thu, 27 Feb 2020 00:18:34 -0600 Subject: [PATCH 1/3] Add core tests - Tests cover Form and Controller - You can check code coverage with "yarn test --coverage" --- .gitignore | 1 + packages/core/jest.setup.js | 1 + packages/core/package.json | 7 ++ packages/core/src/index.ts | 7 -- packages/core/test/blah.test.ts | 7 -- packages/core/test/components/Form.test.tsx | 52 +++++++++ packages/core/test/controller-fixtures.ts | 45 ++++++++ packages/core/test/controller.test.ts | 76 +++++++++++++ yarn.lock | 118 +++++++++++++++++++- 9 files changed, 294 insertions(+), 20 deletions(-) create mode 100644 packages/core/jest.setup.js delete mode 100644 packages/core/test/blah.test.ts create mode 100644 packages/core/test/components/Form.test.tsx create mode 100644 packages/core/test/controller-fixtures.ts create mode 100644 packages/core/test/controller.test.ts diff --git a/.gitignore b/.gitignore index 91780f0d0b..2776debf56 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ node_modules reports *.log **/.env* +coverage diff --git a/packages/core/jest.setup.js b/packages/core/jest.setup.js new file mode 100644 index 0000000000..70c5472408 --- /dev/null +++ b/packages/core/jest.setup.js @@ -0,0 +1 @@ +require('@testing-library/jest-dom') diff --git a/packages/core/package.json b/packages/core/package.json index 1a339fa30b..ea54c55026 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -45,7 +45,14 @@ "pre-commit": "tsdx lint" } }, + "jest": { + "setupFilesAfterEnv": [ + "/jest.setup.js" + ] + }, "devDependencies": { + "@testing-library/react": "9.4.1", + "@testing-library/jest-dom": "5.1.1", "@types/react": "16.9.23", "next": "9.2.3-canary.8", "prisma2": "2.0.0-preview022" diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index e1aaae04d1..87693e5351 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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 -} diff --git a/packages/core/test/blah.test.ts b/packages/core/test/blah.test.ts deleted file mode 100644 index 33c7ef5073..0000000000 --- a/packages/core/test/blah.test.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { sum } from '../src'; - -describe('blah', () => { - it('works', () => { - expect(sum(1, 1)).toEqual(2); - }); -}); diff --git a/packages/core/test/components/Form.test.tsx b/packages/core/test/components/Form.test.tsx new file mode 100644 index 0000000000..2ccca28c79 --- /dev/null +++ b/packages/core/test/components/Form.test.tsx @@ -0,0 +1,52 @@ +import React from 'react' +import Router from 'next/router' +import {render, wait, fireEvent} from '@testing-library/react' +import Form from '../../src/components/Form' +jest.mock('next/router') + +describe('Form', () => { + const TestHarness = () => ( +
+ + + > +
+ ) + + const mockFetch = (res: any = {ok: true}) => { + ;(global as any).fetch = jest.fn().mockImplementation(async () => res) + } + + const submitForm = async (page: any) => { + 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() + expect(page.getByLabelText('Title')).toHaveValue('test') + }) + + describe('with redirect', () => { + beforeEach(() => { + 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() + await submitForm(page) + + expect(Router.push).toBeCalledWith('location', 'xas') + }) + }) +}) diff --git a/packages/core/test/controller-fixtures.ts b/packages/core/test/controller-fixtures.ts new file mode 100644 index 0000000000..3c5e70f88c --- /dev/null +++ b/packages/core/test/controller-fixtures.ts @@ -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) diff --git a/packages/core/test/controller.test.ts b/packages/core/test/controller.test.ts new file mode 100644 index 0000000000..98f8dc6f8d --- /dev/null +++ b/packages/core/test/controller.test.ts @@ -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') + }) +}) diff --git a/yarn.lock b/yarn.lock index b7caae73de..a39bfb5127 100644 --- a/yarn.lock +++ b/yarn.lock @@ -998,7 +998,7 @@ dependencies: regenerator-runtime "^0.13.2" -"@babel/runtime@^7.4.5", "@babel/runtime@^7.6.3", "@babel/runtime@^7.7.2": +"@babel/runtime@^7.4.5", "@babel/runtime@^7.6.2", "@babel/runtime@^7.6.3", "@babel/runtime@^7.7.2", "@babel/runtime@^7.8.3": version "7.8.4" resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.8.4.tgz#d79f5a2040f7caa24d53e563aad49cbc05581308" integrity sha512-neAp3zt80trRVBI1x0azq6c57aNBqYZH8KhMm3TaB7wEI5Q4A2SHfBHE8w9gOhI/lrqxtEbXZgQIrHP+wvSGwQ== @@ -2326,6 +2326,11 @@ dependencies: any-observable "^0.3.0" +"@sheerun/mutationobserver-shim@^0.3.2": + version "0.3.2" + resolved "https://registry.yarnpkg.com/@sheerun/mutationobserver-shim/-/mutationobserver-shim-0.3.2.tgz#8013f2af54a2b7d735f71560ff360d3a8176a87b" + integrity sha512-vTCdPp/T/Q3oSqwHmZ5Kpa9oI7iLtGl3RQaA/NyLHikvcrPxACkkKVr/XzkSPJWXHRhKGzVvb0urJsbMlRxi1Q== + "@sindresorhus/is@^0.14.0": version "0.14.0" resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.14.0.tgz#9fb3a3cf3132328151f353de4632e01e52102bea" @@ -2345,6 +2350,43 @@ dependencies: defer-to-connect "^1.0.1" +"@testing-library/dom@^6.11.0": + version "6.12.2" + resolved "https://registry.yarnpkg.com/@testing-library/dom/-/dom-6.12.2.tgz#5d549acf43f2e0c23b2abfd4e36d65594c3b2741" + integrity sha512-KCnvHra5fV+wDxg3wJObGvZFxq7v1DJt829GNFLuRDjKxVNc/B5AdsylNF5PMHFbWMXDsHwM26d2NZcZO9KjbQ== + dependencies: + "@babel/runtime" "^7.6.2" + "@sheerun/mutationobserver-shim" "^0.3.2" + "@types/testing-library__dom" "^6.0.0" + aria-query "3.0.0" + pretty-format "^24.9.0" + wait-for-expect "^3.0.0" + +"@testing-library/jest-dom@5.1.1": + version "5.1.1" + resolved "https://registry.yarnpkg.com/@testing-library/jest-dom/-/jest-dom-5.1.1.tgz#e88a5c08f9b9f36b384f948a0532eae2abbc8204" + integrity sha512-7xnmBFcUmmUVAUhFiZ/u3CxFh1e46THAwra4SiiKNCW4By26RedCRwEk0rtleFPZG0wlTSNOKDvJjWYy93dp0w== + dependencies: + "@babel/runtime" "^7.8.3" + "@types/testing-library__jest-dom" "^5.0.0" + chalk "^3.0.0" + css "^2.2.4" + css.escape "^1.5.1" + jest-diff "^25.1.0" + jest-matcher-utils "^25.1.0" + lodash "^4.17.15" + pretty-format "^25.1.0" + redent "^3.0.0" + +"@testing-library/react@9.4.1": + version "9.4.1" + resolved "https://registry.yarnpkg.com/@testing-library/react/-/react-9.4.1.tgz#955771568aa3216107d307bfdf470bf2394308a0" + integrity sha512-sta3ui24HPgW92quHyQj6gpOkNgLNx8BX/QOU4k1bddo43ZdqlGwmzCYwL93bExfhergwiau+IzBGl7TCsSFeA== + dependencies: + "@babel/runtime" "^7.8.3" + "@testing-library/dom" "^6.11.0" + "@types/testing-library__react" "^9.1.2" + "@types/babel__core@^7.1.0": version "7.1.5" resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.5.tgz#e4d84704b4df868b3ad538365a13da2fa6dbc023" @@ -2432,7 +2474,7 @@ "@types/istanbul-lib-coverage" "*" "@types/istanbul-lib-report" "*" -"@types/jest@^25.1.3": +"@types/jest@*", "@types/jest@^25.1.3": version "25.1.3" resolved "https://registry.yarnpkg.com/@types/jest/-/jest-25.1.3.tgz#9b0b5addebccfb631175870be8ba62182f1bc35a" integrity sha512-jqargqzyJWgWAJCXX96LBGR/Ei7wQcZBvRv0PLEu9ZByMfcs23keUJrKv9FMR6YZf9YCbfqDqgmY+JUBsnqhrg== @@ -2470,7 +2512,14 @@ resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.3.tgz#2ab0d5da2e5815f94b0b9d4b95d1e5f243ab2ca7" integrity sha512-KfRL3PuHmqQLOG+2tGpRO26Ctg+Cq1E01D2DMriKEATHgWLfeNDmq9e29Q9WIky0dQ3NPkd1mzYH8Lm936Z9qw== -"@types/react@16.9.23": +"@types/react-dom@*": + version "16.9.5" + resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-16.9.5.tgz#5de610b04a35d07ffd8f44edad93a71032d9aaa7" + integrity sha512-BX6RQ8s9D+2/gDhxrj8OW+YD4R+8hj7FEM/OJHGNR0KipE1h1mSsf39YeyC81qafkq+N3rU3h3RFbLSwE5VqUg== + dependencies: + "@types/react" "*" + +"@types/react@*", "@types/react@16.9.23": version "16.9.23" resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.23.tgz#1a66c6d468ba11a8943ad958a8cb3e737568271c" integrity sha512-SsGVT4E7L2wLN3tPYLiF20hmZTPGuzaayVunfgXzUn1x4uHVsKH6QDJQ/TdpHqwsTLd4CwrmQ2vOgxN7gE24gw== @@ -2506,6 +2555,28 @@ resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e" integrity sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw== +"@types/testing-library__dom@*", "@types/testing-library__dom@^6.0.0": + version "6.12.1" + resolved "https://registry.yarnpkg.com/@types/testing-library__dom/-/testing-library__dom-6.12.1.tgz#37af28fae051f9e3feed5684535b1540c97ae28b" + integrity sha512-cgqnEjxKk31tQt29j4baSWaZPNjQf3bHalj2gcHQTpW5SuHRal76gOpF0vypeEo6o+sS5inOvvNdzLY0B3FB2A== + dependencies: + pretty-format "^24.3.0" + +"@types/testing-library__jest-dom@^5.0.0": + version "5.0.1" + resolved "https://registry.yarnpkg.com/@types/testing-library__jest-dom/-/testing-library__jest-dom-5.0.1.tgz#cc7f384535a3d9597e27f58d38a795f5c137cc53" + integrity sha512-GiPXQBVF9O4DG9cssD2d266vozBJvC5Tnv6aeH5ujgYJgys1DYm9AFCz7YC+STR5ksGxq3zCt+yP8T1wbk2DFg== + dependencies: + "@types/jest" "*" + +"@types/testing-library__react@^9.1.2": + version "9.1.2" + resolved "https://registry.yarnpkg.com/@types/testing-library__react/-/testing-library__react-9.1.2.tgz#e33af9124c60a010fc03a34eff8f8a34a75c4351" + integrity sha512-CYaMqrswQ+cJACy268jsLAw355DZtPZGt3Jwmmotlcu8O/tkoXBI6AeZ84oZBJsIsesozPKzWzmv/0TIU+1E9Q== + dependencies: + "@types/react-dom" "*" + "@types/testing-library__dom" "*" + "@types/unist@^2.0.0", "@types/unist@^2.0.2": version "2.0.3" resolved "https://registry.yarnpkg.com/@types/unist/-/unist-2.0.3.tgz#9c088679876f374eb5983f150d4787aa6fb32d7e" @@ -3015,7 +3086,7 @@ argparse@^1.0.7: dependencies: sprintf-js "~1.0.2" -aria-query@^3.0.0: +aria-query@3.0.0, aria-query@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/aria-query/-/aria-query-3.0.0.tgz#65b3fcc1ca1155a8c9ae64d6eee297f15d5133cc" integrity sha1-ZbP8wcoRVajJrmTW7uKX8V1RM8w= @@ -4718,7 +4789,12 @@ css-unit-converter@^1.1.1: resolved "https://registry.yarnpkg.com/css-unit-converter/-/css-unit-converter-1.1.1.tgz#d9b9281adcfd8ced935bdbaba83786897f64e996" integrity sha1-2bkoGtz9jO2TW9urqDeGiX9k6ZY= -css@2.2.4, css@^2.0.0: +css.escape@^1.5.1: + version "1.5.1" + resolved "https://registry.yarnpkg.com/css.escape/-/css.escape-1.5.1.tgz#42e27d4fa04ae32f931a4b4d4191fa9cddee97cb" + integrity sha1-QuJ9T6BK4y+TGktNQZH6nN3ul8s= + +css@2.2.4, css@^2.0.0, css@^2.2.4: version "2.2.4" resolved "https://registry.yarnpkg.com/css/-/css-2.2.4.tgz#c646755c73971f2bba6a601e2cf2fd71b1298929" integrity sha512-oUnjmWpy0niI3x/mPL8dVEI1l7MnG3+HHyRPHf+YFSbK+svOhXpmSOcDURUh2aOCgl2grzrOPt1nHLuCVFULLw== @@ -6927,6 +7003,11 @@ indent-string@^3.0.0: resolved "https://registry.yarnpkg.com/indent-string/-/indent-string-3.2.0.tgz#4a5fd6d27cc332f37e5419a504dbb837105c9289" integrity sha1-Sl/W0nzDMvN+VBmlBNu4NxBckok= +indent-string@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/indent-string/-/indent-string-4.0.0.tgz#624f8f4497d619b2d9768531d58f4122854d7251" + integrity sha512-EdDDZu4A2OyIK7Lr/2zG+w5jmbuk1DVBnEwREQvBzspBJkCEbRa8GxU1lghYcaGJCnRWibjDXlq779X1/y5xwg== + indexes-of@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/indexes-of/-/indexes-of-1.0.1.tgz#f30f716c8e2bd346c7b67d3df3915566a7c05607" @@ -9240,6 +9321,11 @@ mimic-response@^1.0.0, mimic-response@^1.0.1: resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-1.0.1.tgz#4923538878eef42063cb8a3e3b0798781487ab1b" integrity sha512-j5EctnkH7amfV/q5Hgmoal1g2QHFJRraOtmx0JpIqkxhBhI/lJSl1nMpQ45hVarwNETOoWEimndZ4QK0RHxuxQ== +min-indent@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.0.tgz#cfc45c37e9ec0d8f0a0ec3dd4ef7f7c3abe39256" + integrity sha1-z8RcN+nsDY8KDsPdTvf3w6vjklY= + mini-css-extract-plugin@0.8.0: version "0.8.0" resolved "https://registry.yarnpkg.com/mini-css-extract-plugin/-/mini-css-extract-plugin-0.8.0.tgz#81d41ec4fe58c713a96ad7c723cdb2d0bd4d70e1" @@ -11133,7 +11219,7 @@ prettier@^1.19.1: resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.19.1.tgz#f7d7f5ff8a9cd872a7be4ca142095956a60797cb" integrity sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew== -pretty-format@^24.9.0: +pretty-format@^24.3.0, pretty-format@^24.9.0: version "24.9.0" resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-24.9.0.tgz#12fac31b37019a4eea3c11aa9a959eb7628aa7c9" integrity sha512-00ZMZUiHaJrNfk33guavqgvfJS30sLYf0f8+Srklv0AMPodGGHcoHgksZ3OThYnIvOd+8yMCn0YiEOogjlgsnA== @@ -11709,6 +11795,14 @@ redent@^2.0.0: indent-string "^3.0.0" strip-indent "^2.0.0" +redent@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/redent/-/redent-3.0.0.tgz#e557b7998316bb53c9f1f56fa626352c6963059f" + integrity sha512-6tDA8g98We0zd0GvVeMT9arEOnTw9qM03L9cJXaCjrip1OO764RDBLBfrB4cwzNGDj5OA5ioymC9GkizgWJDUg== + dependencies: + indent-string "^4.0.0" + strip-indent "^3.0.0" + reduce-css-calc@^2.1.6: version "2.1.7" resolved "https://registry.yarnpkg.com/reduce-css-calc/-/reduce-css-calc-2.1.7.tgz#1ace2e02c286d78abcd01fd92bfe8097ab0602c2" @@ -13012,6 +13106,13 @@ strip-indent@^2.0.0: resolved "https://registry.yarnpkg.com/strip-indent/-/strip-indent-2.0.0.tgz#5ef8db295d01e6ed6cbf7aab96998d7822527b68" integrity sha1-XvjbKV0B5u1sv3qrlpmNeCJSe2g= +strip-indent@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/strip-indent/-/strip-indent-3.0.0.tgz#c32e1cee940b6b3432c771bc2c54bcce73cd3001" + integrity sha512-laJTa3Jb+VQpaC6DseHhF7dXVqHTfJPCRDaEbid/drOhgitgYku/letMUqOXFoWV0zIIUbjpdH2t+tYj4bQMRQ== + dependencies: + min-indent "^1.0.0" + strip-json-comments@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.0.1.tgz#85713975a91fb87bf1b305cca77395e40d2a64a7" @@ -14143,6 +14244,11 @@ w3c-xmlserializer@^1.1.2: webidl-conversions "^4.0.2" xml-name-validator "^3.0.0" +wait-for-expect@^3.0.0: + version "3.0.2" + resolved "https://registry.yarnpkg.com/wait-for-expect/-/wait-for-expect-3.0.2.tgz#d2f14b2f7b778c9b82144109c8fa89ceaadaa463" + integrity sha512-cfS1+DZxuav1aBYbaO/kE06EOS8yRw7qOFoD3XtjTkYvCvh3zUvNST8DXK/nPaeqIzIv3P3kL3lRJn8iwOiSag== + walker@^1.0.7, walker@~1.0.5: version "1.0.7" resolved "https://registry.yarnpkg.com/walker/-/walker-1.0.7.tgz#2f7f9b8fd10d677262b18a884e28d19618e028fb" From 8592c580fd09de938343484fb24c41bb75c43979 Mon Sep 17 00:00:00 2001 From: Yanick Date: Sun, 1 Mar 2020 20:41:53 -0500 Subject: [PATCH 2/3] A little formatting + form test modifications --- packages/core/src/components/Form.tsx | 7 +++-- packages/core/src/controller.ts | 7 +++-- packages/core/test/components/Form.test.tsx | 30 +++++++++++++-------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/packages/core/src/components/Form.tsx b/packages/core/src/components/Form.tsx index 00e0e5b6e0..5c9afcf6b7 100644 --- a/packages/core/src/components/Form.tsx +++ b/packages/core/src/components/Form.tsx @@ -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) @@ -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, { diff --git a/packages/core/src/controller.ts b/packages/core/src/controller.ts index ba03a6cc0a..f19385bb54 100644 --- a/packages/core/src/controller.ts +++ b/packages/core/src/controller.ts @@ -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' @@ -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()}`, ) diff --git a/packages/core/test/components/Form.test.tsx b/packages/core/test/components/Form.test.tsx index 2ccca28c79..6b14818446 100644 --- a/packages/core/test/components/Form.test.tsx +++ b/packages/core/test/components/Form.test.tsx @@ -1,11 +1,25 @@ -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 = () => (
@@ -13,26 +27,20 @@ describe('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() expect(page.getByLabelText('Title')).toHaveValue('test') }) describe('with redirect', () => { - beforeEach(() => { + beforeAll(() => { const headers = {Location: 'location', 'x-as': 'xas'} as any mockFetch({ ok: true, From 3270c72c704f058c9f40c923970c0b80265b45ca Mon Sep 17 00:00:00 2001 From: Brandon Bayer Date: Mon, 2 Mar 2020 10:15:17 +0700 Subject: [PATCH 3/3] add back console log --- packages/core/src/controller.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/src/controller.ts b/packages/core/src/controller.ts index f19385bb54..6de2104849 100644 --- a/packages/core/src/controller.ts +++ b/packages/core/src/controller.ts @@ -38,6 +38,7 @@ 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()}`, )