-
Notifications
You must be signed in to change notification settings - Fork 227
improve dynamicLists typing #1848
improve dynamicLists typing #1848
Conversation
makeCleanAfterSubmit, | ||
}: FormWithDynamicListsInput<T, D>): FormWithDynamicLists<T, D>; | ||
|
||
export function useForm<T extends FieldBag, D extends DynamicListBag>({ |
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.
I am wondering, would you have to pass in a type for the second part of the generic for every useForm call that already passes in a generic type for the form?
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.
nope, as you can see in the codesandbox (App.tsx), I made two form and I don't need to pass the generic types.
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.
I think this is good! Nice 💯 . Thanks for digging into it 🙂 . I am a bit concerned about modifying the useForm
this way but it's looking good in the sandbox!
I also noticed all the tests in form.test.tsx
are breaking. The full CI does not seem to be running in this PR for some reason but you can try it locally. I can fix the tests in my PR so dont worry about it.
Aaaaah I thought it was all green LOL nice catch! I will run tests locally and see what's going on. |
Its all good! We just have to change the useForm call to something like this
and maybe remove the enable dynamic list prop in productForm and then remove the default test |
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.
🤯🤯🤯
@@ -73,6 +79,9 @@ describe('useDynamicList', () => { | |||
}); | |||
|
|||
it('will throw an error if new position is out of index range', () => { | |||
const consoleErrorMock = jest.spyOn(console, 'error'); | |||
consoleErrorMock.mockImplementation(); |
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.
@@ -203,6 +212,66 @@ describe('useDynamicList', () => { | |||
expect(wrapper).toContainReactComponent(TextField); | |||
}); | |||
}); | |||
|
|||
describe('dirty dynamic list', () => { |
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.
So far there was no dirty related test
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.
hmm 🤔 @sylvhama . There are dirty tests in the baselist since that is where it is originally implemented
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.
So I dont think we will need to have it in the dynamiclist test since dynamic list just returns what the baselist is returning
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.
same case for reset
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.
But useList
test don't cover cases where we add or remove an item. Plus it does not hurt to make sure nothing break the default behavior.
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.
Oh I dont meanuseList
, I meant useBaseList
. Because I noticed in this PR some tests like value changes are being re-created. In that case, could we just write the tests that cover the adding and removing?
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.
but addItem
, moveItem
, removeItem
can only be tested here since they are added by dynamiclist.ts
. Also we want to make sure there is no local logic within dynamiclist.ts
that could break the dirty logic 🤔
|
||
import {submitSuccess, submitFail} from '..'; | ||
|
||
describe('useForm with dynamic list', () => { |
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.
those tests are inspired by origin useForm
test.
makeClean
and makeCleanAfterSubmit
seem broken with dynamic lists 🤔
waitForSubmit, | ||
} from './utilities'; | ||
|
||
import {submitSuccess, submitFail} from '..'; | ||
|
||
describe('useForm', () => { |
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.
All the code removed here has been moved to utilities
.
import {SimpleProduct, Variant} from './types'; | ||
import {TextField} from './TextField'; | ||
|
||
export function FormWithDynamicVariantList({ |
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.
This is a new component to test dynamic lists.
import {SimpleProduct} from './types'; | ||
import {TextField} from './TextField'; | ||
|
||
export function ProductForm({ |
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.
Nothing changed here, I just moved the code.
* improve dynamicLists typing * revert to initial tests * mock console.error to clean logs * add dirty tests for dynamic lists * update form tests * remove useless file
test baselist Update baselist.test.tsx lint fix and additional tests update tests Update CHANGELOG.md Update README.md Update CHANGELOG.md add dynamic list to the form update comments Update form.ts add check for input since theres a possibility of adding undefined input dynamicListFields Update baselist.ts Update baselist.ts added tests to form Update form.test.tsx fixed reset recreating on each render Update listdirty.ts Update README.md Update README.md Update README.md added DynamicListBag Update packages/react-form/README.md Co-authored-by: Sylvain Hamann <[email protected]> Update packages/react-form/README.md Co-authored-by: Sylvain Hamann <[email protected]> Update README.md Update baselist.ts docs used lazy ref fix docs more improvement to docs return default bag on undefined dynamic list Update form.test.tsx rebase gone bad fix improve dynamicLists typing (#1848) * improve dynamicLists typing * revert to initial tests * mock console.error to clean logs * add dirty tests for dynamic lists * update form tests * remove useless file Update dynamiclist.test.tsx
test baselist Update baselist.test.tsx lint fix and additional tests update tests Update CHANGELOG.md Update README.md Update CHANGELOG.md add dynamic list to the form update comments Update form.ts add check for input since theres a possibility of adding undefined input dynamicListFields Update baselist.ts Update baselist.ts added tests to form Update form.test.tsx fixed reset recreating on each render Update listdirty.ts Update README.md Update README.md Update README.md added DynamicListBag Update packages/react-form/README.md Co-authored-by: Sylvain Hamann <[email protected]> Update packages/react-form/README.md Co-authored-by: Sylvain Hamann <[email protected]> Update README.md Update baselist.ts docs used lazy ref fix docs more improvement to docs return default bag on undefined dynamic list Update form.test.tsx rebase gone bad fix improve dynamicLists typing (#1848) * improve dynamicLists typing * revert to initial tests * mock console.error to clean logs * add dirty tests for dynamic lists * update form tests * remove useless file Update dynamiclist.test.tsx
You can test with this condesandbox.
I am using function overloading to define two scenarios:
useForm
(like all existing ones) that does not usedynamicLists
;useForm
relyingdynamicLists
.Result is:
App.tsx
: No need to use!
or to check ifdynamicLists
is defined.In the future we could do the same to also turn
fields
optional 🤔