Skip to content

Commit f41db2b

Browse files
committed
refactor(db): tests for stateChanges and auditTrail
1 parent 4476c1f commit f41db2b

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

src/database/index.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ import './observable/fromRef.spec';
44
import './list/changes.spec';
55
import './list/loaded.spec';
66
import './list/snapshot-changes.spec';
7+
import './list/state-changes.spec';
8+
import './list/audit-trail.spec';

src/database/list/audit-trail.spec.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import * as firebase from 'firebase/app';
2+
import { FirebaseApp, FirebaseAppConfig, AngularFireModule } from 'angularfire2';
3+
import { AngularFireDatabase, AngularFireDatabaseModule, auditTrail, ChildEvent } from 'angularfire2/database';
4+
import { TestBed, inject } from '@angular/core/testing';
5+
import { COMMON_CONFIG } from '../test-config';
6+
import 'rxjs/add/operator/skip';
7+
8+
// generate random string to test fidelity of naming
9+
const rando = () => (Math.random() + 1).toString(36).substring(7);
10+
const FIREBASE_APP_NAME = rando();
11+
12+
describe('stateChanges', () => {
13+
let app: FirebaseApp;
14+
let db: AngularFireDatabase;
15+
let createRef: (path: string) => firebase.database.Reference;
16+
let batch = {};
17+
const items = [{ name: 'zero' }, { name: 'one' }, { name: 'two' }].map((item, i) => ( { key: i.toString(), ...item } ));
18+
Object.keys(items).forEach(function (key, i) {
19+
const itemValue = items[key];
20+
batch[i] = itemValue;
21+
});
22+
// make batch immutable to preserve integrity
23+
batch = Object.freeze(batch);
24+
25+
beforeEach(() => {
26+
TestBed.configureTestingModule({
27+
imports: [
28+
AngularFireModule.initializeApp(COMMON_CONFIG, FIREBASE_APP_NAME),
29+
AngularFireDatabaseModule
30+
]
31+
});
32+
inject([FirebaseApp, AngularFireDatabase], (app_: FirebaseApp, _db: AngularFireDatabase) => {
33+
app = app_;
34+
db = _db;
35+
app.database().goOffline();
36+
createRef = (path: string) => { app.database().goOffline(); return app.database().ref(path); };
37+
})();
38+
});
39+
40+
afterEach(done => {
41+
app.delete().then(done, done.fail);
42+
});
43+
44+
function prepareAuditTrail(opts: { events?: ChildEvent[], skip: number } = { skip: 0 }) {
45+
const { events, skip } = opts;
46+
const aref = createRef(rando());
47+
aref.set(batch);
48+
const changes = auditTrail(aref, events);
49+
return {
50+
changes: changes.skip(skip),
51+
ref: aref
52+
};
53+
}
54+
55+
it('should listen to all events by default', (done) => {
56+
57+
const { changes } = prepareAuditTrail();
58+
changes.subscribe(actions => {
59+
const data = actions.map(a => a.payload!.val());
60+
expect(data).toEqual(items);
61+
done();
62+
});
63+
64+
});
65+
66+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import * as firebase from 'firebase/app';
2+
import { FirebaseApp, FirebaseAppConfig, AngularFireModule } from 'angularfire2';
3+
import { AngularFireDatabase, AngularFireDatabaseModule, stateChanges, ChildEvent } from 'angularfire2/database';
4+
import { TestBed, inject } from '@angular/core/testing';
5+
import { COMMON_CONFIG } from '../test-config';
6+
import 'rxjs/add/operator/skip';
7+
8+
// generate random string to test fidelity of naming
9+
const rando = () => (Math.random() + 1).toString(36).substring(7);
10+
const FIREBASE_APP_NAME = rando();
11+
12+
describe('stateChanges', () => {
13+
let app: FirebaseApp;
14+
let db: AngularFireDatabase;
15+
let createRef: (path: string) => firebase.database.Reference;
16+
let batch = {};
17+
const items = [{ name: 'zero' }, { name: 'one' }, { name: 'two' }].map((item, i) => ( { key: i.toString(), ...item } ));
18+
Object.keys(items).forEach(function (key, i) {
19+
const itemValue = items[key];
20+
batch[i] = itemValue;
21+
});
22+
// make batch immutable to preserve integrity
23+
batch = Object.freeze(batch);
24+
25+
beforeEach(() => {
26+
TestBed.configureTestingModule({
27+
imports: [
28+
AngularFireModule.initializeApp(COMMON_CONFIG, FIREBASE_APP_NAME),
29+
AngularFireDatabaseModule
30+
]
31+
});
32+
inject([FirebaseApp, AngularFireDatabase], (app_: FirebaseApp, _db: AngularFireDatabase) => {
33+
app = app_;
34+
db = _db;
35+
app.database().goOffline();
36+
createRef = (path: string) => { app.database().goOffline(); return app.database().ref(path); };
37+
})();
38+
});
39+
40+
afterEach(done => {
41+
app.delete().then(done, done.fail);
42+
});
43+
44+
function prepareStateChanges(opts: { events?: ChildEvent[], skip: number } = { skip: 0 }) {
45+
const { events, skip } = opts;
46+
const aref = createRef(rando());
47+
aref.set(batch);
48+
const changes = stateChanges(aref, events);
49+
return {
50+
changes: changes.skip(skip),
51+
ref: aref
52+
};
53+
}
54+
55+
it('should listen to all events by default', (done) => {
56+
57+
const { changes } = prepareStateChanges({ skip: 2 });
58+
changes.subscribe(action => {
59+
expect(action.key).toEqual('2');
60+
expect(action.payload!.val()).toEqual(items[items.length - 1]);
61+
done();
62+
});
63+
64+
});
65+
66+
});

src/database/public_api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export * from './database';
22
export * from './list/changes';
33
export * from './list/create-reference';
44
export * from './list/snapshot-changes';
5+
export * from './list/state-changes';
6+
export * from './list/audit-trail';
57
export * from './list/loaded';
68
export * from './observable/fromRef';
79
export * from './database.module'

src/root.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export * from './packages-dist/database/observable/fromRef.spec';
77
export * from './packages-dist/database/list/changes.spec';
88
export * from './packages-dist/database/list/loaded.spec';
99
export * from './packages-dist/database/list/snapshot-changes.spec';
10+
export * from './packages-dist/database/list/state-changes.spec';
11+
export * from './packages-dist/database/list/audit-trail.spec';
1012

1113
// export * from './packages-dist/database-deprecated/firebase_list_factory.spec';
1214
// export * from './packages-dist/database-deprecated/firebase_object_factory.spec';

0 commit comments

Comments
 (0)