Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
wildcard-mock-link
Advanced tools
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.
import { act } from '@testing-library/react'
const CAT_QUALITIES_QUERY = gql`
query($catName: String!) {
qualities(cats: $catName) {
loveliness
}
}
`
const link = new WildcardMockLink(
[
{
request: {
query: CAT_QUALITIES_QUERY,
variables: MATCH_ANY_PARAMETERS,
},
result: { data },
nMatches: 2,
},
],
{ addTypename: true, act },
)
return (
<MockedProvider link={link}>
<MyCatComponent />
</MockedProvider>
)
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 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.
The following returns true if the last query matches CAT_QUALITIES_QUERY
.
link.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
.
await link.waitForLastResponse()
This waits for the last response to complete.
await link.waitForAllResponses()
This waits for all pending responses to complete.
await 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:
import { useQuery } from '@apollo/client'
import { render, act } from '@testing-library/react'
import gql from 'graphql-tag'
import React, { FC } from 'react'
import {
MATCH_ANY_PARAMETERS,
hookWrapperWithApolloMocks,
} from 'wildcard-mock-link'
const CAT_QUALITIES_QUERY = gql`
query($catName: String!) {
qualities(cats: $catName) {
loveliness
}
}
`
it('can be used to mock data for a component tree', async () => {
const data = {
qualities: {
__typename: 'Qualities',
loveliness: 'very',
},
}
const { element, link } = withApolloMocks(
() => <MyCatComponent catName="mr bad actor face" />,
[
{
request: {
query: CAT_QUALITIES_QUERY,
variables: MATCH_ANY_PARAMETERS,
},
result: { data },
},
],
)
const { getByRole } = render(element)
await act(() => link.waitForLastResponse())
expect(link.lastQueryMatches(CAT_QUALITIES_QUERY)).toBeTruthy()
expect(link.lastQuery?.variables).toEqual({ catName: 'mr bad actor face' })
const mainContent = getByRole('main')
expect(mainContent?.textContent).toEqual('Loveliness: very')
})
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:
import { useQuery } from '@apollo/client'
import { renderHook, act as actHook } from '@testing-library/react-hooks'
import gql from 'graphql-tag'
import {
MATCH_ANY_PARAMETERS,
hookWrapperWithApolloMocks,
} from 'wildcard-mock-link'
const CAT_QUALITIES_QUERY = gql`
query($catName: String!) {
qualities(cats: $catName) {
loveliness
}
}
`
it('can be used to mock data for a hook', async () => {
const useQueryOnce = (catName: string) => {
const { data } = useQuery(CAT_QUALITIES_QUERY, { variables: { catName } })
return data
}
const data = {
qualities: {
__typename: 'Qualities',
loveliness: 'very',
},
}
const { wrapper, link } = hookWrapperWithApolloMocks([
{
request: {
query: CAT_QUALITIES_QUERY,
variables: MATCH_ANY_PARAMETERS,
},
result: { data },
},
])
const { result } = renderHook(() => useQueryOnce('tortand'), { wrapper })
await actHook(() => link.waitForLastResponse())
expect(link.lastQueryMatches(CAT_QUALITIES_QUERY)).toBeTruthy()
expect(link.lastQuery!.variables).toEqual({ catName: 'tortand' })
expect(result.current).toEqual(data)
})
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:
import { useQuery } from '@apollo/client'
import { waitFor } from '@testing-library/react'
import { renderHook, act as actHook } from '@testing-library/react-hooks'
import gql from 'graphql-tag'
import {
MATCH_ANY_PARAMETERS,
hookWrapperWithApolloMocks,
} from 'wildcard-mock-link'
const MISCHIEF_SUBSCRIPTION = gql`
subscription($catName: String!) {
actsOfMischief(cats: $catName) {
description
severity
}
}
`
it('can push updates using the API', async () => {
const useActsOfMischief = (catName: string) => {
const { data } = useSubscription(MISCHIEF_SUBSCRIPTION, {
variables: { catName },
})
return data
}
const initialData = {
actsOfMischief: {
__typename: 'ActsOfMischief',
description: 'did not stay away from my bins',
severity: 'extreme',
},
}
const { wrapper, link } = hookWrapperWithApolloMocks([
{
request: {
query: MISCHIEF_SUBSCRIPTION,
variables: MATCH_ANY_PARAMETERS,
},
result: { data: initialData },
},
])
const rendered = renderHook(() => useActsOfMischief('la don'), { wrapper })
expect(link.lastSubscriptionMatches(MISCHIEF_SUBSCRIPTION)).toBeTruthy()
expect(link.lastSubscription?.variables).toEqual({ catName: 'la don' })
await waitFor(() => {
expect(rendered.result.current).toEqual(initialData)
})
const updateData = {
actsOfMischief: {
__typename: 'ActsOfMischief',
description: 'pushed that button',
severity: 'mild',
},
}
actHook(() => {
link.sendWildcardSubscriptionResult(MISCHIEF_SUBSCRIPTION, {
data: updateData,
})
})
await waitFor(() => {
expect(rendered.result.current).toEqual(updateData)
})
})
FAQs
apollo client mocking
The npm package wildcard-mock-link receives a total of 7,115 weekly downloads. As such, wildcard-mock-link popularity was classified as popular.
We found that wildcard-mock-link demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.