Skip to content

Commit

Permalink
Test that computed can recover from a throwing equal implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
leonsenft committed Dec 10, 2024
1 parent b06e445 commit 6f9ae26
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion packages/core/test/signals/computed_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {computed, signal} from '@angular/core';
import {createWatch, ReactiveNode, SIGNAL} from '@angular/core/primitives/signals';
import {createWatch, ReactiveNode, SIGNAL, defaultEquals} from '@angular/core/primitives/signals';

describe('computed', () => {
it('should create computed', () => {
Expand Down Expand Up @@ -291,5 +291,30 @@ describe('computed', () => {
// context either.
expect(outerRunCount).toBe(1);
});

it('should recover from exception', () => {
let shouldThrow = true;
const source = signal(0);
const derived = computed(source, {
equal: (a, b) => {
if (shouldThrow) {
throw new Error('equal');
}
return defaultEquals(a, b);
},
});

// Initial read doesn't throw because it doesn't call `equal`.
expect(derived()).toBe(0);

// Update `source` to begin throwing.
source.set(1);
expect(() => derived()).toThrowError('equal');

// Stop throwing and update `source` to cause `derived` to recompute.
shouldThrow = false;
source.set(2);
expect(derived()).toBe(2);
});
});
});

0 comments on commit 6f9ae26

Please sign in to comment.