Skip to content

Commit

Permalink
add version and fullVersion to ModeDef
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinmombay committed Apr 18, 2016
1 parent d47bf25 commit 107a2ea
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,24 @@
* minified: boolean,
* test: boolean,
* log: (string|undefined),
* version: string,
* }}
*/
let ModeDef;

/** @typedef {?ModeDef} */
let mode = null;

/** @typedef {string} */
const version = '$internalRuntimeVersion$';

/**
* `fullVersion` is the prefixed version we serve off of the cdn.
* The prefix denotes canary(00) or prod(01) or an experiment version ( > 01).
* @type {string}
*/
let fullVersion = '';

/**
* Provides info about the current app.
* @return {!ModeDef}
Expand Down Expand Up @@ -69,13 +80,18 @@ function getMode_() {
// from the URL.
location.originalHash || location.hash);

if (!fullVersion) {
fullVersion = getFullVersion_(window, isLocalDev);
}

return {
localDev: isLocalDev,
// Triggers validation
development: developmentQuery['development'] == '1' || window.AMP_DEV_MODE,
minified: process.env.NODE_ENV == 'production',
test: window.AMP_TEST,
log: developmentQuery['log'],
version: fullVersion,
};
}

Expand Down Expand Up @@ -114,3 +130,27 @@ function parseQueryString_(queryString) {
}
return params;
}

/**
* Retrieve the `fullVersion` which will have a numeric prefix
* denoting canary/prod/experiment.
*
* @param {!Window} win
* @param {boolean} isLocalDev
* @return {string}
* @private
* @visibleForTesting
*/
export function getFullVersion_(win, isLocalDev) {
// If it's local dev then we won't actually have a full version so
// just use the version.
if (isLocalDev) {
return version;
}

if (win.AMP_CONFIG && win.AMP_CONFIG.v) {
return win.AMP_CONFIG.v;
}

return version;
}
3 changes: 2 additions & 1 deletion test/functional/test-3p-frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ describe('3p-frame', () => {
'"canonicalUrl":"https://foo.bar/baz",' +
'"pageViewId":"' + docInfo.pageViewId + '","clientId":"cidValue",' +
'"location":{"href":"' + locationHref + '"},"tagName":"MY-ELEMENT",' +
'"mode":{"localDev":true,"development":false,"minified":false}' +
'"mode":{"localDev":true,"development":false,"minified":false,' +
'"version":"$internalRuntimeVersion$"}' +
',"hidden":false,"initialIntersection":{"time":1234567888,' +
'"rootBounds":{"left":0,"top":0,"width":' + width + ',"height":' +
height + ',"bottom":' + height + ',"right":' + width +
Expand Down
50 changes: 50 additions & 0 deletions test/functional/test-mode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


import {getFullVersion_} from '../../src/mode';
import * as sinon from 'sinon';

describe('mode module', () => {

let sandbox;

beforeEach(() => {
sandbox = sinon.sandbox.create();
});

afterEach(() => {
sandbox.restore();
});

it('should default to version', () => {
// $internalRuntimeVersion$ doesn't get replaced during test
expect(getFullVersion_(window, true)).to.equal('$internalRuntimeVersion$');
expect(getFullVersion_(window, false)).to.equal('$internalRuntimeVersion$');
});

it('should use window.AMP_CONFIG.v if not in dev mode', () => {
const win = {
AMP_CONFIG: {
v: '12345',
},
};
expect(getFullVersion_(win, true)).to.equal('$internalRuntimeVersion$');
expect(getFullVersion_(win, false)).to.equal('12345');
delete win.AMP_CONFIG;
expect(getFullVersion_(win, false)).to.equal('$internalRuntimeVersion$');
});
});

0 comments on commit 107a2ea

Please sign in to comment.