-
-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic tests for persistedExchange
- Loading branch information
Showing
3 changed files
with
146 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { | ||
Source, | ||
pipe, | ||
fromValue, | ||
fromArray, | ||
toPromise, | ||
delay, | ||
take, | ||
tap, | ||
map, | ||
} from 'wonka'; | ||
|
||
import { Client, Operation, OperationResult, CombinedError } from '@urql/core'; | ||
|
||
import { vi, expect, it } from 'vitest'; | ||
import { queryOperation } from '../../../packages/core/src/test-utils'; | ||
import { persistedExchange } from './persistedExchange'; | ||
|
||
const makeExchangeArgs = () => { | ||
const operations: Operation[] = []; | ||
|
||
const result = vi.fn( | ||
(operation: Operation): OperationResult => ({ operation }) | ||
); | ||
|
||
return { | ||
operations, | ||
result, | ||
exchangeArgs: { | ||
forward: (op$: Source<Operation>) => | ||
pipe( | ||
op$, | ||
tap(op => operations.push(op)), | ||
map(result) | ||
), | ||
client: new Client({ url: '/api' }), | ||
} as any, | ||
}; | ||
}; | ||
|
||
it('adds the APQ extensions correctly', async () => { | ||
const { exchangeArgs } = makeExchangeArgs(); | ||
|
||
const res = await pipe( | ||
fromValue(queryOperation), | ||
persistedExchange()(exchangeArgs), | ||
take(1), | ||
toPromise | ||
); | ||
|
||
expect(res.operation.context.persistAttempt).toBe(true); | ||
expect(res.operation.extensions).toEqual({ | ||
persistedQuery: { | ||
version: 1, | ||
sha256Hash: expect.any(String), | ||
miss: undefined, | ||
}, | ||
}); | ||
}); | ||
|
||
it('retries query when persisted query resulted in miss', async () => { | ||
const { result, operations, exchangeArgs } = makeExchangeArgs(); | ||
|
||
result.mockImplementationOnce(operation => ({ | ||
operation, | ||
error: new CombinedError({ | ||
graphQLErrors: [{ message: 'PersistedQueryNotFound' }], | ||
}), | ||
})); | ||
|
||
const res = await pipe( | ||
fromValue(queryOperation), | ||
persistedExchange()(exchangeArgs), | ||
take(1), | ||
toPromise | ||
); | ||
|
||
expect(res.operation.context.persistAttempt).toBe(true); | ||
expect(operations.length).toBe(2); | ||
|
||
expect(operations[1].extensions).toEqual({ | ||
persistedQuery: { | ||
version: 1, | ||
sha256Hash: expect.any(String), | ||
miss: true, | ||
}, | ||
}); | ||
}); | ||
|
||
it('retries query persisted query resulted in unsupported', async () => { | ||
const { result, operations, exchangeArgs } = makeExchangeArgs(); | ||
|
||
result.mockImplementationOnce(operation => ({ | ||
operation, | ||
error: new CombinedError({ | ||
graphQLErrors: [{ message: 'PersistedQueryNotSupported' }], | ||
}), | ||
})); | ||
|
||
await pipe( | ||
fromArray([queryOperation, queryOperation]), | ||
delay(0), | ||
persistedExchange()(exchangeArgs), | ||
take(2), | ||
toPromise | ||
); | ||
|
||
expect(operations.length).toBe(3); | ||
|
||
expect(operations[1].extensions).toEqual({ | ||
persistedQuery: undefined, | ||
}); | ||
|
||
expect(operations[2].extensions).toEqual(undefined); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters