|
| 1 | +import Vue from "vue" |
| 2 | +import Composition from "@vue/composition-api" |
| 3 | +import { ref, Ref, isRef, computed } from "@vue/composition-api" |
| 4 | + |
| 5 | +Vue.use(Composition) |
| 6 | + |
| 7 | +import { useSwitchMap, SetCleanupFunction } from "../src" |
| 8 | +import { timer } from "./util" |
| 9 | + |
| 10 | +const pure = <T>(v: T) => ref(v) |
| 11 | +const pureRefProjection = <T>(v: T, cleanup: SetCleanupFunction) => (pure(v) as Ref<T>) // see https://github.com/vuejs/vue-next/issues/1324#issuecomment-641150163 |
| 12 | +const pureRefObjProjection = <T>(v: T, cleanup: SetCleanupFunction) => (pure({ ...v }) as Ref<T>) // change the reference |
| 13 | + |
| 14 | +describe('it should pass', () => { |
| 15 | + it('should pass', () => { |
| 16 | + expect(true).toBeTruthy() |
| 17 | + }) |
| 18 | +}) |
| 19 | + |
| 20 | +describe('base ref', () => { |
| 21 | + |
| 22 | + it('should return a ref', () => { |
| 23 | + expect(isRef(useSwitchMap(pure(10), pureRefProjection))).toBeTruthy() |
| 24 | + }) |
| 25 | + |
| 26 | + it('should return a ref with value 10', () => { |
| 27 | + const tenRef = useSwitchMap(pure(10), pureRefProjection); |
| 28 | + expect(tenRef.value).toBe(10) |
| 29 | + }) |
| 30 | + |
| 31 | + it('should be updatetd to value 11', async () => { |
| 32 | + const tenRef = pure(10) |
| 33 | + const cloneTenRef = useSwitchMap(tenRef, pureRefProjection); |
| 34 | + |
| 35 | + tenRef.value++ |
| 36 | + // apart for the initial setting, |
| 37 | + // watchers do their work after the current tick |
| 38 | + await timer(0) |
| 39 | + expect(cloneTenRef.value).toBe(11) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should be updatetd to value 11', (done) => { |
| 43 | + const tenRef = pure(10) |
| 44 | + const cloneTenRef = useSwitchMap(tenRef, (v, c) => { |
| 45 | + |
| 46 | + const cloneRef = pure(v) |
| 47 | + |
| 48 | + setTimeout(async () => { |
| 49 | + cloneRef.value++; |
| 50 | + await timer(0) |
| 51 | + |
| 52 | + test() |
| 53 | + done() |
| 54 | + }, 100) |
| 55 | + |
| 56 | + return cloneRef |
| 57 | + }); |
| 58 | + |
| 59 | + function test() { |
| 60 | + expect(cloneTenRef.value).toBe(11) |
| 61 | + done() |
| 62 | + } |
| 63 | + }) |
| 64 | + |
| 65 | + it(`should return a ref with value '10'`, () => { |
| 66 | + const tenRef = pure(10) |
| 67 | + const cloneTenRef = useSwitchMap(tenRef, (v, c) => pureRefProjection(String(v), c)); |
| 68 | + expect(cloneTenRef.value).toBe("10") |
| 69 | + }) |
| 70 | + |
| 71 | + it(`should be updated with value '11'`, async () => { |
| 72 | + const tenRef = pure(10) |
| 73 | + const cloneTenRef = useSwitchMap(tenRef, (v, c) => pureRefProjection(String(v), c)); |
| 74 | + |
| 75 | + tenRef.value++ |
| 76 | + await timer(0) |
| 77 | + expect(cloneTenRef.value).toBe("11") |
| 78 | + }) |
| 79 | + |
| 80 | + it(`should be updated with values 9 -> 11 -> 10`, async () => { |
| 81 | + const tenRef = pure(10) |
| 82 | + const cloneTenRef = useSwitchMap(tenRef, pureRefProjection); |
| 83 | + const cloneCloneTenRef = computed(() => cloneTenRef.value) |
| 84 | + |
| 85 | + tenRef.value = 9 |
| 86 | + await timer(0) |
| 87 | + expect(cloneTenRef.value).toBe(9) |
| 88 | + expect(cloneCloneTenRef.value).toBe(9) |
| 89 | + |
| 90 | + cloneTenRef.value = 11 |
| 91 | + await timer(0) |
| 92 | + expect(cloneTenRef.value).toBe(11) |
| 93 | + expect(cloneCloneTenRef.value).toBe(11) |
| 94 | + |
| 95 | + tenRef.value = 10 |
| 96 | + await timer(0) |
| 97 | + expect(cloneTenRef.value).toBe(10) |
| 98 | + expect(cloneCloneTenRef.value).toBe(10) |
| 99 | + |
| 100 | + }) |
| 101 | + |
| 102 | +}) |
| 103 | + |
| 104 | +describe('object ref', () => { |
| 105 | + |
| 106 | + |
| 107 | + it('should return a ref with value { data: 10 }', () => { |
| 108 | + const tenObjRef = useSwitchMap(pure({ data: 10 }), pureRefObjProjection); |
| 109 | + expect(tenObjRef.value).toEqual({ data: 10 }) |
| 110 | + }) |
| 111 | + |
| 112 | + it('should be different references', async () => { |
| 113 | + const tenObjRef = pure({ data: 10 }) |
| 114 | + const cloneTenObjRef = useSwitchMap(tenObjRef, pureRefObjProjection); |
| 115 | + expect(tenObjRef).not.toBe(cloneTenObjRef) |
| 116 | + }) |
| 117 | + |
| 118 | + it('should be updatetd to value { data: 11 }', async () => { |
| 119 | + const tenObjRef = pure({ data: 10 }) |
| 120 | + const cloneTenObjRef = useSwitchMap(tenObjRef, pureRefObjProjection); |
| 121 | + |
| 122 | + tenObjRef.value.data++ |
| 123 | + await timer(0) |
| 124 | + expect(cloneTenObjRef.value).toEqual({ data: 11 }) |
| 125 | + }) |
| 126 | + |
| 127 | + it('should be updatetd to value { data: 11 }', (done) => { |
| 128 | + const tenObjRef = pure({ data: 10 }) |
| 129 | + const cloneTenObjRef = useSwitchMap(tenObjRef, (v, c) => { |
| 130 | + const clone = { ...v } |
| 131 | + const cloneRef = pure(clone) |
| 132 | + |
| 133 | + setTimeout(async () => { |
| 134 | + cloneRef.value.data++; |
| 135 | + await timer(0) |
| 136 | + |
| 137 | + test() |
| 138 | + done() |
| 139 | + }, 100) |
| 140 | + |
| 141 | + return cloneRef |
| 142 | + }); |
| 143 | + |
| 144 | + function test() { |
| 145 | + expect(cloneTenObjRef.value).toEqual({ data: 11 }) |
| 146 | + done() |
| 147 | + } |
| 148 | + }) |
| 149 | + |
| 150 | +}) |
| 151 | + |
| 152 | +describe('cleanup', () => { |
| 153 | + |
| 154 | + |
| 155 | + it(`should transform the returned ref into '0!!!' and there should not be cleanup calls`, async () => { |
| 156 | + const counterRef = ref(0) |
| 157 | + |
| 158 | + function incrementCounter() { |
| 159 | + counterRef.value++ |
| 160 | + } |
| 161 | + |
| 162 | + let mockedCleanup: any; |
| 163 | + const switchMappedRef = useSwitchMap(counterRef, (value, cleanup) => { |
| 164 | + const newRef = ref(`${value} is now a string`) |
| 165 | + |
| 166 | + const interval = setInterval(() => { |
| 167 | + newRef.value += "!" |
| 168 | + }, 1000) |
| 169 | + |
| 170 | + mockedCleanup = jest.fn(() => { |
| 171 | + clearInterval(interval) |
| 172 | + }); |
| 173 | + |
| 174 | + cleanup(mockedCleanup) |
| 175 | + |
| 176 | + return newRef; |
| 177 | + }) |
| 178 | + |
| 179 | + |
| 180 | + await timer(3200) |
| 181 | + |
| 182 | + expect(switchMappedRef.value).toBe("0 is now a string!!!") |
| 183 | + expect(mockedCleanup!.mock.calls.length).toBe(0) |
| 184 | + |
| 185 | + }) |
| 186 | + |
| 187 | + it(`should transform the returned ref into '4!!!' and there should be 4 cleanup calls`, () => { |
| 188 | + // THIS IS A MESS WITH OR WITHOUT JEST TIMERS. PLZ HELP XD |
| 189 | + expect(1).toBe(1) |
| 190 | + }) |
| 191 | +}) |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
0 commit comments