|
1 | 1 | import { mount } from '@vue/test-utils' |
2 | 2 | import { describe, expect, it } from 'vitest' |
3 | 3 | import { |
| 4 | + addDoc, |
4 | 5 | doc as originalDoc, |
5 | 6 | DocumentData, |
6 | 7 | DocumentReference, |
7 | 8 | FirestoreError, |
8 | 9 | } from 'firebase/firestore' |
9 | 10 | import { expectType, setupFirestoreRefs, tds, firestore } from '../utils' |
10 | 11 | import { nextTick, ref, shallowRef, unref, type Ref } from 'vue' |
11 | | -import { _MaybeRef, _Nullable } from '../../src/shared' |
| 12 | +import { isPOJO, _MaybeRef, _Nullable } from '../../src/shared' |
12 | 13 | import { |
13 | 14 | useDocument, |
14 | 15 | VueFirestoreDocumentData, |
@@ -147,6 +148,39 @@ describe( |
147 | 148 | expect(error.value).toBeUndefined() |
148 | 149 | }) |
149 | 150 |
|
| 151 | + it('can use a custom converter', async () => { |
| 152 | + class MyName { |
| 153 | + private _name: string |
| 154 | + constructor(name: string) { |
| 155 | + this._name = name |
| 156 | + } |
| 157 | + |
| 158 | + get name() { |
| 159 | + return this._name |
| 160 | + } |
| 161 | + |
| 162 | + set name(_newName: string) { |
| 163 | + // do nothing |
| 164 | + } |
| 165 | + } |
| 166 | + const itemRef = doc().withConverter<MyName>({ |
| 167 | + toFirestore: (data) => ({ name: data.name }), |
| 168 | + fromFirestore: (snap) => new MyName(snap.get('name')), |
| 169 | + }) |
| 170 | + await setDoc(itemRef, new MyName('a')) |
| 171 | + |
| 172 | + const { wrapper, data, promise } = factory({ ref: itemRef }) |
| 173 | + |
| 174 | + await promise.value |
| 175 | + |
| 176 | + expect(wrapper.vm.item).toHaveProperty('name', 'a') |
| 177 | + expect(isPOJO(wrapper.vm.item)).toBe(false) |
| 178 | + |
| 179 | + // should respect the setter |
| 180 | + wrapper.vm.item!.name = 'b' |
| 181 | + expect(wrapper.vm.item).toHaveProperty('name', 'a') |
| 182 | + }) |
| 183 | + |
150 | 184 | describe('reset option', () => { |
151 | 185 | it('resets the value when specified', async () => { |
152 | 186 | const { wrapper, itemRef, data } = factory({ |
|
0 commit comments