Skip to content

Commit ad8c414

Browse files
toWritableComputed.test.ts improvements
1 parent 5682972 commit ad8c414

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

tests/toWritableComputed.test.ts

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
import { describe, expect, it } from 'vitest'
2-
import { computed, ref } from 'vue'
3-
import { toWritableComputed } from '../src/toWritableComputed'
1+
import { describe, expect, expectTypeOf, it } from 'vitest'
2+
import { computed, type ComputedRef, type Ref, ref, type WritableComputedRef } from 'vue'
3+
import { toWritableComputed } from '../src'
44

55
describe('toWriteableComputed()', async () => {
66
it('computed() -> ref()', async () => {
7-
const obj = ref({ a: '1', b: '2' })
7+
8+
const av = 'something'
9+
const bv = 99
10+
const obj = ref({ a: av, b: bv })
811

912
const comp = computed(() => obj.value)
1013
const { a, b } = toWritableComputed(comp)
1114

12-
test(obj, comp, a, b)
15+
test(obj, comp, a, b, av, bv)
1316
})
1417

1518
it('writable computed() -> ref()', async () => {
16-
const obj = ref({ a: '1', b: '2' })
19+
const av = 'something'
20+
const bv = 99
21+
const obj = ref({ a: av, b: bv })
1722
const comp = computed({
1823
get: () => obj.value,
1924
set: (val) => {
@@ -23,39 +28,46 @@ describe('toWriteableComputed()', async () => {
2328

2429
const { a, b } = toWritableComputed(comp)
2530

26-
test(obj, comp, a, b)
31+
test(obj, comp, a, b, av, bv)
2732
})
2833

2934
it('ref()', async () => {
30-
const obj = ref({ a: '1', b: '2' })
35+
const av = 'something'
36+
const bv = 99
37+
const obj = ref({ a: av, b: bv })
3138

39+
const comp = computed(() => obj.value)
3240
const { a, b } = toWritableComputed(obj)
3341

34-
expect(obj.value).toEqual({ a: '1', b: '2' })
35-
expect(a.value).toEqual('1')
36-
expect(b.value).toEqual('2')
37-
38-
a.value = 'test'
39-
40-
expect(a.value).toEqual('test')
41-
expect(b.value).toEqual('2')
4242

43-
expect(obj.value).toEqual({ a: 'test', b: '2' })
43+
test(obj, comp, a, b, av, bv)
4444
})
4545
})
4646

47-
// @ts-expect-error
48-
function test(obj, comp, a, b) {
49-
expect(obj.value).toEqual({ a: '1', b: '2' })
50-
expect(comp.value).toEqual({ a: '1', b: '2' })
51-
expect(a.value).toEqual('1')
52-
expect(b.value).toEqual('2')
47+
function test(
48+
obj: Ref,
49+
comp: ComputedRef<any> | WritableComputedRef<any>,
50+
a: WritableComputedRef<string>,
51+
b: WritableComputedRef<number>,
52+
av: string,
53+
bv: number,
54+
) {
55+
56+
expectTypeOf(a).toEqualTypeOf<WritableComputedRef<string>>()
57+
expectTypeOf(b).toEqualTypeOf<WritableComputedRef<number>>()
58+
expectTypeOf(a.value).toEqualTypeOf<string>()
59+
expectTypeOf(b.value).toEqualTypeOf<number>()
60+
61+
expect(obj.value).toEqual({ a: av, b: bv })
62+
expect(comp.value).toEqual({ a: av, b: bv })
63+
expect(a.value).toEqual(av)
64+
expect(b.value).toEqual(bv)
5365

5466
a.value = 'test'
5567

5668
expect(a.value).toEqual('test')
57-
expect(b.value).toEqual('2')
69+
expect(b.value).toEqual(bv)
5870

59-
expect(obj.value).toEqual({ a: 'test', b: '2' })
60-
expect(comp.value).toEqual({ a: 'test', b: '2' })
71+
expect(obj.value).toEqual({ a: 'test', b: bv })
72+
expect(comp.value).toEqual({ a: 'test', b: bv })
6173
}

0 commit comments

Comments
 (0)