Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marceleq27 committed Apr 24, 2020
1 parent f41fa6b commit af9d7cb
Show file tree
Hide file tree
Showing 8 changed files with 4,706 additions and 16 deletions.
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
roots: ["<rootDir>/src"],
testMatch: ["**/__tests__/**/*.+(ts|tsx|js)", "**/?(*.)+(spec|test).+(ts|tsx|js)"],
transform: {
"^.+\\.(ts|tsx)$": "ts-jest",
},
};
4,656 changes: 4,656 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"types": ".src/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "tsc -w",
"build": "tsc"
"build": "tsc",
"test": "jest"
},
"repository": {
"type": "git",
Expand All @@ -21,6 +20,9 @@
},
"homepage": "https://github.com/livesession/livesession-sdk#readme",
"devDependencies": {
"@types/jest": "^25.2.1",
"jest": "^25.4.0",
"ts-jest": "^25.4.0",
"typescript": "^3.8.3"
},
"dependencies": {}
Expand Down
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const apiCall = (name: string, ...args: (object | boolean | string | void)[]) =>
return window.__ls(name, ...args);
};

const api = {
init: (trackID: string, options: object[]) => apiCall("init", trackID, options),
const api: any = {
init: (trackID: string, options?: object) => apiCall("init", trackID, options),
getSessionURL: (callback: void) => apiCall("getSessionURL", callback),
identify: (data: object) => apiCall("identify", data),
invalidateSession: () => apiCall("invalidateSession"),
Expand Down
24 changes: 24 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import livesession from "./index";
import api from "./api";

test("Call any other function before init", () => {
expect(() => {
livesession.setOptions({ rootHostname: "test" });
throw new Error();
}).toThrow();
});

it("should call init method", () => {
const initScript = jest.fn((trackID: string, options?: object) => {
livesession.init("test");
return api.init(trackID, options);
});
initScript("jkjfdsfds");
expect(initScript).toBeCalledTimes(1);
});

test("Check if script is added", () => {
const initScript = jest.fn(() => window.__ls && console.warn("LiveSession already inited (skipping init() call)"));
initScript();
expect(initScript).toHaveReturned();
});
12 changes: 6 additions & 6 deletions index.ts → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import snippet from "./src/snippet";
import api from "./src/api";
import snippet from "./snippet";
import api from "./api";

declare global {
interface Window {
Expand All @@ -11,22 +11,22 @@ const isLoaded = () => window.__ls;
const safeCall = (name: string) => {
return (...args: object[]) => {
if (!isLoaded()) {
throw Error("LiveSession is not loaded. Call init() before calling other API functions");
throw new Error("LiveSession is not loaded. Call init() before calling other API functions");
}
if (!api[name]) {
throw Error(`method "${name}" doesn't exist`);
throw new Error(`method "${name}" doesn't exist`);
}
return api[name](...args);
};
};

const _init = (trackID: string, options: object[]) => {
const _init = (trackID: string, options?: object) => {
if (isLoaded()) {
console.warn("LiveSession already inited (skipping init() call)");
return;
}
if (!trackID) {
throw Error(`trackID is required`);
throw new Error(`trackID is required`);
}
snippet();
return api.init(trackID, options);
Expand Down
4 changes: 2 additions & 2 deletions src/snippet.js → src/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const snippet = (
if (w.__ls) {
throw new Error("LiveSession script already added");
}
const f = (w.__ls = function () {
const f: any = (w.__ls = function () {
f.push ? f.push.apply(f, arguments) : f.store.push(arguments);
});
if (!w.__ls) w.__ls = f;
f.store = [];
f.v = SDK_VERSION;

const ls = d.createElement(t);
const ls = <HTMLScriptElement>d.createElement(t);
ls.async = true;
ls.src = u;
ls.charset = "utf-8";
Expand Down
7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"compilerOptions": {
"target": "es5",
"target": "es2015",
"module": "commonjs",
"outDir": "./dist"
"outDir": "dist"
},
"include": ["./src/**/*", "index.ts", "src/index.d.ts"]
"include": ["./src/**/*", "src/index.ts", "src/index.d.ts"],
"exclude": ["node_modules", "**/*.spec.ts"]
}

0 comments on commit af9d7cb

Please sign in to comment.