Skip to content

Commit 7a522db

Browse files
committed
Minimal setup neccessary for a passing unit test
1 parent 6097e9a commit 7a522db

File tree

8 files changed

+140
-21
lines changed

8 files changed

+140
-21
lines changed

.eslintrc.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es6": true,
5+
"jasmine": true,
6+
"node": true
7+
},
8+
"extends": [
9+
"closure-es6",
10+
"plugin:jasmine/recommended",
11+
"google"
12+
],
13+
"parserOptions": {
14+
"sourceType": "module"
15+
},
16+
"plugins": [
17+
"jasmine",
18+
"closure"
19+
],
20+
"rules": {
21+
"prefer-rest-params": "off", // We don't want this, adds a polyfill
22+
// This library requires this to be used in methods as part of the API,
23+
// and sometimes it's useful to reference this in jasmine as there is
24+
// special handling.
25+
"no-invalid-this": "off",
26+
"prefer-spread": "off", // As per Tagging ES6 guidelines.
27+
"indent": "off", // Contradicts closure/indent.
28+
// We use the hasOwn function to validate object properties.
29+
"guard-for-in": "off"
30+
}
31+
}

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "data-layer-helper"]
2+
path = data-layer-helper
3+
url = https://github.com/google/data-layer-helper

Gruntfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module.exports = function(grunt) {
2727
},
2828
options: {
2929
js: [
30-
'node_modules/google-closure-library/closure/goog/base.js'
30+
'node_modules/google-closure-library/closure/goog/base.js',
31+
'data-layer-helper/src/**/*.js',
3132
],
3233
hide_warnings_for: 'google-closure-library',
3334
warning_level: 'VERBOSE',

data-layer-helper

Submodule data-layer-helper added at a558bc0

karma.conf.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ module.exports = function(config) {
88
// Automatically run tests for files matching these regex.
99
files: [
1010
// ----------------- Third Party Dependencies ----------------------------
11-
{pattern: 'node_modules/google-closure-library/closure/goog/base.js'},
12-
// ------------------------ Source Files ---------------------------------
1311
// Dependencies must be listed before the file they are used in for
1412
// the googmodule preprocessor to function properly.
13+
{pattern: 'node_modules/google-closure-library/closure/goog/base.js'},
14+
{pattern: 'data-layer-helper/src/plain/is_plain_object.js'},
15+
{pattern: 'data-layer-helper/src/helper/helper.js'},
16+
// ------------------------ Source Files ---------------------------------
1517
{pattern: 'src/**/*.js'},
1618
// ------------------------- Test Files ----------------------------------
1719
'test/*_test.js'

src/measure.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
goog.module('measurementlibrary.measure');
2+
3+
const {DataLayerHelper} = goog.require('helper');
4+
5+
/**
6+
* Listen to events passed to the current window.dataLayer (or a new one
7+
* if none exist).
8+
*/
9+
function setup() {
10+
/**
11+
* @global {!Array} The data layer of the application, storing a record of
12+
* events that have changed over time.
13+
*/
14+
window.dataLayer = window.dataLayer || [];
15+
16+
/**
17+
* @const {!DataLayerHelper} The DataLayerHelper to use with this application.
18+
*/
19+
const helper = new DataLayerHelper(window.dataLayer);
20+
21+
// TODO
22+
}
23+
24+
setup();
25+
26+
exports = {setup};

test/config_test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
goog.module('measurementlibrary.measure.testing.setup');
2+
goog.setTestOnly();
3+
4+
const {setup} = goog.require('measurementlibrary.measure');
5+
6+
let storageInterface;
7+
let eventProcessor;
8+
let measure;
9+
10+
/**
11+
* Run a test, once assuming that the measure snippet fired before the call
12+
* to setup, then again assuming that the gtag snippet fired after the call
13+
* to setup.
14+
* @param {function} config The configuration done in the gtag code snippet
15+
* @param {function} test The tests to run after both have fired
16+
*/
17+
const runInBothOrders = (config, test) => {
18+
// The code snippet that is run asynchronously.
19+
const snippet = () => {
20+
window.dataLayer = window.dataLayer || [];
21+
measure = function() {
22+
dataLayer.push(arguments);
23+
};
24+
config();
25+
};
26+
27+
// Test when the snippet fires first. Does not work now.
28+
snippet();
29+
setup();
30+
test();
31+
32+
window.dataLayer = undefined;
33+
// Test when the setup function fires first.
34+
setup();
35+
snippet();
36+
test();
37+
};
38+
39+
describe(`The behavior of the setup function of measurement library`, () => {
40+
41+
beforeEach(() => {
42+
storageInterface = jasmine.createSpyObj('storageInterface',
43+
['get', 'set', 'config']);
44+
eventProcessor = jasmine.createSpyObj('eventProcessor',
45+
['shouldPersist', 'processEvent', 'config']);
46+
});
47+
48+
describe('the state immediately after calling config', () => {
49+
it('does not call any eventProcessor functions when initially configured',
50+
() => {
51+
runInBothOrders(
52+
/* config= */
53+
() => measure('config', eventProcessor, storageInterface),
54+
/* test= */ () => {
55+
expect(eventProcessor.shouldPersist).not.toHaveBeenCalled();
56+
expect(eventProcessor.processEvent).not.toHaveBeenCalled();
57+
expect(eventProcessor.config).not.toHaveBeenCalled();
58+
});
59+
});
60+
61+
it('does not call any storageInterface functions when initially configured',
62+
() => {
63+
runInBothOrders(
64+
/* config= */
65+
() => measure('config', eventProcessor, storageInterface),
66+
/* test= */ () => {
67+
expect(storageInterface.get).not.toHaveBeenCalled();
68+
expect(storageInterface.set).not.toHaveBeenCalled();
69+
expect(storageInterface.config).not.toHaveBeenCalled();
70+
});
71+
});
72+
});
73+
});

test/example_test.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)