Skip to content

Commit

Permalink
fix(tests): Fix tests and code to get full coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Briani committed Jun 18, 2018
1 parent 2a32619 commit 508b65b
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 6 deletions.
14 changes: 14 additions & 0 deletions __mocks__/react-native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

const ReactNative = jest.genMockFromModule("react-native");

let connectionInfo = { type: "wifi" };

export const NetInfo = {
__setConnectionInfo: info => {
connectionInfo = info;
},
getConnectionInfo: () => connectionInfo
};

export default ReactNative;
20 changes: 20 additions & 0 deletions src/__mocks__/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

const utils = jest.genMockFromModule("./utils");

let environment = "WEB";

function __setEnvironment(env) {
environment = env;
}

utils.__setEnvironment = __setEnvironment;
utils.isString = (s: any) => typeof s === "string" || s instanceof String;

Object.defineProperty(utils, "environment", {
get: function() {
return environment;
}
});

module.exports = utils;
4 changes: 1 addition & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { NetInfo } from "react-native";
import fetch from "cross-fetch";
import utils from "./utils";

const isString = s => typeof s === "string" || s instanceof String;

type Options = {
urls: Array<string>,
timeout: number
Expand Down Expand Up @@ -34,7 +32,7 @@ const isOnline = async (options?: Options = defaultOptions) => {
}

let { urls, timeout } = options;
if (!Array.isArray(urls) || !urls.every(isString)) {
if (!Array.isArray(urls) || !urls.every(utils.isString)) {
urls = defaultOptions.urls;
}
if (!timeout || typeof timeout !== "number") {
Expand Down
131 changes: 129 additions & 2 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,132 @@
import { NetInfo } from "react-native";
import fetch from "cross-fetch";
import isOnline from "./index";
import utils from "./utils";

it("should be all good", () => {
expect(1 + 2).toBe(3);
jest.mock("react-native");
jest.mock("cross-fetch");
jest.mock("./utils");

const defaultOptions = {
urls: ["//1.1.1.1"],
timeout: 3000
};

describe("WEB", () => {
beforeEach(() => {
jest.clearAllMocks();
utils.__setEnvironment("WEB");
});

test("calls cross-fetch once by default", async done => {
try {
fetch.mockResolvedValue(true);
const online = await isOnline();
expect(fetch.mock.calls.length).toBe(1);
expect(fetch.mock.calls[0][0]).toBe(defaultOptions.urls[0]);
expect(online).toBe(true);
done();
} catch (e) {
done.fail(e);
}
});

test("calls cross-fetch three times for three urls", async done => {
try {
fetch.mockResolvedValue(true);
await isOnline({
urls: ["//1.1.1.1", "https://www.apple.com", "BAD URI"]
});
expect(fetch.mock.calls.length).toBe(3);
done();
} catch (e) {
done.fail(e);
}
});

test("uses default options when supplied malformed ones", async done => {
try {
fetch.mockResolvedValue(true);
await isOnline({
urls: ["//1.1.1.1", "https://www.apple.com", 1234567890],
timeout: "3000"
});
expect(fetch.mock.calls.length).toBe(1);
expect(fetch.mock.calls[0][0]).toBe(defaultOptions.urls[0]);
await isOnline({
urls: "//1.1.1.1",
timeout: 3000
});
expect(fetch.mock.calls.length).toBe(2);
expect(fetch.mock.calls[1][0]).toBe(defaultOptions.urls[0]);
done();
} catch (e) {
done.fail(e);
}
});

test("returns offline when every url is rejected", async done => {
try {
fetch.mockResolvedValue(Promise.reject());
const online = await isOnline({
urls: ["//1.1.1.1", "https://www.apple.com", "1234567890"],
timeout: 3000
});
expect(fetch.mock.calls.length).toBe(3);
expect(online).toBe(false);
done();
} catch (e) {
done.fail(e);
}
});
});

const getConnectionInfoSpy = jest.spyOn(NetInfo, "getConnectionInfo");
describe("REACT-NATIVE", () => {
beforeEach(() => {
jest.clearAllMocks();
utils.__setEnvironment("REACT-NATIVE");
});

test("calls NetInfo once", async done => {
try {
NetInfo.__setConnectionInfo({ type: "wifi" });
const online = await isOnline();
expect(getConnectionInfoSpy.mock.calls.length).toBe(1);
expect(online).toBe(true);
done();
} catch (e) {
done.fail(e);
}
});

test("returns correct connection status", async done => {
try {
let online = false;
const getConnectionInfoSpy = jest.spyOn(NetInfo, "getConnectionInfo");
NetInfo.__setConnectionInfo({ type: "none" });
online = await isOnline();
expect(getConnectionInfoSpy.mock.calls.length).toBe(1);
expect(online).toBe(false);
NetInfo.__setConnectionInfo({ type: "wifi" });
online = await isOnline();
expect(getConnectionInfoSpy.mock.calls.length).toBe(2);
expect(online).toBe(true);
NetInfo.__setConnectionInfo({ type: "cellular" });
online = await isOnline();
expect(getConnectionInfoSpy.mock.calls.length).toBe(3);
expect(online).toBe(true);
NetInfo.__setConnectionInfo({ type: "unknown" });
online = await isOnline();
expect(getConnectionInfoSpy.mock.calls.length).toBe(4);
expect(online).toBe(false);
NetInfo.__setConnectionInfo({ type: "rubbish" });
online = await isOnline();
expect(getConnectionInfoSpy.mock.calls.length).toBe(5);
expect(online).toBe(false);
done();
} catch (e) {
done.fail(e);
}
});
});
3 changes: 2 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export default {
} else {
return "NODE";
}
}
},
isString: (s: any) => typeof s === "string" || s instanceof String
};

0 comments on commit 508b65b

Please sign in to comment.