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 2.0.0-rc.3 to 2.0.0-rc.4

10

changelog.md
# Changelog
- 2021/04/26 - 2.0.0
- 2021/05/11 - 2.0.0-rc.4
- Make `nMatches` default to 1.
- Allow `nMatches` to be used for non-wildcard matches.
- Allow request window for `waitForAllResponsesRecursively` to be specified as argument.
- 2021/04/30 - 2.0.0-rc.3
- Migrate to apollo client 3

@@ -9,2 +15,2 @@ - 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
- Initial release.

16

dist.es2015/index.d.ts

@@ -11,7 +11,11 @@ /// <reference types="zen-observable" />

}
export interface WildcardMockedResponse extends Omit<MockedResponse, 'request'> {
request: GraphQLRequestWithWildcard;
interface MockedResponseWithMatchCount extends MockedResponse {
/** Use Number.POSITIVE_INFINITY to allow infinite matches */
nMatches?: number;
}
export declare type MockedResponses = ReadonlyArray<WildcardMockedResponse | MockedResponse>;
declare type WildcardMock = Omit<MockedResponseWithMatchCount, 'request'>;
export interface WildcardMockedResponse extends WildcardMock {
request: GraphQLRequestWithWildcard;
}
export declare type MockedResponses = readonly WildcardMockedResponse[];
declare type Act = (fun: () => void) => void;

@@ -64,3 +68,3 @@ export interface WildcardMockLinkOptions {

closeSubscription(request: DocumentNode): void;
addMockedResponse(...responses: Array<MockedResponse | WildcardMockedResponse>): void;
addMockedResponse(...responses: MockedResponses): void;
/**

@@ -94,4 +98,6 @@ * Remove the wildcard mocked response for `request`.

* as a result of those responses being sent.
* @param requestWindow How long to wait between between requests for a new request to be
* made, by default use 0 which allows one tick.
*/
waitForAllResponsesRecursively(): Promise<void>;
waitForAllResponsesRecursively(requestWindow?: number): Promise<void>;
private queryToString;

@@ -98,0 +104,0 @@ private queryAndVariablesToString;

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

export const MATCH_ANY_PARAMETERS = Symbol();
function isWildcard(mock) {
return mock.request.variables === MATCH_ANY_PARAMETERS;
// MockedResponseWithMatchCount is a narrower type than WildcardMockedResponse so it
// has to be isNotWildcard rather than isWildcard
function isNotWildcard(mock) {
return mock.request.variables !== MATCH_ANY_PARAMETERS;
}

@@ -109,3 +111,3 @@ const getResultFromFetchResult = (result) => (typeof result === 'function' ? result() : result);

if (!wildcardMock.error && !wildcardMock.result) {
return observableWithError(Error('Must provide error or result for query/mutation mocks'));
return observableWithError(new Error('Must provide error or result for query/mutation mocks'));
}

@@ -126,3 +128,3 @@ const response = new Observable((observer) => {

else if (!regularMock.error && !regularMock.result) {
return observableWithError(Error('Must provide error or result for query/mutation mocks'));
return observableWithError(new Error('Must provide error or result for query/mutation mocks'));
}

@@ -206,3 +208,13 @@ const response = new Observable((observer) => {

responses.forEach((response) => {
if (isWildcard(response)) {
if (isNotWildcard(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]);
}
}
else {
const mockKey = this.queryToString(response.request.query);

@@ -212,3 +224,3 @@ const storedMocks = this.wildcardMatches.get(mockKey);

result: response.result,
nMatches: response.nMatches || Number.POSITIVE_INFINITY,
nMatches: response.nMatches,
delay: response.delay || 0,

@@ -223,12 +235,2 @@ };

}
else {
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]);
}
}
});

@@ -282,4 +284,6 @@ }

* as a result of those responses being sent.
* @param requestWindow How long to wait between between requests for a new request to be
* made, by default use 0 which allows one tick.
*/
waitForAllResponsesRecursively() {
waitForAllResponsesRecursively(requestWindow = 0) {
return __awaiter(this, void 0, void 0, function* () {

@@ -290,4 +294,4 @@ if (!this.pendingResponses.size) {

yield this.waitForAllResponses();
// allow code to issue new requests within the next tick
yield delay(0);
// allow code to issue new requests within the request window
yield delay(requestWindow);
yield this.waitForAllResponsesRecursively();

@@ -313,3 +317,3 @@ });

const nextMock = mocks[0];
if (--nextMock.nMatches === 0) {
if (nextMock.nMatches === undefined || --nextMock.nMatches === 0) {
mocks.shift();

@@ -325,3 +329,13 @@ if (!mocks.length) {

const mocks = this.regularMatches.get(mockKey);
return mocks === null || mocks === void 0 ? void 0 : mocks.shift();
if (!mocks) {
return undefined;
}
const nextMock = mocks[0];
if (nextMock.nMatches === undefined || --nextMock.nMatches === 0) {
mocks.shift();
if (!mocks.length) {
this.regularMatches.delete(mockKey);
}
}
return nextMock;
}

@@ -328,0 +342,0 @@ setLastResponsePromiseFromObservable(observable) {

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

const CAT_QUALITIES_QUERY = gql `
query($catName: String!) {
query ($catName: String!) {
qualities(cats: $catName) {

@@ -87,3 +87,3 @@ loveliness

}));
it('for multiple requests with the same mock', () => __awaiter(void 0, void 0, void 0, function* () {
it('for multiple requests with the same mock when using nMatches', () => __awaiter(void 0, void 0, void 0, function* () {
var _b;

@@ -112,2 +112,3 @@ const useQueryTwice = () => {

result: { data },
nMatches: 2,
},

@@ -128,3 +129,3 @@ ], undefined, { act: actHook });

const MISCHIEF_SUBSCRIPTION = gql `
subscription($catName: String!) {
subscription ($catName: String!) {
actsOfMischief(cats: $catName) {

@@ -131,0 +132,0 @@ description

@@ -11,7 +11,11 @@ /// <reference types="zen-observable" />

}
export interface WildcardMockedResponse extends Omit<MockedResponse, 'request'> {
request: GraphQLRequestWithWildcard;
interface MockedResponseWithMatchCount extends MockedResponse {
/** Use Number.POSITIVE_INFINITY to allow infinite matches */
nMatches?: number;
}
export declare type MockedResponses = ReadonlyArray<WildcardMockedResponse | MockedResponse>;
declare type WildcardMock = Omit<MockedResponseWithMatchCount, 'request'>;
export interface WildcardMockedResponse extends WildcardMock {
request: GraphQLRequestWithWildcard;
}
export declare type MockedResponses = readonly WildcardMockedResponse[];
declare type Act = (fun: () => void) => void;

@@ -64,3 +68,3 @@ export interface WildcardMockLinkOptions {

closeSubscription(request: DocumentNode): void;
addMockedResponse(...responses: Array<MockedResponse | WildcardMockedResponse>): void;
addMockedResponse(...responses: MockedResponses): void;
/**

@@ -94,4 +98,6 @@ * Remove the wildcard mocked response for `request`.

* as a result of those responses being sent.
* @param requestWindow How long to wait between between requests for a new request to be
* made, by default use 0 which allows one tick.
*/
waitForAllResponsesRecursively(): Promise<void>;
waitForAllResponsesRecursively(requestWindow?: number): Promise<void>;
private queryToString;

@@ -98,0 +104,0 @@ private queryAndVariablesToString;

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

exports.MATCH_ANY_PARAMETERS = Symbol();
function isWildcard(mock) {
return mock.request.variables === exports.MATCH_ANY_PARAMETERS;
// MockedResponseWithMatchCount is a narrower type than WildcardMockedResponse so it
// has to be isNotWildcard rather than isWildcard
function isNotWildcard(mock) {
return mock.request.variables !== exports.MATCH_ANY_PARAMETERS;
}

@@ -177,3 +179,3 @@ var getResultFromFetchResult = function (result) { return (typeof result === 'function' ? result() : result); };

if (!wildcardMock.error && !wildcardMock.result) {
return observableWithError(Error('Must provide error or result for query/mutation mocks'));
return observableWithError(new Error('Must provide error or result for query/mutation mocks'));
}

@@ -194,3 +196,3 @@ var response = new client_1.Observable(function (observer) {

else if (!regularMock_1.error && !regularMock_1.result) {
return observableWithError(Error('Must provide error or result for query/mutation mocks'));
return observableWithError(new Error('Must provide error or result for query/mutation mocks'));
}

@@ -280,3 +282,13 @@ var response = new client_1.Observable(function (observer) {

responses.forEach(function (response) {
if (isWildcard(response)) {
if (isNotWildcard(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]);
}
}
else {
var mockKey = _this.queryToString(response.request.query);

@@ -286,3 +298,3 @@ var storedMocks = _this.wildcardMatches.get(mockKey);

result: response.result,
nMatches: response.nMatches || Number.POSITIVE_INFINITY,
nMatches: response.nMatches,
delay: response.delay || 0,

@@ -297,12 +309,2 @@ };

}
else {
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]);
}
}
});

@@ -356,4 +358,7 @@ };

* as a result of those responses being sent.
* @param requestWindow How long to wait between between requests for a new request to be
* made, by default use 0 which allows one tick.
*/
WildcardMockLink.prototype.waitForAllResponsesRecursively = function () {
WildcardMockLink.prototype.waitForAllResponsesRecursively = function (requestWindow) {
if (requestWindow === void 0) { requestWindow = 0; }
return __awaiter(this, void 0, void 0, function () {

@@ -367,10 +372,10 @@ return __generator(this, function (_a) {

return [4 /*yield*/, this.waitForAllResponses()
// allow code to issue new requests within the next tick
// allow code to issue new requests within the request window
];
case 1:
_a.sent();
// allow code to issue new requests within the next tick
return [4 /*yield*/, delay_1.default(0)];
// allow code to issue new requests within the request window
return [4 /*yield*/, delay_1.default(requestWindow)];
case 2:
// allow code to issue new requests within the next tick
// allow code to issue new requests within the request window
_a.sent();

@@ -402,3 +407,3 @@ return [4 /*yield*/, this.waitForAllResponsesRecursively()];

var nextMock = mocks[0];
if (--nextMock.nMatches === 0) {
if (nextMock.nMatches === undefined || --nextMock.nMatches === 0) {
mocks.shift();

@@ -414,3 +419,13 @@ if (!mocks.length) {

var mocks = this.regularMatches.get(mockKey);
return mocks === null || mocks === void 0 ? void 0 : mocks.shift();
if (!mocks) {
return undefined;
}
var nextMock = mocks[0];
if (nextMock.nMatches === undefined || --nextMock.nMatches === 0) {
mocks.shift();
if (!mocks.length) {
this.regularMatches.delete(mockKey);
}
}
return nextMock;
};

@@ -417,0 +432,0 @@ WildcardMockLink.prototype.setLastResponsePromiseFromObservable = function (observable) {

@@ -53,3 +53,3 @@ "use strict";

/* eslint-disable @typescript-eslint/explicit-function-return-type */
var CAT_QUALITIES_QUERY = graphql_tag_1.default(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n query($catName: String!) {\n qualities(cats: $catName) {\n loveliness\n }\n }\n"], ["\n query($catName: String!) {\n qualities(cats: $catName) {\n loveliness\n }\n }\n"])));
var CAT_QUALITIES_QUERY = graphql_tag_1.default(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n query ($catName: String!) {\n qualities(cats: $catName) {\n loveliness\n }\n }\n"], ["\n query ($catName: String!) {\n qualities(cats: $catName) {\n loveliness\n }\n }\n"])));
describe('WildcardMockLink', function () {

@@ -135,3 +135,3 @@ describe('can be used for non-subscription queries', function () {

}); });
it('for multiple requests with the same mock', function () { return __awaiter(void 0, void 0, void 0, function () {
it('for multiple requests with the same mock when using nMatches', function () { return __awaiter(void 0, void 0, void 0, function () {
var useQueryTwice, data, _a, wrapper, link, rendered;

@@ -164,2 +164,3 @@ var _b;

result: { data: data },
nMatches: 2,
},

@@ -184,3 +185,3 @@ ], undefined, { act: react_hooks_1.act }), wrapper = _a.wrapper, link = _a.link;

describe('can be used to mock subscriptions', function () {
var MISCHIEF_SUBSCRIPTION = graphql_tag_1.default(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n subscription($catName: String!) {\n actsOfMischief(cats: $catName) {\n description\n severity\n }\n }\n "], ["\n subscription($catName: String!) {\n actsOfMischief(cats: $catName) {\n description\n severity\n }\n }\n "])));
var MISCHIEF_SUBSCRIPTION = graphql_tag_1.default(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n subscription ($catName: String!) {\n actsOfMischief(cats: $catName) {\n description\n severity\n }\n }\n "], ["\n subscription ($catName: String!) {\n actsOfMischief(cats: $catName) {\n description\n severity\n }\n }\n "])));
var useActsOfMischief = function (catName) {

@@ -187,0 +188,0 @@ var data = client_1.useSubscription(MISCHIEF_SUBSCRIPTION, {

@@ -11,7 +11,11 @@ /// <reference types="zen-observable" />

}
export interface WildcardMockedResponse extends Omit<MockedResponse, 'request'> {
request: GraphQLRequestWithWildcard;
interface MockedResponseWithMatchCount extends MockedResponse {
/** Use Number.POSITIVE_INFINITY to allow infinite matches */
nMatches?: number;
}
export declare type MockedResponses = ReadonlyArray<WildcardMockedResponse | MockedResponse>;
declare type WildcardMock = Omit<MockedResponseWithMatchCount, 'request'>;
export interface WildcardMockedResponse extends WildcardMock {
request: GraphQLRequestWithWildcard;
}
export declare type MockedResponses = readonly WildcardMockedResponse[];
declare type Act = (fun: () => void) => void;

@@ -64,3 +68,3 @@ export interface WildcardMockLinkOptions {

closeSubscription(request: DocumentNode): void;
addMockedResponse(...responses: Array<MockedResponse | WildcardMockedResponse>): void;
addMockedResponse(...responses: MockedResponses): void;
/**

@@ -94,4 +98,6 @@ * Remove the wildcard mocked response for `request`.

* as a result of those responses being sent.
* @param requestWindow How long to wait between between requests for a new request to be
* made, by default use 0 which allows one tick.
*/
waitForAllResponsesRecursively(): Promise<void>;
waitForAllResponsesRecursively(requestWindow?: number): Promise<void>;
private queryToString;

@@ -98,0 +104,0 @@ private queryAndVariablesToString;

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

export var MATCH_ANY_PARAMETERS = Symbol();
function isWildcard(mock) {
return mock.request.variables === MATCH_ANY_PARAMETERS;
// MockedResponseWithMatchCount is a narrower type than WildcardMockedResponse so it
// has to be isNotWildcard rather than isWildcard
function isNotWildcard(mock) {
return mock.request.variables !== MATCH_ANY_PARAMETERS;
}

@@ -169,3 +171,3 @@ var getResultFromFetchResult = function (result) { return (typeof result === 'function' ? result() : result); };

if (!wildcardMock.error && !wildcardMock.result) {
return observableWithError(Error('Must provide error or result for query/mutation mocks'));
return observableWithError(new Error('Must provide error or result for query/mutation mocks'));
}

@@ -186,3 +188,3 @@ var response = new Observable(function (observer) {

else if (!regularMock_1.error && !regularMock_1.result) {
return observableWithError(Error('Must provide error or result for query/mutation mocks'));
return observableWithError(new Error('Must provide error or result for query/mutation mocks'));
}

@@ -272,3 +274,13 @@ var response = new Observable(function (observer) {

responses.forEach(function (response) {
if (isWildcard(response)) {
if (isNotWildcard(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]);
}
}
else {
var mockKey = _this.queryToString(response.request.query);

@@ -278,3 +290,3 @@ var storedMocks = _this.wildcardMatches.get(mockKey);

result: response.result,
nMatches: response.nMatches || Number.POSITIVE_INFINITY,
nMatches: response.nMatches,
delay: response.delay || 0,

@@ -289,12 +301,2 @@ };

}
else {
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]);
}
}
});

@@ -348,4 +350,7 @@ };

* as a result of those responses being sent.
* @param requestWindow How long to wait between between requests for a new request to be
* made, by default use 0 which allows one tick.
*/
WildcardMockLink.prototype.waitForAllResponsesRecursively = function () {
WildcardMockLink.prototype.waitForAllResponsesRecursively = function (requestWindow) {
if (requestWindow === void 0) { requestWindow = 0; }
return __awaiter(this, void 0, void 0, function () {

@@ -359,10 +364,10 @@ return __generator(this, function (_a) {

return [4 /*yield*/, this.waitForAllResponses()
// allow code to issue new requests within the next tick
// allow code to issue new requests within the request window
];
case 1:
_a.sent();
// allow code to issue new requests within the next tick
return [4 /*yield*/, delay(0)];
// allow code to issue new requests within the request window
return [4 /*yield*/, delay(requestWindow)];
case 2:
// allow code to issue new requests within the next tick
// allow code to issue new requests within the request window
_a.sent();

@@ -394,3 +399,3 @@ return [4 /*yield*/, this.waitForAllResponsesRecursively()];

var nextMock = mocks[0];
if (--nextMock.nMatches === 0) {
if (nextMock.nMatches === undefined || --nextMock.nMatches === 0) {
mocks.shift();

@@ -406,3 +411,13 @@ if (!mocks.length) {

var mocks = this.regularMatches.get(mockKey);
return mocks === null || mocks === void 0 ? void 0 : mocks.shift();
if (!mocks) {
return undefined;
}
var nextMock = mocks[0];
if (nextMock.nMatches === undefined || --nextMock.nMatches === 0) {
mocks.shift();
if (!mocks.length) {
this.regularMatches.delete(mockKey);
}
}
return nextMock;
};

@@ -409,0 +424,0 @@ WildcardMockLink.prototype.setLastResponsePromiseFromObservable = function (observable) {

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

/* eslint-disable @typescript-eslint/explicit-function-return-type */
var CAT_QUALITIES_QUERY = gql(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n query($catName: String!) {\n qualities(cats: $catName) {\n loveliness\n }\n }\n"], ["\n query($catName: String!) {\n qualities(cats: $catName) {\n loveliness\n }\n }\n"])));
var CAT_QUALITIES_QUERY = gql(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n query ($catName: String!) {\n qualities(cats: $catName) {\n loveliness\n }\n }\n"], ["\n query ($catName: String!) {\n qualities(cats: $catName) {\n loveliness\n }\n }\n"])));
describe('WildcardMockLink', function () {

@@ -130,3 +130,3 @@ describe('can be used for non-subscription queries', function () {

}); });
it('for multiple requests with the same mock', function () { return __awaiter(void 0, void 0, void 0, function () {
it('for multiple requests with the same mock when using nMatches', function () { return __awaiter(void 0, void 0, void 0, function () {
var useQueryTwice, data, _a, wrapper, link, rendered;

@@ -159,2 +159,3 @@ var _b;

result: { data: data },
nMatches: 2,
},

@@ -179,3 +180,3 @@ ], undefined, { act: actHook }), wrapper = _a.wrapper, link = _a.link;

describe('can be used to mock subscriptions', function () {
var MISCHIEF_SUBSCRIPTION = gql(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n subscription($catName: String!) {\n actsOfMischief(cats: $catName) {\n description\n severity\n }\n }\n "], ["\n subscription($catName: String!) {\n actsOfMischief(cats: $catName) {\n description\n severity\n }\n }\n "])));
var MISCHIEF_SUBSCRIPTION = gql(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n subscription ($catName: String!) {\n actsOfMischief(cats: $catName) {\n description\n severity\n }\n }\n "], ["\n subscription ($catName: String!) {\n actsOfMischief(cats: $catName) {\n description\n severity\n }\n }\n "])));
var useActsOfMischief = function (catName) {

@@ -182,0 +183,0 @@ var data = useSubscription(MISCHIEF_SUBSCRIPTION, {

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

@@ -47,11 +47,11 @@ "author": "insidewhy <github@chilon.net>",

"devDependencies": {
"@apollo/client": "3.3.15",
"@apollo/client": "3.3.16",
"@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",
"@types/jest": "26.0.23",
"@types/react": "17.0.5",
"@typescript-eslint/eslint-plugin": "4.23.0",
"@typescript-eslint/parser": "4.23.0",
"concurrently": "6.1.0",
"eslint": "7.26.0",
"eslint-config-prettier": "8.3.0",

@@ -62,6 +62,6 @@ "eslint-plugin-import": "2.22.1",

"graphql": "15.5.0",
"graphql-tag": "2.11.0",
"graphql-tag": "2.12.4",
"husky": "6.0.0",
"jest": "26.6.3",
"prettier": "2.2.1",
"prettier": "2.3.0",
"pretty-quick": "3.1.0",

@@ -79,4 +79,4 @@ "react": "17.0.2",

"resolutions": {
"@types/react": "17.0.3"
"@types/react": "17.0.5"
}
}

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

const CAT_QUALITIES_QUERY = gql`
query($catName: String!) {
query ($catName: String!) {
qualities(cats: $catName) {

@@ -53,3 +53,3 @@ loveliness

The above mocked provider will match two requests for `CAT_QUALITIES_QUERY` no matter what the variables are. Here `nMatches` is used to restrict the mock to the first two requests that match, when `nMatches` is omitted the mock will match an infinite number of requests.
The above mocked provider will match two requests for `CAT_QUALITIES_QUERY` no matter what the variables are. Here `nMatches` is used to restrict the mock to the first two requests that match, when `nMatches` is omitted the mock will match one request. `Number.POSITIVE_INFINITY` can be used to allow an inifinite number of matchs.

@@ -105,3 +105,3 @@ The instantiation of `WildcardMockLink` also shows the options, `addTypename` which works the same as apollo's `MockLink` and `act` which can be used to ensure all operations that emit data to components are wrapped in an `act` function.

const CAT_QUALITIES_QUERY = gql`
query($catName: String!) {
query ($catName: String!) {
qualities(cats: $catName) {

@@ -157,3 +157,3 @@ loveliness

const CAT_QUALITIES_QUERY = gql`
query($catName: String!) {
query ($catName: String!) {
qualities(cats: $catName) {

@@ -209,3 +209,3 @@ loveliness

const MISCHIEF_SUBSCRIPTION = gql`
subscription($catName: String!) {
subscription ($catName: String!) {
actsOfMischief(cats: $catName) {

@@ -212,0 +212,0 @@ description

@@ -37,14 +37,15 @@ import {

export interface WildcardMockedResponse
extends Omit<MockedResponse, 'request'> {
request: GraphQLRequestWithWildcard
interface MockedResponseWithMatchCount extends MockedResponse {
/** Use Number.POSITIVE_INFINITY to allow infinite matches */
nMatches?: number
}
type MockedResponseWithoutRequest = Omit<MockedResponse, 'request'>
type WildcardMock = Omit<MockedResponseWithMatchCount, 'request'>
export type MockedResponses = ReadonlyArray<
WildcardMockedResponse | MockedResponse
>
export interface WildcardMockedResponse extends WildcardMock {
request: GraphQLRequestWithWildcard
}
export type MockedResponses = readonly WildcardMockedResponse[]
type Act = (fun: () => void) => void

@@ -57,12 +58,10 @@

interface WildcardMock extends MockedResponseWithoutRequest {
nMatches: number
// MockedResponseWithMatchCount is a narrower type than WildcardMockedResponse so it
// has to be isNotWildcard rather than isWildcard
function isNotWildcard(
mock: WildcardMockedResponse,
): mock is MockedResponseWithMatchCount {
return mock.request.variables !== MATCH_ANY_PARAMETERS
}
function isWildcard(
mock: WildcardMockedResponse | MockedResponse,
): mock is WildcardMockedResponse {
return mock.request.variables === MATCH_ANY_PARAMETERS
}
const getResultFromFetchResult = (

@@ -88,3 +87,3 @@ result: FetchResult | (() => FetchResult),

observer: FetchResultObserver,
response: MockedResponseWithoutRequest,
response: WildcardMock,
complete: boolean,

@@ -134,3 +133,3 @@ act: Act,

private wildcardMatches = new Map<string, WildcardMock[]>()
private regularMatches = new Map<string, MockedResponse[]>()
private regularMatches = new Map<string, MockedResponseWithMatchCount[]>()

@@ -195,3 +194,3 @@ queries: StoredOperation[] = []

return observableWithError(
Error('Must provide error or result for query/mutation mocks'),
new Error('Must provide error or result for query/mutation mocks'),
)

@@ -215,3 +214,3 @@ }

return observableWithError(
Error('Must provide error or result for query/mutation mocks'),
new Error('Must provide error or result for query/mutation mocks'),
)

@@ -319,7 +318,16 @@ }

addMockedResponse(
...responses: Array<MockedResponse | WildcardMockedResponse>
): void {
addMockedResponse(...responses: MockedResponses): void {
responses.forEach((response) => {
if (isWildcard(response)) {
if (isNotWildcard(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])
}
} else {
const mockKey = this.queryToString(response.request.query)

@@ -329,3 +337,3 @@ const storedMocks = this.wildcardMatches.get(mockKey)

result: response.result,
nMatches: response.nMatches || Number.POSITIVE_INFINITY,
nMatches: response.nMatches,
delay: response.delay || 0,

@@ -338,13 +346,2 @@ }

}
} else {
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])
}
}

@@ -405,5 +402,5 @@ })

waitForAllResponses(): Promise<void> {
return (Promise.all(
return Promise.all(
Array.from(this.pendingResponses),
) as unknown) as Promise<void>
) as unknown as Promise<void>
}

@@ -414,4 +411,6 @@

* as a result of those responses being sent.
* @param requestWindow How long to wait between between requests for a new request to be
* made, by default use 0 which allows one tick.
*/
async waitForAllResponsesRecursively(): Promise<void> {
async waitForAllResponsesRecursively(requestWindow = 0): Promise<void> {
if (!this.pendingResponses.size) {

@@ -422,4 +421,4 @@ return

await this.waitForAllResponses()
// allow code to issue new requests within the next tick
await delay(0)
// allow code to issue new requests within the request window
await delay(requestWindow)
await this.waitForAllResponsesRecursively()

@@ -446,3 +445,2 @@ }

const mocks = this.wildcardMatches.get(mockKey)
if (!mocks) {

@@ -453,3 +451,3 @@ return undefined

const nextMock = mocks[0]
if (--nextMock.nMatches === 0) {
if (nextMock.nMatches === undefined || --nextMock.nMatches === 0) {
mocks.shift()

@@ -463,6 +461,19 @@ if (!mocks.length) {

private getRegularMockMatch(op: Operation): MockedResponse | undefined {
private getRegularMockMatch(
op: Operation,
): MockedResponseWithMatchCount | undefined {
const mockKey = this.queryAndVariablesToString(op.query, op.variables)
const mocks = this.regularMatches.get(mockKey)
return mocks?.shift()
if (!mocks) {
return undefined
}
const nextMock = mocks[0]
if (nextMock.nMatches === undefined || --nextMock.nMatches === 0) {
mocks.shift()
if (!mocks.length) {
this.regularMatches.delete(mockKey)
}
}
return nextMock
}

@@ -469,0 +480,0 @@

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