-
Notifications
You must be signed in to change notification settings - Fork 424
fix(vue): keep renderer bindings stable when they do not change #2634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KiaraGrouwstra
wants to merge
3
commits into
eclipsesource:master
Choose a base branch
from
KiaraGrouwstra:vue-stable-computed
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
c240ec2
fix(vue): keep renderer bindings stable when they do not change
KiaraGrouwstra f5166d9
fix(vue): compare only plain objects and arrays structurally
KiaraGrouwstra 2ecf3f4
fix(vue): do not re-render the dispatched renderer for forwarded slots
KiaraGrouwstra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| /* eslint-disable vue/one-component-per-file */ | ||
| import { | ||
| ControlElement, | ||
| isControl, | ||
| isLayout, | ||
| JsonFormsRendererRegistryEntry, | ||
| Layout, | ||
| rankWith, | ||
| } from '@jsonforms/core'; | ||
| import { mount } from '@vue/test-utils'; | ||
| import cloneDeep from 'lodash/cloneDeep'; | ||
| import { defineComponent, h, markRaw, nextTick } from 'vue'; | ||
| import { | ||
| DispatchRenderer, | ||
| JsonForms, | ||
| rendererProps, | ||
| useJsonFormsControl, | ||
| useJsonFormsLayout, | ||
| } from '../../src'; | ||
|
|
||
| const renders: Record<string, number> = {}; | ||
| let changeHandlers: Record<string, (path: string, value: unknown) => void> = {}; | ||
|
|
||
| const TestControl = defineComponent({ | ||
| props: rendererProps<ControlElement>(), | ||
| setup(props) { | ||
| const { control, handleChange } = useJsonFormsControl(props); | ||
| changeHandlers[control.value.path] = handleChange; | ||
| return () => { | ||
| const path = control.value.path; | ||
| renders[path] = (renders[path] ?? 0) + 1; | ||
| const min = control.value.uischema.options?.min; | ||
| return h( | ||
| 'span', | ||
| min instanceof Date ? min.toISOString() : String(control.value.data) | ||
| ); | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| // A layout that dispatches its elements. With `clone`, it gives a new clone of | ||
| // each element to the dispatch on each render, as wrapper renderers often do. | ||
| const createTestLayout = (clone: boolean) => | ||
| defineComponent({ | ||
| props: rendererProps<Layout>(), | ||
| setup(props) { | ||
| const { layout } = useJsonFormsLayout(props); | ||
| return () => | ||
| h( | ||
| 'div', | ||
| (layout.value.uischema as Layout).elements.map((element) => | ||
| h(DispatchRenderer, { | ||
| schema: layout.value.schema, | ||
| uischema: clone ? cloneDeep(element) : element, | ||
| path: layout.value.path, | ||
| enabled: layout.value.enabled, | ||
| }) | ||
| ) | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| const createRenderers = (clone: boolean): JsonFormsRendererRegistryEntry[] => [ | ||
| { tester: rankWith(1, isControl), renderer: markRaw(TestControl) }, | ||
| { | ||
| tester: rankWith(1, isLayout), | ||
| renderer: markRaw(createTestLayout(clone)), | ||
| }, | ||
| ]; | ||
|
|
||
| const renderers = createRenderers(false); | ||
|
|
||
| const schema = { | ||
| type: 'object', | ||
| properties: { | ||
| first: { type: 'string' }, | ||
| second: { type: 'string' }, | ||
| }, | ||
| }; | ||
|
|
||
| const uischema = { | ||
| type: 'VerticalLayout', | ||
| elements: [ | ||
| { type: 'Control', scope: '#/properties/first' }, | ||
| { type: 'Control', scope: '#/properties/second' }, | ||
| ], | ||
| }; | ||
|
|
||
| describe('stable bindings', () => { | ||
| beforeEach(() => { | ||
| for (const key of Object.keys(renders)) { | ||
| delete renders[key]; | ||
| } | ||
| changeHandlers = {}; | ||
| }); | ||
|
|
||
| it('does not re-render a control whose bindings did not change', async () => { | ||
| const wrapper = mount(JsonForms, { | ||
| props: { | ||
| data: { first: 'a', second: 'b' }, | ||
| schema, | ||
| uischema, | ||
| renderers, | ||
| }, | ||
| }); | ||
| await nextTick(); | ||
| expect(renders).toEqual({ first: 1, second: 1 }); | ||
|
|
||
| changeHandlers.first('first', 'changed'); | ||
| await nextTick(); | ||
|
|
||
| expect(wrapper.text()).toContain('changed'); | ||
| expect(renders.first).toBe(2); | ||
| expect(renders.second).toBe(1); | ||
| }); | ||
|
|
||
| it('re-renders a control when its bindings change', async () => { | ||
| const wrapper = mount(JsonForms, { | ||
| props: { | ||
| data: { first: 'a', second: 'b' }, | ||
| schema, | ||
| uischema, | ||
| renderers, | ||
| }, | ||
| }); | ||
| await nextTick(); | ||
|
|
||
| changeHandlers.second('second', 'changed'); | ||
| await nextTick(); | ||
|
|
||
| expect(wrapper.text()).toContain('changed'); | ||
| expect(renders.first).toBe(1); | ||
| expect(renders.second).toBe(2); | ||
| }); | ||
|
|
||
| it('does not re-render a control when its layout dispatches a cloned UI schema element', async () => { | ||
| const wrapper = mount(JsonForms, { | ||
| props: { | ||
| data: { first: 'a', second: 'b' }, | ||
| schema, | ||
| uischema, | ||
| renderers: createRenderers(true), | ||
| }, | ||
| }); | ||
| await nextTick(); | ||
| expect(renders).toEqual({ first: 1, second: 1 }); | ||
|
|
||
| changeHandlers.first('first', 'changed'); | ||
| await nextTick(); | ||
|
|
||
| expect(wrapper.text()).toContain('changed'); | ||
| expect(renders.first).toBe(2); | ||
| expect(renders.second).toBe(1); | ||
| }); | ||
|
|
||
| it('re-renders a control when a date in its UI schema changes', async () => { | ||
| const uischemaWithDate = (min: Date) => ({ | ||
| type: 'VerticalLayout', | ||
| elements: [ | ||
| { type: 'Control', scope: '#/properties/first', options: { min } }, | ||
| { type: 'Control', scope: '#/properties/second' }, | ||
| ], | ||
| }); | ||
| const wrapper = mount(JsonForms, { | ||
| props: { | ||
| data: { first: 'a', second: 'b' }, | ||
| schema, | ||
| uischema: uischemaWithDate(new Date(Date.UTC(1970, 0, 1))), | ||
| renderers, | ||
| }, | ||
| }); | ||
| await nextTick(); | ||
| expect(wrapper.text()).toContain('1970-01-01'); | ||
|
|
||
| await wrapper.setProps({ | ||
| uischema: uischemaWithDate(new Date(Date.UTC(2030, 0, 1))), | ||
| }); | ||
| await nextTick(); | ||
|
|
||
| expect(wrapper.text()).toContain('2030-01-01'); | ||
| }); | ||
| }); | ||
|
KiaraGrouwstra marked this conversation as resolved.
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.