|
13 | 13 | * See the License for the specific language governing permissions and
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 |
| - |
17 | 16 | import { expect } from 'chai';
|
18 | 17 | import { initializeApp, firestore, app } from 'firebase/app';
|
19 | 18 | import 'firebase/firestore';
|
20 | 19 | import {
|
21 | 20 | collection,
|
22 | 21 | docChanges,
|
23 | 22 | sortedChanges,
|
24 |
| - auditTrail |
| 23 | + auditTrail, |
| 24 | + docData, |
| 25 | + collectionData |
25 | 26 | } from '../firestore';
|
26 | 27 | import { map, take, skip } from 'rxjs/operators';
|
27 | 28 |
|
@@ -253,4 +254,88 @@ describe('RxFire Firestore', () => {
|
253 | 254 | davidDoc.update({ name: 'David!' });
|
254 | 255 | });
|
255 | 256 | });
|
| 257 | + |
| 258 | + describe('auditTrail', () => { |
| 259 | + /** |
| 260 | + * The `auditTrail()` method returns an array of every change that has |
| 261 | + * occured in the application. This test seeds two "people" into the |
| 262 | + * collection and checks that the two added events are there. It then |
| 263 | + * modifies a "person" and makes sure that event is on the array as well. |
| 264 | + */ |
| 265 | + it('should keep create a list of all changes', (done: MochaDone) => { |
| 266 | + const { colRef, expectedEvents, davidDoc } = seedTest(firestore); |
| 267 | + |
| 268 | + const firstAudit = auditTrail(colRef).pipe(unwrapChange, take(1)); |
| 269 | + const secondAudit = auditTrail(colRef).pipe(unwrapChange, skip(1)); |
| 270 | + |
| 271 | + firstAudit.subscribe(list => { |
| 272 | + expect(list).to.eql(expectedEvents); |
| 273 | + davidDoc.update({ name: 'David!' }); |
| 274 | + }); |
| 275 | + |
| 276 | + secondAudit.subscribe(list => { |
| 277 | + const modifiedList = [ |
| 278 | + ...expectedEvents, |
| 279 | + { name: 'David!', type: 'modified' } |
| 280 | + ]; |
| 281 | + expect(list).to.eql(modifiedList); |
| 282 | + done(); |
| 283 | + }); |
| 284 | + }); |
| 285 | + |
| 286 | + /** |
| 287 | + * This test seeds two "people" into the collection. The wrap operator then converts |
| 288 | + */ |
| 289 | + it('should filter the trail of events by event type', (done: MochaDone) => { |
| 290 | + const { colRef, davidDoc } = seedTest(firestore); |
| 291 | + |
| 292 | + const modifiedAudit = auditTrail(colRef, ['modified']).pipe(unwrapChange); |
| 293 | + |
| 294 | + modifiedAudit.subscribe(updateList => { |
| 295 | + const expectedEvents = [{ type: 'modified', name: 'David!' }]; |
| 296 | + expect(updateList).to.eql(expectedEvents); |
| 297 | + done(); |
| 298 | + }); |
| 299 | + |
| 300 | + davidDoc.update({ name: 'David!' }); |
| 301 | + }); |
| 302 | + }); |
| 303 | + |
| 304 | + describe('Data Mapping Functions', () => { |
| 305 | + /** |
| 306 | + * The `unwrap(id)` method will map a collection to its data payload and map the doc ID to a the specificed key. |
| 307 | + */ |
| 308 | + it('collectionData should map a QueryDocumentSnapshot[] to an array of plain objects', (done: MochaDone) => { |
| 309 | + const { colRef } = seedTest(firestore); |
| 310 | + |
| 311 | + // const unwrapped = collection(colRef).pipe(unwrap('userId')); |
| 312 | + const unwrapped = collectionData(colRef, 'userId'); |
| 313 | + |
| 314 | + unwrapped.subscribe(val => { |
| 315 | + const expectedDoc = { |
| 316 | + name: 'David', |
| 317 | + userId: 'david' |
| 318 | + }; |
| 319 | + expect(val).to.be.instanceof(Array); |
| 320 | + expect(val[0]).to.eql(expectedDoc); |
| 321 | + done(); |
| 322 | + }); |
| 323 | + }); |
| 324 | + |
| 325 | + it('docData should map a QueryDocumentSnapshot to a plain object', (done: MochaDone) => { |
| 326 | + const { davidDoc } = seedTest(firestore); |
| 327 | + |
| 328 | + // const unwrapped = doc(davidDoc).pipe(unwrap('UID')); |
| 329 | + const unwrapped = docData(davidDoc, 'UID'); |
| 330 | + |
| 331 | + unwrapped.subscribe(val => { |
| 332 | + const expectedDoc = { |
| 333 | + name: 'David', |
| 334 | + UID: 'david' |
| 335 | + }; |
| 336 | + expect(val).to.eql(expectedDoc); |
| 337 | + done(); |
| 338 | + }); |
| 339 | + }); |
| 340 | + }); |
256 | 341 | });
|
0 commit comments