Skip to content

Commit

Permalink
Change createGetDataElementValue so that it uses the executeDelegateM…
Browse files Browse the repository at this point in the history
…odule. Have one place for enviornment data.
  • Loading branch information
dompuiu committed Mar 26, 2024
1 parent c96a25e commit 1b61f5b
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 118 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"jest-runner-eslint": "^2.2.0",
"kebab-case": "1.0.2",
"lint-staged": "^15.2.2",
"lodash": "^4.17.21",
"prettier": "^3.2.5",
"proxyquire": "^2.1.3",
"rollup": "^4.13.0",
Expand Down
4 changes: 2 additions & 2 deletions src/__mocks__/containerInitFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ module.exports = (getDataElementValues) => ({
},
dataElements: {
myPrecious: {
modulePath: 'core/src/lib/conditions/customCode.js',
modulePath: 'core/src/lib/dataElements/customCode.js',
getSettings: () =>
Promise.resolve({
source: () => 'precious'
})
},

UID: {
modulePath: 'core/src/lib/conditions/customCode.js',
modulePath: 'core/src/lib/dataElements/customCode.js',
getSettings: () =>
Promise.resolve({
source: () => 'UA-X-123'
Expand Down
62 changes: 41 additions & 21 deletions src/__tests__/createGetDataElementValue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,28 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

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

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

const defaultContext = { arcAndUtils: {} };
const getContext = (context = {}) => {
const logs = [];
return lodash.merge(
{
arcAndUtils: {
arc: { event: {} },
utils: {
logger: {
log: (m) => logs.push(m)
}
}
}
},
context
);
};

const createGetDataElementDefinitionDefault = (dataDef) =>
jest.fn().mockImplementation((dataElementName) => {
Expand Down Expand Up @@ -64,7 +81,7 @@ describe('function returned by createGetDataElementValue', () => {
}
});

return getDataElementValue('testDataElement', defaultContext).then(
return getDataElementValue('testDataElement', getContext()).then(
(dataElementValue) => expect(dataElementValue).toBe('bar')
);
});
Expand All @@ -87,12 +104,12 @@ describe('function returned by createGetDataElementValue', () => {
{ moduleProvider }
);

return getDataElementValue('testDataElement', context).then(
return getDataElementValue('testDataElement', getContext(context)).then(
(dataElementValue) => expect(dataElementValue).toBe('bar')
);
});

test('gives extension settings access to the data element module', async () => {
test.only('gives extension settings access to the data element module', async () => {
const moduleProvider = createGetModuleProvider({
getModuleExports:
() =>
Expand All @@ -105,7 +122,7 @@ describe('function returned by createGetDataElementValue', () => {
{ moduleProvider }
);

return getDataElementValue('testDataElement', defaultContext).then(
return getDataElementValue('testDataElement', getContext()).then(
(dataElementValue) =>
expect(dataElementValue).toEqual({ data: 'extension settings' })
);
Expand Down Expand Up @@ -134,7 +151,7 @@ describe('function returned by createGetDataElementValue', () => {
{ moduleProvider }
);

return getDataElementValue('testDataElement', context).then(
return getDataElementValue('testDataElement', getContext(context)).then(
(dataElementValue) => expect(dataElementValue).toEqual({ foo: 'bar' })
);
});
Expand All @@ -159,7 +176,7 @@ describe('function returned by createGetDataElementValue', () => {
{ moduleProvider }
);

return getDataElementValue('testDataElement', context).then(
return getDataElementValue('testDataElement', getContext(context)).then(
(dataElementValue) => expect(dataElementValue).toEqual({})
);
});
Expand All @@ -170,7 +187,7 @@ describe('function returned by createGetDataElementValue', () => {
settings: { foo: 'bar' }
});

return getDataElementValue('testDataElement', defaultContext).then(
return getDataElementValue('testDataElement', getContext()).then(
(dataElementValue) => expect(dataElementValue).toBe('cleaned:bar')
);
});
Expand All @@ -189,7 +206,7 @@ describe('function returned by createGetDataElementValue', () => {
moduleProvider
);

return getDataElementValue('testDataElement', defaultContext).then(
return getDataElementValue('testDataElement', getContext()).then(
(dataElementValue) => expect(dataElementValue).toBe('defaultValue')
);
});
Expand All @@ -203,7 +220,7 @@ describe('function returned by createGetDataElementValue', () => {
{ moduleProvider }
);

return getDataElementValue('testDataElement', defaultContext).then(
return getDataElementValue('testDataElement', getContext()).then(
(dataElementValue) =>
expect(dataElementValue).toBe(testDataElementValue)
);
Expand All @@ -224,7 +241,7 @@ describe('function returned by createGetDataElementValue', () => {
{ moduleProvider }
);

return getDataElementValue('testDataElement', defaultContext).then(
return getDataElementValue('testDataElement', getContext()).then(
(dataElementValue) =>
expect(dataElementValue).toBe(testDataElementValue)
);
Expand All @@ -239,7 +256,7 @@ describe('function returned by createGetDataElementValue', () => {
}
});

return getDataElementValue('testDataElement', defaultContext).then(
return getDataElementValue('testDataElement', getContext()).then(
(dataElementValue) => expect(dataElementValue).toBe('bar')
);
});
Expand All @@ -251,7 +268,7 @@ describe('function returned by createGetDataElementValue', () => {
settings: {}
});

return getDataElementValue('testDataElement', defaultContext).then(
return getDataElementValue('testDataElement', getContext()).then(
(dataElementValue) => expect(dataElementValue).toBe('bar')
);
});
Expand All @@ -271,7 +288,7 @@ describe('function returned by createGetDataElementValue', () => {
{ moduleProvider }
);

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

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

return getDataElementValue('testDataElement', defaultContext)
return getDataElementValue('testDataElement', getContext())
.then(() => {
throw new Error('This section should not have been called.');
})
Expand All @@ -330,10 +347,13 @@ describe('function returned by createGetDataElementValue', () => {
}
});

return getDataElementValue('testDataElement', {
dataElementCallStack: ['testDataElement'],
arcAndUtils: {}
})
return getDataElementValue(
'testDataElement',
getContext({
dataElementCallStack: ['testDataElement'],
arcAndUtils: {}
})
)
.then(() => {
throw new Error('This section should not have been called.');
})
Expand Down Expand Up @@ -361,7 +381,7 @@ describe('function returned by createGetDataElementValue', () => {
{ moduleProvider }
);

return getDataElementValue('testDataElement', defaultContext).then(
return getDataElementValue('testDataElement', getContext()).then(
(dataElementValue) =>
expect(dataElementValue).toEqual({
id: 'DE123',
Expand Down
32 changes: 6 additions & 26 deletions src/__tests__/createGetDataElementValues.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,16 @@ describe('function returned by createGetDataElementValues', () => {
);
});

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

const context = {};
return getDataElementValues(['de1'], context).then(() => {
expect(context.dataElementCallStack).toStrictEqual([]);
});
});

test('sends context to getDataElementValue', () => {
const getDataElementValues =
createGetDataElementValues(getDataElementValue);
const context = { a: 1 };
await getDataElementValues(['de1'], context);

const context = { a: 1, dataElementCallStack: [1] };
return getDataElementValues(['de1'], context).then(() => {
expect(getDataElementValue).toHaveBeenCalledWith('de1', context);
expect(getDataElementValue).toHaveBeenCalledWith('de1', {
a: 1,
dataElementCallStack: []
});
});

test('sends a cloned dataElementCallStack to getDataElementValue', () => {
const getDataElementValues = createGetDataElementValues(
(dataElementName, { dataElementCallStack }) => {
dataElementCallStack.push(dataElementName);

expect(dataElementCallStack).toStrictEqual([dataElementName]);
}
);

const context = {};
return getDataElementValues(['de1', 'de2'], context);
});
});
32 changes: 32 additions & 0 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ describe('index', () => {
'{}',
'log'
],
['Resolving the data element "myPrecious".', 'log'],
[
'Calling "Custom Code Data Element" module from the "Core" extension.',
'Event: ',
'{"xdm":{},"data":{}}',
'Rule Stash: ',
'{}',
'log'
],
[
'"Custom Code Data Element" module from the "Core" extension returned.',
'Output:',
'precious',
'log'
],
['The "myPrecious" data element value is "precious".', 'log'],
[
'FETCH',
'Resource',
Expand All @@ -95,6 +111,22 @@ describe('index', () => {
'{"adobe-cloud-connector":"send data done"}',
'log'
],
['Resolving the data element "UID".', 'log'],
[
'Calling "Custom Code Data Element" module from the "Core" extension.',
'Event: ',
'{"xdm":{},"data":{}}',
'Rule Stash: ',
'{"adobe-cloud-connector":"send data done"}',
'log'
],
[
'"Custom Code Data Element" module from the "Core" extension returned.',
'Output:',
'UA-X-123',
'log'
],
['The "UID" data element value is "UA-X-123".', 'log'],
[
'"Send Beacon" module from the "Demo Extensions With Settings" extension returned.',
'Output:',
Expand Down
Loading

0 comments on commit 1b61f5b

Please sign in to comment.