|
| 1 | +import { computed, deferredComputed, effect, ref } from '../src' |
| 2 | + |
| 3 | +describe('deferred computed', () => { |
| 4 | + const tick = Promise.resolve() |
| 5 | + |
| 6 | + test('should only trigger once on multiple mutations', async () => { |
| 7 | + const src = ref(0) |
| 8 | + const c = deferredComputed(() => src.value) |
| 9 | + const spy = jest.fn() |
| 10 | + effect(() => { |
| 11 | + spy(c.value) |
| 12 | + }) |
| 13 | + expect(spy).toHaveBeenCalledTimes(1) |
| 14 | + src.value = 1 |
| 15 | + src.value = 2 |
| 16 | + src.value = 3 |
| 17 | + // not called yet |
| 18 | + expect(spy).toHaveBeenCalledTimes(1) |
| 19 | + await tick |
| 20 | + // should only trigger once |
| 21 | + expect(spy).toHaveBeenCalledTimes(2) |
| 22 | + expect(spy).toHaveBeenCalledWith(c.value) |
| 23 | + }) |
| 24 | + |
| 25 | + test('should not trigger if value did not change', async () => { |
| 26 | + const src = ref(0) |
| 27 | + const c = deferredComputed(() => src.value % 2) |
| 28 | + const spy = jest.fn() |
| 29 | + effect(() => { |
| 30 | + spy(c.value) |
| 31 | + }) |
| 32 | + expect(spy).toHaveBeenCalledTimes(1) |
| 33 | + src.value = 1 |
| 34 | + src.value = 2 |
| 35 | + |
| 36 | + await tick |
| 37 | + // should not trigger |
| 38 | + expect(spy).toHaveBeenCalledTimes(1) |
| 39 | + |
| 40 | + src.value = 3 |
| 41 | + src.value = 4 |
| 42 | + src.value = 5 |
| 43 | + await tick |
| 44 | + // should trigger because latest value changes |
| 45 | + expect(spy).toHaveBeenCalledTimes(2) |
| 46 | + }) |
| 47 | + |
| 48 | + test('chained computed trigger', async () => { |
| 49 | + const effectSpy = jest.fn() |
| 50 | + const c1Spy = jest.fn() |
| 51 | + const c2Spy = jest.fn() |
| 52 | + |
| 53 | + const src = ref(0) |
| 54 | + const c1 = deferredComputed(() => { |
| 55 | + c1Spy() |
| 56 | + return src.value % 2 |
| 57 | + }) |
| 58 | + const c2 = computed(() => { |
| 59 | + c2Spy() |
| 60 | + return c1.value + 1 |
| 61 | + }) |
| 62 | + |
| 63 | + effect(() => { |
| 64 | + effectSpy(c2.value) |
| 65 | + }) |
| 66 | + |
| 67 | + expect(c1Spy).toHaveBeenCalledTimes(1) |
| 68 | + expect(c2Spy).toHaveBeenCalledTimes(1) |
| 69 | + expect(effectSpy).toHaveBeenCalledTimes(1) |
| 70 | + |
| 71 | + src.value = 1 |
| 72 | + await tick |
| 73 | + expect(c1Spy).toHaveBeenCalledTimes(2) |
| 74 | + expect(c2Spy).toHaveBeenCalledTimes(2) |
| 75 | + expect(effectSpy).toHaveBeenCalledTimes(2) |
| 76 | + }) |
| 77 | + |
| 78 | + test('chained computed avoid re-compute', async () => { |
| 79 | + const effectSpy = jest.fn() |
| 80 | + const c1Spy = jest.fn() |
| 81 | + const c2Spy = jest.fn() |
| 82 | + |
| 83 | + const src = ref(0) |
| 84 | + const c1 = deferredComputed(() => { |
| 85 | + c1Spy() |
| 86 | + return src.value % 2 |
| 87 | + }) |
| 88 | + const c2 = computed(() => { |
| 89 | + c2Spy() |
| 90 | + return c1.value + 1 |
| 91 | + }) |
| 92 | + |
| 93 | + effect(() => { |
| 94 | + effectSpy(c2.value) |
| 95 | + }) |
| 96 | + |
| 97 | + expect(effectSpy).toHaveBeenCalledTimes(1) |
| 98 | + src.value = 2 |
| 99 | + src.value = 4 |
| 100 | + src.value = 6 |
| 101 | + await tick |
| 102 | + // c1 should re-compute once. |
| 103 | + expect(c1Spy).toHaveBeenCalledTimes(2) |
| 104 | + // c2 should not have to re-compute because c1 did not change. |
| 105 | + expect(c2Spy).toHaveBeenCalledTimes(1) |
| 106 | + // effect should not trigger because c2 did not change. |
| 107 | + expect(effectSpy).toHaveBeenCalledTimes(1) |
| 108 | + }) |
| 109 | + |
| 110 | + test('chained computed value invalidation', async () => { |
| 111 | + const effectSpy = jest.fn() |
| 112 | + const c1Spy = jest.fn() |
| 113 | + const c2Spy = jest.fn() |
| 114 | + |
| 115 | + const src = ref(0) |
| 116 | + const c1 = deferredComputed(() => { |
| 117 | + c1Spy() |
| 118 | + return src.value % 2 |
| 119 | + }) |
| 120 | + const c2 = deferredComputed(() => { |
| 121 | + c2Spy() |
| 122 | + return c1.value + 1 |
| 123 | + }) |
| 124 | + |
| 125 | + effect(() => { |
| 126 | + effectSpy(c2.value) |
| 127 | + }) |
| 128 | + |
| 129 | + expect(effectSpy).toHaveBeenCalledTimes(1) |
| 130 | + expect(effectSpy).toHaveBeenCalledWith(1) |
| 131 | + expect(c2.value).toBe(1) |
| 132 | + |
| 133 | + expect(c1Spy).toHaveBeenCalledTimes(1) |
| 134 | + expect(c2Spy).toHaveBeenCalledTimes(1) |
| 135 | + |
| 136 | + src.value = 1 |
| 137 | + // value should be available sync |
| 138 | + expect(c2.value).toBe(2) |
| 139 | + expect(c2Spy).toHaveBeenCalledTimes(2) |
| 140 | + }) |
| 141 | + |
| 142 | + test('sync access of invalidated chained computed should not prevent final effect from running', async () => { |
| 143 | + const effectSpy = jest.fn() |
| 144 | + const c1Spy = jest.fn() |
| 145 | + const c2Spy = jest.fn() |
| 146 | + |
| 147 | + const src = ref(0) |
| 148 | + const c1 = deferredComputed(() => { |
| 149 | + c1Spy() |
| 150 | + return src.value % 2 |
| 151 | + }) |
| 152 | + const c2 = deferredComputed(() => { |
| 153 | + c2Spy() |
| 154 | + return c1.value + 1 |
| 155 | + }) |
| 156 | + |
| 157 | + effect(() => { |
| 158 | + effectSpy(c2.value) |
| 159 | + }) |
| 160 | + expect(effectSpy).toHaveBeenCalledTimes(1) |
| 161 | + |
| 162 | + src.value = 1 |
| 163 | + // sync access c2 |
| 164 | + c2.value |
| 165 | + await tick |
| 166 | + expect(effectSpy).toHaveBeenCalledTimes(2) |
| 167 | + }) |
| 168 | + |
| 169 | + test('should not compute if deactivated before scheduler is called', async () => { |
| 170 | + const c1Spy = jest.fn() |
| 171 | + const src = ref(0) |
| 172 | + const c1 = deferredComputed(() => { |
| 173 | + c1Spy() |
| 174 | + return src.value % 2 |
| 175 | + }) |
| 176 | + effect(() => c1.value) |
| 177 | + expect(c1Spy).toHaveBeenCalledTimes(1) |
| 178 | + |
| 179 | + c1.effect.stop() |
| 180 | + // trigger |
| 181 | + src.value++ |
| 182 | + await tick |
| 183 | + expect(c1Spy).toHaveBeenCalledTimes(1) |
| 184 | + }) |
| 185 | +}) |
0 commit comments