|
| 1 | +'use strict' |
| 2 | + |
| 3 | +export class Database { |
| 4 | + ref = (path) => { |
| 5 | + if (!this[path]) { |
| 6 | + this[path] = new Reference(path) |
| 7 | + } |
| 8 | + return this[path] |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +export class Reference { |
| 13 | + constructor(path) { |
| 14 | + this.path = path |
| 15 | + this.snap = { val: () => this._val()} |
| 16 | + this.data = null |
| 17 | + } |
| 18 | + |
| 19 | + _val = jest.fn(() => { |
| 20 | + return this.data |
| 21 | + }) |
| 22 | + |
| 23 | + once = jest.fn((param, callback) => { |
| 24 | + const promise = new Promise ((resolve, reject) => { |
| 25 | + if (callback) { |
| 26 | + callback(this.snap) |
| 27 | + resolve() |
| 28 | + } else { |
| 29 | + resolve(this.snap) |
| 30 | + } |
| 31 | + }) |
| 32 | + RNFirebase.promises.push(promise) |
| 33 | + return promise |
| 34 | + }) |
| 35 | + |
| 36 | + on = jest.fn((param, callback) => { |
| 37 | + const promise = new Promise ((resolve, reject) => { |
| 38 | + if (callback) { |
| 39 | + callback(this.snap) |
| 40 | + resolve() |
| 41 | + } else { |
| 42 | + resolve(this.snap) |
| 43 | + } |
| 44 | + }) |
| 45 | + RNFirebase.promises.push(promise) |
| 46 | + return promise |
| 47 | + }) |
| 48 | + |
| 49 | + off = jest.fn((param, callback) => { |
| 50 | + const promise = Promise.resolve() |
| 51 | + RNFirebase.promises.push(promise) |
| 52 | + return promise |
| 53 | + }) |
| 54 | + |
| 55 | + update = jest.fn((data) => { |
| 56 | + const promise = Promise.resolve() |
| 57 | + RNFirebase.promises.push(promise) |
| 58 | + return promise |
| 59 | + }) |
| 60 | + |
| 61 | + remove = jest.fn(() => { |
| 62 | + const promise = Promise.resolve() |
| 63 | + RNFirebase.promises.push(promise) |
| 64 | + return promise |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +export class MockFirebase { |
| 69 | + constructor() { |
| 70 | + this.database = () => { |
| 71 | + if (!this.databaseInstance) { |
| 72 | + this.databaseInstance = new Database() |
| 73 | + } |
| 74 | + return this.databaseInstance |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +export default class RNFirebase { |
| 80 | + static initializeApp() { |
| 81 | + RNFirebase.firebase = new MockFirebase() |
| 82 | + RNFirebase.promises = [] |
| 83 | + return RNFirebase.firebase |
| 84 | + } |
| 85 | + |
| 86 | + static reset() { |
| 87 | + RNFirebase.promises = [] |
| 88 | + RNFirebase.firebase.databaseInstance = null |
| 89 | + } |
| 90 | + |
| 91 | + static waitForPromises() { |
| 92 | + return Promise.all(RNFirebase.promises) |
| 93 | + } |
| 94 | +} |
0 commit comments