|
| 1 | +import{ |
| 2 | +EffectScope, |
| 3 | +typeRef, |
| 4 | +WatchErrorCodes, |
| 5 | +typeWatchOptions, |
| 6 | +typeWatchScheduler, |
| 7 | +onWatcherCleanup, |
| 8 | +ref, |
| 9 | +watch, |
| 10 | +}from'../src' |
| 11 | + |
| 12 | +constqueue:(()=>void)[]=[] |
| 13 | + |
| 14 | +// a simple scheduler for testing purposes |
| 15 | +letisFlushPending=false |
| 16 | +constresolvedPromise=/*#__PURE__*/Promise.resolve()asPromise<any> |
| 17 | +constnextTick=(fn?:()=>any)=> |
| 18 | +fn ?resolvedPromise.then(fn) :resolvedPromise |
| 19 | + |
| 20 | +constscheduler:WatchScheduler=(job,isFirstRun)=>{ |
| 21 | +if(isFirstRun){ |
| 22 | +job() |
| 23 | +}else{ |
| 24 | +queue.push(job) |
| 25 | +flushJobs() |
| 26 | +} |
| 27 | +} |
| 28 | + |
| 29 | +constflushJobs=()=>{ |
| 30 | +if(isFlushPending)return |
| 31 | +isFlushPending=true |
| 32 | +resolvedPromise.then(()=>{ |
| 33 | +queue.forEach(job=>job()) |
| 34 | +queue.length=0 |
| 35 | +isFlushPending=false |
| 36 | +}) |
| 37 | +} |
| 38 | + |
| 39 | +describe('watch',()=>{ |
| 40 | +test('effect',()=>{ |
| 41 | +letdummy:any |
| 42 | +constsource=ref(0) |
| 43 | +watch(()=>{ |
| 44 | +dummy=source.value |
| 45 | +}) |
| 46 | +expect(dummy).toBe(0) |
| 47 | +source.value++ |
| 48 | +expect(dummy).toBe(1) |
| 49 | +}) |
| 50 | + |
| 51 | +test('with callback',()=>{ |
| 52 | +letdummy:any |
| 53 | +constsource=ref(0) |
| 54 | +watch(source,()=>{ |
| 55 | +dummy=source.value |
| 56 | +}) |
| 57 | +expect(dummy).toBe(undefined) |
| 58 | +source.value++ |
| 59 | +expect(dummy).toBe(1) |
| 60 | +}) |
| 61 | + |
| 62 | +test('call option with error handling',()=>{ |
| 63 | +constonError=vi.fn() |
| 64 | +constcall:WatchOptions['call']=functioncall(fn,type,args){ |
| 65 | +if(Array.isArray(fn)){ |
| 66 | +fn.forEach(f=>call(f,type,args)) |
| 67 | +return |
| 68 | +} |
| 69 | +try{ |
| 70 | +fn.apply(null,args) |
| 71 | +}catch(e){ |
| 72 | +onError(e,type) |
| 73 | +} |
| 74 | +} |
| 75 | + |
| 76 | +watch( |
| 77 | +()=>{ |
| 78 | +throw'oops in effect' |
| 79 | +}, |
| 80 | +null, |
| 81 | +{ call}, |
| 82 | +) |
| 83 | + |
| 84 | +constsource=ref(0) |
| 85 | +consteffect=watch( |
| 86 | +source, |
| 87 | +()=>{ |
| 88 | +onWatcherCleanup(()=>{ |
| 89 | +throw'oops in cleanup' |
| 90 | +}) |
| 91 | +throw'oops in watch' |
| 92 | +}, |
| 93 | +{ call}, |
| 94 | +) |
| 95 | + |
| 96 | +expect(onError.mock.calls.length).toBe(1) |
| 97 | +expect(onError.mock.calls[0]).toMatchObject([ |
| 98 | +'oops in effect', |
| 99 | +WatchErrorCodes.WATCH_CALLBACK, |
| 100 | +]) |
| 101 | + |
| 102 | +source.value++ |
| 103 | +expect(onError.mock.calls.length).toBe(2) |
| 104 | +expect(onError.mock.calls[1]).toMatchObject([ |
| 105 | +'oops in watch', |
| 106 | +WatchErrorCodes.WATCH_CALLBACK, |
| 107 | +]) |
| 108 | + |
| 109 | +effect!.stop() |
| 110 | +source.value++ |
| 111 | +expect(onError.mock.calls.length).toBe(3) |
| 112 | +expect(onError.mock.calls[2]).toMatchObject([ |
| 113 | +'oops in cleanup', |
| 114 | +WatchErrorCodes.WATCH_CLEANUP, |
| 115 | +]) |
| 116 | +}) |
| 117 | + |
| 118 | +test('watch with onWatcherCleanup',async()=>{ |
| 119 | +letdummy=0 |
| 120 | +letsource:Ref<number> |
| 121 | +constscope=newEffectScope() |
| 122 | + |
| 123 | +scope.run(()=>{ |
| 124 | +source=ref(0) |
| 125 | +watch(onCleanup=>{ |
| 126 | +source.value |
| 127 | + |
| 128 | +onCleanup(()=>(dummy+=2)) |
| 129 | +onWatcherCleanup(()=>(dummy+=3)) |
| 130 | +onWatcherCleanup(()=>(dummy+=5)) |
| 131 | +}) |
| 132 | +}) |
| 133 | +expect(dummy).toBe(0) |
| 134 | + |
| 135 | +scope.run(()=>{ |
| 136 | +source.value++ |
| 137 | +}) |
| 138 | +expect(dummy).toBe(10) |
| 139 | + |
| 140 | +scope.run(()=>{ |
| 141 | +source.value++ |
| 142 | +}) |
| 143 | +expect(dummy).toBe(20) |
| 144 | + |
| 145 | +scope.stop() |
| 146 | +expect(dummy).toBe(30) |
| 147 | +}) |
| 148 | + |
| 149 | +test('nested calls to baseWatch and onWatcherCleanup',async()=>{ |
| 150 | +letcalls:string[]=[] |
| 151 | +letsource:Ref<number> |
| 152 | +letcopyist:Ref<number> |
| 153 | +constscope=newEffectScope() |
| 154 | + |
| 155 | +scope.run(()=>{ |
| 156 | +source=ref(0) |
| 157 | +copyist=ref(0) |
| 158 | +// sync by default |
| 159 | +watch( |
| 160 | +()=>{ |
| 161 | +constcurrent=(copyist.value=source.value) |
| 162 | +onWatcherCleanup(()=>calls.push(`sync${current}`)) |
| 163 | +}, |
| 164 | +null, |
| 165 | +{}, |
| 166 | +) |
| 167 | +// with scheduler |
| 168 | +watch( |
| 169 | +()=>{ |
| 170 | +constcurrent=copyist.value |
| 171 | +onWatcherCleanup(()=>calls.push(`post${current}`)) |
| 172 | +}, |
| 173 | +null, |
| 174 | +{ scheduler}, |
| 175 | +) |
| 176 | +}) |
| 177 | + |
| 178 | +awaitnextTick() |
| 179 | +expect(calls).toEqual([]) |
| 180 | + |
| 181 | +scope.run(()=>source.value++) |
| 182 | +expect(calls).toEqual(['sync 0']) |
| 183 | +awaitnextTick() |
| 184 | +expect(calls).toEqual(['sync 0','post 0']) |
| 185 | +calls.length=0 |
| 186 | + |
| 187 | +scope.run(()=>source.value++) |
| 188 | +expect(calls).toEqual(['sync 1']) |
| 189 | +awaitnextTick() |
| 190 | +expect(calls).toEqual(['sync 1','post 1']) |
| 191 | +calls.length=0 |
| 192 | + |
| 193 | +scope.stop() |
| 194 | +expect(calls).toEqual(['sync 2','post 2']) |
| 195 | +}) |
| 196 | +}) |