|  | 
| 1 | 1 | import { Component, input, signal } from '@angular/core' | 
| 2 | 2 | import { QueryClient } from '@tanstack/query-core' | 
| 3 | 3 | import { TestBed } from '@angular/core/testing' | 
| 4 |  | -import { describe, expect, test, vi } from 'vitest' | 
|  | 4 | +import { describe, expect, vi } from 'vitest' | 
| 5 | 5 | import { By } from '@angular/platform-browser' | 
| 6 | 6 | import { injectMutation } from '../inject-mutation' | 
| 7 | 7 | import { provideAngularQuery } from '../providers' | 
| @@ -402,4 +402,57 @@ describe('injectMutation', () => { | 
| 402 | 402 |     expect(mutation1!.options.mutationKey).toEqual(['fake', 'value']) | 
| 403 | 403 |     expect(mutation2!.options.mutationKey).toEqual(['fake', 'updatedValue']) | 
| 404 | 404 |   }) | 
|  | 405 | + | 
|  | 406 | +  describe('throwOnError', () => { | 
|  | 407 | +    test('should evaluate throwOnError when mutation is expected to throw', async () => { | 
|  | 408 | +      const err = new Error('Expected mock error. All is well!') | 
|  | 409 | +      const boundaryFn = vi.fn() | 
|  | 410 | +      const { mutate } = TestBed.runInInjectionContext(() => { | 
|  | 411 | +        return injectMutation(() => ({ | 
|  | 412 | +          mutationKey: ['fake'], | 
|  | 413 | +          mutationFn: () => { | 
|  | 414 | +            return Promise.reject(err) | 
|  | 415 | +          }, | 
|  | 416 | +          throwOnError: boundaryFn, | 
|  | 417 | +        })) | 
|  | 418 | +      }) | 
|  | 419 | + | 
|  | 420 | +      mutate() | 
|  | 421 | + | 
|  | 422 | +      await resolveMutations() | 
|  | 423 | + | 
|  | 424 | +      expect(boundaryFn).toHaveBeenCalledTimes(1) | 
|  | 425 | +      expect(boundaryFn).toHaveBeenCalledWith(err) | 
|  | 426 | +    }) | 
|  | 427 | +  }) | 
|  | 428 | + | 
|  | 429 | +  test('should throw when throwOnError is true', async () => { | 
|  | 430 | +    const err = new Error('Expected mock error. All is well!') | 
|  | 431 | +    const { mutateAsync } = TestBed.runInInjectionContext(() => { | 
|  | 432 | +      return injectMutation(() => ({ | 
|  | 433 | +        mutationKey: ['fake'], | 
|  | 434 | +        mutationFn: () => { | 
|  | 435 | +          return Promise.reject(err) | 
|  | 436 | +        }, | 
|  | 437 | +        throwOnError: true, | 
|  | 438 | +      })) | 
|  | 439 | +    }) | 
|  | 440 | + | 
|  | 441 | +    await expect(() => mutateAsync()).rejects.toThrowError(err) | 
|  | 442 | +  }) | 
|  | 443 | + | 
|  | 444 | +  test('should throw when throwOnError function returns true', async () => { | 
|  | 445 | +    const err = new Error('Expected mock error. All is well!') | 
|  | 446 | +    const { mutateAsync } = TestBed.runInInjectionContext(() => { | 
|  | 447 | +      return injectMutation(() => ({ | 
|  | 448 | +        mutationKey: ['fake'], | 
|  | 449 | +        mutationFn: () => { | 
|  | 450 | +          return Promise.reject(err) | 
|  | 451 | +        }, | 
|  | 452 | +        throwOnError: () => true, | 
|  | 453 | +      })) | 
|  | 454 | +    }) | 
|  | 455 | + | 
|  | 456 | +    await expect(() => mutateAsync()).rejects.toThrowError(err) | 
|  | 457 | +  }) | 
| 405 | 458 | }) | 
0 commit comments