-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathaudit-trail.spec.ts
More file actions
64 lines (55 loc) · 1.91 KB
/
audit-trail.spec.ts
File metadata and controls
64 lines (55 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { DatabaseReference } from '../interfaces';
import { AngularFireModule, FirebaseApp } from '@angular/fire';
import { AngularFireDatabase, AngularFireDatabaseModule, auditTrail, ChildEvent, URL } from '../public_api';
import { TestBed } from '@angular/core/testing';
import { COMMON_CONFIG } from '../../test-config';
import { skip } from 'rxjs/operators';
import 'firebase/database';
import { rando } from '../../firestore/utils.spec';
describe('auditTrail', () => {
let app: FirebaseApp;
let db: AngularFireDatabase;
let createRef: (path: string) => DatabaseReference;
let batch = {};
const items = [{ name: 'zero' }, { name: 'one' }, { name: 'two' }].map((item, i) => ({ key: i.toString(), ...item }));
Object.keys(items).forEach((key, i) => {
batch[i] = items[key];
});
// make batch immutable to preserve integrity
batch = Object.freeze(batch);
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
AngularFireModule.initializeApp(COMMON_CONFIG, rando()),
AngularFireDatabaseModule
],
providers: [
{ provide: URL, useValue: 'http://localhost:9000' }
]
});
app = TestBed.inject(FirebaseApp);
db = TestBed.inject(AngularFireDatabase);
createRef = (path: string) => db.database.ref(path);
});
afterEach(() => {
app.delete();
});
function prepareAuditTrail(opts: { events?: ChildEvent[], skipnumber: number } = { skipnumber: 0 }) {
const { events, skipnumber } = opts;
const aref = createRef(rando());
aref.set(batch);
const changes = auditTrail(aref, events);
return {
changes: changes.pipe(skip(skipnumber)),
ref: aref
};
}
it('should listen to all events by default', (done) => {
const { changes } = prepareAuditTrail();
changes.subscribe(actions => {
const data = actions.map(a => a.payload.val());
expect(data).toEqual(items);
done();
});
});
});