Gathering detailed insights and metrics for wildcard-mock-link
Gathering detailed insights and metrics for wildcard-mock-link
npm install wildcard-mock-link
Typescript
Module System
Node Version
NPM Version
68.4
Supply Chain
96.4
Quality
75.3
Maintenance
100
Vulnerability
100
License
TypeScript (99.68%)
JavaScript (0.32%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
2,071,035
Last Day
2,356
Last Week
10,384
Last Month
40,319
Last Year
519,075
ISC License
17 Stars
80 Commits
8 Forks
5 Watchers
22 Branches
9 Contributors
Updated on Nov 27, 2024
Minified
Minified + Gzipped
Latest Version
2.0.4
Package Id
wildcard-mock-link@2.0.4
Unpacked Size
289.28 kB
Size
35.62 kB
File Count
41
NPM Version
8.19.3
Node Version
16.19.0
Published on
Aug 25, 2023
Cumulative downloads
Total Downloads
Last Day
65.1%
2,356
Compared to previous day
Last Week
30.7%
10,384
Compared to previous week
Last Month
64.2%
40,319
Compared to previous month
Last Year
-6.3%
519,075
Compared to previous year
2
2
25
WildcardMockLink
is a replacement for MockLink
which can:
MockedProvider
in a test via method calls.The MockLink
provided with apollo requires the variables for every matching query to be specified ahead of time. In certain circumstances this is not convenient, i.e. using the MockedProvider
in storybook stories. WildcardMockLink
allows mocks to be specified that match a query with any variables, and these mocks can be configured to match more than once.
1import { act } from '@testing-library/react' 2 3const CAT_QUALITIES_QUERY = gql` 4 query ($catName: String!) { 5 qualities(cats: $catName) { 6 loveliness 7 } 8 } 9` 10 11const link = new WildcardMockLink( 12 [ 13 { 14 request: { 15 query: CAT_QUALITIES_QUERY, 16 variables: MATCH_ANY_PARAMETERS, 17 }, 18 result: { data }, 19 nMatches: 2, 20 }, 21 ], 22 { addTypename: true, act }, 23) 24 25return ( 26 <MockedProvider link={link}> 27 <MyCatComponent /> 28 </MockedProvider> 29)
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.
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. suppressMissingMockWarnings
will disable the console.warn
about missing mocks, defaults to false
.
The following returns true if the last query matches CAT_QUALITIES_QUERY
.
1link.lastQueryMatches(CAT_QUALITIES_QUERY)
There are also lastMutationMatches
and lastSubscriptionMatches
. The arrays queries
, mutations
and subscriptions
contain all of the corresponding requests.
These methods can be used to ensure updates don't happen outside of act
.
1await link.waitForLastResponse()
This waits for the last response to complete.
1await link.waitForAllResponses()
This waits for all pending responses to complete.
1await link.waitForAllResponsesRecursively()
This can be used when a component makes a new request based on data received from another response. It waits until there are no more pending responses.
This library provides a utility function withApolloMocks
which can be used to created a component tree with access to mocked data. It returns the react element at the head of the component tree and a WildcardMockLink
object and can be used in conjunction with the functionality mentioned above to create a test like this:
1import { useQuery } from '@apollo/client' 2import { render, act } from '@testing-library/react' 3import gql from 'graphql-tag' 4import React, { FC } from 'react' 5import { 6 MATCH_ANY_PARAMETERS, 7 hookWrapperWithApolloMocks, 8} from 'wildcard-mock-link' 9 10const CAT_QUALITIES_QUERY = gql` 11 query ($catName: String!) { 12 qualities(cats: $catName) { 13 loveliness 14 } 15 } 16` 17 18it('can be used to mock data for a component tree', async () => { 19 const data = { 20 qualities: { 21 __typename: 'Qualities', 22 loveliness: 'very', 23 }, 24 } 25 26 const { element, link } = withApolloMocks( 27 () => <MyCatComponent catName="mr bad actor face" />, 28 [ 29 { 30 request: { 31 query: CAT_QUALITIES_QUERY, 32 variables: MATCH_ANY_PARAMETERS, 33 }, 34 result: { data }, 35 }, 36 ], 37 ) 38 const { getByRole } = render(element) 39 await link.waitForLastResponse() 40 41 expect(link.lastQueryMatches(CAT_QUALITIES_QUERY)).toBeTruthy() 42 expect(link.lastQuery?.variables).toEqual({ catName: 'mr bad actor face' }) 43 const mainContent = getByRole('main') 44 expect(mainContent?.textContent).toEqual('Loveliness: very') 45})
This library provides a utility function hookWrapperWithApolloMocks
for creating a wrapper object which can be used with @testing-library/react-hooks
. It returns a WildcardMockLink
and a wrapper
and can be used in conjunction with the functionality mentioned above to create a test like this:
1import { useQuery } from '@apollo/client' 2import { renderHook, act as actHook } from '@testing-library/react-hooks' 3import gql from 'graphql-tag' 4import { 5 MATCH_ANY_PARAMETERS, 6 hookWrapperWithApolloMocks, 7} from 'wildcard-mock-link' 8 9const CAT_QUALITIES_QUERY = gql` 10 query ($catName: String!) { 11 qualities(cats: $catName) { 12 loveliness 13 } 14 } 15` 16 17it('can be used to mock data for a hook', async () => { 18 const useQueryOnce = (catName: string) => { 19 const { data } = useQuery(CAT_QUALITIES_QUERY, { variables: { catName } }) 20 return data 21 } 22 23 const data = { 24 qualities: { 25 __typename: 'Qualities', 26 loveliness: 'very', 27 }, 28 } 29 const { wrapper, link } = hookWrapperWithApolloMocks([ 30 { 31 request: { 32 query: CAT_QUALITIES_QUERY, 33 variables: MATCH_ANY_PARAMETERS, 34 }, 35 result: { data }, 36 }, 37 ]) 38 const { result } = renderHook(() => useQueryOnce('tortand'), { wrapper }) 39 await link.waitForLastResponse() 40 expect(link.lastQueryMatches(CAT_QUALITIES_QUERY)).toBeTruthy() 41 expect(link.lastQuery!.variables).toEqual({ catName: 'tortand' }) 42 expect(result.current).toEqual(data) 43})
The WildcardMockLink
provides a way to push new responses out to subscriptions. This can be used during tests to make it easier to test how components respond to subscription updates. The sendWildcardSubscriptionResult
method can be used to send a new response which matches a wildcard mock, otherwise sendSubscriptionResult
can be used. Here is an example:
1import { useQuery } from '@apollo/client' 2import { waitFor } from '@testing-library/react' 3import { renderHook, act as actHook } from '@testing-library/react-hooks' 4import gql from 'graphql-tag' 5import { 6 MATCH_ANY_PARAMETERS, 7 hookWrapperWithApolloMocks, 8} from 'wildcard-mock-link' 9 10const MISCHIEF_SUBSCRIPTION = gql` 11 subscription ($catName: String!) { 12 actsOfMischief(cats: $catName) { 13 description 14 severity 15 } 16 } 17` 18 19it('can push updates using the API', async () => { 20 const useActsOfMischief = (catName: string) => { 21 const { data } = useSubscription(MISCHIEF_SUBSCRIPTION, { 22 variables: { catName }, 23 }) 24 return data 25 } 26 27 const initialData = { 28 actsOfMischief: { 29 __typename: 'ActsOfMischief', 30 description: 'did not stay away from my bins', 31 severity: 'extreme', 32 }, 33 } 34 const { wrapper, link } = hookWrapperWithApolloMocks( 35 [ 36 { 37 request: { 38 query: MISCHIEF_SUBSCRIPTION, 39 variables: MATCH_ANY_PARAMETERS, 40 }, 41 result: { data: initialData }, 42 }, 43 ], 44 undefined, 45 { act: actHook }, 46 ) 47 const rendered = renderHook(() => useActsOfMischief('la don'), { wrapper }) 48 expect(link.lastSubscriptionMatches(MISCHIEF_SUBSCRIPTION)).toBeTruthy() 49 expect(link.lastSubscription?.variables).toEqual({ catName: 'la don' }) 50 51 await waitFor(() => { 52 expect(rendered.result.current).toEqual(initialData) 53 }) 54 55 const updateData = { 56 actsOfMischief: { 57 __typename: 'ActsOfMischief', 58 description: 'pushed that button', 59 severity: 'mild', 60 }, 61 } 62 link.sendWildcardSubscriptionResult(MISCHIEF_SUBSCRIPTION, { 63 data: updateData, 64 }) 65 await waitFor(() => { 66 expect(rendered.result.current).toEqual(updateData) 67 }) 68})
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 5/22 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
20 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More