|
1 |
| -import { rtdbPlugin } from '../../src' |
2 |
| -import { Vue, tick, MockFirebase } from '@posva/vuefire-test-helpers' |
3 |
| - |
4 |
| -Vue.use(rtdbPlugin) |
| 1 | +import { mount } from '@vue/test-utils' |
| 2 | +import { rtdbPlugin } from '../../../src' |
| 3 | +import { tick, MockFirebase } from '../../src' |
5 | 4 |
|
6 | 5 | describe('RTDB: manual bind', () => {
|
7 |
| - async function createVm() { |
| 6 | + async function factory() { |
8 | 7 | const source = new MockFirebase().child('data')
|
9 |
| - const vm = new Vue({ |
10 |
| - // purposely set items as null |
11 |
| - // but it's a good practice to set it to an empty array |
12 |
| - data: () => ({ |
13 |
| - items: [], |
14 |
| - item: null, |
15 |
| - }), |
16 |
| - }) |
| 8 | + const wrapper = mount( |
| 9 | + { |
| 10 | + template: 'no', |
| 11 | + // purposely set items as null |
| 12 | + // but it's a good practice to set it to an empty array |
| 13 | + data: () => ({ |
| 14 | + items: [], |
| 15 | + item: null, |
| 16 | + }), |
| 17 | + }, |
| 18 | + { |
| 19 | + global: { |
| 20 | + plugins: [rtdbPlugin], |
| 21 | + }, |
| 22 | + } |
| 23 | + ) |
| 24 | + |
17 | 25 | await tick()
|
18 | 26 |
|
19 |
| - return { vm, source } |
| 27 | + return { wrapper, source } |
20 | 28 | }
|
21 | 29 |
|
22 | 30 | it('manually binds as an array', async () => {
|
23 |
| - const { vm, source } = await createVm() |
24 |
| - expect(vm.items).toEqual([]) |
25 |
| - const promise = vm.$rtdbBind('items', source) |
26 |
| - expect(vm.items).toEqual([]) |
| 31 | + const { wrapper, source } = await factory() |
| 32 | + expect(wrapper.vm.items).toEqual([]) |
| 33 | + const promise = wrapper.vm.$rtdbBind('items', source) |
| 34 | + expect(wrapper.vm.items).toEqual([]) |
27 | 35 | source.push({ text: 'foo' })
|
28 | 36 | source.flush()
|
29 | 37 | await promise
|
30 |
| - expect(vm.items).toEqual([{ text: 'foo' }]) |
| 38 | + expect(wrapper.vm.items).toEqual([{ text: 'foo' }]) |
31 | 39 | })
|
32 | 40 |
|
33 | 41 | it('removes children in arrays', async () => {
|
34 |
| - const { vm, source } = await createVm() |
| 42 | + const { wrapper, source } = await factory() |
35 | 43 | source.autoFlush()
|
36 | 44 | source.push({ name: 'one' })
|
37 | 45 | source.push({ name: 'two' })
|
38 | 46 |
|
39 |
| - await vm.$rtdbBind('items', source) |
40 |
| - source.child(vm.items[1]['.key']).remove() |
41 |
| - expect(vm.items).toEqual([{ name: 'one' }]) |
| 47 | + await wrapper.vm.$rtdbBind('items', source) |
| 48 | + source.child(wrapper.vm.items[1]['.key']).remove() |
| 49 | + expect(wrapper.vm.items).toEqual([{ name: 'one' }]) |
42 | 50 | })
|
43 | 51 |
|
44 | 52 | it('returs a promise', async () => {
|
45 |
| - const { vm, source } = await createVm() |
46 |
| - expect(vm.$rtdbBind('items', source) instanceof Promise).toBe(true) |
47 |
| - expect(vm.$rtdbBind('item', source) instanceof Promise).toBe(true) |
| 53 | + const { wrapper, source } = await factory() |
| 54 | + expect(wrapper.vm.$rtdbBind('items', source) instanceof Promise).toBe(true) |
| 55 | + expect(wrapper.vm.$rtdbBind('item', source) instanceof Promise).toBe(true) |
48 | 56 | })
|
49 | 57 |
|
50 | 58 | it('manually binds as an object', async () => {
|
51 |
| - const { vm, source } = await createVm() |
52 |
| - expect(vm.item).toEqual(null) |
53 |
| - const promise = vm.$rtdbBind('item', source) |
54 |
| - expect(vm.item).toEqual(null) |
| 59 | + const { wrapper, source } = await factory() |
| 60 | + expect(wrapper.vm.item).toEqual(null) |
| 61 | + const promise = wrapper.vm.$rtdbBind('item', source) |
| 62 | + expect(wrapper.vm.item).toEqual(null) |
55 | 63 | source.set({ text: 'foo' })
|
56 | 64 | source.flush()
|
57 | 65 | await promise
|
58 |
| - expect(vm.item).toEqual({ text: 'foo' }) |
| 66 | + expect(wrapper.vm.item).toEqual({ text: 'foo' }) |
59 | 67 | })
|
60 | 68 |
|
61 | 69 | it('unbinds when overriting existing bindings', async () => {
|
62 |
| - const { vm, source } = await createVm() |
| 70 | + const { wrapper, source } = await factory() |
63 | 71 | source.autoFlush()
|
64 | 72 | source.set({ name: 'foo' })
|
65 |
| - await vm.$rtdbBind('item', source) |
66 |
| - expect(vm.item).toEqual({ name: 'foo' }) |
| 73 | + await wrapper.vm.$rtdbBind('item', source) |
| 74 | + expect(wrapper.vm.item).toEqual({ name: 'foo' }) |
67 | 75 | const other = new MockFirebase().child('other')
|
68 | 76 | other.autoFlush()
|
69 | 77 | other.set({ name: 'bar' })
|
70 |
| - await vm.$rtdbBind('item', other) |
71 |
| - expect(vm.item).toEqual({ name: 'bar' }) |
| 78 | + await wrapper.vm.$rtdbBind('item', other) |
| 79 | + expect(wrapper.vm.item).toEqual({ name: 'bar' }) |
72 | 80 |
|
73 | 81 | source.set({ name: 'new foo' })
|
74 |
| - expect(vm.item).toEqual({ name: 'bar' }) |
| 82 | + expect(wrapper.vm.item).toEqual({ name: 'bar' }) |
75 | 83 | })
|
76 | 84 |
|
77 | 85 | it('manually unbinds a ref', async () => {
|
78 |
| - const { vm, source } = await createVm() |
| 86 | + const { wrapper, source } = await factory() |
79 | 87 | source.autoFlush()
|
80 | 88 | source.set({ name: 'foo' })
|
81 |
| - await vm.$rtdbBind('item', source) |
82 |
| - expect(vm.item).toEqual({ name: 'foo' }) |
83 |
| - vm.$rtdbUnbind('item') |
| 89 | + await wrapper.vm.$rtdbBind('item', source) |
| 90 | + expect(wrapper.vm.item).toEqual({ name: 'foo' }) |
| 91 | + wrapper.vm.$rtdbUnbind('item') |
84 | 92 | source.set({ name: 'bar' })
|
85 |
| - expect(vm.item).toEqual(null) |
| 93 | + expect(wrapper.vm.item).toEqual(null) |
86 | 94 | })
|
87 | 95 |
|
88 | 96 | it('can customize the reset option through $rtdbBind', async () => {
|
89 |
| - const { vm, source } = await createVm() |
| 97 | + const { wrapper, source } = await factory() |
90 | 98 | const otherSource = new MockFirebase().child('data2')
|
91 | 99 | source.set({ name: 'foo' })
|
92 | 100 | otherSource.set({ name: 'bar' })
|
93 |
| - let p = vm.$rtdbBind('item', source) |
| 101 | + let p = wrapper.vm.$rtdbBind('item', source) |
94 | 102 | source.flush()
|
95 | 103 | await p
|
96 |
| - p = vm.$rtdbBind('item', otherSource, { reset: false }) |
97 |
| - expect(vm.item).toEqual({ name: 'foo' }) |
| 104 | + p = wrapper.vm.$rtdbBind('item', otherSource, { reset: false }) |
| 105 | + expect(wrapper.vm.item).toEqual({ name: 'foo' }) |
98 | 106 | otherSource.flush()
|
99 | 107 | await p
|
100 |
| - expect(vm.item).toEqual({ name: 'bar' }) |
| 108 | + expect(wrapper.vm.item).toEqual({ name: 'bar' }) |
101 | 109 | // should not apply last used option
|
102 |
| - p = vm.$rtdbBind('item', source) |
103 |
| - expect(vm.item).toEqual(null) |
| 110 | + p = wrapper.vm.$rtdbBind('item', source) |
| 111 | + expect(wrapper.vm.item).toEqual(null) |
104 | 112 | source.flush()
|
105 | 113 | })
|
106 | 114 |
|
107 | 115 | it('can customize the reset option through $rtdbUnbind', async () => {
|
108 |
| - const { vm, source } = await createVm() |
| 116 | + const { wrapper, source } = await factory() |
109 | 117 | source.autoFlush()
|
110 | 118 | source.set({ name: 'foo' })
|
111 | 119 | const otherSource = new MockFirebase().child('data2')
|
112 | 120 | otherSource.set({ name: 'bar' })
|
113 | 121 | otherSource.autoFlush()
|
114 |
| - await vm.$rtdbBind('item', source) |
115 |
| - expect(vm.item).toEqual({ name: 'foo' }) |
116 |
| - vm.$rtdbUnbind('item', false) |
117 |
| - expect(vm.item).toEqual({ name: 'foo' }) |
| 122 | + await wrapper.vm.$rtdbBind('item', source) |
| 123 | + expect(wrapper.vm.item).toEqual({ name: 'foo' }) |
| 124 | + wrapper.vm.$rtdbUnbind('item', false) |
| 125 | + expect(wrapper.vm.item).toEqual({ name: 'foo' }) |
118 | 126 | // should not apply the option to the next unbind call
|
119 |
| - await vm.$rtdbBind('item', otherSource, { reset: false }) |
120 |
| - expect(vm.item).toEqual({ name: 'bar' }) |
121 |
| - vm.$rtdbUnbind('item') |
122 |
| - expect(vm.item).toEqual(null) |
| 127 | + await wrapper.vm.$rtdbBind('item', otherSource, { reset: false }) |
| 128 | + expect(wrapper.vm.item).toEqual({ name: 'bar' }) |
| 129 | + wrapper.vm.$rtdbUnbind('item') |
| 130 | + expect(wrapper.vm.item).toEqual(null) |
123 | 131 | })
|
124 | 132 |
|
125 | 133 | it('do not reset if wait: true', async () => {
|
126 |
| - const { vm, source } = await createVm() |
| 134 | + const { wrapper, source } = await factory() |
127 | 135 | const otherSource = new MockFirebase().child('data2')
|
128 | 136 |
|
129 | 137 | // source.autoFlush()
|
130 |
| - let p = vm.$rtdbBind('items', source) |
| 138 | + let p = wrapper.vm.$rtdbBind('items', source) |
131 | 139 | source.push({ name: 'foo' })
|
132 | 140 | source.flush()
|
133 | 141 | await p
|
134 |
| - p = vm.$rtdbBind('items', otherSource, { wait: true, reset: true }) |
135 |
| - expect(vm.items).toEqual([{ name: 'foo' }]) |
| 142 | + p = wrapper.vm.$rtdbBind('items', otherSource, { wait: true, reset: true }) |
| 143 | + expect(wrapper.vm.items).toEqual([{ name: 'foo' }]) |
136 | 144 | otherSource.push({ name: 'bar' })
|
137 | 145 | otherSource.flush()
|
138 | 146 | await p
|
139 |
| - expect(vm.items).toEqual([{ name: 'bar' }]) |
| 147 | + expect(wrapper.vm.items).toEqual([{ name: 'bar' }]) |
140 | 148 | })
|
141 | 149 |
|
142 | 150 | it('wait + reset can be overriden with a function', async () => {
|
143 |
| - const { vm, source } = await createVm() |
| 151 | + const { wrapper, source } = await factory() |
144 | 152 | const otherSource = new MockFirebase().child('data2')
|
145 | 153 |
|
146 | 154 | // source.autoFlush()
|
147 |
| - let p = vm.$rtdbBind('items', source) |
| 155 | + let p = wrapper.vm.$rtdbBind('items', source) |
148 | 156 | source.push({ name: 'foo' })
|
149 | 157 | source.flush()
|
150 | 158 | await p
|
151 | 159 | // using an array is important as we use that to choose between bindAsObject and bindAsArray
|
152 |
| - p = vm.$rtdbBind('items', otherSource, { wait: true, reset: () => ['foo'] }) |
153 |
| - expect(vm.items).toEqual(['foo']) |
| 160 | + p = wrapper.vm.$rtdbBind('items', otherSource, { |
| 161 | + wait: true, |
| 162 | + reset: () => ['foo'], |
| 163 | + }) |
| 164 | + expect(wrapper.vm.items).toEqual(['foo']) |
154 | 165 | otherSource.push({ name: 'bar' })
|
155 | 166 | otherSource.flush()
|
156 | 167 | await p
|
157 |
| - expect(vm.items).toEqual([{ name: 'bar' }]) |
| 168 | + expect(wrapper.vm.items).toEqual([{ name: 'bar' }]) |
158 | 169 | })
|
159 | 170 | })
|
0 commit comments