Skip to content

Commit

Permalink
Added initial version of new context API.
Browse files Browse the repository at this point in the history
  • Loading branch information
radekmie committed Oct 9, 2019
1 parent 3ef942d commit 1ec3b53
Show file tree
Hide file tree
Showing 23 changed files with 155 additions and 277 deletions.
2 changes: 1 addition & 1 deletion docs/examples-custom-form.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const Modifier = parent =>
getModel(mode) {
if (mode === 'submit') {
const doc = super.getModel('submit');
const keys = this.getChildContextSchema().getSubfields();
const keys = this.getContextSchema().getSubfields();

const update = keys.filter(key => doc[key] !== undefined);
const remove = keys.filter(key => doc[key] === undefined);
Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-antd/src/AutoField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import TextField from './TextField';
export default class AutoField extends BaseField {
static displayName = 'AutoField';

getChildContextName() {
getContextName() {
return this.context.uniforms.name;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-bootstrap3/src/AutoField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import TextField from './TextField';
export default class AutoField extends BaseField {
static displayName = 'AutoField';

getChildContextName() {
getContextName() {
return this.context.uniforms.name;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/uniforms-bootstrap3/src/BaseForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ const Bootstrap3 = (parent: any) =>
])
};

getChildContextState() {
getContextState() {
return {
...super.getChildContextState(),
...super.getContextState(),
grid: this.props.grid
};
}

getNativeFormProps() {
const error = this.getChildContextError();
const error = this.getContextError();
const {
className,
grid,
Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-bootstrap4/src/AutoField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import TextField from './TextField';
export default class AutoField extends BaseField {
static displayName = 'AutoField';

getChildContextName() {
getContextName() {
return this.context.uniforms.name;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/uniforms-bootstrap4/src/BaseForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ const Bootstrap4 = parent =>
])
};

getChildContextState() {
getContextState() {
return {
...super.getChildContextState(),
...super.getContextState(),
grid: this.props.grid
};
}

getNativeFormProps() {
const error = this.getChildContextError();
const error = this.getContextError();
const {
className,
grid, // eslint-disable-line no-unused-vars
Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-material/src/AutoField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import TextField from './TextField';
export default class AutoField extends BaseField {
static displayName = 'AutoField';

getChildContextName() {
getContextName() {
return this.context.uniforms.name;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-semantic/src/AutoField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import TextField from './TextField';
export default class AutoField extends BaseField {
static displayName = 'AutoField';

getChildContextName() {
getContextName() {
return this.context.uniforms.name;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-semantic/src/BaseForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Semantic = parent =>

getNativeFormProps() {
const props = super.getNativeFormProps();
const error = this.getChildContextError();
const error = this.getContextError();

return {
...props,
Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-unstyled/src/AutoField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import TextField from './TextField';
export default class AutoField extends BaseField {
static displayName = 'AutoField';

getChildContextName() {
getContextName() {
return this.context.uniforms.name;
}

Expand Down
14 changes: 7 additions & 7 deletions packages/uniforms/__tests__/AutoForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('AutoForm', () => {
it('updates', () => {
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', '2');

expect(onChange).toHaveBeenCalledTimes(1);
Expand All @@ -49,7 +49,7 @@ describe('AutoForm', () => {
it('calls `onChangeModel`', () => {
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', '2');

expect(onChangeModel).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -84,7 +84,7 @@ describe('AutoForm', () => {
expect(onSubmit).not.toBeCalled();
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 1);

await new Promise(resolve => process.nextTick(resolve));
Expand All @@ -107,22 +107,22 @@ describe('AutoForm', () => {

it('reset `model`', () => {
wrapper.instance().reset();
expect(wrapper.instance().getChildContext().uniforms.model).toEqual(
expect(wrapper.instance().getContext().uniforms.model).toEqual(
intialModel
);
});

it('resets state `changedMap`', () => {
wrapper.instance().reset();
expect(
wrapper.instance().getChildContext().uniforms.state.changedMap
wrapper.instance().getContext().uniforms.state.changedMap
).toEqual({});
});

it('resets state `changed`', () => {
wrapper.instance().reset();
expect(
wrapper.instance().getChildContext().uniforms.state.changed
wrapper.instance().getContext().uniforms.state.changed
).toEqual(false);
});
});
Expand All @@ -133,7 +133,7 @@ describe('AutoForm', () => {
it('updates when changed', () => {
wrapper.setProps({ model: {} });

expect(wrapper.instance().getChildContext().uniforms.model).toEqual({});
expect(wrapper.instance().getContext().uniforms.model).toEqual({});
});

it('validates', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms/__tests__/BaseField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('BaseField', () => {
describe('child context', () => {
const wrapper = mount(<TestField name="a" />, reactContext1);

const context = wrapper.instance().getChildContext();
const context = wrapper.instance().getContext();

it('exists', () => {
expect(context).toHaveProperty('uniforms', expect.any(Object));
Expand Down
50 changes: 25 additions & 25 deletions packages/uniforms/__tests__/BaseForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('BaseForm', () => {
<BaseForm error={error} model={model} schema={schema} />
);

const context = wrapper.instance().getChildContext();
const context = wrapper.instance().getContext();

it('exists', () => {
expect(context).toHaveProperty('uniforms', expect.any(Object));
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('BaseForm', () => {
});

it('have correct `state`', () => {
const context = wrapper.instance().getChildContext();
const context = wrapper.instance().getContext();

expect(context.uniforms).toHaveProperty('state', expect.any(Object));
expect(context.uniforms.state).toHaveProperty('label', false);
Expand All @@ -126,7 +126,7 @@ describe('BaseForm', () => {

wrapper.setProps({ schema: schema2 });

const context = wrapper.instance().getChildContext();
const context = wrapper.instance().getContext();

expect(context.uniforms).toHaveProperty('schema', schema2);
});
Expand All @@ -143,16 +143,16 @@ describe('BaseForm', () => {
);

it('updates `changed` and `changedMap`', () => {
const context1 = wrapper.instance().getChildContext().uniforms.state;
const context1 = wrapper.instance().getContext().uniforms.state;
expect(context1).toHaveProperty('changed', false);
expect(context1).toHaveProperty('changedMap', {});

wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('$', [1, 2]);

const context2 = wrapper.instance().getChildContext().uniforms.state;
const context2 = wrapper.instance().getContext().uniforms.state;
expect(context2).toHaveProperty('changed', true);
expect(context2).toHaveProperty('changedMap.$');
expect(context2.changedMap.$).toBeTruthy();
Expand All @@ -164,7 +164,7 @@ describe('BaseForm', () => {
wrapper.setProps({ autosave: true });
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 1);

expect(onSubmit).toHaveBeenCalledTimes(1);
Expand All @@ -174,15 +174,15 @@ describe('BaseForm', () => {
it('autosaves are not delayed', () => {
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 1);
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 2);
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 3);

expect(onSubmit).toHaveBeenCalledTimes(3);
Expand All @@ -193,15 +193,15 @@ describe('BaseForm', () => {
wrapper.setProps({ autosaveDelay: 10 });
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 1);
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 2);
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 3);

await new Promise(resolve => setTimeout(resolve, 25));
Expand All @@ -214,30 +214,30 @@ describe('BaseForm', () => {
wrapper.setProps({ autosaveDelay: 10 });
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 1);
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 2);
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 3);

await new Promise(resolve => setTimeout(resolve, 25));

wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 1);
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 2);
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 3);

await new Promise(resolve => setTimeout(resolve, 25));
Expand All @@ -250,7 +250,7 @@ describe('BaseForm', () => {
wrapper.setProps({ autosave: false });
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 1);

expect(onSubmit).not.toBeCalled();
Expand All @@ -259,7 +259,7 @@ describe('BaseForm', () => {
it('calls `onChange` with correct name and value', () => {
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 1);

expect(onChange).toHaveBeenCalledTimes(1);
Expand All @@ -276,7 +276,7 @@ describe('BaseForm', () => {
wrapper.setProps({ onChange: undefined });
wrapper
.instance()
.getChildContext()
.getContext()
.uniforms.onChange('a', 1);

expect(onChange).not.toBeCalled();
Expand Down Expand Up @@ -348,19 +348,19 @@ describe('BaseForm', () => {
onSubmit: () => new Promise(resolve => (resolveSubmit = resolve))
});

const context1 = wrapper.instance().getChildContext().uniforms.state;
const context1 = wrapper.instance().getContext().uniforms.state;
expect(context1).toHaveProperty('submitting', false);

wrapper.find('form').simulate('submit');
await new Promise(resolve => process.nextTick(resolve));

const context2 = wrapper.instance().getChildContext().uniforms.state;
const context2 = wrapper.instance().getContext().uniforms.state;
expect(context2).toHaveProperty('submitting', true);

resolveSubmit();
await new Promise(resolve => process.nextTick(resolve));

const context3 = wrapper.instance().getChildContext().uniforms.state;
const context3 = wrapper.instance().getContext().uniforms.state;
expect(context3).toHaveProperty('submitting', false);
});

Expand Down
Loading

0 comments on commit 1ec3b53

Please sign in to comment.