Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/adding tests #15

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
dist
coverage

28 changes: 28 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const path = require("path");
module.exports = {
rootDir: path.join(__dirname, "./"),
preset: "ts-jest",
testEnvironment: "node",

collectCoverage: false,
collectCoverageFrom: ["<rootDir>/src/**/*.ts"],
coverageDirectory: "coverage",
coveragePathIgnorePatterns: ["/node_modules/"],
coverageReporters: ["json", "lcov", "text", "clover"],
coverageThreshold: {
global: {
branches: 60,
functions: 60,
lines: 60,
statements: -10
}
},
testPathIgnorePatterns: ["\\.snap$", "<rootDir>/node_modules/", "dist"],
setupFiles: ["<rootDir>/setupJest.js"]

// globals: {
// "ts-jest": {
// diagnostics: false
// }
// }
};
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"author": "James Kusachi",
"license": "MIT",
"dependencies": {
"@types/jest": "^24.0.12",
"apollo-link-http-common": "^0.2.8",
"apollo-utilities": "^1.0.27",
"extract-files": "^5.0.0",
Expand All @@ -18,6 +19,8 @@
"scripts": {
"lint": "tslint -c tslint.json -p tsconfig.json",
"build": "tsc",
"test": "jest",
"coverage": "jest --coverage",
"jsdoc": "jsdoc-md",
"prepublish": "npm run build"
},
Expand All @@ -27,9 +30,16 @@
"react-apollo": "^2"
},
"devDependencies": {
"@types/jest": "^24.0.20",
"@types/redux-mock-store": "^1.0.1",
"fetch-mock": "^8.0.0-alpha.11",
"graphql": "^14",
"graphql-tag": "^2",
"jest-fetch-mock": "^2.1.2",
"jsdoc-md": "^1.7.0",
"react-apollo": "^2"
"react-apollo": "^2",
"jest": "^24.9.0",
"redux-mock-store": "^1.5.3",
"ts-jest": "^24.1.0"
}
}
}
2 changes: 2 additions & 0 deletions setupJest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const fetch = require("jest-fetch-mock");
jest.setMock("node-fetch", fetch);
265 changes: 265 additions & 0 deletions src/__tests__/redux-offline-apollo-link.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
import { execute } from "apollo-link";
import gql from "graphql-tag";
import configureStore from "redux-mock-store";
import { reduxOfflineApolloLink } from "..";

const middlewares = [];
const store = configureStore(middlewares)({
offline: {
online: true
}
});

const sampleQuery = gql`
query SampleQuery {
stub {
id
}
}
`;

// const sampleMutation = gql`
// mutation SampleMutation {
// stub {
// id
// }
// }
// `;

// const makeCallback = (done, body) => {
// return (...args) => {
// try {
// body(...args);
// done();
// } catch (error) {
// done.fail(error);
// }
// };
// };

describe("#reduxOfflineApolloLink", () => {
const fetchMock = jest.fn();

beforeEach(() => {
fetchMock.mockClear();
});

it("Doesn't throw error on basic constructor", () => {
expect(() =>
reduxOfflineApolloLink(
{
uri: "https://www.example.com",
fetch: fetchMock
},
store
)
).not.toThrow();
});

it("constructor creates link that can call next and then complete", done => {
fetchMock.mockResolvedValueOnce({
status: 200,
text() {
return Promise.resolve(
JSON.stringify({
data: "success"
})
);
}
});
const link = reduxOfflineApolloLink(
{
uri: "https://www.example.com",
fetch: fetchMock
},
store
);

const operation = {
query: sampleQuery,
variables: {
actionType: "TEST"
}
};

const observable = execute(link, operation);

observable.subscribe({
next: jest.fn(),
error: error => expect(false),
complete: () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
done();
}
});
});

it("Throws an error is redux variables.actionType is not supplied", done => {
const link = reduxOfflineApolloLink(
{
uri: "https://www.example.com",
fetch: fetchMock
},
store
);

const operation = {
query: sampleQuery
};

expect(() => execute(link, operation)).toThrowError(
`This custom link requires you to specify an \`options.variables.actionType\`
to handle redux offline actions in the event the device is offline`
);

done();
});

describe("#Options", () => {
it("parseAndHandleHttpResponse - Called if provided in options", done => {
const mockResponse = {
data: "success"
};
fetchMock.mockResolvedValueOnce({
status: 200,
text() {
return Promise.resolve(JSON.stringify(mockResponse));
}
});

const link = reduxOfflineApolloLink(
{
uri: "https://www.example.com",
fetch: fetchMock
},
store
);

const operation = {
query: sampleQuery,
variables: {
actionType: "ACTION_TYPE",
options: {
parseAndHandleHttpResponse: jest.fn(() => mockResponse)
}
}
};

const observable = execute(link, operation);

observable.subscribe({
next: jest.fn(),
error: error => expect(false),
complete: () => {
expect(
operation.variables.options.parseAndHandleHttpResponse
).toHaveBeenCalledWith(
expect.objectContaining(operation),
mockResponse
);
done();
}
});
});

it(`globalErrorsCheck - Called if response has any errors in it, and main result throws
using default errorsCheck`, done => {
const mockResponse = {
data: "success",
errors: ["error one"]
};

fetchMock.mockResolvedValueOnce({
status: 200,
text() {
return Promise.resolve(JSON.stringify(mockResponse));
}
});

const globalErrorsCheck = jest.fn();

const link = reduxOfflineApolloLink(
{
uri: "https://www.example.com",
fetch: fetchMock,
globalErrorsCheck
},
store
);

const operation = {
query: sampleQuery,
variables: {
actionType: "ACTION_TYPE"
}
};

const observable = execute(link, operation);

observable.subscribe({
next: jest.fn(),
error: error => {
expect(globalErrorsCheck).toHaveBeenCalledWith(mockResponse);
expect(error).toEqual(mockResponse);
done();
},
complete: () => {
done();
}
});
});

// In this test, we do have an error but sometimes for GraphQL, having errors is OK and we don't want to throw
// "errorsCheck" allows you to override on a per-call basis, allowing you dedide if you should throw or continue
// If you return inside this method, the pipeline will continue on, therefore in this test we simply return the
// result, even if it has errors
it("errorsCheck - Option called if provided, and can continue if function does not throw inside", done => {
const mockResponse = {
data: "success",
errors: ["error one"]
};

fetchMock.mockResolvedValueOnce({
status: 200,
text() {
return Promise.resolve(JSON.stringify(mockResponse));
}
});

const errorsCheck = jest.fn(res => {
return res;
});

const link = reduxOfflineApolloLink(
{
uri: "https://www.example.com",
fetch: fetchMock
},
store
);

const operation = {
query: sampleQuery,
variables: {
actionType: "ACTION_TYPE",
options: {
errorsCheck
}
}
};

const observable = execute(link, operation);

observable.subscribe({
next: jest.fn(),
error: error => {
done();
},
complete: () => {
expect(errorsCheck).toHaveBeenCalledWith(mockResponse);
done();
}
});
});
});
});
Loading