Skip to content

Commit

Permalink
feat: add react test (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTK committed Nov 6, 2023
1 parent a86d851 commit f0f4f0e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ jobs:
run: yarn tsc --noEmit
working-directory: packages/core
- name: Run vitest
run: yarn vitest
run: yarn vitest --dom
working-directory: packages/core
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"dev": "vite example",
"build": "tsc && vite build",
"preview": "vite preview",
"protoc": "pbjs ./example/example.proto --out ./example/proto.json"
"protoc": "pbjs ./example/example.proto --out ./example/proto.json",
"test": "vitest --dom"
},
"devDependencies": {
"@changesets/cli": "^2.25.2",
Expand All @@ -39,7 +40,6 @@
"jsdom": "^19.0.0",
"lodash.get": "^4.4.2",
"postcss": "^8.4.14",
"react-test-renderer": "^17.0.2",
"rollup-plugin-visualizer": "^5.8.3",
"rome": "^12.1.0",
"tailwindcss": "^3.3.5",
Expand Down
41 changes: 41 additions & 0 deletions packages/core/src/AutoForm.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, it, vi, expect } from 'vitest';
import { fireEvent, render, prettyDOM } from '@testing-library/react';
import { createAutoForm } from './AutoForm';
import { useForm } from 'react-hook-form';
import protobuf from 'protobufjs';
import React from 'react';

const namespace = protobuf.parse(`
syntax = "proto3";
message Child {
map<int32, string> items = 1;
}
message Parent {
repeated Child children = 1;
}
`).root;

const MockApp: React.FC<{ onSubmit: (values: unknown) => void }> = (props) => {
const form = useForm();
const { AutoForm } = createAutoForm({
form,
messageType: 'Parent',
namespace,
})

return <AutoForm onSubmitValid={props.onSubmit}><button id="submit">submit</button></AutoForm>
}

describe('AutoForm', () => {
it('Form submission', async () => {
const handleSubmit = vi.fn();
const dom = render(<MockApp onSubmit={handleSubmit}><button id="submit" /></MockApp>);
const submitButton = dom.container.querySelector('#submit');
expect(submitButton).toBeDefined();
fireEvent.click(submitButton!);
await vi.waitFor(() => handleSubmit.mock.calls.length > 0);
expect(handleSubmit.mock.calls.length).toBe(1);
});
})
43 changes: 8 additions & 35 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11516,7 +11516,6 @@ __metadata:
react: ^18.2.0
react-dom: ^18.2.0
react-hook-form: ^7.39.5
react-test-renderer: ^17.0.2
rollup-plugin-visualizer: ^5.8.3
rome: ^12.1.0
tailwindcss: ^3.3.5
Expand Down Expand Up @@ -11824,27 +11823,27 @@ __metadata:
languageName: node
linkType: hard

"react-is@npm:^16.12.0 || ^17.0.0 || ^18.0.0, react-is@npm:^18.0.0":
version: 18.2.0
resolution: "react-is@npm:18.2.0"
checksum: e72d0ba81b5922759e4aff17e0252bd29988f9642ed817f56b25a3e217e13eea8a7f2322af99a06edb779da12d5d636e9fda473d620df9a3da0df2a74141d53e
languageName: node
linkType: hard

"react-is@npm:^16.13.1, react-is@npm:^16.6.0, react-is@npm:^16.7.0":
version: 16.13.1
resolution: "react-is@npm:16.13.1"
checksum: f7a19ac3496de32ca9ae12aa030f00f14a3d45374f1ceca0af707c831b2a6098ef0d6bdae51bd437b0a306d7f01d4677fcc8de7c0d331eb47ad0f46130e53c5f
languageName: node
linkType: hard

"react-is@npm:^17.0.1, react-is@npm:^17.0.2":
"react-is@npm:^17.0.1":
version: 17.0.2
resolution: "react-is@npm:17.0.2"
checksum: 9d6d111d8990dc98bc5402c1266a808b0459b5d54830bbea24c12d908b536df7883f268a7868cfaedde3dd9d4e0d574db456f84d2e6df9c4526f99bb4b5344d8
languageName: node
linkType: hard

"react-is@npm:^18.0.0":
version: 18.2.0
resolution: "react-is@npm:18.2.0"
checksum: e72d0ba81b5922759e4aff17e0252bd29988f9642ed817f56b25a3e217e13eea8a7f2322af99a06edb779da12d5d636e9fda473d620df9a3da0df2a74141d53e
languageName: node
linkType: hard

"react-json-view@npm:^1.21.3":
version: 1.21.3
resolution: "react-json-view@npm:1.21.3"
Expand Down Expand Up @@ -11934,32 +11933,6 @@ __metadata:
languageName: node
linkType: hard

"react-shallow-renderer@npm:^16.13.1":
version: 16.15.0
resolution: "react-shallow-renderer@npm:16.15.0"
dependencies:
object-assign: ^4.1.1
react-is: ^16.12.0 || ^17.0.0 || ^18.0.0
peerDependencies:
react: ^16.0.0 || ^17.0.0 || ^18.0.0
checksum: 6052c7e3e9627485120ebd8257f128aad8f56386fe8d42374b7743eac1be457c33506d153c7886b4e32923c0c352d402ab805ef9ca02dbcd8393b2bdeb6e5af8
languageName: node
linkType: hard

"react-test-renderer@npm:^17.0.2":
version: 17.0.2
resolution: "react-test-renderer@npm:17.0.2"
dependencies:
object-assign: ^4.1.1
react-is: ^17.0.2
react-shallow-renderer: ^16.13.1
scheduler: ^0.20.2
peerDependencies:
react: 17.0.2
checksum: e6b5c6ed2a0bde2c34f1ab9523ff9bc4c141a271daf730d6b852374e83acc0155d58ab71a318251e953ebfa65b8bebb9c5dce3eba1ccfcbef7cc4e1e8261c401
languageName: node
linkType: hard

"react-textarea-autosize@npm:^8.3.2":
version: 8.4.0
resolution: "react-textarea-autosize@npm:8.4.0"
Expand Down

0 comments on commit f0f4f0e

Please sign in to comment.