Skip to content

Commit c4f8eb2

Browse files
authored
Include componentId in props passed to static options generator (#6453)
Up until now the static options generator was invoked with passProps while the component's props contain passProps as well as componentId. This commit eliminates this inconsistency as it's a source for confusion and from now on passProps will also contain the componentId. Note that if the user has specified a prop named `componentId` then Navigation won't override it.
1 parent 813c1a2 commit c4f8eb2

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

lib/src/commands/LayoutTreeCrawler.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,57 @@ describe('LayoutTreeCrawler', () => {
155155
uut.crawl(node, CommandName.SetRoot);
156156
expect(node.data.passProps).toBeUndefined();
157157
});
158+
159+
it('componentId is included in props passed to options generator', () => {
160+
let componentIdInProps: String = '';
161+
162+
when(mockedStore.getComponentClassForName('theComponentName')).thenReturn(
163+
() =>
164+
class extends React.Component {
165+
static options(props: any) {
166+
componentIdInProps = props.componentId;
167+
return {};
168+
}
169+
}
170+
);
171+
const node = {
172+
id: 'testId',
173+
type: LayoutType.Component,
174+
data: {
175+
name: 'theComponentName',
176+
passProps: { someProp: 'here' },
177+
},
178+
children: [],
179+
};
180+
uut.crawl(node, CommandName.SetRoot);
181+
expect(componentIdInProps).toEqual('testId');
182+
});
183+
184+
it('componentId does not override componentId in passProps', () => {
185+
let componentIdInProps: String = '';
186+
187+
when(mockedStore.getComponentClassForName('theComponentName')).thenReturn(
188+
() =>
189+
class extends React.Component {
190+
static options(props: any) {
191+
componentIdInProps = props.componentId;
192+
return {};
193+
}
194+
}
195+
);
196+
const node = {
197+
id: 'testId',
198+
type: LayoutType.Component,
199+
data: {
200+
name: 'theComponentName',
201+
passProps: {
202+
someProp: 'here',
203+
componentId: 'compIdFromPassProps',
204+
},
205+
},
206+
children: [],
207+
};
208+
uut.crawl(node, CommandName.SetRoot);
209+
expect(componentIdInProps).toEqual('compIdFromPassProps');
210+
});
158211
});

lib/src/commands/LayoutTreeCrawler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class LayoutTreeCrawler {
5757
const reactComponent = foundReactGenerator ? foundReactGenerator() : undefined;
5858
if (reactComponent && this.isComponentWithOptions(reactComponent)) {
5959
return isFunction(reactComponent.options)
60-
? reactComponent.options(node.data.passProps || {})
60+
? reactComponent.options({ componentId: node.id, ...node.data.passProps } || {})
6161
: reactComponent.options;
6262
}
6363
return {};

0 commit comments

Comments
 (0)