@@ -2,116 +2,96 @@ import { defineComponent } from 'vue'
2
2
import { mount } from '@vue/test-utils'
3
3
import { describe , expect , it , vi } from 'vitest'
4
4
import { firestorePlugin , FirestorePluginOptions } from '../../src'
5
- import { addDoc , DocumentData } from 'firebase/firestore'
5
+ import { DocumentData } from 'firebase/firestore'
6
6
import { setupFirestoreRefs } from '../utils'
7
7
8
- const component = defineComponent ( { template : 'no' } )
8
+ const component = defineComponent ( {
9
+ template : 'no' ,
10
+ data : ( ) => ( { itemList : [ ] , item : null } ) ,
11
+ } )
9
12
10
13
describe ( 'Firestore: Options API' , ( ) => {
11
- const { collection, doc } = setupFirestoreRefs ( )
12
-
13
- it ( 'allows customizing $rtdbBind' , ( ) => {
14
- const wrapper = mount ( component , {
15
- global : {
16
- plugins : [
17
- [
18
- firestorePlugin ,
19
- {
20
- bindName : '$myBind' ,
21
- unbindName : '$myUnbind' ,
22
- } ,
23
- ] ,
24
- ] ,
25
- } ,
26
- } )
14
+ const { collection, doc, addDoc, setDoc } = setupFirestoreRefs ( )
27
15
28
- // @ts -expect-error: haven't extended the types
29
- expect ( wrapper . vm . $myBind ) . toBeTypeOf ( 'function' )
30
- // @ts -expect-error: haven't extended the types
31
- expect ( wrapper . vm . $myUnbind ) . toBeTypeOf ( 'function' )
32
- } )
33
-
34
- it ( 'calls custom serialize function with collection' , async ( ) => {
35
- const pluginOptions : FirestorePluginOptions = {
36
- converter : {
37
- fromFirestore : vi . fn ( ( snapshot , options ?) => ( {
38
- foo : 'bar' ,
39
- } ) ) ,
40
- toFirestore ( data : DocumentData ) {
41
- return data
42
- } ,
43
- } ,
44
- }
45
- const wrapper = mount (
46
- {
47
- template : 'no' ,
48
- data : ( ) => ( { items : [ ] } ) ,
49
- } ,
50
- {
16
+ describe ( '$firestoreBind' , ( ) => {
17
+ function factory ( pluginOptions ?: FirestorePluginOptions ) {
18
+ return mount ( component , {
51
19
global : {
52
20
plugins : [ [ firestorePlugin , pluginOptions ] ] ,
53
21
} ,
54
- }
55
- )
56
-
57
- const itemsRef = collection ( )
58
- await addDoc ( itemsRef , { } )
22
+ } )
23
+ }
59
24
60
- await wrapper . vm . $firestoreBind ( 'items' , itemsRef )
25
+ it ( 'allows customizing $rtdbBind' , ( ) => {
26
+ const wrapper = factory ( {
27
+ bindName : '$myBind' ,
28
+ unbindName : '$myUnbind' ,
29
+ } )
61
30
62
- expect ( pluginOptions . converter ?. fromFirestore ) . toHaveBeenCalledTimes ( 1 )
63
- expect ( pluginOptions . converter ?. fromFirestore ) . toHaveBeenCalledWith (
64
- expect . objectContaining ( { data : expect . any ( Function ) } ) ,
65
- expect . anything ( )
66
- )
67
- expect ( wrapper . vm . items ) . toEqual ( [ { foo : 'bar' } ] )
68
- } )
31
+ // @ts -expect-error: haven't extended the types
32
+ expect ( wrapper . vm . $myBind ) . toBeTypeOf ( 'function' )
33
+ // @ts -expect-error: haven't extended the types
34
+ expect ( wrapper . vm . $myUnbind ) . toBeTypeOf ( 'function' )
35
+ } )
69
36
70
- it ( 'can be overridden by local option ' , async ( ) => {
71
- const pluginOptions : FirestorePluginOptions = {
72
- converter : {
73
- fromFirestore : vi . fn ( ( snapshot , options ? ) => ( {
74
- foo : 'bar' ,
75
- } ) ) ,
76
- toFirestore ( data : DocumentData ) {
77
- return data
37
+ it ( 'calls custom serialize function with collection ' , async ( ) => {
38
+ const fromFirestore = vi . fn ( ( ) => ( {
39
+ foo : 'bar' ,
40
+ } ) )
41
+ const wrapper = factory ( {
42
+ converter : {
43
+ fromFirestore ,
44
+ toFirestore : ( data : DocumentData ) => data ,
78
45
} ,
79
- } ,
80
- }
81
- const wrapper = mount (
82
- {
83
- template : 'no' ,
84
- data : ( ) => ( { items : [ ] } ) ,
85
- } ,
86
- {
87
- global : {
88
- plugins : [ [ firestorePlugin , pluginOptions ] ] ,
89
- } ,
90
- }
91
- )
46
+ } )
47
+
48
+ const itemsRef = collection ( )
49
+ await addDoc ( itemsRef , { } )
92
50
93
- const itemsRef = collection ( )
94
- await addDoc ( itemsRef , { } )
51
+ await wrapper . vm . $firestoreBind ( 'itemList' , itemsRef )
95
52
96
- const spy = vi . fn ( ( ) => ( { bar : 'bar' } ) )
53
+ expect ( fromFirestore ) . toHaveBeenCalledTimes ( 1 )
54
+ expect ( fromFirestore ) . toHaveBeenCalledWith (
55
+ expect . objectContaining ( { data : expect . any ( Function ) } ) ,
56
+ expect . anything ( )
57
+ )
58
+ expect ( wrapper . vm . itemList ) . toEqual ( [ { foo : 'bar' } ] )
59
+ } )
97
60
98
- await wrapper . vm . $firestoreBind (
99
- 'items' ,
100
- itemsRef . withConverter ( {
101
- fromFirestore : spy ,
102
- toFirestore ( data : DocumentData ) {
103
- return data
61
+ it ( 'can be overridden by local option' , async ( ) => {
62
+ const fromFirestore = vi . fn ( ( ) => ( {
63
+ foo : 'bar' ,
64
+ } ) )
65
+ const wrapper = factory ( {
66
+ converter : {
67
+ fromFirestore,
68
+ toFirestore : ( data : DocumentData ) => data ,
104
69
} ,
105
- } ) ,
106
- { }
107
- )
70
+ } )
71
+
72
+ const itemsRef = collection ( )
73
+ await addDoc ( itemsRef , { } )
74
+
75
+ const spy = vi . fn ( ( ) => ( { bar : 'bar' } ) )
108
76
109
- expect ( pluginOptions . converter ?. fromFirestore ) . not . toHaveBeenCalled ( )
110
- expect ( spy ) . toHaveBeenCalledTimes ( 1 )
111
- expect ( spy ) . toHaveBeenCalledWith (
112
- expect . objectContaining ( { data : expect . any ( Function ) } ) ,
113
- expect . anything ( )
114
- )
115
- expect ( wrapper . vm . items ) . toEqual ( [ { bar : 'bar' } ] )
77
+ await wrapper . vm . $firestoreBind (
78
+ 'itemList' ,
79
+ itemsRef . withConverter ( {
80
+ fromFirestore : spy ,
81
+ toFirestore ( data : DocumentData ) {
82
+ return data
83
+ } ,
84
+ } ) ,
85
+ { }
86
+ )
87
+
88
+ expect ( fromFirestore ) . not . toHaveBeenCalled ( )
89
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 )
90
+ expect ( spy ) . toHaveBeenCalledWith (
91
+ expect . objectContaining ( { data : expect . any ( Function ) } ) ,
92
+ expect . anything ( )
93
+ )
94
+ expect ( wrapper . vm . itemList ) . toEqual ( [ { bar : 'bar' } ] )
95
+ } )
116
96
} )
117
97
} )
0 commit comments