|
| 1 | +import {describe, it, expect, vi} from 'vitest'; |
| 2 | +import {Signal} from '../../../src'; |
| 3 | + |
| 4 | +describe('Ported - Preact', () => { |
| 5 | + // https://github.com/preactjs/signals/blob/main/packages/core/test/signal.test.tsx#L1078 |
| 6 | + it('should not leak errors raised by dependencies', () => { |
| 7 | + const a = new Signal.State(0); |
| 8 | + const b = new Signal.Computed(() => { |
| 9 | + a.get(); |
| 10 | + throw new Error('error'); |
| 11 | + }); |
| 12 | + const c = new Signal.Computed(() => { |
| 13 | + try { |
| 14 | + b.get(); |
| 15 | + } catch { |
| 16 | + return 'ok'; |
| 17 | + } |
| 18 | + }); |
| 19 | + expect(c.get()).to.equal('ok'); |
| 20 | + a.set(1); |
| 21 | + expect(c.get()).to.equal('ok'); |
| 22 | + }); |
| 23 | + |
| 24 | + // https://github.com/preactjs/signals/blob/main/packages/core/test/signal.test.tsx#L914 |
| 25 | + it('should return updated value', () => { |
| 26 | + const a = new Signal.State('a'); |
| 27 | + const b = new Signal.State('b'); |
| 28 | + |
| 29 | + const c = new Signal.Computed(() => a.get() + b.get()); |
| 30 | + expect(c.get()).to.equal('ab'); |
| 31 | + |
| 32 | + a.set('aa'); |
| 33 | + expect(c.get()).to.equal('aab'); |
| 34 | + }); |
| 35 | + |
| 36 | + // https://github.com/preactjs/signals/blob/main/packages/core/test/signal.test.tsx#L925 |
| 37 | + it('should be lazily computed on demand', () => { |
| 38 | + const a = new Signal.State('a'); |
| 39 | + const b = new Signal.State('b'); |
| 40 | + const spy = vi.fn(() => a.get() + b.get()); |
| 41 | + const c = new Signal.Computed(spy); |
| 42 | + expect(spy).not.toHaveBeenCalled(); |
| 43 | + c.get(); |
| 44 | + expect(spy).toHaveBeenCalledOnce(); |
| 45 | + a.set('x'); |
| 46 | + b.set('y'); |
| 47 | + expect(spy).toHaveBeenCalledOnce(); |
| 48 | + c.get(); |
| 49 | + expect(spy).toHaveBeenCalledTimes(2); |
| 50 | + }); |
| 51 | + |
| 52 | + // https://github.com/preactjs/signals/blob/main/packages/core/test/signal.test.tsx#L940 |
| 53 | + it('should be computed only when a dependency has changed at some point', () => { |
| 54 | + const a = new Signal.State('a'); |
| 55 | + const spy = vi.fn(() => { |
| 56 | + return a.get(); |
| 57 | + }); |
| 58 | + const c = new Signal.Computed(spy); |
| 59 | + c.get(); |
| 60 | + expect(spy).toHaveBeenCalledOnce(); |
| 61 | + a.set('a'); |
| 62 | + c.get(); |
| 63 | + expect(spy).toHaveBeenCalledOnce(); |
| 64 | + }); |
| 65 | + |
| 66 | + // https://github.com/preactjs/signals/blob/main/packages/core/test/signal.test.tsx#L1693 |
| 67 | + it('should support lazy branches', () => { |
| 68 | + const a = new Signal.State(0); |
| 69 | + const b = new Signal.Computed(() => a.get()); |
| 70 | + const c = new Signal.Computed(() => (a.get() > 0 ? a.get() : b.get())); |
| 71 | + |
| 72 | + expect(c.get()).to.equal(0); |
| 73 | + a.set(1); |
| 74 | + expect(c.get()).to.equal(1); |
| 75 | + |
| 76 | + a.set(0); |
| 77 | + expect(c.get()).to.equal(0); |
| 78 | + }); |
| 79 | +}); |
0 commit comments