Skip to content

Commit 1b61f5b

Browse files
committed
Change createGetDataElementValue so that it uses the executeDelegateModule. Have one place for enviornment data.
1 parent c96a25e commit 1b61f5b

12 files changed

+159
-118
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"jest-runner-eslint": "^2.2.0",
4242
"kebab-case": "1.0.2",
4343
"lint-staged": "^15.2.2",
44+
"lodash": "^4.17.21",
4445
"prettier": "^3.2.5",
4546
"proxyquire": "^2.1.3",
4647
"rollup": "^4.13.0",

src/__mocks__/containerInitFunction.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ module.exports = (getDataElementValues) => ({
3737
},
3838
dataElements: {
3939
myPrecious: {
40-
modulePath: 'core/src/lib/conditions/customCode.js',
40+
modulePath: 'core/src/lib/dataElements/customCode.js',
4141
getSettings: () =>
4242
Promise.resolve({
4343
source: () => 'precious'
4444
})
4545
},
4646

4747
UID: {
48-
modulePath: 'core/src/lib/conditions/customCode.js',
48+
modulePath: 'core/src/lib/dataElements/customCode.js',
4949
getSettings: () =>
5050
Promise.resolve({
5151
source: () => 'UA-X-123'

src/__tests__/createGetDataElementValue.test.js

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,28 @@ OF ANY KIND, either express or implied. See the License for the specific languag
99
governing permissions and limitations under the License.
1010
*/
1111

12+
// eslint-disable-next-line import/no-extraneous-dependencies
13+
const lodash = require('lodash');
1214
const createGetDataElementValueModule = require('../createGetDataElementValue');
1315

1416
jest.mock('../cleanText.js');
1517

16-
const defaultContext = { arcAndUtils: {} };
18+
const getContext = (context = {}) => {
19+
const logs = [];
20+
return lodash.merge(
21+
{
22+
arcAndUtils: {
23+
arc: { event: {} },
24+
utils: {
25+
logger: {
26+
log: (m) => logs.push(m)
27+
}
28+
}
29+
}
30+
},
31+
context
32+
);
33+
};
1734

1835
const createGetDataElementDefinitionDefault = (dataDef) =>
1936
jest.fn().mockImplementation((dataElementName) => {
@@ -64,7 +81,7 @@ describe('function returned by createGetDataElementValue', () => {
6481
}
6582
});
6683

67-
return getDataElementValue('testDataElement', defaultContext).then(
84+
return getDataElementValue('testDataElement', getContext()).then(
6885
(dataElementValue) => expect(dataElementValue).toBe('bar')
6986
);
7087
});
@@ -87,12 +104,12 @@ describe('function returned by createGetDataElementValue', () => {
87104
{ moduleProvider }
88105
);
89106

90-
return getDataElementValue('testDataElement', context).then(
107+
return getDataElementValue('testDataElement', getContext(context)).then(
91108
(dataElementValue) => expect(dataElementValue).toBe('bar')
92109
);
93110
});
94111

95-
test('gives extension settings access to the data element module', async () => {
112+
test.only('gives extension settings access to the data element module', async () => {
96113
const moduleProvider = createGetModuleProvider({
97114
getModuleExports:
98115
() =>
@@ -105,7 +122,7 @@ describe('function returned by createGetDataElementValue', () => {
105122
{ moduleProvider }
106123
);
107124

108-
return getDataElementValue('testDataElement', defaultContext).then(
125+
return getDataElementValue('testDataElement', getContext()).then(
109126
(dataElementValue) =>
110127
expect(dataElementValue).toEqual({ data: 'extension settings' })
111128
);
@@ -134,7 +151,7 @@ describe('function returned by createGetDataElementValue', () => {
134151
{ moduleProvider }
135152
);
136153

137-
return getDataElementValue('testDataElement', context).then(
154+
return getDataElementValue('testDataElement', getContext(context)).then(
138155
(dataElementValue) => expect(dataElementValue).toEqual({ foo: 'bar' })
139156
);
140157
});
@@ -159,7 +176,7 @@ describe('function returned by createGetDataElementValue', () => {
159176
{ moduleProvider }
160177
);
161178

162-
return getDataElementValue('testDataElement', context).then(
179+
return getDataElementValue('testDataElement', getContext(context)).then(
163180
(dataElementValue) => expect(dataElementValue).toEqual({})
164181
);
165182
});
@@ -170,7 +187,7 @@ describe('function returned by createGetDataElementValue', () => {
170187
settings: { foo: 'bar' }
171188
});
172189

173-
return getDataElementValue('testDataElement', defaultContext).then(
190+
return getDataElementValue('testDataElement', getContext()).then(
174191
(dataElementValue) => expect(dataElementValue).toBe('cleaned:bar')
175192
);
176193
});
@@ -189,7 +206,7 @@ describe('function returned by createGetDataElementValue', () => {
189206
moduleProvider
190207
);
191208

192-
return getDataElementValue('testDataElement', defaultContext).then(
209+
return getDataElementValue('testDataElement', getContext()).then(
193210
(dataElementValue) => expect(dataElementValue).toBe('defaultValue')
194211
);
195212
});
@@ -203,7 +220,7 @@ describe('function returned by createGetDataElementValue', () => {
203220
{ moduleProvider }
204221
);
205222

206-
return getDataElementValue('testDataElement', defaultContext).then(
223+
return getDataElementValue('testDataElement', getContext()).then(
207224
(dataElementValue) =>
208225
expect(dataElementValue).toBe(testDataElementValue)
209226
);
@@ -224,7 +241,7 @@ describe('function returned by createGetDataElementValue', () => {
224241
{ moduleProvider }
225242
);
226243

227-
return getDataElementValue('testDataElement', defaultContext).then(
244+
return getDataElementValue('testDataElement', getContext()).then(
228245
(dataElementValue) =>
229246
expect(dataElementValue).toBe(testDataElementValue)
230247
);
@@ -239,7 +256,7 @@ describe('function returned by createGetDataElementValue', () => {
239256
}
240257
});
241258

242-
return getDataElementValue('testDataElement', defaultContext).then(
259+
return getDataElementValue('testDataElement', getContext()).then(
243260
(dataElementValue) => expect(dataElementValue).toBe('bar')
244261
);
245262
});
@@ -251,7 +268,7 @@ describe('function returned by createGetDataElementValue', () => {
251268
settings: {}
252269
});
253270

254-
return getDataElementValue('testDataElement', defaultContext).then(
271+
return getDataElementValue('testDataElement', getContext()).then(
255272
(dataElementValue) => expect(dataElementValue).toBe('bar')
256273
);
257274
});
@@ -271,7 +288,7 @@ describe('function returned by createGetDataElementValue', () => {
271288
{ moduleProvider }
272289
);
273290

274-
return getDataElementValue('testDataElement', defaultContext).catch((e) => {
291+
return getDataElementValue('testDataElement', getContext()).catch((e) => {
275292
expect(e.message).toMatch(
276293
'Failed to execute module for data element "testDataElement". noob tried to divide by zero'
277294
);
@@ -292,7 +309,7 @@ describe('function returned by createGetDataElementValue', () => {
292309
{ moduleProvider }
293310
);
294311

295-
return getDataElementValue('testDataElement', defaultContext)
312+
return getDataElementValue('testDataElement', getContext())
296313
.then(() => {
297314
throw new Error('This section should not have been called.');
298315
})
@@ -312,7 +329,7 @@ describe('function returned by createGetDataElementValue', () => {
312329
{ createGetDataElementDefinition: () => () => {} }
313330
);
314331

315-
return getDataElementValue('testDataElement', defaultContext)
332+
return getDataElementValue('testDataElement', getContext())
316333
.then(() => {
317334
throw new Error('This section should not have been called.');
318335
})
@@ -330,10 +347,13 @@ describe('function returned by createGetDataElementValue', () => {
330347
}
331348
});
332349

333-
return getDataElementValue('testDataElement', {
334-
dataElementCallStack: ['testDataElement'],
335-
arcAndUtils: {}
336-
})
350+
return getDataElementValue(
351+
'testDataElement',
352+
getContext({
353+
dataElementCallStack: ['testDataElement'],
354+
arcAndUtils: {}
355+
})
356+
)
337357
.then(() => {
338358
throw new Error('This section should not have been called.');
339359
})
@@ -361,7 +381,7 @@ describe('function returned by createGetDataElementValue', () => {
361381
{ moduleProvider }
362382
);
363383

364-
return getDataElementValue('testDataElement', defaultContext).then(
384+
return getDataElementValue('testDataElement', getContext()).then(
365385
(dataElementValue) =>
366386
expect(dataElementValue).toEqual({
367387
id: 'DE123',

src/__tests__/createGetDataElementValues.test.js

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,16 @@ describe('function returned by createGetDataElementValues', () => {
2525
);
2626
});
2727

28-
test('adds a dataElementCallStack to the context if it does not exist', () => {
28+
test('sends context to getDataElementValue', async () => {
2929
const getDataElementValues =
3030
createGetDataElementValues(getDataElementValue);
3131

32-
const context = {};
33-
return getDataElementValues(['de1'], context).then(() => {
34-
expect(context.dataElementCallStack).toStrictEqual([]);
35-
});
36-
});
37-
38-
test('sends context to getDataElementValue', () => {
39-
const getDataElementValues =
40-
createGetDataElementValues(getDataElementValue);
32+
const context = { a: 1 };
33+
await getDataElementValues(['de1'], context);
4134

42-
const context = { a: 1, dataElementCallStack: [1] };
43-
return getDataElementValues(['de1'], context).then(() => {
44-
expect(getDataElementValue).toHaveBeenCalledWith('de1', context);
35+
expect(getDataElementValue).toHaveBeenCalledWith('de1', {
36+
a: 1,
37+
dataElementCallStack: []
4538
});
4639
});
47-
48-
test('sends a cloned dataElementCallStack to getDataElementValue', () => {
49-
const getDataElementValues = createGetDataElementValues(
50-
(dataElementName, { dataElementCallStack }) => {
51-
dataElementCallStack.push(dataElementName);
52-
53-
expect(dataElementCallStack).toStrictEqual([dataElementName]);
54-
}
55-
);
56-
57-
const context = {};
58-
return getDataElementValues(['de1', 'de2'], context);
59-
});
6040
});

src/__tests__/index.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ describe('index', () => {
6969
'{}',
7070
'log'
7171
],
72+
['Resolving the data element "myPrecious".', 'log'],
73+
[
74+
'Calling "Custom Code Data Element" module from the "Core" extension.',
75+
'Event: ',
76+
'{"xdm":{},"data":{}}',
77+
'Rule Stash: ',
78+
'{}',
79+
'log'
80+
],
81+
[
82+
'"Custom Code Data Element" module from the "Core" extension returned.',
83+
'Output:',
84+
'precious',
85+
'log'
86+
],
87+
['The "myPrecious" data element value is "precious".', 'log'],
7288
[
7389
'FETCH',
7490
'Resource',
@@ -95,6 +111,22 @@ describe('index', () => {
95111
'{"adobe-cloud-connector":"send data done"}',
96112
'log'
97113
],
114+
['Resolving the data element "UID".', 'log'],
115+
[
116+
'Calling "Custom Code Data Element" module from the "Core" extension.',
117+
'Event: ',
118+
'{"xdm":{},"data":{}}',
119+
'Rule Stash: ',
120+
'{"adobe-cloud-connector":"send data done"}',
121+
'log'
122+
],
123+
[
124+
'"Custom Code Data Element" module from the "Core" extension returned.',
125+
'Output:',
126+
'UA-X-123',
127+
'log'
128+
],
129+
['The "UID" data element value is "UA-X-123".', 'log'],
98130
[
99131
'"Send Beacon" module from the "Demo Extensions With Settings" extension returned.',
100132
'Output:',

0 commit comments

Comments
 (0)