From 6c7d78134e3a875af4bea6dd8cdb775e48754dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Miernik?= Date: Fri, 10 Jul 2020 21:32:07 +0200 Subject: [PATCH] Fixed changed state of AutoForm. --- packages/uniforms/__tests__/AutoForm.tsx | 81 ++++++++++++++++-------- packages/uniforms/src/AutoForm.tsx | 2 +- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/packages/uniforms/__tests__/AutoForm.tsx b/packages/uniforms/__tests__/AutoForm.tsx index 91eb63417..999361008 100644 --- a/packages/uniforms/__tests__/AutoForm.tsx +++ b/packages/uniforms/__tests__/AutoForm.tsx @@ -28,16 +28,12 @@ describe('AutoForm', () => { }); describe('when changed', () => { - // FIXME: AutoForm is not a valid Component. - const wrapper = mount( - , - ); - it('updates', () => { + // FIXME: AutoForm is not a valid Component. + const wrapper = mount( + , + ); + wrapper.instance().getContext().onChange('a', '2'); expect(onChange).toHaveBeenCalledTimes(1); @@ -45,11 +41,32 @@ describe('AutoForm', () => { }); it('calls `onChangeModel`', () => { + // FIXME: AutoForm is not a valid Component. + const wrapper = mount( + , + ); + wrapper.instance().getContext().onChange('a', '2'); expect(onChangeModel).toHaveBeenCalledTimes(1); expect(onChangeModel).toHaveBeenLastCalledWith({ a: '2' }); }); + + it('updates `changed` and `changedMap`', () => { + // FIXME: AutoForm is not a valid Component. + const wrapper = mount(); + + const context1 = wrapper.instance().getContext(); + expect(context1).toHaveProperty('changed', false); + expect(context1).toHaveProperty('changedMap', {}); + + wrapper.instance().getContext().onChange('a', '2'); + + const context2 = wrapper.instance().getContext(); + expect(context2).toHaveProperty('changed', true); + expect(context2).toHaveProperty('changedMap.a'); + expect(context2.changedMap.a).toBeTruthy(); + }); }); describe('when rendered', () => { @@ -57,12 +74,13 @@ describe('AutoForm', () => { const field = () => null; const Field = connectField(field); - mount( + // FIXME: AutoForm is not a valid Component. + mount( , ); @@ -74,7 +92,7 @@ describe('AutoForm', () => { it('skips `onSubmit` until rendered (`autosave` = true)', async () => { // FIXME: AutoForm is not a valid Component. const wrapper = mount( - , + , ); expect(onSubmit).not.toBeCalled(); @@ -88,43 +106,50 @@ describe('AutoForm', () => { }); describe('when reset', () => { - const intialModel = { a: 'foo' }; - // FIXME: AutoForm is not a valid Component. - const wrapper = mount( - , - ); - it('reset `model`', () => { + // FIXME: AutoForm is not a valid Component. + const wrapper = mount( + , + ); + wrapper.instance().reset(); - expect(wrapper.instance().getContext().model).toEqual(intialModel); + expect(wrapper.instance().getContext().model).toEqual(model); }); it('resets state `changedMap`', () => { + // FIXME: AutoForm is not a valid Component. + const wrapper = mount( + , + ); + wrapper.instance().reset(); expect(wrapper.instance().getContext().changedMap).toEqual({}); }); it('resets state `changed`', () => { + // FIXME: AutoForm is not a valid Component. + const wrapper = mount( + , + ); + wrapper.instance().reset(); expect(wrapper.instance().getContext().changed).toEqual(false); }); }); describe('when updated', () => { - // FIXME: AutoForm is not a valid Component. - const wrapper = mount(); - it('updates', () => { + // FIXME: AutoForm is not a valid Component. + const wrapper = mount(); + wrapper.setProps({ model: {} }); expect(wrapper.instance().props.model).toEqual({}); }); it('validates', () => { + // FIXME: AutoForm is not a valid Component. + const wrapper = mount(); + wrapper.setProps({ model, validate: 'onChange' }); expect(validator).toHaveBeenCalledTimes(1); }); diff --git a/packages/uniforms/src/AutoForm.tsx b/packages/uniforms/src/AutoForm.tsx index f88237b57..b15f4fad7 100644 --- a/packages/uniforms/src/AutoForm.tsx +++ b/packages/uniforms/src/AutoForm.tsx @@ -58,10 +58,10 @@ export function Auto(Base: Base) { } onChange(key: string, value: any) { + super.onChange(key, value); this.setState( state => ({ model: setWith(clone(state.model), key, value, clone) }), () => { - super.onChange(key, value); if (this.props.onChangeModel) this.props.onChangeModel(this.state.model); },