Skip to content

Commit

Permalink
Merge pull request #2314 from IDEMSInternational/feat/data-items-loca…
Browse files Browse the repository at this point in the history
…l-context-2

feat: data-items local context (alt)
  • Loading branch information
esmeetewinkel authored May 7, 2024
2 parents a8ba99e + 00610d6 commit 26c172f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ export class TmplDataItemsComponent extends TemplateBaseComponent implements OnD
rows: [],
},
} as any);
// HACK - still want to be able to use localContext from parent rows so copy to child processor
processor.templateRowMap = JSON.parse(
JSON.stringify(this.parent.templateRowService.templateRowMap)
);
await processor.processContainerTemplateRows();
return processor.renderedRows;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export class TemplateRowService extends SyncServiceBase {
public templateRowMap: ITemplateRowMap = {};
public renderedRows: FlowTypes.TemplateRow[]; // rows processed and filtered by condition

constructor(private injector: Injector, public container: TemplateContainerComponent) {
constructor(
private injector: Injector,
public container: TemplateContainerComponent
) {
super("TemplateRow");
/**
* Avoid initialisation logic and prefer to ensure services ready
Expand Down Expand Up @@ -278,6 +281,9 @@ export class TemplateRowService extends SyncServiceBase {

if (type === "template") isNestedTemplate = true;

// data_items still need to process on render so avoid populating child rows to templateRowMap
if (type === "data_items") isNestedTemplate = true;

// Instead of returning themselves items looped child rows
if (type === "items") {
// extract raw parameter list
Expand Down Expand Up @@ -358,9 +364,8 @@ export class TemplateRowService extends SyncServiceBase {
parsed[listKey] = listValue;
for (const [itemKey, itemValue] of Object.entries(listValue)) {
if (typeof itemValue === "string") {
parsed[listKey][itemKey] = await this.templateVariablesService.evaluateConditionString(
itemValue
);
parsed[listKey][itemKey] =
await this.templateVariablesService.evaluateConditionString(itemValue);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,33 @@ const TEST_ITEM_CONTEXT: IVariableContext = {
},
};

const TEST_LOCAL_CONTEXT: IVariableContext = {
...MOCK_CONTEXT_BASE,
templateRowMap: {
// Mock row setting a local variable
string_local: {
name: "string_local",
value: "Jasper",
type: "set_variable",
_nested_name: "string_local",
},
},
row: {
...MOCK_CONTEXT_BASE.row,
value: "Hello @local.string_local",
_dynamicFields: {
value: [
{
fullExpression: "Hello @local.string_local",
matchedExpression: "@local.string_local",
type: "local",
fieldName: "string_local",
},
],
},
},
};

/**
* Call standalone tests via:
* yarn ng test --include src/app/shared/components/template/services/template-variables.service.spec.ts
Expand Down Expand Up @@ -178,4 +205,13 @@ describe("TemplateVariablesService", () => {
);
expect(resWithoutItemContext).toEqual(MOCK_ITEM_STRING);
});

it("Evaluates string containing local variable", async () => {
const MOCK_LOCAL_STRING = "Hello @local.string_local";
const resWithLocalContext = await service.evaluatePLHData(
MOCK_LOCAL_STRING,
TEST_LOCAL_CONTEXT
);
expect(resWithLocalContext).toEqual("Hello Jasper");
});
});

0 comments on commit 26c172f

Please sign in to comment.