Skip to content

Commit

Permalink
fix: add test coverage + circleci
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejonas committed May 14, 2019
1 parent d677e0b commit e0ffac0
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 5 deletions.
89 changes: 89 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
version: 2

aliases:
# -------------------------
# ALIASES: Caches
# -------------------------
- &restore-deps-cache
key: deps-cache-{{ checksum "package-lock.json" }}

- &save-deps-cache
key: deps-cache-{{ checksum "package-lock.json" }}
paths:
- ~/client-js/node_modules

# -------------------------
# ALIASES: Branch Filters
# -------------------------
- &filter-only-master
branches:
only: master
- &filter-only-semantic-pr
branches:
only: /^(pull|dependabot|fix|feat)\/.*$/

defaults: &defaults
working_directory: ~/client-js

jobs:
test:
<<: *defaults
docker:
- image: circleci/node:10
steps:
- checkout
- restore_cache: *restore-deps-cache
- run: npm install
- run: npm install codecov
- run: npm test
- run: ./node_modules/.bin/codecov
- save_cache: *save-deps-cache

build:
<<: *defaults
docker:
- image: circleci/node:10
steps:
- checkout
- restore_cache: *restore-deps-cache
- run: npm install
- run: npm run build
- save_cache: *save-deps-cache

release:
<<: *defaults
docker:
- image: circleci/node:10
steps:
- checkout
- restore_cache: *restore-deps-cache
- run: npm install
- run: npm run build
- run: npx semantic-release
- save_cache: *save-deps-cache

workflows:
version: 2
analysis:
jobs:
- test:
filters: *filter-only-semantic-pr
- build:
filters: *filter-only-semantic-pr

release:
jobs:
- test:
filters: *filter-only-master
- build:
filters: *filter-only-master
- hold:
filters: *filter-only-master
type: approval
requires:
- test
- build
- release:
filters: *filter-only-master
requires:
- hold
14 changes: 14 additions & 0 deletions src/RequestManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ describe("client-js", () => {
return c.connect();
});

it("can close", () => {
const transport = new EventEmitterTransport("foo://unique-uri");
const c = new RequestManager([transport]);
c.close();
});

it("can send a request", (done) => {
const transport = new EventEmitterTransport("foo://unique-uri");
const c = new RequestManager([transport]);
Expand All @@ -33,4 +39,12 @@ describe("client-js", () => {
c.request("foo", []);
});

it("can handle onData", () => {
const transport = new EventEmitterTransport("foo://unique-uri");
const c = new RequestManager([transport]);
const data = "{\"result\": \"bar\", \"id\": 1}";
c.request("foo", []);
c.onData(data);
});

});
6 changes: 4 additions & 2 deletions src/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ class RequestManager {
return;
}
// call request callback for id
this.requests[parsedData.id](parsedData);
delete this.requests[parsedData.id];
if (this.requests[parsedData.id]) {
this.requests[parsedData.id](parsedData);
delete this.requests[parsedData.id];
}
}
public async request(method: string, params: any): Promise<any> {
await this.connectPromise;
Expand Down
10 changes: 10 additions & 0 deletions src/__mocks__/isomorphic-fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const Fetch = (url: string, options: any): Promise<any> => {
const resultPromise = {
text: () => {
return Promise.resolve(options.body);
},
};
return Promise.resolve(resultPromise);
};

export default Fetch;
28 changes: 28 additions & 0 deletions src/__mocks__/isomorphic-ws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class WebSocket {
private callbacks: any;
constructor(uri: string, props: any) {
this.callbacks = {};
}
public addEventListener(eventName: string, callback: any) {
this.callbacks[eventName] = callback;
if (eventName === "open") {
setTimeout(() => {
callback();
}, 10);
}
}
public removeEventListener(eventName: string, callback: any) {
delete this.callbacks[eventName];
}
public send(data: any) {
Object.entries(this.callbacks).forEach(([eventName, callback]: [string, any]) => {
if (eventName === "message") {
callback({data});
}
});
}
public close() {
this.callbacks = {};
}
}
export default WebSocket;
21 changes: 21 additions & 0 deletions src/transports/HTTPTransport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import HTTPTransport from "./HTTPTransport";

describe("HTTPTransport", () => {
it("can connect", () => {
const wst = new HTTPTransport("http://localhost:8545");
return wst.connect();
});
it("can close", () => {
const wst = new HTTPTransport("http://localhost:8545");
wst.close();
});
it("can send and receive data", (done) => {
const wst = new HTTPTransport("http://localhost:8545");
wst.onData((data: any) => {
const d = JSON.parse(data);
expect(d.foo).toEqual("bar");
done();
});
wst.sendData(JSON.stringify({foo: "bar"}));
});
});
21 changes: 21 additions & 0 deletions src/transports/WebSocketTransport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import WebSocketTransport from "./WebSocketTransport";

describe("WebSocketTransport", () => {
it("can connect", () => {
const wst = new WebSocketTransport("http://localhost:8545");
return wst.connect();
});
it("can close", () => {
const wst = new WebSocketTransport("http://localhost:8545");
wst.close();
});
it("can send and receive data", (done) => {
const wst = new WebSocketTransport("http://localhost:8545");
wst.onData((data: any) => {
const d = JSON.parse(data);
expect(d.foo).toEqual("bar");
done();
});
wst.sendData(JSON.stringify({foo: "bar"}));
});
});
6 changes: 3 additions & 3 deletions src/transports/WebSocketTransport.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import WebSocket from "isomorphic-ws";
import WS from "isomorphic-ws";
import ITransport from "./Transport";

class WebSocketTransport implements ITransport {
public connection: WebSocket;
public connection: WS;
constructor(uri: string) {
this.connection = new WebSocket(uri);
this.connection = new WS(uri);
}
public connect(): Promise<any> {
return new Promise((resolve, reject) => {
Expand Down

0 comments on commit e0ffac0

Please sign in to comment.