-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtemplate-variables.service.spec.ts
237 lines (221 loc) · 7.4 KB
/
template-variables.service.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { TestBed } from "@angular/core/testing";
import { IVariableContext, TemplateVariablesService } from "./template-variables.service";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { TemplateFieldService } from "./template-field.service";
import { MockTemplateFieldService } from "./template-field.service.spec";
import { AppDataService } from "src/app/shared/services/data/app-data.service";
import { CampaignService } from "src/app/feature/campaign/campaign.service";
import { MockAppDataService } from "src/app/shared/services/data/app-data.service.spec";
import { ICalcContext, TemplateCalcService } from "./template-calc.service";
import { MockTemplateCalcService } from "./template-calc.service.spec";
import clone from "clone";
const MOCK_APP_DATA = {};
// Fields populated to mock field service
const MOCK_FIELDS = {
_app_language: "gb_en",
_app_skin: "default",
string_field: "test_string_value",
number_field: 2,
};
const MOCK_CONTEXT_BASE: IVariableContext = {
// Assume the row will have a dynamic 'field' entry
field: "value",
row: {
type: "text",
value: "",
name: "test_row",
_nested_name: "test_row",
},
templateRowMap: {},
calcContext: {
globalConstants: {},
globalFunctions: {},
thisCtxt: {},
},
};
const TEST_FIELD_CONTEXT: IVariableContext = {
...MOCK_CONTEXT_BASE,
row: {
...MOCK_CONTEXT_BASE.row,
value: "Hello @fields.string_field",
_dynamicFields: {
value: [
{
fullExpression: "Hello @fields.string_field",
matchedExpression: "@fields.string_field",
type: "fields",
fieldName: "string_field",
},
],
},
},
};
const MOCK_ITEM_CONTEXT: IVariableContext["itemContext"] = {
id: "id1",
number: 1,
string: "hello",
boolean: true,
_index: 0,
_id: "id1",
_first: true,
_last: false,
};
// Context adapted from this debug template:
// https://docs.google.com/spreadsheets/d/1tL6CPHEIW-GPMYjdhVKQToy_hZ1H5qNIBkkh9XnA5QM/edit#gid=114708400
const TEST_ITEM_CONTEXT: IVariableContext = {
...MOCK_CONTEXT_BASE,
row: {
...MOCK_CONTEXT_BASE.row,
value: "@item._index + 1",
// NOTE - any evaluated fields should appea
_dynamicFields: {
value: [
{
fullExpression: "@item._index + 1",
matchedExpression: "@item._index",
type: "item",
fieldName: "_index",
},
],
},
},
itemContext: MOCK_ITEM_CONTEXT,
};
const TEST_LOCAL_CONTEXT: IVariableContext = {
...MOCK_CONTEXT_BASE,
templateRowMap: {
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",
},
],
},
},
};
const TEST_LOCAL_CONTEXT_WITH_ITEM_CONTEXT = {
...TEST_LOCAL_CONTEXT,
itemContext: MOCK_ITEM_CONTEXT,
};
const MOCK_CALC_CONTEXT: Partial<ICalcContext> = {
thisCtxt: { local: { string_local: "Jasper2" } },
};
/**
* Call standalone tests via:
* yarn ng test --include src/app/shared/components/template/services/template-variables.service.spec.ts
*/
describe("TemplateVariablesService", () => {
let service: TemplateVariablesService;
let getNextCampaignRowsSpy: jasmine.Spy<jasmine.Func>;
beforeEach(async () => {
getNextCampaignRowsSpy = jasmine.createSpy();
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
{
provide: TemplateFieldService,
useValue: new MockTemplateFieldService(MOCK_FIELDS),
},
{
provide: AppDataService,
useValue: new MockAppDataService(MOCK_APP_DATA),
},
{
provide: TemplateCalcService,
// HACK: hardcoded calcContext from mock context is overridden by calcContext returned from MockTemplateCalcService,
// so insert values here for testing evaluation of local variable inside item loop
useValue: new MockTemplateCalcService(clone(MOCK_CALC_CONTEXT)),
},
// Mock single method from campaign service called
{
provide: CampaignService,
useValue: {
ready: async () => {
return true;
},
getNextCampaignRows: getNextCampaignRowsSpy,
},
},
],
});
service = TestBed.inject(TemplateVariablesService);
await service.ready();
});
it("should be created", () => {
expect(service).toBeTruthy();
});
it("Evaluates PLH Data String", async () => {
console.log({ TEST_FIELD_CONTEXT });
const res = await service.evaluatePLHData("Hello @fields.string_field", TEST_FIELD_CONTEXT);
expect(res).toEqual("Hello test_string_value");
// Data will only be evaluated if it has been pre-parsed, extracting dynamic references
// If not returns raw value
delete TEST_FIELD_CONTEXT.row._dynamicFields;
const resWithoutDynamicContext = await service.evaluatePLHData(
"@fields.string_field",
TEST_FIELD_CONTEXT
);
expect(resWithoutDynamicContext).toEqual("@fields.string_field");
/**
* TODO - include all edge cases, e.g. raw, item, calc, deep-nested, object, array etc.
const res = await service.evaluatePLHData(["@fields.string_field"], MOCK_CONTEXT);
expect(res).toEqual({ 1: "test_string_value" });
const res = await service.evaluatePLHData(
{
nested: "@fields.string_field",
},
MOCK_CONTEXT
);
expect(res).toEqual({ nested: "test_string_value" });
*/
});
it("Evaluates condition strings", async () => {
// Condition strings are evaluated without any previous pre-parsed dynamic fields
const res = await service.evaluateConditionString("@fields.number_field > 3");
expect(res).toEqual(false);
});
it("evaluates string containing item variable", async () => {
const MOCK_ITEM_STRING = "@item._index + 1";
// Parse expression when item context included
const resWithItemContext = await service.evaluatePLHData(MOCK_ITEM_STRING, TEST_ITEM_CONTEXT);
expect(resWithItemContext).toEqual(1);
// Retain raw expression if evaluating outside of item context
// https://github.com/IDEMSInternational/parenting-app-ui/pull/2215#discussion_r1514757364
delete TEST_ITEM_CONTEXT.itemContext;
const resWithoutItemContext = await service.evaluatePLHData(
MOCK_ITEM_STRING,
TEST_ITEM_CONTEXT
);
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");
});
it("Evaluates string containing local variable, inside item loop", async () => {
const MOCK_LOCAL_STRING = "Hello @local.string_local";
// When itemContext is included (i.e. in an item loop), look to thisCtxt for the parsed local var
const resWithLocalContext = await service.evaluatePLHData(
MOCK_LOCAL_STRING,
TEST_LOCAL_CONTEXT_WITH_ITEM_CONTEXT
);
expect(resWithLocalContext).toEqual("Hello Jasper2");
});
});