Skip to content

Commit

Permalink
fix: get grand child from component tree (#394)
Browse files Browse the repository at this point in the history
* fix: get grand child from component tree

* test: add snapshot
  • Loading branch information
dpilch authored and alharris-at committed Feb 25, 2022
1 parent ab323b9 commit b427d09
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Array [
]
`;

exports[`getComponentFromComponentTree not found 1`] = `"Component NotFoundComponent not found in component tree Component"`;

exports[`getComponentStateReferences basic 1`] = `
Array [
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
limitations under the License.
*/
import { MutationAction, DataStoreUpdateItemAction } from '@aws-amplify/codegen-ui';
import { getComponentStateReferences, getActionStateParameters } from '../../workflow/mutation';
import {
getComponentStateReferences,
getActionStateParameters,
getComponentFromComponentTree,
} from '../../workflow/mutation';

describe('getComponentStateReferences', () => {
test('basic', () => {
Expand Down Expand Up @@ -93,3 +97,39 @@ describe('getActionStateParameters', () => {
expect(getActionStateParameters(action)).toMatchSnapshot();
});
});

describe('getComponentFromComponentTree', () => {
const grandChildComponent = {
componentType: 'TextField',
name: 'GrandChildComponent',
properties: {},
};
const childComponent = {
componentType: 'Flex',
name: 'ChildComponent',
properties: {},
children: [grandChildComponent],
};
const component = {
componentType: 'Flex',
name: 'Component',
properties: {},
bindingProperties: {},
children: [childComponent],
};
test('same as root component', () => {
expect(getComponentFromComponentTree(component, 'Component')).toEqual(component);
});

test('child component', () => {
expect(getComponentFromComponentTree(component, 'ChildComponent')).toEqual(childComponent);
});

test('grandchild component', () => {
expect(getComponentFromComponentTree(component, 'GrandChildComponent')).toEqual(grandChildComponent);
});

test('not found', () => {
expect(() => getComponentFromComponentTree(component, 'NotFoundComponent')).toThrowErrorMatchingSnapshot();
});
});
4 changes: 1 addition & 3 deletions packages/codegen-ui-react/lib/workflow/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,7 @@ export function getComponentFromComponentTree(
}

if (currentComponent.children) {
return currentComponent.children.find(
(child: StudioComponentChild) => getComponentFromComponentTreeHelper(child) !== undefined,
);
return currentComponent.children.map(getComponentFromComponentTreeHelper).find((child) => child !== undefined);
}

return undefined;
Expand Down

0 comments on commit b427d09

Please sign in to comment.