|
| 1 | +import firestore, { firebase } from '../lib'; |
| 2 | + |
| 3 | +const COLLECTION = 'firestore'; |
| 4 | + |
| 5 | +describe('Storage', function () { |
| 6 | + describe('namespace', function () { |
| 7 | + it('accessible from firebase.app()', function () { |
| 8 | + const app = firebase.app(); |
| 9 | + expect(app.firestore).toBeDefined(); |
| 10 | + expect(app.firestore().settings).toBeDefined(); |
| 11 | + }); |
| 12 | + }); |
| 13 | + |
| 14 | + describe('batch()', function () { |
| 15 | + it('returns a new WriteBatch instance', function () { |
| 16 | + const instance = firebase.firestore().batch(); |
| 17 | + return expect(instance.constructor.name).toEqual('FirestoreWriteBatch'); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('settings', function () { |
| 22 | + it('throws if settings is not an object', async function () { |
| 23 | + try { |
| 24 | + // @ts-ignore the type is incorrect *on purpose* to test type checking in javascript |
| 25 | + await firebase.firestore().settings('foo'); |
| 26 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 27 | + } catch (e) { |
| 28 | + return expect(e.message).toContain("'settings' must be an object"); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + it('throws if passing an incorrect setting key', async function () { |
| 33 | + try { |
| 34 | + // @ts-ignore the type is incorrect *on purpose* to test type checking in javascript |
| 35 | + await firebase.firestore().settings({ foo: 'bar' }); |
| 36 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 37 | + } catch (e) { |
| 38 | + return expect(e.message).toContain("'settings.foo' is not a valid settings field"); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + it('throws if cacheSizeBytes is not a number', async function () { |
| 43 | + try { |
| 44 | + // @ts-ignore the type is incorrect *on purpose* to test type checking in javascript |
| 45 | + await firebase.firestore().settings({ cacheSizeBytes: 'foo' }); |
| 46 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 47 | + } catch (e) { |
| 48 | + return expect(e.message).toContain("'settings.cacheSizeBytes' must be a number value"); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + it('throws if cacheSizeBytes is less than 1MB', async function () { |
| 53 | + try { |
| 54 | + await firebase.firestore().settings({ cacheSizeBytes: 123 }); |
| 55 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 56 | + } catch (e) { |
| 57 | + return expect(e.message).toContain("'settings.cacheSizeBytes' the minimum cache size"); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + it('accepts an unlimited cache size', async function () { |
| 62 | + await firebase |
| 63 | + .firestore() |
| 64 | + .settings({ cacheSizeBytes: firebase.firestore.CACHE_SIZE_UNLIMITED }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('throws if host is not a string', async function () { |
| 68 | + try { |
| 69 | + // @ts-ignore the type is incorrect *on purpose* to test type checking in javascript |
| 70 | + await firebase.firestore().settings({ host: 123 }); |
| 71 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 72 | + } catch (e) { |
| 73 | + return expect(e.message).toContain("'settings.host' must be a string value"); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + it('throws if host is an empty string', async function () { |
| 78 | + try { |
| 79 | + await firebase.firestore().settings({ host: '' }); |
| 80 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 81 | + } catch (e) { |
| 82 | + return expect(e.message).toContain("'settings.host' must not be an empty string"); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + it('throws if persistence is not a boolean', async function () { |
| 87 | + try { |
| 88 | + // @ts-ignore the type is incorrect *on purpose* to test type checking in javascript |
| 89 | + await firebase.firestore().settings({ persistence: 'true' }); |
| 90 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 91 | + } catch (e) { |
| 92 | + return expect(e.message).toContain("'settings.persistence' must be a boolean value"); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + it('throws if ssl is not a boolean', async function () { |
| 97 | + try { |
| 98 | + // @ts-ignore the type is incorrect *on purpose* to test type checking in javascript |
| 99 | + await firebase.firestore().settings({ ssl: 'true' }); |
| 100 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 101 | + } catch (e) { |
| 102 | + return expect(e.message).toContain("'settings.ssl' must be a boolean value"); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + it('throws if ignoreUndefinedProperties is not a boolean', async function () { |
| 107 | + try { |
| 108 | + // @ts-ignore the type is incorrect *on purpose* to test type checking in javascript |
| 109 | + await firestore().settings({ ignoreUndefinedProperties: 'bogus' }); |
| 110 | + return Promise.reject(new Error('Should throw')); |
| 111 | + } catch (e) { |
| 112 | + return expect(e.message).toContain("ignoreUndefinedProperties' must be a boolean value."); |
| 113 | + } |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('runTransaction()', function () { |
| 118 | + it('throws if updateFunction is not a function', async function () { |
| 119 | + try { |
| 120 | + // @ts-ignore the type is incorrect *on purpose* to test type checking in javascript |
| 121 | + await firebase.firestore().runTransaction('foo'); |
| 122 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 123 | + } catch (e) { |
| 124 | + return expect(e.message).toContain("'updateFunction' must be a function"); |
| 125 | + } |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe('collectionGroup()', function () { |
| 130 | + it('returns a new query instance', function () { |
| 131 | + const query = firebase.firestore().collectionGroup(COLLECTION); |
| 132 | + expect(query.constructor.name).toEqual('FirestoreQuery'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('throws if id is not a string', function () { |
| 136 | + try { |
| 137 | + // @ts-ignore the type is incorrect *on purpose* to test type checking in javascript |
| 138 | + firebase.firestore().collectionGroup(123); |
| 139 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 140 | + } catch (e) { |
| 141 | + return expect(e.message).toContain("'collectionId' must be a string value"); |
| 142 | + } |
| 143 | + }); |
| 144 | + |
| 145 | + it('throws if id is empty', function () { |
| 146 | + try { |
| 147 | + firebase.firestore().collectionGroup(''); |
| 148 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 149 | + } catch (e) { |
| 150 | + return expect(e.message).toContain("'collectionId' must be a non-empty string"); |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + it('throws if id contains forward-slash', function () { |
| 155 | + try { |
| 156 | + firebase.firestore().collectionGroup(`someCollection/bar`); |
| 157 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 158 | + } catch (e) { |
| 159 | + return expect(e.message).toContain("'collectionId' must not contain '/'"); |
| 160 | + } |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + describe('collection()', function () { |
| 165 | + it('throws if path is not a string', function () { |
| 166 | + try { |
| 167 | + // @ts-ignore the type is incorrect *on purpose* to test type checking in javascript |
| 168 | + firebase.firestore().collection(123); |
| 169 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 170 | + } catch (e) { |
| 171 | + return expect(e.message).toContain("'collectionPath' must be a string value"); |
| 172 | + } |
| 173 | + }); |
| 174 | + |
| 175 | + it('throws if path is empty string', function () { |
| 176 | + try { |
| 177 | + firebase.firestore().collection(''); |
| 178 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 179 | + } catch (e) { |
| 180 | + return expect(e.message).toContain("'collectionPath' must be a non-empty string"); |
| 181 | + } |
| 182 | + }); |
| 183 | + |
| 184 | + it('throws if path does not point to a collection', function () { |
| 185 | + try { |
| 186 | + firebase.firestore().collection(`firestore/bar`); |
| 187 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 188 | + } catch (e) { |
| 189 | + return expect(e.message).toContain("'collectionPath' must point to a collection"); |
| 190 | + } |
| 191 | + }); |
| 192 | + |
| 193 | + it('returns a new CollectionReference', function () { |
| 194 | + const collectionReference = firebase.firestore().collection('firestore'); |
| 195 | + expect(collectionReference.constructor.name).toEqual('FirestoreCollectionReference'); |
| 196 | + expect(collectionReference.path).toEqual('firestore'); |
| 197 | + }); |
| 198 | + }); |
| 199 | + |
| 200 | + describe('doc()', function () { |
| 201 | + it('throws if path is not a string', function () { |
| 202 | + try { |
| 203 | + // @ts-ignore the type is incorrect *on purpose* to test type checking in javascript |
| 204 | + firebase.firestore().doc(123); |
| 205 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 206 | + } catch (e) { |
| 207 | + return expect(e.message).toContain("'documentPath' must be a string value"); |
| 208 | + } |
| 209 | + }); |
| 210 | + |
| 211 | + it('throws if path is empty string', function () { |
| 212 | + try { |
| 213 | + firebase.firestore().doc(''); |
| 214 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 215 | + } catch (e) { |
| 216 | + return expect(e.message).toContain("'documentPath' must be a non-empty string"); |
| 217 | + } |
| 218 | + }); |
| 219 | + |
| 220 | + it('throws if path does not point to a document', function () { |
| 221 | + try { |
| 222 | + firebase.firestore().doc(`${COLLECTION}/bar/baz`); |
| 223 | + return Promise.reject(new Error('Did not throw an Error.')); |
| 224 | + } catch (e) { |
| 225 | + return expect(e.message).toContain("'documentPath' must point to a document"); |
| 226 | + } |
| 227 | + }); |
| 228 | + |
| 229 | + it('returns a new DocumentReference', function () { |
| 230 | + const docRef = firebase.firestore().doc(`${COLLECTION}/bar`); |
| 231 | + expect(docRef.constructor.name).toEqual('FirestoreDocumentReference'); |
| 232 | + expect(docRef.path).toEqual(`${COLLECTION}/bar`); |
| 233 | + }); |
| 234 | + |
| 235 | + it('throws when undefined value provided and ignored undefined is false', async function () { |
| 236 | + await firebase.firestore().settings({ ignoreUndefinedProperties: false }); |
| 237 | + const docRef = firebase.firestore().doc(`${COLLECTION}/bar`); |
| 238 | + try { |
| 239 | + await docRef.set({ |
| 240 | + field1: 1, |
| 241 | + field2: undefined, |
| 242 | + }); |
| 243 | + |
| 244 | + return Promise.reject(new Error('Expected set() to throw')); |
| 245 | + } catch (e) { |
| 246 | + return expect(e.message).toEqual('Unsupported field value: undefined'); |
| 247 | + } |
| 248 | + }); |
| 249 | + |
| 250 | + it('throws when nested undefined value provided and ignored undefined is false', async function () { |
| 251 | + await firebase.firestore().settings({ ignoreUndefinedProperties: false }); |
| 252 | + const docRef = firebase.firestore().doc(`${COLLECTION}/bar`); |
| 253 | + try { |
| 254 | + await docRef.set({ |
| 255 | + field1: 1, |
| 256 | + field2: { |
| 257 | + shouldNotWork: undefined, |
| 258 | + }, |
| 259 | + }); |
| 260 | + return Promise.reject(new Error('Expected set() to throw')); |
| 261 | + } catch (e) { |
| 262 | + return expect(e.message).toEqual('Unsupported field value: undefined'); |
| 263 | + } |
| 264 | + }); |
| 265 | + }); |
| 266 | +}); |
0 commit comments