Skip to content

Commit

Permalink
Jest tests (#24)
Browse files Browse the repository at this point in the history
* Introduce jest and test coverage like other SDKs
* Introduce test-sdk make target and update buildkite
* lib/addons/try-identify.ts test coverage
  • Loading branch information
bmilekic authored Nov 11, 2020
1 parent 707564c commit cc18e73
Show file tree
Hide file tree
Showing 9 changed files with 9,158 additions and 2,495 deletions.
4 changes: 2 additions & 2 deletions .buildkite/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ steps:
- docker#v3.5.0:
image: "node:14.5.0-alpine3.10"
mount-buildkite-agent: false
- name: docker-build-sdk
- name: docker-build-and-test-sdk
<<: *dind
if: "!build.pull_request.draft"
commands:
- "apk add make git"
- "cat /etc/service_key/service-account-key.json | docker login -u _json_key --password-stdin https://gcr.io"
- "make build-sdk"
- "make test-sdk"
- name: docker-build-demos
<<: *dind
if: "!build.pull_request.draft"
Expand Down
1 change: 0 additions & 1 deletion .buildkite/tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ steps:
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /etc/service_key:/etc/service_key

- name: demos-publish
commands:
- "apk add make git"
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ build: build-sdk build-demos
#
.PHONY: build-sdk
build-sdk:
docker build . $(BUILD_ARGS) --target build -t optable-web-sdk:$(TAG)-build
docker build . $(BUILD_ARGS) --target run -t optable-web-sdk:$(TAG)

.PHONY: build-demos
build-demos:
docker build . $(BUILD_ARGS) $(BUILD_DEMOS_ARGS) --target run_demos -t optable-web-sdk-demos:$(TAG)

#
# Run web SDK tests
#
.PHONY: test-sdk
test-sdk: build-sdk
docker run -it optable-web-sdk:$(TAG)-build npm run test

#
# Publish web SDK and web demos container images
#
Expand Down
6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
["@babel/preset-typescript", { targets: { node: "current" } }],
],
};
56 changes: 56 additions & 0 deletions lib/addons/try-identify.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import OptableSDK from "../sdk";
import "./try-identify";

describe("tryIdentifyFromParams", () => {
var SDK = null;

beforeEach(() => {
delete window.location;
SDK = new OptableSDK({ host: "localhost", site: "test" });
SDK.identify = jest.fn();
});

function setURL(url) {
window.location = {
search: url,
};
}

test("is correct", () => {
setURL(
"http://some.domain.com/some/path?some=query&something=else&oeid=a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3&foo=bar&baz"
);
SDK.tryIdentifyFromParams();

const expected = "e:a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3";
expect(SDK.identify.mock.calls.length).toBe(1);
expect(SDK.identify.mock.calls[0][0]).toEqual(expected);
});

test("identify not called when oeid absent", () => {
setURL("http://some.domain.com/some/path?some=query&something=else");
SDK.tryIdentifyFromParams();

expect(SDK.identify.mock.calls.length).toBe(0);
});

test("identify not called when oeid not a SHA256 value", () => {
setURL(
"http://some.domain.com/some/path?some=query&something=else&oeid=AAAAAAAa665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3&foo=bar&baz"
);
SDK.tryIdentifyFromParams();

expect(SDK.identify.mock.calls.length).toBe(0);
});

test("detects oeid regardless of case", () => {
setURL(
"http://some.domain.com/some/path?some=query&something=else&oEId=A665A45920422F9D417E4867EFDC4FB8A04A1F3FFF1FA07E998E86f7f7A27AE3&foo=bar&baz"
);
SDK.tryIdentifyFromParams();

const expected = "e:a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3";
expect(SDK.identify.mock.calls.length).toBe(1);
expect(SDK.identify.mock.calls[0][0]).toEqual(expected);
});
});
16 changes: 13 additions & 3 deletions lib/addons/try-identify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ function maybeValidEID(eid: string): boolean {

OptableSDK.prototype.tryIdentifyFromParams = function () {
const qstr = new URLSearchParams(window.location.search);
const eid = qstr.get("oeid");
if (maybeValidEID(eid || "")) {
this.identify("e:" + eid);
const keys = qstr.keys();
var eid: string | null = "";

for (const key of keys) {
if (key.match(/^oeid$/i)) {
eid = qstr.get(key);
break;
}
}

eid = eid || "";
if (maybeValidEID(eid)) {
this.identify("e:" + eid.toLowerCase());
}
};
37 changes: 37 additions & 0 deletions lib/sdk.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import OptableSDK from "./sdk";

test("eid is correct", () => {
const expected = "e:a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3";

expect(OptableSDK.eid("123")).toEqual(expected);
expect(OptableSDK.eid("123 ")).toEqual(expected);
expect(OptableSDK.eid(" 123")).toEqual(expected);
expect(OptableSDK.eid(" 123 ")).toEqual(expected);
});

test("eid ignores case", () => {
const var1 = "tEsT@FooBarBaz.CoM";
const var2 = "test@foobarbaz.com";
const var3 = "TEST@FOOBARBAZ.COM";
const var4 = "TeSt@fOObARbAZ.cOm";
const eid = OptableSDK.eid(var1);

expect(eid).toEqual(OptableSDK.eid(var2));
expect(eid).toEqual(OptableSDK.eid(var3));
expect(eid).toEqual(OptableSDK.eid(var4));
});

test("cid is correct", () => {
const expected = "c:FooBarBAZ-01234#98765.!!!";

expect(expected).toEqual(OptableSDK.cid("FooBarBAZ-01234#98765.!!!"));
expect(expected).toEqual(OptableSDK.cid(" FooBarBAZ-01234#98765.!!!"));
expect(expected).toEqual(OptableSDK.cid("FooBarBAZ-01234#98765.!!! "));
expect(expected).toEqual(OptableSDK.cid(" FooBarBAZ-01234#98765.!!! "));
});

test("cid is case sensitive", () => {
const unexpected = "c:FooBarBAZ-01234#98765.!!!";

expect(OptableSDK.cid("foobarBAZ-01234#98765.!!!")).not.toEqual(unexpected);
});
Loading

0 comments on commit cc18e73

Please sign in to comment.