Skip to content
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

Parse multi node switch fixv1 #347

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

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) {
mercillo marked this conversation as resolved.
Show resolved Hide resolved
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