|
| 1 | +/** |
| 2 | + * Copyright 2018 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as firestore from '@firebase/firestore-types'; |
| 18 | +import { expect } from 'chai'; |
| 19 | +import { EventsAccumulator } from './util/events_accumulator'; |
| 20 | +import { withTestDoc } from './util/helpers'; |
| 21 | + |
| 22 | +// Firestore databases can be subject to a ~30s "cold start" delay if they have not been used |
| 23 | +// recently, so before any tests run we "prime" the backend. |
| 24 | + |
| 25 | +const PRIMING_TIMEOUT_MS = 45000; |
| 26 | + |
| 27 | +before( |
| 28 | + 'Prime backend by waiting for a write to show up in the watch stream', |
| 29 | + function(): Promise<void> { |
| 30 | + this.timeout(PRIMING_TIMEOUT_MS); |
| 31 | + |
| 32 | + return withTestDoc(/*persistence=*/ false, async doc => { |
| 33 | + const accumulator = new EventsAccumulator<firestore.DocumentSnapshot>(); |
| 34 | + const unsubscribe = doc.onSnapshot(accumulator.storeEvent); |
| 35 | + |
| 36 | + // Wait for watch to initialize and deliver first event. |
| 37 | + await accumulator.awaitRemoteEvent(); |
| 38 | + |
| 39 | + // Use a transaction to perform a write without triggering any local events. |
| 40 | + await doc.firestore.runTransaction(async txn => { |
| 41 | + txn.set(doc, { value: 'done' }); |
| 42 | + }); |
| 43 | + |
| 44 | + // Wait to see the write on the watch stream. |
| 45 | + const docSnap = await accumulator.awaitRemoteEvent(); |
| 46 | + expect(docSnap.get('value')).to.equal('done'); |
| 47 | + |
| 48 | + unsubscribe(); |
| 49 | + }); |
| 50 | + } |
| 51 | +); |
0 commit comments