Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests #5

Merged
merged 4 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
language: node_js
node_js:
- 10
- 10
after_success: 'yarn coverage'
23 changes: 14 additions & 9 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
module.exports = {
transform: {
"^.+\\.tsx?$": "ts-jest"
},
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
collectCoverage: true,
moduleNameMapper: {
"@/(.*)": "src/$1"
}
transform: {
"^.+\\.tsx?$": "ts-jest"
},
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
collectCoverage: true,
collectCoverageFrom: [
"src/**/*.ts",
"!**/*.d.ts",
"!**/node_modules/**",
],
moduleNameMapper: {
"@/(.*)": "src/$1"
}
};
89 changes: 47 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
{
"name": "vue-tag-manager",
"version": "0.1.1",
"description": "Easy to use Google Tag Manager implementation for Vue",
"keywords": [
"vue",
"tag manager",
"google tag manager",
"analytics",
"gtm"
],
"main": "lib/index.cjs.js",
"module": "lib/index.esm.js",
"unpkg": "lib/index.js",
"jsdelivr": "lib/index.js",
"types": "lib/index.d.ts",
"sideEffects": false,
"repository": "http://github.com:cajames/vue-tag-manager",
"author": "Chris James <chris@webglowit.net>",
"license": "MIT",
"scripts": {
"dev": "rollup -c -w",
"prepare": "NODE_ENV=production rollup -c",
"test": "jest",
"version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md",
"bump": "np"
},
"files": [
"lib",
"types",
"src",
"scripts"
],
"devDependencies": {
"@types/jest": "^23.3.0",
"conventional-changelog-cli": "^2.0.1",
"jest": "^23.4.1",
"np": "^3.0.4",
"rollup": "^0.63.5",
"rollup-plugin-typescript2": "^0.16.1",
"ts-jest": "^23.0.1",
"typescript": "^3.0.1"
}
"name": "vue-tag-manager",
"version": "0.1.1",
"description": "Easy to use Google Tag Manager implementation for Vue",
"keywords": [
"vue",
"tag manager",
"google tag manager",
"analytics",
"gtm"
],
"main": "lib/index.cjs.js",
"module": "lib/index.esm.js",
"unpkg": "lib/index.js",
"jsdelivr": "lib/index.js",
"types": "lib/index.d.ts",
"sideEffects": false,
"repository": {
"type": "git",
"url": "git+https://github.com/cajames/vue-tag-manager.git"
},
"author": "Chris James <chris@webglowit.net>",
"license": "MIT",
"scripts": {
"dev": "rollup -c -w",
"prepare": "NODE_ENV=production rollup -c",
"test": "jest",
"version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md",
"bump": "np",
"coverage": "cat ./coverage/lcov.info | coveralls"
},
"files": [
"lib",
"types",
"src",
"scripts"
],
"devDependencies": {
"@types/jest": "^23.3.0",
"conventional-changelog-cli": "^2.0.1",
"coveralls": "^3.0.2",
"jest": "^23.4.1",
"np": "^3.0.4",
"rollup": "^0.63.5",
"rollup-plugin-typescript2": "^0.16.1",
"ts-jest": "^23.0.1",
"typescript": "^3.0.1"
}
}
1 change: 0 additions & 1 deletion src/__tests__/dom-injector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ declare const global: any;
import * as injector from "../dom-injector";

describe("dom-injector", () => {
const originalDocument = document;
beforeEach(() => {
jest.resetAllMocks();
});
Expand Down
33 changes: 30 additions & 3 deletions src/__tests__/native-entry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@

import * as native from "../native-entry";

describe("NativeEntry", () => {

it("should provide an initialize function", () => {
expect(native.initialize).toBeDefined();
expect(native.initialize).toBeInstanceOf(Function);
});

describe("#initialize", () => {
xit("should only warn when an error is raised", () => {});
xit("should install GTM once when called multiple times", () => {});
xit("should initialize `TagManager` in the global namespace", () => {});

afterEach(() => {
delete (global as any).TagManager
delete (global as any).vgtmInstalled
})

it("should only warn when an error is raised", () => {
expect(() => native.initialize({ gtmId: null })).not.toThrowError()
});

it("should initialize `TagManager` in the global namespace by default", () => {
native.initialize({ gtmId: '1234' })
expect((global as any).TagManager).toBeDefined()
});

it("should initialize the TagManager in the global namespace when variable defined", () => {});

it("should install GTM once when called multiple times", () => {

native.initialize({ gtmId: "123" })

expect((global as any).TagManager).toBeDefined()
delete (global as any).TagManager

native.initialize({ gtmId: "1234" })
expect((global as any).TagManager).not.toBeDefined()
});

});
});
10 changes: 7 additions & 3 deletions src/__tests__/tag-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ describe("TagManager", () => {
});

it("should default the data layer with `gtm.js` event and `gtm.start`", () => {

const nowTime = new Date().getTime();
// TODO: This shows knowledge of implementation. Should clean this up
const spy = jest.spyOn(Date.prototype, 'getTime').mockImplementation(() => nowTime)

const gtm = new TagManager({ gtmId: "GTM-123" });
const script = gtm.getDataLayerScriptContent();

Expand All @@ -94,9 +98,9 @@ describe("TagManager", () => {
};
eval(script);
expect(global.dataLayer.length).toBe(1);
expect(global.dataLayer[0]).toHaveProperty("event", "gtm.js");
expect(global.dataLayer[0]).toHaveProperty(["gtm.start"]);
expect(global.dataLayer[0]["gtm.start"]).toBeCloseTo(nowTime);
expect(global.dataLayer[0]).toMatchObject(expectedValue)

spy.mockClear()
});

it("should use dataLayerName property for the data layer if provided", () => {
Expand Down
19 changes: 8 additions & 11 deletions src/native-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ export interface NativeOptions extends TagManagerOptions {
* Variable name of Tag Manager injected into the DOM.
* Defaults to `TagManager`
*/
TagManagerVariableName?: string
TagManagerVariableName?: string;
}


let vgtmInstalled = false;
(window as any).vgtmInstalled = false;

const initialize = (options: NativeOptions) => {
if (vgtmInstalled) return;

try {
if ((window as any).vgtmInstalled) return;

try {
const tagManager = new TagManager(options);
injectScriptTagIntoHead(
getScriptTagWithContent(tagManager.getDataLayerScriptContent())
Expand All @@ -30,15 +29,13 @@ const initialize = (options: NativeOptions) => {
getScriptTagWithSrc(tagManager.getScriptUrl(), true)
);

const variableName = options.TagManagerVariableName || 'TagManager';
(window as any)[variableName] = tagManager

vgtmInstalled = true
const variableName = options.TagManagerVariableName || "TagManager";
(window as any)[variableName] = tagManager;

(window as any).vgtmInstalled = true;
} catch (error) {
warn(error)
warn(error);
}

};

export { initialize };
6 changes: 2 additions & 4 deletions src/tag-manager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

export interface TagManagerOptions {

/** GTM ID */
gtmId: string;

/** Query parameters to be added to the script URL, such as
* `gtm_preview` any `gtm_id` for environment switching. Defaults to
/** Query parameters to be added to the script URL, such as
* `gtm_preview` any `gtm_id` for environment switching. Defaults to
* empty object.
*/
queryParams?: object;
Expand Down
27 changes: 25 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,17 @@ core-util-is@1.0.2, core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"

coveralls@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/coveralls/-/coveralls-3.0.2.tgz#f5a0bcd90ca4e64e088b710fa8dda640aea4884f"
dependencies:
growl "~> 1.10.0"
js-yaml "^3.11.0"
lcov-parse "^0.0.10"
log-driver "^1.2.7"
minimist "^1.2.0"
request "^2.85.0"

cpx@^1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/cpx/-/cpx-1.5.0.tgz#185be018511d87270dedccc293171e37655ab88f"
Expand Down Expand Up @@ -1614,6 +1625,10 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6:
version "4.1.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"

"growl@~> 1.10.0":
version "1.10.5"
resolved "https://registry.yarnpkg.com/growl/-/growl-1.10.5.tgz#f2735dc2283674fa67478b10181059355c369e5e"

growly@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
Expand Down Expand Up @@ -2455,7 +2470,7 @@ js-tokens@^3.0.0, js-tokens@^3.0.2:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"

js-yaml@^3.7.0:
js-yaml@^3.11.0, js-yaml@^3.7.0:
version "3.12.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.12.0.tgz#eaed656ec8344f10f527c6bfa1b6e2244de167d1"
dependencies:
Expand Down Expand Up @@ -2584,6 +2599,10 @@ lcid@^1.0.0:
dependencies:
invert-kv "^1.0.0"

lcov-parse@^0.0.10:
version "0.0.10"
resolved "https://registry.yarnpkg.com/lcov-parse/-/lcov-parse-0.0.10.tgz#1b0b8ff9ac9c7889250582b70b71315d9da6d9a3"

left-pad@^1.2.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e"
Expand Down Expand Up @@ -2712,6 +2731,10 @@ lodash@^4.13.1, lodash@^4.17.10, lodash@^4.17.4, lodash@^4.2.1, lodash@^4.3.0:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"

log-driver@^1.2.7:
version "1.2.7"
resolved "https://registry.yarnpkg.com/log-driver/-/log-driver-1.2.7.tgz#63b95021f0702fedfa2c9bb0a24e7797d71871d8"

log-symbols@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-1.0.2.tgz#376ff7b58ea3086a0f09facc74617eca501e1a18"
Expand Down Expand Up @@ -3605,7 +3628,7 @@ request-promise-native@^1.0.5:
stealthy-require "^1.1.0"
tough-cookie ">=2.3.3"

request@^2.83.0:
request@^2.83.0, request@^2.85.0:
version "2.87.0"
resolved "https://registry.yarnpkg.com/request/-/request-2.87.0.tgz#32f00235cd08d482b4d0d68db93a829c0ed5756e"
dependencies:
Expand Down