Skip to content

Commit

Permalink
Move expandConfigRequest method to config.js to avoid dependency. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lannka authored and erwinmombay committed Jul 19, 2018
1 parent b3ef5b8 commit 47a9a6b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 60 deletions.
31 changes: 30 additions & 1 deletion extensions/amp-analytics/0.1/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {Services} from '../../../src/services';
import {assertHttpsUrl} from '../../../src/url';
import {dev, user} from '../../../src/log';
import {dict, hasOwn} from '../../../src/utils/object';
import {expandConfigRequest} from './requests';
import {getMode} from '../../../src/mode';
import {isArray, isObject} from '../../../src/types';
import {isJsonScriptTag} from '../../../src/dom';
Expand Down Expand Up @@ -328,3 +327,33 @@ export function mergeObjects(from, to, opt_predefinedConfig) {
}
return to;
}

/**
* Expand config's request to object
* @param {!JsonObject} config
* @visibleForTesting
*/
export function expandConfigRequest(config) {
if (!config['requests']) {
return config;
}
for (const k in config['requests']) {
if (hasOwn(config['requests'], k)) {
config['requests'][k] = expandRequestStr(config['requests'][k]);
}
}
return config;
}

/**
* Expand single request to an object
* @param {!JsonObject} request
*/
function expandRequestStr(request) {
if (isObject(request)) {
return request;
}
return {
'baseUrl': request,
};
}
32 changes: 1 addition & 31 deletions extensions/amp-analytics/0.1/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import {appendEncodedParamStringToUrl} from '../../../src/url';
import {dev, user} from '../../../src/log';
import {dict} from '../../../src/utils/object';
import {filterSplice} from '../../../src/utils/array';
import {hasOwn, map} from '../../../src/utils/object';
import {isArray, isFiniteNumber} from '../../../src/types';
import {isObject} from '../../../src/types';
import {map} from '../../../src/utils/object';
import {parseQueryString} from '../../../src/url';

const TAG = 'AMP-ANALYTICS';
Expand Down Expand Up @@ -418,32 +417,3 @@ export class RequestHandler {
}, interval);
}
}

/**
* Expand config's request to object
* @param {!JsonObject} config
*/
export function expandConfigRequest(config) {
if (!config['requests']) {
return config;
}
for (const k in config['requests']) {
if (hasOwn(config['requests'], k)) {
config['requests'][k] = expandRequestStr(config['requests'][k]);
}
}
return config;
}

/**
* Expand single request to an object
* @param {!JsonObject} request
*/
function expandRequestStr(request) {
if (isObject(request)) {
return request;
}
return {
'baseUrl': request,
};
}
28 changes: 27 additions & 1 deletion extensions/amp-analytics/0.1/test/test-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import {ANALYTICS_CONFIG} from '../vendors';
import {AnalyticsConfig, mergeObjects} from '../config';
import {AnalyticsConfig, expandConfigRequest, mergeObjects} from '../config';
import {installDocService} from '../../../../src/service/ampdoc-impl';
import {map} from '../../../../src/utils/object';
import {stubService, stubServiceForDoc} from '../../../../testing/test-helper';
Expand Down Expand Up @@ -533,6 +533,32 @@ describes.realWin('AnalyticsConfig', {amp: false}, env => {
});
});

describe('expandConfigRequest', () => {
it('expandConfigRequest function', () => {
let config = {
'requests': {
'foo': 'test',
'bar': {
'baseUrl': 'test1',
},
'foobar': {},
},
};
config = expandConfigRequest(config);
expect(config).to.jsonEqual({
'requests': {
'foo': {
'baseUrl': 'test',
},
'bar': {
'baseUrl': 'test1',
},
'foobar': {},
},
});
});
});

function getAnalyticsTag(config, attrs) {
config = JSON.stringify(config);
const el = doc.createElement('amp-analytics');
Expand Down
28 changes: 1 addition & 27 deletions extensions/amp-analytics/0.1/test/test-requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import * as lolex from 'lolex';
import {ExpansionOptions, installVariableService} from '../variables';
import {RequestHandler, expandConfigRequest} from '../requests';
import {RequestHandler} from '../requests';
import {dict} from '../../../../src/utils/object';
import {macroTask} from '../../../../testing/yield';
import {toggleExperiment} from '../../../../src/experiments';
Expand Down Expand Up @@ -423,32 +423,6 @@ describes.realWin('Requests', {amp: 1}, env => {
});
});

//TODO: Move the expansion related tests here.

it('expandConfigRequest function', () => {
let config = {
'requests': {
'foo': 'test',
'bar': {
'baseUrl': 'test1',
},
'foobar': {},
},
};
config = expandConfigRequest(config);
expect(config).to.jsonEqual({
'requests': {
'foo': {
'baseUrl': 'test',
},
'bar': {
'baseUrl': 'test1',
},
'foobar': {},
},
});
});

it('should replace dynamic bindings', function* () {
const spy = sandbox.spy();
const r = {'baseUrl': 'r1&${extraUrlParams}&BASE_VALUE'};
Expand Down

0 comments on commit 47a9a6b

Please sign in to comment.