Skip to content

Commit

Permalink
Changes needed for moving to the ES modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
dompuiu committed Mar 6, 2024
1 parent 5747e14 commit f59b7fa
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 43 deletions.
139 changes: 111 additions & 28 deletions src/__tests__/createGetDataElementValue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,23 @@ const createGetDataElementDefinitionDefault = (dataDef) =>
return null;
});

const createGetModuleProvider = (o = {}) => {
return {
getModuleExports: () => (context) => context.utils.getSettings().foo,
getModuleDefinition: () => ({
extensionName: 'someextension'
}),
getExtensionDefinition: () => ({
getSettings: () => ({ data: 'extension settings' })
}),
...o
};
};

const createGetDataElementValue = (
delegateDefinition,
{
moduleProvider = {
getModuleExports: () => (context) => context.utils.getSettings().foo
},
moduleProvider = createGetModuleProvider(),
createGetDataElementDefinition = createGetDataElementDefinitionDefault
} = {}
) => {
Expand All @@ -46,7 +57,7 @@ const createGetDataElementValue = (
};

describe('function returned by createGetDataElementValue', () => {
test('returns a value from the settings object as value', () => {
test('returns a value from the settings object as value', async () => {
const getDataElementValue = createGetDataElementValue({
settings: {
foo: 'bar'
Expand All @@ -58,7 +69,7 @@ describe('function returned by createGetDataElementValue', () => {
);
});

test('returns a value from the contextData object as value', () => {
test('returns a value from the contextData object as value', async () => {
const context = {
arcAndUtils: {
arc: {
Expand All @@ -67,9 +78,9 @@ describe('function returned by createGetDataElementValue', () => {
}
};

const moduleProvider = {
const moduleProvider = createGetModuleProvider({
getModuleExports: () => (c) => c.arc.foo
};
});

const getDataElementValue = createGetDataElementValue(
{},
Expand All @@ -81,7 +92,79 @@ describe('function returned by createGetDataElementValue', () => {
);
});

test('cleans the value when cleanText = true', () => {
test('gives extension settings access to the data element module', async () => {
const moduleProvider = createGetModuleProvider({
getModuleExports:
() =>
({ utils }) =>
utils.getExtensionSettings()
});

const getDataElementValue = createGetDataElementValue(
{},
{ moduleProvider }
);

return getDataElementValue('testDataElement', defaultContext).then(
(dataElementValue) =>
expect(dataElementValue).toEqual({ data: 'extension settings' })
);
});

test('gives env access to the data element module called by the core extension', async () => {
const context = {
arcAndUtils: {},
env: {
foo: 'bar'
}
};

const moduleProvider = createGetModuleProvider({
getModuleDefinition: () => ({
extensionName: 'core'
}),
getModuleExports:
() =>
({ utils }) =>
utils.getEnv()
});

const getDataElementValue = createGetDataElementValue(
{},
{ moduleProvider }
);

return getDataElementValue('testDataElement', context).then(
(dataElementValue) => expect(dataElementValue).toEqual({ foo: 'bar' })
);
});

test('does not give env access to the data element module called by extensions that are not core', async () => {
const context = {
arcAndUtils: {},
env: {
foo: 'bar'
}
};

const moduleProvider = createGetModuleProvider({
getModuleExports:
() =>
({ utils }) =>
utils.getEnv()
});

const getDataElementValue = createGetDataElementValue(
{},
{ moduleProvider }
);

return getDataElementValue('testDataElement', context).then(
(dataElementValue) => expect(dataElementValue).toEqual({})
);
});

test('cleans the value when cleanText = true', async () => {
const getDataElementValue = createGetDataElementValue({
cleanText: true,
settings: { foo: 'bar' }
Expand All @@ -93,11 +176,11 @@ describe('function returned by createGetDataElementValue', () => {
});

[undefined, null].forEach((testDataElementValue) => {
const moduleProvider = {
const moduleProvider = createGetModuleProvider({
getModuleExports: () => () => testDataElementValue
};
});

test(`returns a default value if data element value is ${testDataElementValue}`, () => {
test(`returns a default value if data element value is ${testDataElementValue}`, async () => {
const getDataElementValue = createGetDataElementValue(
{
defaultValue: 'defaultValue',
Expand All @@ -112,7 +195,7 @@ describe('function returned by createGetDataElementValue', () => {
});

test(`returns ${testDataElementValue} if data element value is ${testDataElementValue}
and default is undefined`, () => {
and default is undefined`, async () => {
const getDataElementValue = createGetDataElementValue(
{
settings: {}
Expand All @@ -128,11 +211,11 @@ describe('function returned by createGetDataElementValue', () => {
});

['', 0, false, NaN].forEach((testDataElementValue) => {
const moduleProvider = {
const moduleProvider = createGetModuleProvider({
getModuleExports: () => () => testDataElementValue
};
});

test(`does not return a default value if value is ${testDataElementValue}`, () => {
test(`does not return a default value if value is ${testDataElementValue}`, async () => {
const getDataElementValue = createGetDataElementValue(
{
defaultValue: 'defaultValue',
Expand All @@ -148,7 +231,7 @@ describe('function returned by createGetDataElementValue', () => {
});
});

test('lowercases the value if forceLowerCase = true', () => {
test('lowercases the value if forceLowerCase = true', async () => {
const getDataElementValue = createGetDataElementValue({
forceLowerCase: true,
settings: {
Expand All @@ -161,7 +244,7 @@ describe('function returned by createGetDataElementValue', () => {
);
});

test('lowercases the default value if forceLowerCase = true', () => {
test('lowercases the default value if forceLowerCase = true', async () => {
const getDataElementValue = createGetDataElementValue({
forceLowerCase: true,
defaultValue: 'bAr',
Expand All @@ -173,12 +256,12 @@ describe('function returned by createGetDataElementValue', () => {
);
});

test('throws an error when calling data element module exports fails', () => {
const moduleProvider = {
test('throws an error when calling data element module exports fails', async () => {
const moduleProvider = createGetModuleProvider({
getModuleExports: () => () => {
throw new Error('noob tried to divide by zero');
}
};
});

const getDataElementValue = createGetDataElementValue(
{
Expand All @@ -195,12 +278,12 @@ describe('function returned by createGetDataElementValue', () => {
});
});

test('throws an error when calling data element module exports fails', () => {
const moduleProvider = {
test('throws an error when calling data element module exports fails', async () => {
const moduleProvider = createGetModuleProvider({
getModuleExports: () => () => {
throw new Error('noob tried to divide by zero');
}
};
});

const getDataElementValue = createGetDataElementValue(
{
Expand All @@ -221,7 +304,7 @@ describe('function returned by createGetDataElementValue', () => {
});
});

test('throws an error when data element definition is not found', () => {
test('throws an error when data element definition is not found', async () => {
const getDataElementValue = createGetDataElementValue(
{
settings: {}
Expand All @@ -240,7 +323,7 @@ describe('function returned by createGetDataElementValue', () => {
});
});

test('throws an error when data element circular reference is detected', () => {
test('throws an error when data element circular reference is detected', async () => {
const getDataElementValue = createGetDataElementValue({
settings: {
foo: 'bar'
Expand All @@ -261,13 +344,13 @@ describe('function returned by createGetDataElementValue', () => {
});
});

test('provides access to getComponent method when it calls the data element module ', () => {
const moduleProvider = {
test('provides access to getComponent method when it calls the data element module ', async () => {
const moduleProvider = createGetModuleProvider({
getModuleExports:
() =>
({ utils: { getComponent } }) =>
getComponent()
};
});

const getDataElementValue = createGetDataElementValue(
{
Expand Down
12 changes: 12 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

module.exports = { CORE: 'core' };
18 changes: 15 additions & 3 deletions src/createGetDataElementValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ governing permissions and limitations under the License.
*/

const cleanTextFn = require('./cleanText');
const constants = require('./constants');

const { CORE } = constants;

const enhanceErrorMessage = (dataElementName, e) => {
e.message = `Failed to execute module for data element "${dataElementName}". ${e.message}`;
};

module.exports =
(moduleProvider, getDataElementDefinition) => (dataElementName, context) => {
const { dataElementCallStack = [], arcAndUtils } = context;
const { dataElementCallStack = [], arcAndUtils, env } = context;
const { utils } = arcAndUtils;

const dataDef = getDataElementDefinition(dataElementName);
Expand Down Expand Up @@ -54,14 +57,23 @@ module.exports =
} = dataDef;

const moduleExports = moduleProvider.getModuleExports(modulePath);
const valuePromise = getSettings(context).then((settings) => {
const moduleDefinition = moduleProvider.getModuleDefinition(modulePath);
const { getSettings: getExtensionSettings = () => Promise.resolve({}) } =
moduleProvider.getExtensionDefinition(modulePath);

const valuePromise = Promise.all([
getSettings(context),
getExtensionSettings(context)
]).then(([settings, extensionSettings]) => {
try {
return moduleExports({
...arcAndUtils,
utils: {
...utils,
getSettings: () => settings,
getComponent: () => ({ id, name })
getExtensionSettings: () => extensionSettings,
getComponent: () => ({ id, name }),
getEnv: () => (moduleDefinition.extensionName === CORE ? env : {})
}
});
} catch (e) {
Expand Down
14 changes: 10 additions & 4 deletions src/executeRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ module.exports = (
container,
globalFetch,
callData,
env,
{ headersForSubrequests } = {}
) => {
const rulePromises = [];

const {
rules = [],
headerOverrides = [],
logSensitiveTokens = [],

getHeaderOverrides = () => [],
getLogSensitiveTokens = () => [],
buildInfo
} = container;

Expand All @@ -40,11 +42,14 @@ module.exports = (
rules.forEach((rule) => {
const { id, name } = rule;

const logger = createNewLogger({ ruleId: rule.id }, logSensitiveTokens);
const logger = createNewLogger(
{ ruleId: rule.id },
getLogSensitiveTokens(env)
);

const fetch = getRuleFetchFn(
globalFetch,
headerOverrides,
getHeaderOverrides(env),
headersForSubrequests,
logger
);
Expand All @@ -57,6 +62,7 @@ module.exports = (
};

const initialContext = {
env,
arcAndUtils: {
utils,
arc: {
Expand Down
Loading

0 comments on commit f59b7fa

Please sign in to comment.