Skip to content

Commit d681ed0

Browse files
committed
add tests
1 parent e818e75 commit d681ed0

File tree

6 files changed

+305
-5
lines changed

6 files changed

+305
-5
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
dist
2-
node_modules
3-
test
2+
node_modules

jest.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
2-
verbose: true,
2+
verbose: false,
33
collectCoverage: true,
4-
coverageReporters: ['text'],
4+
coverageReporters: ['text', 'html'],
55
preset: 'ts-jest',
66
testRegex: '/test/.*\\.test\\.ts$',
77
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"watch": "tsc --watch",
1313
"types:check": "tsc --noEmit",
1414
"test": "jest",
15-
"test:watch": "jest --watchAll"
15+
"test:watch": "jest --watchAll --verbose false"
1616
},
1717
"files": [
1818
"dist"

test/useSwitchMap.test.ts

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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+

test/useSwitchMapO.test.ts

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 { useSwitchMapO, SetCleanupFunction } from "../src"
8+
import { timer } from "./util"
9+
10+
const testObj = {
11+
p1: ref(0),
12+
p2: ref(""),
13+
p3: ref({
14+
p4: true
15+
}),
16+
p5: 42
17+
}
18+
19+
const tOProjection = <T>(v: T, cleanup: SetCleanupFunction) => {
20+
return testObj
21+
}
22+
23+
const threePropsProjectionInnerMutation = <T>(v: T, cleanup: SetCleanupFunction) => {
24+
const toRet = { number: ref(0), string: ref(""), boolean: true }
25+
26+
const interval = setInterval(() => {
27+
console.log("setInterval")
28+
toRet.number.value++;
29+
toRet.string.value += "!"
30+
}, 500)
31+
32+
cleanup(() => {
33+
console.log("clearInterval")
34+
clearInterval(interval)
35+
})
36+
37+
return toRet
38+
}
39+
40+
describe('it should pass', () => {
41+
it('should pass', () => {
42+
expect(true).toBeTruthy()
43+
})
44+
})
45+
46+
describe('useSwicthMapO', () => {
47+
48+
it('should return the test obj', () => {
49+
const aRef = ref(0)
50+
const tO = useSwitchMapO(aRef, tOProjection)
51+
expect(tO).toStrictEqual(testObj)
52+
})
53+
54+
it('should allow the projection to update the returned refs', async () => {
55+
const aRef = ref(0)
56+
const obj = useSwitchMapO(aRef, threePropsProjectionInnerMutation)
57+
58+
expect(obj.number.value).toBe(0)
59+
expect(obj.string.value).toBe("")
60+
expect(obj.boolean).toBe(true)
61+
62+
await timer(1100)
63+
64+
expect(obj.number.value).toBe(2)
65+
expect(obj.string.value).toBe("!!")
66+
expect(obj.boolean).toBe(true)
67+
})
68+
69+
it('should watch the input ref changes to than call again the projection, updating the refs', async () => {
70+
const aRef = ref(0)
71+
const obj = useSwitchMapO(aRef, threePropsProjectionInnerMutation)
72+
73+
expect(obj.number.value).toBe(0)
74+
expect(obj.string.value).toBe("")
75+
expect(obj.boolean).toBe(true)
76+
77+
await timer(1600)
78+
aRef.value++;
79+
await timer(0)
80+
81+
expect(obj.number.value).toBe(0)
82+
expect(obj.string.value).toBe("")
83+
expect(obj.boolean).toBe(true)
84+
})
85+
86+
it(`should be uable to externally update the swicthMappedRef`, async () => {
87+
const aRef = ref(0)
88+
const obj = useSwitchMapO(aRef, threePropsProjectionInnerMutation)
89+
90+
const cloneNumberRef = computed(() => obj.number.value)
91+
92+
obj.number.value = 9
93+
await timer(0)
94+
expect(cloneNumberRef.value).toBe(9)
95+
96+
})
97+
98+
})

test/util.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function timer(ms: number) {
2+
return new Promise(ok => setTimeout(ok, ms))
3+
}

0 commit comments

Comments
 (0)