-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tests): Fix tests and code to get full coverage
- Loading branch information
Davide Briani
committed
Jun 18, 2018
1 parent
2a32619
commit 508b65b
Showing
5 changed files
with
166 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters