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.4 to 1.0.5

2

dist.es2015/index.d.ts
import { MockLink, MockedResponse } from '@apollo/react-testing';
import { Operation, FetchResult, Observable, GraphQLRequest } from 'apollo-link';
import { DocumentNode } from 'graphql';
export { withApolloMocks, hookWrapperWithApolloMocks } from './utils';
export { withApolloMocks, hookWrapperWithApolloMocks, HookWrapperAndLink, } from './utils';
export declare const MATCH_ANY_PARAMETERS: unique symbol;

@@ -6,0 +6,0 @@ export declare type GraphQLVariables = Record<string, any>;

@@ -6,3 +6,3 @@ import { MockLink } from '@apollo/react-testing';

import { print } from 'graphql';
export { withApolloMocks, hookWrapperWithApolloMocks } from './utils';
export { withApolloMocks, hookWrapperWithApolloMocks, } from './utils';
export const MATCH_ANY_PARAMETERS = Symbol();

@@ -15,3 +15,3 @@ function notWildcard(mock) {

}
const getResultFromFetchResult = (result) => typeof result === 'function' ? result() : result;
const getResultFromFetchResult = (result) => (typeof result === 'function' ? result() : result);
const toStoredOperation = (op) => ({

@@ -167,3 +167,4 @@ query: op.query,

lastQueryMatches(request) {
return (!!this.lastQuery && this.queryToString(request) === this.queryToString(this.lastQuery.query));
return (!!this.lastQuery &&
this.queryToString(request) === this.queryToString(this.lastQuery.query));
}

@@ -175,3 +176,4 @@ /**

return (!!this.lastMutation &&
this.queryToString(request) === this.queryToString(this.lastMutation.query));
this.queryToString(request) ===
this.queryToString(this.lastMutation.query));
}

@@ -183,3 +185,4 @@ /**

return (!!this.lastSubscription &&
this.queryToString(request) === this.queryToString(this.lastSubscription.query));
this.queryToString(request) ===
this.queryToString(this.lastSubscription.query));
}

@@ -186,0 +189,0 @@ /**

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

import React, { FC, ReactElement } from 'react';
import { ApolloCache } from 'apollo-cache';
import { FC, ReactElement } from 'react';
import { WildcardMockLink, MockedResponses } from '.';

@@ -7,4 +8,5 @@ export interface MockLinkAndElement {

}
export interface WildcardMockOptions {
export interface WildcardMockOptions<TSerializedCache = {}> {
addTypename?: boolean;
cache?: ApolloCache<TSerializedCache>;
}

@@ -14,3 +16,3 @@ /**

*/
export declare const withApolloMocks: (testComponentFactory: () => React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)>, apolloMocks?: MockedResponses, options?: WildcardMockOptions) => MockLinkAndElement;
export declare function withApolloMocks<TSerializedCache = {}>(testComponentFactory: () => ReactElement, apolloMocks?: MockedResponses, options?: WildcardMockOptions<TSerializedCache>): MockLinkAndElement;
export interface HookWrapperAndLink {

@@ -17,0 +19,0 @@ wrapper: FC;

@@ -7,7 +7,7 @@ import { MockedProvider } from '@apollo/react-testing';

*/
export const withApolloMocks = (testComponentFactory, apolloMocks = [], options = { addTypename: true }) => {
export function withApolloMocks(testComponentFactory, apolloMocks = [], options = { addTypename: true }) {
const link = new WildcardMockLink(apolloMocks);
const element = (React.createElement(MockedProvider, { addTypename: options.addTypename, link: link }, testComponentFactory()));
const element = (React.createElement(MockedProvider, { addTypename: options.addTypename, link: link, cache: options.cache }, testComponentFactory()));
return { element, link };
};
}
/**

@@ -14,0 +14,0 @@ * Make a wrapper (which can be passed to renderHook's `wrapper`) that provided

import { MockLink, MockedResponse } from '@apollo/react-testing';
import { Operation, FetchResult, Observable, GraphQLRequest } from 'apollo-link';
import { DocumentNode } from 'graphql';
export { withApolloMocks, hookWrapperWithApolloMocks } from './utils';
export { withApolloMocks, hookWrapperWithApolloMocks, HookWrapperAndLink, } from './utils';
export declare const MATCH_ANY_PARAMETERS: unique symbol;

@@ -6,0 +6,0 @@ export declare type GraphQLVariables = Record<string, any>;

@@ -34,5 +34,3 @@ "use strict";

}
var getResultFromFetchResult = function (result) {
return typeof result === 'function' ? result() : result;
};
var getResultFromFetchResult = function (result) { return (typeof result === 'function' ? result() : result); };
var toStoredOperation = function (op) { return ({

@@ -196,3 +194,4 @@ query: op.query,

WildcardMockLink.prototype.lastQueryMatches = function (request) {
return (!!this.lastQuery && this.queryToString(request) === this.queryToString(this.lastQuery.query));
return (!!this.lastQuery &&
this.queryToString(request) === this.queryToString(this.lastQuery.query));
};

@@ -204,3 +203,4 @@ /**

return (!!this.lastMutation &&
this.queryToString(request) === this.queryToString(this.lastMutation.query));
this.queryToString(request) ===
this.queryToString(this.lastMutation.query));
};

@@ -212,3 +212,4 @@ /**

return (!!this.lastSubscription &&
this.queryToString(request) === this.queryToString(this.lastSubscription.query));
this.queryToString(request) ===
this.queryToString(this.lastSubscription.query));
};

@@ -215,0 +216,0 @@ /**

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

import React, { FC, ReactElement } from 'react';
import { ApolloCache } from 'apollo-cache';
import { FC, ReactElement } from 'react';
import { WildcardMockLink, MockedResponses } from '.';

@@ -7,4 +8,5 @@ export interface MockLinkAndElement {

}
export interface WildcardMockOptions {
export interface WildcardMockOptions<TSerializedCache = {}> {
addTypename?: boolean;
cache?: ApolloCache<TSerializedCache>;
}

@@ -14,3 +16,3 @@ /**

*/
export declare const withApolloMocks: (testComponentFactory: () => React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)>, apolloMocks?: MockedResponses, options?: WildcardMockOptions) => MockLinkAndElement;
export declare function withApolloMocks<TSerializedCache = {}>(testComponentFactory: () => ReactElement, apolloMocks?: MockedResponses, options?: WildcardMockOptions<TSerializedCache>): MockLinkAndElement;
export interface HookWrapperAndLink {

@@ -17,0 +19,0 @@ wrapper: FC;

@@ -12,9 +12,10 @@ "use strict";

*/
exports.withApolloMocks = function (testComponentFactory, apolloMocks, options) {
function withApolloMocks(testComponentFactory, apolloMocks, options) {
if (apolloMocks === void 0) { apolloMocks = []; }
if (options === void 0) { options = { addTypename: true }; }
var link = new _1.WildcardMockLink(apolloMocks);
var element = (react_1.default.createElement(react_testing_1.MockedProvider, { addTypename: options.addTypename, link: link }, testComponentFactory()));
var element = (react_1.default.createElement(react_testing_1.MockedProvider, { addTypename: options.addTypename, link: link, cache: options.cache }, testComponentFactory()));
return { element: element, link: link };
};
}
exports.withApolloMocks = withApolloMocks;
/**

@@ -21,0 +22,0 @@ * Make a wrapper (which can be passed to renderHook's `wrapper`) that provided

import { MockLink, MockedResponse } from '@apollo/react-testing';
import { Operation, FetchResult, Observable, GraphQLRequest } from 'apollo-link';
import { DocumentNode } from 'graphql';
export { withApolloMocks, hookWrapperWithApolloMocks } from './utils';
export { withApolloMocks, hookWrapperWithApolloMocks, HookWrapperAndLink, } from './utils';
export declare const MATCH_ANY_PARAMETERS: unique symbol;

@@ -6,0 +6,0 @@ export declare type GraphQLVariables = Record<string, any>;

@@ -19,3 +19,3 @@ var __extends = (this && this.__extends) || (function () {

import { print } from 'graphql';
export { withApolloMocks, hookWrapperWithApolloMocks } from './utils';
export { withApolloMocks, hookWrapperWithApolloMocks, } from './utils';
export var MATCH_ANY_PARAMETERS = Symbol();

@@ -28,5 +28,3 @@ function notWildcard(mock) {

}
var getResultFromFetchResult = function (result) {
return typeof result === 'function' ? result() : result;
};
var getResultFromFetchResult = function (result) { return (typeof result === 'function' ? result() : result); };
var toStoredOperation = function (op) { return ({

@@ -190,3 +188,4 @@ query: op.query,

WildcardMockLink.prototype.lastQueryMatches = function (request) {
return (!!this.lastQuery && this.queryToString(request) === this.queryToString(this.lastQuery.query));
return (!!this.lastQuery &&
this.queryToString(request) === this.queryToString(this.lastQuery.query));
};

@@ -198,3 +197,4 @@ /**

return (!!this.lastMutation &&
this.queryToString(request) === this.queryToString(this.lastMutation.query));
this.queryToString(request) ===
this.queryToString(this.lastMutation.query));
};

@@ -206,3 +206,4 @@ /**

return (!!this.lastSubscription &&
this.queryToString(request) === this.queryToString(this.lastSubscription.query));
this.queryToString(request) ===
this.queryToString(this.lastSubscription.query));
};

@@ -209,0 +210,0 @@ /**

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

import React, { FC, ReactElement } from 'react';
import { ApolloCache } from 'apollo-cache';
import { FC, ReactElement } from 'react';
import { WildcardMockLink, MockedResponses } from '.';

@@ -7,4 +8,5 @@ export interface MockLinkAndElement {

}
export interface WildcardMockOptions {
export interface WildcardMockOptions<TSerializedCache = {}> {
addTypename?: boolean;
cache?: ApolloCache<TSerializedCache>;
}

@@ -14,3 +16,3 @@ /**

*/
export declare const withApolloMocks: (testComponentFactory: () => React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)>, apolloMocks?: MockedResponses, options?: WildcardMockOptions) => MockLinkAndElement;
export declare function withApolloMocks<TSerializedCache = {}>(testComponentFactory: () => ReactElement, apolloMocks?: MockedResponses, options?: WildcardMockOptions<TSerializedCache>): MockLinkAndElement;
export interface HookWrapperAndLink {

@@ -17,0 +19,0 @@ wrapper: FC;

@@ -7,9 +7,9 @@ import { MockedProvider } from '@apollo/react-testing';

*/
export var withApolloMocks = function (testComponentFactory, apolloMocks, options) {
export function withApolloMocks(testComponentFactory, apolloMocks, options) {
if (apolloMocks === void 0) { apolloMocks = []; }
if (options === void 0) { options = { addTypename: true }; }
var link = new WildcardMockLink(apolloMocks);
var element = (React.createElement(MockedProvider, { addTypename: options.addTypename, link: link }, testComponentFactory()));
var element = (React.createElement(MockedProvider, { addTypename: options.addTypename, link: link, cache: options.cache }, testComponentFactory()));
return { element: element, link: link };
};
}
/**

@@ -16,0 +16,0 @@ * Make a wrapper (which can be passed to renderHook's `wrapper`) that provided

{
"name": "wildcard-mock-link",
"version": "1.0.4",
"version": "1.0.5",
"description": "apollo client mocking",

@@ -5,0 +5,0 @@ "author": "insidewhy <github@chilon.net>",

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

The above mocked provider will match two requests for `CAT_QUALITIES_QUERY` no matter what the variables are due to `nMatches: 2`. If `nMatches` is omitted then 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 an infinite number of requests.

@@ -77,3 +77,6 @@ ### Asserting against the latest request/mutation/subscription.

import React, { FC } from 'react'
import { MATCH_ANY_PARAMETERS, hookWrapperWithApolloMocks } from 'wildcard-mock-link'
import {
MATCH_ANY_PARAMETERS,
hookWrapperWithApolloMocks,
} from 'wildcard-mock-link'

@@ -126,3 +129,6 @@ const CAT_QUALITIES_QUERY = gql`

import gql from 'graphql-tag'
import { MATCH_ANY_PARAMETERS, hookWrapperWithApolloMocks } from 'wildcard-mock-link'
import {
MATCH_ANY_PARAMETERS,
hookWrapperWithApolloMocks,
} from 'wildcard-mock-link'

@@ -175,3 +181,6 @@ const CAT_QUALITIES_QUERY = gql`

import gql from 'graphql-tag'
import { MATCH_ANY_PARAMETERS, hookWrapperWithApolloMocks } from 'wildcard-mock-link'
import {
MATCH_ANY_PARAMETERS,
hookWrapperWithApolloMocks,
} from 'wildcard-mock-link'

@@ -189,3 +198,5 @@ const MISCHIEF_SUBSCRIPTION = gql`

const useActsOfMischief = (catName: string) => {
const { data } = useSubscription(MISCHIEF_SUBSCRIPTION, { variables: { catName } })
const { data } = useSubscription(MISCHIEF_SUBSCRIPTION, {
variables: { catName },
})
return data

@@ -228,3 +239,5 @@ }

actHook(() => {
link.sendWildcardSubscriptionResult(MISCHIEF_SUBSCRIPTION, { data: updateData })
link.sendWildcardSubscriptionResult(MISCHIEF_SUBSCRIPTION, {
data: updateData,
})
})

@@ -231,0 +244,0 @@ await actHook(() =>

@@ -7,3 +7,7 @@ import { MockLink, MockedResponse } from '@apollo/react-testing'

export { withApolloMocks, hookWrapperWithApolloMocks } from './utils'
export {
withApolloMocks,
hookWrapperWithApolloMocks,
HookWrapperAndLink,
} from './utils'

@@ -14,7 +18,9 @@ export const MATCH_ANY_PARAMETERS = Symbol()

export interface GraphQLRequestWithWildcard extends Omit<GraphQLRequest, 'variables'> {
export interface GraphQLRequestWithWildcard
extends Omit<GraphQLRequest, 'variables'> {
variables?: GraphQLRequest['variables'] | symbol
}
export interface WildcardMockedResponse extends Omit<MockedResponse, 'request'> {
export interface WildcardMockedResponse
extends Omit<MockedResponse, 'request'> {
request: GraphQLRequestWithWildcard

@@ -24,3 +30,5 @@ nMatches?: number

export type MockedResponses = ReadonlyArray<WildcardMockedResponse | MockedResponse>
export type MockedResponses = ReadonlyArray<
WildcardMockedResponse | MockedResponse
>

@@ -34,3 +42,5 @@ interface WildcardMock {

function notWildcard(mock: WildcardMockedResponse | MockedResponse): mock is MockedResponse {
function notWildcard(
mock: WildcardMockedResponse | MockedResponse,
): mock is MockedResponse {
return mock.request.variables !== MATCH_ANY_PARAMETERS

@@ -45,4 +55,5 @@ }

const getResultFromFetchResult = (result: FetchResult | (() => FetchResult)): FetchResult =>
typeof result === 'function' ? result() : result
const getResultFromFetchResult = (
result: FetchResult | (() => FetchResult),
): FetchResult => (typeof result === 'function' ? result() : result)

@@ -55,3 +66,3 @@ interface StoredOperation {

const toStoredOperation = (op: Operation) => ({
const toStoredOperation = (op: Operation): StoredOperation => ({
query: op.query,

@@ -75,3 +86,6 @@ variables: op.variables,

private openSubscriptions = new Map<string, ZenObservable.SubscriptionObserver<FetchResult>>()
private openSubscriptions = new Map<
string,
ZenObservable.SubscriptionObserver<FetchResult>
>()

@@ -84,3 +98,4 @@ constructor(mockedResponses: MockedResponses, addTypename?: boolean) {

request(op: Operation): Observable<FetchResult> | null {
const operationType = (op.query.definitions?.[0] as OperationDefinitionNode)?.operation
const operationType = (op.query.definitions?.[0] as OperationDefinitionNode)
?.operation

@@ -166,3 +181,5 @@ if (operationType === 'subscription') {

if (!subscription) {
throw new Error('Could not send subscription result for subscription that is not open')
throw new Error(
'Could not send subscription result for subscription that is not open',
)
} else {

@@ -177,6 +194,11 @@ subscription.next(response)

*/
sendWildcardSubscriptionResult(request: DocumentNode, response: FetchResult): void {
sendWildcardSubscriptionResult(
request: DocumentNode,
response: FetchResult,
): void {
const subscription = this.openSubscriptions.get(this.queryToString(request))
if (!subscription) {
throw new Error('Could not send subscription result for subscription that is not open')
throw new Error(
'Could not send subscription result for subscription that is not open',
)
} else {

@@ -194,3 +216,5 @@ subscription.next(response)

if (!subscription) {
throw new Error('Could not close subscription subscription that is not open')
throw new Error(
'Could not close subscription subscription that is not open',
)
} else {

@@ -234,3 +258,4 @@ subscription.complete()

return (
!!this.lastQuery && this.queryToString(request) === this.queryToString(this.lastQuery.query)
!!this.lastQuery &&
this.queryToString(request) === this.queryToString(this.lastQuery.query)
)

@@ -245,3 +270,4 @@ }

!!this.lastMutation &&
this.queryToString(request) === this.queryToString(this.lastMutation.query)
this.queryToString(request) ===
this.queryToString(this.lastMutation.query)
)

@@ -256,3 +282,4 @@ }

!!this.lastSubscription &&
this.queryToString(request) === this.queryToString(this.lastSubscription.query)
this.queryToString(request) ===
this.queryToString(this.lastSubscription.query)
)

@@ -259,0 +286,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

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