Skip to content

Commit

Permalink
Merge pull request #347 from mercillo/parseMultiNodeSwitchFixv1
Browse files Browse the repository at this point in the history
Parse multi node switch fixv1
  • Loading branch information
KetanReddy authored Apr 25, 2024
2 parents 039b6dc + 8a0d69f commit f087f75
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
registry=https://registry.npmjs.com
registry=https://registry.npmjs.com
48 changes: 48 additions & 0 deletions core/player/src/view/__tests__/view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,54 @@ describe('view', () => {
expect(updated).toBe(resolved);
});

test('works with no valid switch cases in an array', () => {
const model = withParser(new LocalModel({}), parseBinding);
const evaluator = new ExpressionEvaluator({ model });
const schema = new SchemaController();

const view = new ViewInstance(
{
id: 'test',
type: 'view',
title: [
{
staticSwitch: [
{
case: false,
asset: {
id: 'false-case',
type: 'text',
value: 'some text',
},
},
{
case: false,
asset: {
id: 'false-case-2',
type: 'text',
value: 'some text',
},
},
],
},
],
},
{
model,
parseBinding,
evaluator,
schema,
}
);

const resolved = view.update();

expect(resolved).toStrictEqual({
id: 'test',
type: 'view',
});
});

it('does not return a field object if the case does not resolve an asset', () => {
const model = withParser(
new LocalModel({
Expand Down
16 changes: 12 additions & 4 deletions core/player/src/view/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export class Parser {
);
}

private hasSwitchKey(localKey: string) {
return localKey === ('staticSwitch' || 'dynamicSwitch');
}

public parseObject(
obj: object,
type: Node.ChildrenTypes = NodeType.Value,
Expand Down Expand Up @@ -173,6 +177,7 @@ export class Parser {
const newValue = objEntries.reduce((accumulation, current): NestedObj => {
const { children, ...rest } = accumulation;
const [localKey, localValue] = current;

if (localKey === 'asset' && typeof localValue === 'object') {
const assetAST = this.parseObject(
localValue,
Expand Down Expand Up @@ -233,11 +238,15 @@ export class Parser {
children: [...children, ...templateChildren],
} as NestedObj;
} else if (
localValue &&
this.hooks.determineNodeType.call(localValue) === NodeType.Switch
(localValue &&
this.hooks.determineNodeType.call(localValue) ===
NodeType.Switch) ||
this.hasSwitchKey(localKey)
) {
const localSwitch = this.hooks.parseNode.call(
localValue,
this.hasSwitchKey(localKey)
? { [localKey]: localValue }
: localValue,
NodeType.Value,
options,
NodeType.Switch
Expand Down Expand Up @@ -310,7 +319,6 @@ export class Parser {
v.parent = multiNode;
});
}

if (multiNode) {
return {
...rest,
Expand Down
1 change: 0 additions & 1 deletion core/player/src/view/plugins/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export default class SwitchPlugin implements ViewPlugin {
private resolveSwitch(node: Node.Switch, options: Options): Node.Node {
for (const switchCase of node.cases) {
const isApplicable = options.evaluate(switchCase.case);

if (isApplicable) {
return switchCase.value;
}
Expand Down

0 comments on commit f087f75

Please sign in to comment.