Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

wildcard-mock-link

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wildcard-mock-link - npm Package Compare versions

Comparing version 1.0.13 to 2.0.0-rc.1

5

changelog.md
# Changelog
- 2021/04/26 - 2.0.0
- Migrate to apollo client 3
- Remove `addWildcardMockedResponse`, now `addMockedResponse` tests if the mocked response contains a wildcard before deciding what to do.
- 2020/04/22 - 1.0.0
- Initial release

13

dist.es2015/index.d.ts

@@ -1,3 +0,4 @@

import { MockedResponse } from '@apollo/react-testing';
import { Operation, FetchResult, Observable, GraphQLRequest, ApolloLink } from 'apollo-link';
/// <reference types="zen-observable" />
import { Operation, FetchResult, Observable, GraphQLRequest, ApolloLink } from '@apollo/client';
import { MockedResponse } from '@apollo/client/testing';
import { DocumentNode } from 'graphql';

@@ -15,2 +16,3 @@ export { withApolloMocks, hookWrapperWithApolloMocks, HookWrapperAndLink, WildcardMockOptions, MockLinkAndElement, } from './utils';

export declare type MockedResponses = ReadonlyArray<WildcardMockedResponse | MockedResponse>;
declare type Act = (fun: () => void) => void;
export interface WildcardMockLinkOptions {

@@ -25,3 +27,2 @@ addTypename?: boolean;

}
declare type Act = (fun: () => void) => void;
/**

@@ -64,8 +65,4 @@ * Extends MockLink to provide the ability to match request queries independent

closeSubscription(request: DocumentNode): void;
addMockedResponse(...responses: Array<MockedResponse | WildcardMockedResponse>): void;
/**
* Add one or more mocked responses that match any variables.
*/
addWildcardMockedResponse(...responses: WildcardMockedResponse[]): void;
addMockedResponse(...responses: MockedResponse[]): void;
/**
* Remove the wildcard mocked response for `request`.

@@ -72,0 +69,0 @@ */

@@ -10,4 +10,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

};
import { Observable, ApolloLink, } from 'apollo-link';
import { addTypenameToDocument } from 'apollo-utilities';
import { Observable, ApolloLink, } from '@apollo/client';
import { addTypenameToDocument } from '@apollo/client/utilities';
import delay from 'delay';

@@ -79,8 +79,3 @@ import stringify from 'fast-json-stable-stringify';

mockedResponses.forEach((mockedResponse) => {
if (isWildcard(mockedResponse)) {
this.addWildcardMockedResponse(mockedResponse);
}
else {
this.addMockedResponse(mockedResponse);
}
this.addMockedResponse(mockedResponse);
});

@@ -201,31 +196,28 @@ }

}
/**
* Add one or more mocked responses that match any variables.
*/
addWildcardMockedResponse(...responses) {
responses.forEach((response) => {
const mockKey = this.queryToString(response.request.query);
const storedMocks = this.wildcardMatches.get(mockKey);
const storedMock = {
result: response.result,
nMatches: response.nMatches || Number.POSITIVE_INFINITY,
delay: response.delay || 0,
};
if (storedMocks) {
storedMocks.push(storedMock);
}
else {
this.wildcardMatches.set(mockKey, [storedMock]);
}
});
}
addMockedResponse(...responses) {
responses.forEach((response) => {
const mockKey = this.queryAndVariablesToString(response.request.query, response.request.variables);
const matchesForKey = this.regularMatches.get(mockKey);
if (matchesForKey) {
matchesForKey.push(response);
if (isWildcard(response)) {
const mockKey = this.queryToString(response.request.query);
const storedMocks = this.wildcardMatches.get(mockKey);
const storedMock = {
result: response.result,
nMatches: response.nMatches || Number.POSITIVE_INFINITY,
delay: response.delay || 0,
};
if (storedMocks) {
storedMocks.push(storedMock);
}
else {
this.wildcardMatches.set(mockKey, [storedMock]);
}
}
else {
this.regularMatches.set(mockKey, [response]);
const mockKey = this.queryAndVariablesToString(response.request.query, response.request.variables);
const matchesForKey = this.regularMatches.get(mockKey);
if (matchesForKey) {
matchesForKey.push(response);
}
else {
this.regularMatches.set(mockKey, [response]);
}
}

@@ -232,0 +224,0 @@ });

@@ -10,3 +10,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

};
import { useQuery, useSubscription } from '@apollo/react-hooks';
import { useQuery, useSubscription } from '@apollo/client';
import { render, act, waitFor } from '@testing-library/react';

@@ -13,0 +13,0 @@ import { renderHook, act as actHook } from '@testing-library/react-hooks';

@@ -1,2 +0,2 @@

import { ApolloCache } from 'apollo-cache';
import { ApolloCache } from '@apollo/client/cache';
import { FC, ReactElement } from 'react';

@@ -3,0 +3,0 @@ import { WildcardMockLink, WildcardMockLinkOptions, MockedResponses } from '.';

@@ -1,2 +0,2 @@

import { MockedProvider } from '@apollo/react-testing';
import { MockedProvider } from '@apollo/client/testing';
import React from 'react';

@@ -3,0 +3,0 @@ import { WildcardMockLink } from '.';

@@ -1,3 +0,4 @@

import { MockedResponse } from '@apollo/react-testing';
import { Operation, FetchResult, Observable, GraphQLRequest, ApolloLink } from 'apollo-link';
/// <reference types="zen-observable" />
import { Operation, FetchResult, Observable, GraphQLRequest, ApolloLink } from '@apollo/client';
import { MockedResponse } from '@apollo/client/testing';
import { DocumentNode } from 'graphql';

@@ -15,2 +16,3 @@ export { withApolloMocks, hookWrapperWithApolloMocks, HookWrapperAndLink, WildcardMockOptions, MockLinkAndElement, } from './utils';

export declare type MockedResponses = ReadonlyArray<WildcardMockedResponse | MockedResponse>;
declare type Act = (fun: () => void) => void;
export interface WildcardMockLinkOptions {

@@ -25,3 +27,2 @@ addTypename?: boolean;

}
declare type Act = (fun: () => void) => void;
/**

@@ -64,8 +65,4 @@ * Extends MockLink to provide the ability to match request queries independent

closeSubscription(request: DocumentNode): void;
addMockedResponse(...responses: Array<MockedResponse | WildcardMockedResponse>): void;
/**
* Add one or more mocked responses that match any variables.
*/
addWildcardMockedResponse(...responses: WildcardMockedResponse[]): void;
addMockedResponse(...responses: MockedResponse[]): void;
/**
* Remove the wildcard mocked response for `request`.

@@ -72,0 +69,0 @@ */

@@ -6,6 +6,8 @@ "use strict";

({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);

@@ -56,4 +58,5 @@ function __() { this.constructor = d; }

Object.defineProperty(exports, "__esModule", { value: true });
var apollo_link_1 = require("apollo-link");
var apollo_utilities_1 = require("apollo-utilities");
exports.WildcardMockLink = exports.MATCH_ANY_PARAMETERS = exports.hookWrapperWithApolloMocks = exports.withApolloMocks = void 0;
var client_1 = require("@apollo/client");
var utilities_1 = require("@apollo/client/utilities");
var delay_1 = __importDefault(require("delay"));

@@ -63,4 +66,4 @@ var fast_json_stable_stringify_1 = __importDefault(require("fast-json-stable-stringify"));

var utils_1 = require("./utils");
exports.withApolloMocks = utils_1.withApolloMocks;
exports.hookWrapperWithApolloMocks = utils_1.hookWrapperWithApolloMocks;
Object.defineProperty(exports, "withApolloMocks", { enumerable: true, get: function () { return utils_1.withApolloMocks; } });
Object.defineProperty(exports, "hookWrapperWithApolloMocks", { enumerable: true, get: function () { return utils_1.hookWrapperWithApolloMocks; } });
exports.MATCH_ANY_PARAMETERS = Symbol();

@@ -130,8 +133,3 @@ function isWildcard(mock) {

mockedResponses.forEach(function (mockedResponse) {
if (isWildcard(mockedResponse)) {
_this.addWildcardMockedResponse(mockedResponse);
}
else {
_this.addMockedResponse(mockedResponse);
}
_this.addMockedResponse(mockedResponse);
});

@@ -144,3 +142,3 @@ return _this;

},
enumerable: true,
enumerable: false,
configurable: true

@@ -152,3 +150,3 @@ });

},
enumerable: true,
enumerable: false,
configurable: true

@@ -160,3 +158,3 @@ });

},
enumerable: true,
enumerable: false,
configurable: true

@@ -182,3 +180,3 @@ });

}
var response = new apollo_link_1.Observable(function (observer) {
var response = new client_1.Observable(function (observer) {
forwardResponseToObserver(observer, wildcardMock, true, _this.act);

@@ -197,3 +195,3 @@ });

}
var response = new apollo_link_1.Observable(function (observer) {
var response = new client_1.Observable(function (observer) {
forwardResponseToObserver(observer, regularMock_1, true, _this.act);

@@ -210,3 +208,3 @@ });

if (wildcardMock) {
return new apollo_link_1.Observable(function (observer) {
return new client_1.Observable(function (observer) {
_this.openSubscriptions.set(_this.queryToString(op.query), observer);

@@ -221,3 +219,3 @@ forwardResponseToObserver(observer, wildcardMock, false, _this.act);

}
return new apollo_link_1.Observable(function (observer) {
return new client_1.Observable(function (observer) {
// if there are multiple subscriptions for the same request with the

@@ -274,27 +272,2 @@ // same variables, the last one will be lost

};
/**
* Add one or more mocked responses that match any variables.
*/
WildcardMockLink.prototype.addWildcardMockedResponse = function () {
var _this = this;
var responses = [];
for (var _i = 0; _i < arguments.length; _i++) {
responses[_i] = arguments[_i];
}
responses.forEach(function (response) {
var mockKey = _this.queryToString(response.request.query);
var storedMocks = _this.wildcardMatches.get(mockKey);
var storedMock = {
result: response.result,
nMatches: response.nMatches || Number.POSITIVE_INFINITY,
delay: response.delay || 0,
};
if (storedMocks) {
storedMocks.push(storedMock);
}
else {
_this.wildcardMatches.set(mockKey, [storedMock]);
}
});
};
WildcardMockLink.prototype.addMockedResponse = function () {

@@ -307,9 +280,26 @@ var _this = this;

responses.forEach(function (response) {
var mockKey = _this.queryAndVariablesToString(response.request.query, response.request.variables);
var matchesForKey = _this.regularMatches.get(mockKey);
if (matchesForKey) {
matchesForKey.push(response);
if (isWildcard(response)) {
var mockKey = _this.queryToString(response.request.query);
var storedMocks = _this.wildcardMatches.get(mockKey);
var storedMock = {
result: response.result,
nMatches: response.nMatches || Number.POSITIVE_INFINITY,
delay: response.delay || 0,
};
if (storedMocks) {
storedMocks.push(storedMock);
}
else {
_this.wildcardMatches.set(mockKey, [storedMock]);
}
}
else {
_this.regularMatches.set(mockKey, [response]);
var mockKey = _this.queryAndVariablesToString(response.request.query, response.request.variables);
var matchesForKey = _this.regularMatches.get(mockKey);
if (matchesForKey) {
matchesForKey.push(response);
}
else {
_this.regularMatches.set(mockKey, [response]);
}
}

@@ -392,3 +382,3 @@ });

WildcardMockLink.prototype.queryToString = function (query) {
return graphql_1.print(this.addTypename ? apollo_utilities_1.addTypenameToDocument(query) : query);
return graphql_1.print(this.addTypename ? utilities_1.addTypenameToDocument(query) : query);
};

@@ -436,4 +426,4 @@ WildcardMockLink.prototype.queryAndVariablesToString = function (query, variables) {

return WildcardMockLink;
}(apollo_link_1.ApolloLink));
}(client_1.ApolloLink));
exports.WildcardMockLink = WildcardMockLink;
//# sourceMappingURL=index.js.map

@@ -46,5 +46,5 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
var react_hooks_1 = require("@apollo/react-hooks");
var client_1 = require("@apollo/client");
var react_1 = require("@testing-library/react");
var react_hooks_2 = require("@testing-library/react-hooks");
var react_hooks_1 = require("@testing-library/react-hooks");
var graphql_tag_1 = __importDefault(require("graphql-tag"));

@@ -58,3 +58,3 @@ var react_2 = __importDefault(require("react"));

var useQueryOnce = function (catName) {
var data = react_hooks_1.useQuery(CAT_QUALITIES_QUERY, {
var data = client_1.useQuery(CAT_QUALITIES_QUERY, {
variables: { catName: catName },

@@ -86,7 +86,7 @@ }).data;

},
], undefined, { act: react_hooks_2.act }), wrapper = _a.wrapper, link = _a.link;
result = react_hooks_2.renderHook(function () { return useQueryOnce(variables.catName); }, {
], undefined, { act: react_hooks_1.act }), wrapper = _a.wrapper, link = _a.link;
result = react_hooks_1.renderHook(function () { return useQueryOnce(variables.catName); }, {
wrapper: wrapper,
}).result;
return [4 /*yield*/, react_hooks_2.act(function () { return link.waitForLastResponse(); })];
return [4 /*yield*/, react_hooks_1.act(function () { return link.waitForLastResponse(); })];
case 1:

@@ -123,7 +123,7 @@ _c.sent();

},
], undefined, { act: react_hooks_2.act }), wrapper = _a.wrapper, link = _a.link;
result = react_hooks_2.renderHook(function () { return useQueryOnce('tortand'); }, {
], undefined, { act: react_hooks_1.act }), wrapper = _a.wrapper, link = _a.link;
result = react_hooks_1.renderHook(function () { return useQueryOnce('tortand'); }, {
wrapper: wrapper,
}).result;
return [4 /*yield*/, react_hooks_2.act(function () { return link.waitForLastResponse(); })];
return [4 /*yield*/, react_hooks_1.act(function () { return link.waitForLastResponse(); })];
case 1:

@@ -145,6 +145,6 @@ _c.sent();

useQueryTwice = function () {
var firstData = react_hooks_1.useQuery(CAT_QUALITIES_QUERY, {
var firstData = client_1.useQuery(CAT_QUALITIES_QUERY, {
variables: { catName: 'snorf' },
}).data;
var secondData = react_hooks_1.useQuery(CAT_QUALITIES_QUERY, {
var secondData = client_1.useQuery(CAT_QUALITIES_QUERY, {
variables: { catName: 'candrle' },

@@ -168,5 +168,5 @@ }).data;

},
], undefined, { act: react_hooks_2.act }), wrapper = _a.wrapper, link = _a.link;
rendered = react_hooks_2.renderHook(useQueryTwice, { wrapper: wrapper });
return [4 /*yield*/, react_hooks_2.act(function () { return link.waitForLastResponse(); })];
], undefined, { act: react_hooks_1.act }), wrapper = _a.wrapper, link = _a.link;
rendered = react_hooks_1.renderHook(useQueryTwice, { wrapper: wrapper });
return [4 /*yield*/, react_hooks_1.act(function () { return link.waitForLastResponse(); })];
case 1:

@@ -189,3 +189,3 @@ _c.sent();

var useActsOfMischief = function (catName) {
var data = react_hooks_1.useSubscription(MISCHIEF_SUBSCRIPTION, {
var data = client_1.useSubscription(MISCHIEF_SUBSCRIPTION, {
variables: { catName: catName },

@@ -217,4 +217,4 @@ }).data;

},
], undefined, { act: react_hooks_2.act }), wrapper = _a.wrapper, link = _a.link;
rendered = react_hooks_2.renderHook(function () { return useActsOfMischief(variables.catName); }, {
], undefined, { act: react_hooks_1.act }), wrapper = _a.wrapper, link = _a.link;
rendered = react_hooks_1.renderHook(function () { return useActsOfMischief(variables.catName); }, {
wrapper: wrapper,

@@ -236,3 +236,3 @@ });

};
react_hooks_2.act(function () {
react_hooks_1.act(function () {
link.sendWildcardSubscriptionResult(MISCHIEF_SUBSCRIPTION, {

@@ -273,4 +273,4 @@ data: updateData,

},
], undefined, { act: react_hooks_2.act }), wrapper = _a.wrapper, link = _a.link;
rendered = react_hooks_2.renderHook(function () { return useActsOfMischief(variables.catName); }, {
], undefined, { act: react_hooks_1.act }), wrapper = _a.wrapper, link = _a.link;
rendered = react_hooks_1.renderHook(function () { return useActsOfMischief(variables.catName); }, {
wrapper: wrapper,

@@ -292,3 +292,3 @@ });

};
react_hooks_2.act(function () {
react_hooks_1.act(function () {
link.sendSubscriptionResult(MISCHIEF_SUBSCRIPTION, variables, {

@@ -322,4 +322,4 @@ data: updateData,

},
], undefined, { act: react_hooks_2.act }), wrapper = _a.wrapper, link = _a.link;
rendered = react_hooks_2.renderHook(function () { return useActsOfMischief(variables.catName); }, {
], undefined, { act: react_hooks_1.act }), wrapper = _a.wrapper, link = _a.link;
rendered = react_hooks_1.renderHook(function () { return useActsOfMischief(variables.catName); }, {
wrapper: wrapper,

@@ -336,3 +336,3 @@ });

};
react_hooks_2.act(function () {
react_hooks_1.act(function () {
link.sendWildcardSubscriptionResult(MISCHIEF_SUBSCRIPTION, {

@@ -366,4 +366,4 @@ data: updateData,

},
], undefined, { act: react_hooks_2.act }), wrapper = _a.wrapper, link = _a.link;
rendered = react_hooks_2.renderHook(function () { return useActsOfMischief(variables.catName); }, {
], undefined, { act: react_hooks_1.act }), wrapper = _a.wrapper, link = _a.link;
rendered = react_hooks_1.renderHook(function () { return useActsOfMischief(variables.catName); }, {
wrapper: wrapper,

@@ -380,3 +380,3 @@ });

};
react_hooks_2.act(function () {
react_hooks_1.act(function () {
link.sendSubscriptionResult(MISCHIEF_SUBSCRIPTION, variables, {

@@ -402,3 +402,3 @@ data: updateData,

useCuddles = function () {
var data = react_hooks_1.useSubscription(CUDDLES_SUBSCRIPTION).data;
var data = client_1.useSubscription(CUDDLES_SUBSCRIPTION).data;
return data;

@@ -413,4 +413,4 @@ };

},
], undefined, { act: react_hooks_2.act }), wrapper = _a.wrapper, link = _a.link;
rendered = react_hooks_2.renderHook(function () { return useCuddles(); }, {
], undefined, { act: react_hooks_1.act }), wrapper = _a.wrapper, link = _a.link;
rendered = react_hooks_1.renderHook(function () { return useCuddles(); }, {
wrapper: wrapper,

@@ -426,3 +426,3 @@ });

};
react_hooks_2.act(function () {
react_hooks_1.act(function () {
link.sendSubscriptionResult(CUDDLES_SUBSCRIPTION, undefined, {

@@ -450,10 +450,10 @@ data: updateData,

useThreeQueries = function () {
var firstData = react_hooks_1.useQuery(CAT_QUALITIES_QUERY, {
var firstData = client_1.useQuery(CAT_QUALITIES_QUERY, {
variables: { catName: catNames[0] },
}).data;
var secondData = react_hooks_1.useQuery(CAT_QUALITIES_QUERY, {
var secondData = client_1.useQuery(CAT_QUALITIES_QUERY, {
variables: { catName: catNames[1] },
skip: !firstData,
}).data;
var thirdData = react_hooks_1.useQuery(CAT_QUALITIES_QUERY, {
var thirdData = client_1.useQuery(CAT_QUALITIES_QUERY, {
variables: { catName: catNames[2] },

@@ -477,7 +477,7 @@ skip: !firstData || !secondData,

},
}); }), undefined, { act: react_hooks_2.act }), wrapper = _a.wrapper, link = _a.link;
result = react_hooks_2.renderHook(useThreeQueries, {
}); }), undefined, { act: react_hooks_1.act }), wrapper = _a.wrapper, link = _a.link;
result = react_hooks_1.renderHook(useThreeQueries, {
wrapper: wrapper,
}).result;
return [4 /*yield*/, react_hooks_2.act(function () { return link.waitForAllResponsesRecursively(); })];
return [4 /*yield*/, react_hooks_1.act(function () { return link.waitForAllResponsesRecursively(); })];
case 1:

@@ -500,3 +500,3 @@ _b.sent();

var catName = _a.catName;
var data = react_hooks_1.useQuery(CAT_QUALITIES_QUERY, { variables: { catName: catName } }).data;
var data = client_1.useQuery(CAT_QUALITIES_QUERY, { variables: { catName: catName } }).data;
if (!data) {

@@ -503,0 +503,0 @@ return react_2.default.createElement("h1", { role: "main" }, "Loading");

@@ -1,2 +0,2 @@

import { ApolloCache } from 'apollo-cache';
import { ApolloCache } from '@apollo/client/cache';
import { FC, ReactElement } from 'react';

@@ -3,0 +3,0 @@ import { WildcardMockLink, WildcardMockLinkOptions, MockedResponses } from '.';

@@ -6,3 +6,4 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
var react_testing_1 = require("@apollo/react-testing");
exports.hookWrapperWithApolloMocks = exports.withApolloMocks = void 0;
var testing_1 = require("@apollo/client/testing");
var react_1 = __importDefault(require("react"));

@@ -17,3 +18,3 @@ var _1 = require(".");

var link = new _1.WildcardMockLink(apolloMocks, options);
var element = (react_1.default.createElement(react_testing_1.MockedProvider, { addTypename: options.addTypename, link: link, cache: options.cache }, testComponentFactory()));
var element = (react_1.default.createElement(testing_1.MockedProvider, { addTypename: options.addTypename, link: link, cache: options.cache }, testComponentFactory()));
return { element: element, link: link };

@@ -32,3 +33,3 @@ }

var children = _a.children;
return (react_1.default.createElement(react_testing_1.MockedProvider, { addTypename: options.addTypename, link: link }, Wrap ? react_1.default.createElement(Wrap, null, children) : children));
return (react_1.default.createElement(testing_1.MockedProvider, { addTypename: options.addTypename, link: link }, Wrap ? react_1.default.createElement(Wrap, null, children) : children));
};

@@ -35,0 +36,0 @@ return { wrapper: wrapper, link: link };

@@ -1,3 +0,4 @@

import { MockedResponse } from '@apollo/react-testing';
import { Operation, FetchResult, Observable, GraphQLRequest, ApolloLink } from 'apollo-link';
/// <reference types="zen-observable" />
import { Operation, FetchResult, Observable, GraphQLRequest, ApolloLink } from '@apollo/client';
import { MockedResponse } from '@apollo/client/testing';
import { DocumentNode } from 'graphql';

@@ -15,2 +16,3 @@ export { withApolloMocks, hookWrapperWithApolloMocks, HookWrapperAndLink, WildcardMockOptions, MockLinkAndElement, } from './utils';

export declare type MockedResponses = ReadonlyArray<WildcardMockedResponse | MockedResponse>;
declare type Act = (fun: () => void) => void;
export interface WildcardMockLinkOptions {

@@ -25,3 +27,2 @@ addTypename?: boolean;

}
declare type Act = (fun: () => void) => void;
/**

@@ -64,8 +65,4 @@ * Extends MockLink to provide the ability to match request queries independent

closeSubscription(request: DocumentNode): void;
addMockedResponse(...responses: Array<MockedResponse | WildcardMockedResponse>): void;
/**
* Add one or more mocked responses that match any variables.
*/
addWildcardMockedResponse(...responses: WildcardMockedResponse[]): void;
addMockedResponse(...responses: MockedResponse[]): void;
/**
* Remove the wildcard mocked response for `request`.

@@ -72,0 +69,0 @@ */

@@ -5,6 +5,8 @@ var __extends = (this && this.__extends) || (function () {

({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);

@@ -51,4 +53,4 @@ function __() { this.constructor = d; }

};
import { Observable, ApolloLink, } from 'apollo-link';
import { addTypenameToDocument } from 'apollo-utilities';
import { Observable, ApolloLink, } from '@apollo/client';
import { addTypenameToDocument } from '@apollo/client/utilities';
import delay from 'delay';

@@ -122,8 +124,3 @@ import stringify from 'fast-json-stable-stringify';

mockedResponses.forEach(function (mockedResponse) {
if (isWildcard(mockedResponse)) {
_this.addWildcardMockedResponse(mockedResponse);
}
else {
_this.addMockedResponse(mockedResponse);
}
_this.addMockedResponse(mockedResponse);
});

@@ -136,3 +133,3 @@ return _this;

},
enumerable: true,
enumerable: false,
configurable: true

@@ -144,3 +141,3 @@ });

},
enumerable: true,
enumerable: false,
configurable: true

@@ -152,3 +149,3 @@ });

},
enumerable: true,
enumerable: false,
configurable: true

@@ -262,27 +259,2 @@ });

};
/**
* Add one or more mocked responses that match any variables.
*/
WildcardMockLink.prototype.addWildcardMockedResponse = function () {
var _this = this;
var responses = [];
for (var _i = 0; _i < arguments.length; _i++) {
responses[_i] = arguments[_i];
}
responses.forEach(function (response) {
var mockKey = _this.queryToString(response.request.query);
var storedMocks = _this.wildcardMatches.get(mockKey);
var storedMock = {
result: response.result,
nMatches: response.nMatches || Number.POSITIVE_INFINITY,
delay: response.delay || 0,
};
if (storedMocks) {
storedMocks.push(storedMock);
}
else {
_this.wildcardMatches.set(mockKey, [storedMock]);
}
});
};
WildcardMockLink.prototype.addMockedResponse = function () {

@@ -295,9 +267,26 @@ var _this = this;

responses.forEach(function (response) {
var mockKey = _this.queryAndVariablesToString(response.request.query, response.request.variables);
var matchesForKey = _this.regularMatches.get(mockKey);
if (matchesForKey) {
matchesForKey.push(response);
if (isWildcard(response)) {
var mockKey = _this.queryToString(response.request.query);
var storedMocks = _this.wildcardMatches.get(mockKey);
var storedMock = {
result: response.result,
nMatches: response.nMatches || Number.POSITIVE_INFINITY,
delay: response.delay || 0,
};
if (storedMocks) {
storedMocks.push(storedMock);
}
else {
_this.wildcardMatches.set(mockKey, [storedMock]);
}
}
else {
_this.regularMatches.set(mockKey, [response]);
var mockKey = _this.queryAndVariablesToString(response.request.query, response.request.variables);
var matchesForKey = _this.regularMatches.get(mockKey);
if (matchesForKey) {
matchesForKey.push(response);
}
else {
_this.regularMatches.set(mockKey, [response]);
}
}

@@ -304,0 +293,0 @@ });

@@ -41,3 +41,3 @@ var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {

};
import { useQuery, useSubscription } from '@apollo/react-hooks';
import { useQuery, useSubscription } from '@apollo/client';
import { render, act, waitFor } from '@testing-library/react';

@@ -44,0 +44,0 @@ import { renderHook, act as actHook } from '@testing-library/react-hooks';

@@ -1,2 +0,2 @@

import { ApolloCache } from 'apollo-cache';
import { ApolloCache } from '@apollo/client/cache';
import { FC, ReactElement } from 'react';

@@ -3,0 +3,0 @@ import { WildcardMockLink, WildcardMockLinkOptions, MockedResponses } from '.';

@@ -1,2 +0,2 @@

import { MockedProvider } from '@apollo/react-testing';
import { MockedProvider } from '@apollo/client/testing';
import React from 'react';

@@ -3,0 +3,0 @@ import { WildcardMockLink } from '.';

{
"name": "wildcard-mock-link",
"version": "1.0.13",
"version": "2.0.0-rc.1",
"description": "apollo client mocking",

@@ -43,42 +43,38 @@ "author": "insidewhy <github@chilon.net>",

"dependencies": {
"apollo-utilities": "^1.3.3",
"delay": "^4.3.0",
"delay": "^5.0.0",
"fast-json-stable-stringify": "^2.1.0"
},
"devDependencies": {
"@apollo/react-hooks": "3.1.5",
"@apollo/react-testing": "3.1.4",
"@testing-library/react": "10.0.4",
"@testing-library/react-hooks": "3.3.0",
"@types/jest": "25.2.3",
"@types/react": "16.9.34",
"@typescript-eslint/eslint-plugin": "2.34.0",
"@typescript-eslint/parser": "2.34.0",
"apollo-cache": "1.3.5",
"apollo-cache-inmemory": "1.6.6",
"apollo-client": "2.6.10",
"apollo-link": "1.2.14",
"concurrently": "5.2.0",
"eslint": "7.2.0",
"eslint-config-prettier": "6.11.0",
"eslint-plugin-import": "2.20.2",
"eslint-plugin-react": "7.20.0",
"eslint-plugin-react-hooks": "4.0.4",
"graphql": "15.0.0",
"graphql-tag": "2.10.3",
"husky": "4.2.5",
"jest": "26.0.1",
"prettier": "2.0.5",
"pretty-quick": "2.0.1",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-test-renderer": "16.13.1",
"@apollo/client": "3.3.15",
"@testing-library/react": "11.2.6",
"@testing-library/react-hooks": "5.1.2",
"@types/jest": "26.0.22",
"@types/react": "17.0.3",
"@typescript-eslint/eslint-plugin": "4.22.0",
"@typescript-eslint/parser": "4.22.0",
"concurrently": "6.0.2",
"eslint": "7.25.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-react": "7.23.2",
"eslint-plugin-react-hooks": "4.2.0",
"graphql": "15.5.0",
"graphql-tag": "2.11.0",
"husky": "6.0.0",
"jest": "26.6.3",
"prettier": "2.2.1",
"pretty-quick": "3.1.0",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-test-renderer": "17.0.2",
"rimraf": "3.0.2",
"typescript": "3.9.5"
"typescript": "4.2.4"
},
"peerDependencies": {
"@apollo/react-testing": "^3.1.3",
"apollo-link": "^1.2.14",
"@apollo/client": "^3.3.15",
"graphql": "^14.5.8 || ^15.0.0"
},
"resolutions": {
"@types/react": "17.0.3"
}
}

@@ -93,3 +93,3 @@ # wildcard-mock-link

```typescript
import { useQuery } from '@apollo/react-hooks'
import { useQuery } from '@apollo/client'
import { render, act } from '@testing-library/react'

@@ -146,3 +146,3 @@ import gql from 'graphql-tag'

```typescript
import { useQuery } from '@apollo/react-hooks'
import { useQuery } from '@apollo/client'
import { renderHook, act as actHook } from '@testing-library/react-hooks'

@@ -197,3 +197,3 @@ import gql from 'graphql-tag'

```typescript
import { useQuery } from '@apollo/react-hooks'
import { useQuery } from '@apollo/client'
import { waitFor } from '@testing-library/react'

@@ -200,0 +200,0 @@ import { renderHook, act as actHook } from '@testing-library/react-hooks'

@@ -1,2 +0,1 @@

import { MockedResponse } from '@apollo/react-testing'
import {

@@ -8,4 +7,5 @@ Operation,

ApolloLink,
} from 'apollo-link'
import { addTypenameToDocument } from 'apollo-utilities'
} from '@apollo/client'
import { MockedResponse } from '@apollo/client/testing'
import { addTypenameToDocument } from '@apollo/client/utilities'
import delay from 'delay'

@@ -50,2 +50,4 @@ import stringify from 'fast-json-stable-stringify'

type Act = (fun: () => void) => void
export interface WildcardMockLinkOptions {

@@ -115,4 +117,2 @@ addTypename?: boolean

type Act = (fun: () => void) => void
const callFunction: Act = (fun: () => void) => {

@@ -167,7 +167,3 @@ fun()

mockedResponses.forEach((mockedResponse) => {
if (isWildcard(mockedResponse)) {
this.addWildcardMockedResponse(mockedResponse)
} else {
this.addMockedResponse(mockedResponse)
}
this.addMockedResponse(mockedResponse)
})

@@ -304,18 +300,30 @@ }

/**
* Add one or more mocked responses that match any variables.
*/
addWildcardMockedResponse(...responses: WildcardMockedResponse[]): void {
addMockedResponse(
...responses: Array<MockedResponse | WildcardMockedResponse>
): void {
responses.forEach((response) => {
const mockKey = this.queryToString(response.request.query)
const storedMocks = this.wildcardMatches.get(mockKey)
const storedMock = {
result: response.result,
nMatches: response.nMatches || Number.POSITIVE_INFINITY,
delay: response.delay || 0,
}
if (storedMocks) {
storedMocks.push(storedMock)
if (isWildcard(response)) {
const mockKey = this.queryToString(response.request.query)
const storedMocks = this.wildcardMatches.get(mockKey)
const storedMock = {
result: response.result,
nMatches: response.nMatches || Number.POSITIVE_INFINITY,
delay: response.delay || 0,
}
if (storedMocks) {
storedMocks.push(storedMock)
} else {
this.wildcardMatches.set(mockKey, [storedMock])
}
} else {
this.wildcardMatches.set(mockKey, [storedMock])
const mockKey = this.queryAndVariablesToString(
response.request.query,
response.request.variables,
)
const matchesForKey = this.regularMatches.get(mockKey)
if (matchesForKey) {
matchesForKey.push(response)
} else {
this.regularMatches.set(mockKey, [response])
}
}

@@ -325,17 +333,2 @@ })

addMockedResponse(...responses: MockedResponse[]): void {
responses.forEach((response) => {
const mockKey = this.queryAndVariablesToString(
response.request.query,
response.request.variables,
)
const matchesForKey = this.regularMatches.get(mockKey)
if (matchesForKey) {
matchesForKey.push(response)
} else {
this.regularMatches.set(mockKey, [response])
}
})
}
/**

@@ -342,0 +335,0 @@ * Remove the wildcard mocked response for `request`.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc