|
1 | 1 | import { ORPCError } from '@orpc/client'
|
| 2 | +import { HibernationEventIterator } from '@orpc/standard-server' |
2 | 3 | import * as z from 'zod'
|
3 | 4 | import { createORPCErrorConstructorMap, validateORPCError } from './error'
|
4 | 5 | import { isLazy, lazy, unlazy } from './lazy'
|
@@ -39,6 +40,16 @@ const procedure = new Procedure({
|
39 | 40 | meta: {},
|
40 | 41 | })
|
41 | 42 |
|
| 43 | +const unvalidatedProcedure = new Procedure({ |
| 44 | + errorMap: baseErrors, |
| 45 | + route: {}, |
| 46 | + handler, |
| 47 | + middlewares: [preMid1, preMid2, postMid1, postMid2], |
| 48 | + inputValidationIndex: 2, |
| 49 | + outputValidationIndex: 2, |
| 50 | + meta: {}, |
| 51 | +}) |
| 52 | + |
42 | 53 | const procedureCases = [
|
43 | 54 | ['without lazy', procedure],
|
44 | 55 | ['with lazy', lazy(() => Promise.resolve({ default: procedure }))],
|
@@ -462,6 +473,39 @@ describe.each(procedureCases)('createProcedureClient - case %s', async (_, proce
|
462 | 473 | expect(validateORPCError).toBeCalledTimes(1)
|
463 | 474 | expect(validateORPCError).toBeCalledWith(baseErrors, e1)
|
464 | 475 | })
|
| 476 | + |
| 477 | + describe('event iterator', async () => { |
| 478 | + const client = createProcedureClient(unvalidatedProcedure) |
| 479 | + |
| 480 | + it('throw non-ORPCError right away', async () => { |
| 481 | + const e1 = new Error('non-ORPC Error') |
| 482 | + handler.mockImplementationOnce(async function* () { |
| 483 | + throw e1 |
| 484 | + } as any) |
| 485 | + |
| 486 | + const iterator = await client({ val: '123' }) as any |
| 487 | + |
| 488 | + await expect(iterator.next()).rejects.toBe(e1) |
| 489 | + }) |
| 490 | + |
| 491 | + it('validate ORPC Error', async () => { |
| 492 | + const e1 = new ORPCError('BAD_REQUEST') |
| 493 | + const e2 = new ORPCError('BAD_REQUEST', { defined: true }) |
| 494 | + |
| 495 | + handler.mockImplementationOnce(async function* () { |
| 496 | + throw e1 |
| 497 | + } as any) |
| 498 | + vi.mocked(validateORPCError).mockReturnValueOnce(Promise.resolve(e2)) |
| 499 | + |
| 500 | + // signal here for test coverage |
| 501 | + const iterator = await client({ val: '123' }, { signal: AbortSignal.timeout(10) }) as any |
| 502 | + |
| 503 | + await expect(iterator.next()).rejects.toBe(e2) |
| 504 | + |
| 505 | + expect(validateORPCError).toBeCalledTimes(1) |
| 506 | + expect(validateORPCError).toBeCalledWith(baseErrors, e1) |
| 507 | + }) |
| 508 | + }) |
465 | 509 | })
|
466 | 510 |
|
467 | 511 | it('with client context', async () => {
|
@@ -510,6 +554,13 @@ describe.each(procedureCases)('createProcedureClient - case %s', async (_, proce
|
510 | 554 | expect((handler as any).mock.calls[3][0].context.preMid2).toBe(6)
|
511 | 555 | expect((handler as any).mock.calls[3][0].context.postMid1).toBe(7)
|
512 | 556 | })
|
| 557 | + |
| 558 | + it('not modify HibernationEventIterator', async () => { |
| 559 | + const client = createProcedureClient(unvalidatedProcedure) |
| 560 | + const iterator = new HibernationEventIterator(() => {}) |
| 561 | + handler.mockResolvedValueOnce(iterator as any) |
| 562 | + await expect(client({ val: '123' })).resolves.toBe(iterator) |
| 563 | + }) |
513 | 564 | })
|
514 | 565 |
|
515 | 566 | it('still work without InputSchema', async () => {
|
|
0 commit comments