Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass variables to result function #91

Merged
merged 4 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,45 @@ describe('WildcardMockLink', () => {
})
})
})

describe('handles wildcard queries with response function', () => {
it('for a single request', async () => {
const data = {
qualities: {
__typename: 'Qualities',
loveliness: 'scruffy',
},
}
const { wrapper, link } = hookWrapperWithApolloMocks(
[
{
request: {
query: CAT_QUALITIES_QUERY,
variables: MATCH_ANY_PARAMETERS,
},
result: (variables) => {
return {
data: {
qualities: {
__typename: 'Qualities',
loveliness: variables?.catName,
},
},
}
},
},
],
{ act: actHook },
)
const { result } = renderHook(() => useQueryOnce('scruffy'), {
wrapper,
})
await link.waitForLastResponse()
expect(link.lastQueryMatches(CAT_QUALITIES_QUERY)).toBeTruthy()
expect(link.lastQuery?.variables).toEqual({ catName: 'scruffy' })
expect(result.current).toEqual(data)
})
})
})

describe('can be used to mock subscriptions', () => {
Expand Down
33 changes: 24 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ export interface GraphQLRequestWithWildcard
variables?: GraphQLRequest['variables'] | typeof MATCH_ANY_PARAMETERS
}

interface MockedResponseWithMatchCount extends MockedResponse {
interface MockedResponseWithMatchCount extends Omit<MockedResponse, 'result'> {
/** Use Number.POSITIVE_INFINITY to allow infinite matches */
nMatches?: number
result?: FetchResult | ((variables?: GraphQLVariables) => FetchResult)
}

type WildcardMock = Omit<MockedResponseWithMatchCount, 'request'>
type WildcardMock = Omit<MockedResponseWithMatchCount, 'request'> & {
request: GraphQLRequest
}

export interface WildcardMockedResponse extends WildcardMock {
export interface WildcardMockedResponse
extends Omit<Omit<WildcardMock, 'request'>, 'result'> {
request: GraphQLRequestWithWildcard
result?: FetchResult | ((variables?: GraphQLVariables) => FetchResult)
}

export type MockedResponses = readonly WildcardMockedResponse[]
Expand All @@ -67,8 +72,9 @@ function isNotWildcard(
}

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

interface StoredOperation {
query: DocumentNode
Expand All @@ -90,11 +96,11 @@ const forwardResponseToObserver = (
complete: boolean,
act: Act,
): void => {
const { result, error, delay } = response
const { result, error, delay, request } = response
if (result) {
setTimeout(() => {
act(() => {
observer.next(getResultFromFetchResult(result))
observer.next(getResultFromFetchResult(result, request.variables))
})
if (complete) {
observer.complete()
Expand Down Expand Up @@ -347,6 +353,13 @@ export class WildcardMockLink extends ApolloLink {
result: response.result,
nMatches: response.nMatches,
delay: response.delay ?? 0,
request: {
...response.request,
variables:
response.request.variables === MATCH_ANY_PARAMETERS
? undefined
: response.request.variables,
},
}
if (storedMocks) {
storedMocks.push(storedMock)
Expand Down Expand Up @@ -469,7 +482,10 @@ export class WildcardMockLink extends ApolloLink {
this.wildcardMatches.delete(mockKey)
}
}
return nextMock
return {
...nextMock,
request: { ...nextMock.request, variables: op.variables },
}
}

private getRegularMockMatch(
Expand Down Expand Up @@ -501,7 +517,6 @@ export class WildcardMockLink extends ApolloLink {
this.pendingResponses.delete(responsePromise)
})
})

this.lastResponse = responsePromise
this.pendingResponses.add(responsePromise)
}
Expand Down