Skip to content

Commit

Permalink
Pass variables to result function (#91)
Browse files Browse the repository at this point in the history
Pass variables into result function

Co-authored-by: Andrew Dean <a.dean@ract.com.au>
  • Loading branch information
ajdean and Andrew Dean authored Jun 18, 2021
1 parent 83ab266 commit bdc5c99
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
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

0 comments on commit bdc5c99

Please sign in to comment.