From a09d9f444337cc54a45bb33e5129abe8fc0be75d Mon Sep 17 00:00:00 2001 From: Wojciech Trocki Date: Mon, 29 Apr 2019 16:40:02 +0200 Subject: [PATCH] fix: top level api unit tests Kinda raising this coverage thing that people get focused on --- packages/sync/package.json | 2 ++ .../test/LocalDirectiveFilterLink.test.ts | 4 ++-- packages/sync/test/OfflineClient.ts | 24 +++++++++++++++++++ packages/sync/test/mock/NetworkState.ts | 8 +++++++ packages/sync/test/mock/Storage.ts | 12 ++++++++++ packages/sync/test/{ => mock}/TestUtils.ts | 0 packages/sync/test/{ => mock}/operations.ts | 0 7 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 packages/sync/test/OfflineClient.ts create mode 100644 packages/sync/test/mock/NetworkState.ts create mode 100644 packages/sync/test/mock/Storage.ts rename packages/sync/test/{ => mock}/TestUtils.ts (100%) rename packages/sync/test/{ => mock}/operations.ts (100%) diff --git a/packages/sync/package.json b/packages/sync/package.json index a5a5e985..3648065f 100644 --- a/packages/sync/package.json +++ b/packages/sync/package.json @@ -37,8 +37,10 @@ "@types/graphql": "14.2.0", "@types/mocha": "5.2.6", "@types/ws": "6.0.1", + "@types/fetch-mock": "^7.2.3", "chai": "4.2.0", "del": "4.1.1", + "fetch-mock": "^7.3.3", "mocha": "6.1.4", "ts-node": "8.1.0", "typescript": "3.4.5" diff --git a/packages/sync/test/LocalDirectiveFilterLink.test.ts b/packages/sync/test/LocalDirectiveFilterLink.test.ts index 4a887480..bdf9ae0b 100644 --- a/packages/sync/test/LocalDirectiveFilterLink.test.ts +++ b/packages/sync/test/LocalDirectiveFilterLink.test.ts @@ -1,6 +1,6 @@ import { expect } from "chai"; -import { requestWithMultipleDirectives, requestWithOnlineDirective } from "./operations"; -import { TestLink } from "./TestUtils"; +import { requestWithMultipleDirectives, requestWithOnlineDirective } from "./mock/operations"; +import { TestLink } from "./mock/TestUtils"; import { ApolloLink, execute } from "apollo-link"; import { LocalDirectiveFilterLink } from "../src/links/LocalDirectiveFilterLink"; import { hasDirectives } from "apollo-utilities"; diff --git a/packages/sync/test/OfflineClient.ts b/packages/sync/test/OfflineClient.ts new file mode 100644 index 00000000..8f23e4b0 --- /dev/null +++ b/packages/sync/test/OfflineClient.ts @@ -0,0 +1,24 @@ +import { createClient, OfflineClient } from "../src"; +import { expect, should } from "chai"; +import { mock } from "fetch-mock"; +import { storage } from "./mock/Storage"; +import { networkStatus } from "./mock/NetworkState"; + +const url = "http://test"; + +describe("Top level api tests", () => { + before(() => { + mock(url, 200); + }); + it("check old api", async () => { + const client = await createClient({ + httpUrl: url, storage, networkStatus}); + should().exist(client.offlineStore); + }); + + it("check new api", async () => { + const client = new OfflineClient({ httpUrl: url, storage, networkStatus }); + const initClient = await client.init(); + should().exist(initClient.offlineStore); + }); +}); diff --git a/packages/sync/test/mock/NetworkState.ts b/packages/sync/test/mock/NetworkState.ts new file mode 100644 index 00000000..0f48b4dd --- /dev/null +++ b/packages/sync/test/mock/NetworkState.ts @@ -0,0 +1,8 @@ +export const networkStatus = { + isOffline() { + return Promise.resolve(false); + }, + onStatusChangeListener() { + return; + } +}; diff --git a/packages/sync/test/mock/Storage.ts b/packages/sync/test/mock/Storage.ts new file mode 100644 index 00000000..55b4b90d --- /dev/null +++ b/packages/sync/test/mock/Storage.ts @@ -0,0 +1,12 @@ +export const storage = { + getItem: () => { + console.info("Called"); + return "{}"; + }, + setItem: () => { + console.info("Called"); + }, + removeItem: () => { + console.info("Called"); + } +}; diff --git a/packages/sync/test/TestUtils.ts b/packages/sync/test/mock/TestUtils.ts similarity index 100% rename from packages/sync/test/TestUtils.ts rename to packages/sync/test/mock/TestUtils.ts diff --git a/packages/sync/test/operations.ts b/packages/sync/test/mock/operations.ts similarity index 100% rename from packages/sync/test/operations.ts rename to packages/sync/test/mock/operations.ts