|
| 1 | +/* |
| 2 | + * Copyright 2017 The boardgame.io Authors |
| 3 | + * |
| 4 | + * Use of this source code is governed by a MIT-style |
| 5 | + * license that can be found in the LICENSE file or at |
| 6 | + * https://opensource.org/licenses/MIT. |
| 7 | + */ |
| 8 | + |
| 9 | +import { Firebase } from './firebase'; |
| 10 | +import firebasemock from 'firebase-mock'; |
| 11 | + |
| 12 | +function NewFirebase(args) { |
| 13 | + const mockDatabase = new firebasemock.MockFirebase(); |
| 14 | + const mockFirestore = new firebasemock.MockFirestore(); |
| 15 | + |
| 16 | + const config = { |
| 17 | + apiKey: 'apikey', |
| 18 | + authDomain: 'authDomain', |
| 19 | + databaseURL: 'databaseURL', |
| 20 | + projectId: 'projectId', |
| 21 | + }; |
| 22 | + |
| 23 | + var mockSDK = new firebasemock.MockFirebaseSdk( |
| 24 | + // use null if your code does not use RTDB |
| 25 | + () => mockDatabase, |
| 26 | + // use null if your code does not use AUTHENTICATION |
| 27 | + () => null, |
| 28 | + // use null if your code does not use FIRESTORE |
| 29 | + () => mockFirestore, |
| 30 | + // use null if your code does not use STORAGE |
| 31 | + () => null, |
| 32 | + // use null if your code does not use MESSAGING |
| 33 | + () => null |
| 34 | + ); |
| 35 | + |
| 36 | + const db = new Firebase({ ...args, config }); |
| 37 | + db.client = mockSDK; |
| 38 | + return db; |
| 39 | +} |
| 40 | + |
| 41 | +test('construction', () => { |
| 42 | + const dbname = 'a'; |
| 43 | + const db = new Firebase({ dbname }); |
| 44 | + expect(db.dbname).toBe(dbname); |
| 45 | + expect(db.config).toEqual({}); |
| 46 | +}); |
| 47 | + |
| 48 | +describe('Firestore', async () => { |
| 49 | + let db = null; |
| 50 | + |
| 51 | + beforeEach(async () => { |
| 52 | + db = NewFirebase({ |
| 53 | + engine: 'Firestore', |
| 54 | + }); |
| 55 | + await db.connect(); |
| 56 | + db.db.autoFlush(); |
| 57 | + }); |
| 58 | + |
| 59 | + test('must return undefined when no game exists', async () => { |
| 60 | + const state = await db.get('gameID'); |
| 61 | + expect(state).toEqual(null); |
| 62 | + }); |
| 63 | + |
| 64 | + test('cache hit', async () => { |
| 65 | + // Create game. |
| 66 | + await db.set('gameID', { a: 1 }); |
| 67 | + |
| 68 | + // Must return created game. |
| 69 | + const state = await db.get('gameID'); |
| 70 | + expect(state).toMatchObject({ a: 1 }); |
| 71 | + |
| 72 | + // Must return true if game exists |
| 73 | + const has = await db.has('gameID'); |
| 74 | + expect(has).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + test('cache miss', async () => { |
| 78 | + // Create game. |
| 79 | + await db.set('gameID', { a: 1 }); |
| 80 | + |
| 81 | + // Must return created game. |
| 82 | + db.cache.reset(); |
| 83 | + const state = await db.get('gameID'); |
| 84 | + expect(state).toMatchObject({ a: 1 }); |
| 85 | + |
| 86 | + // Must return true if game exists |
| 87 | + db.cache.reset(); |
| 88 | + const has = await db.has('gameID'); |
| 89 | + expect(has).toBe(true); |
| 90 | + }); |
| 91 | + |
| 92 | + test('cache size', async () => { |
| 93 | + const db = NewFirebase({ |
| 94 | + cacheSize: 1, |
| 95 | + engine: 'Firestore', |
| 96 | + }); |
| 97 | + await db.connect(); |
| 98 | + db.db.autoFlush(); |
| 99 | + await db.set('gameID', { a: 1 }); |
| 100 | + await db.set('another', { b: 1 }); |
| 101 | + const state = await db.get('gameID'); |
| 102 | + // Check that it came from Firebase and not the cache. |
| 103 | + expect(state._id).toBeDefined(); |
| 104 | + }); |
| 105 | + |
| 106 | + test('race conditions', async () => { |
| 107 | + // Out of order set()'s. |
| 108 | + await db.set('gameID', { _stateID: 1 }); |
| 109 | + await db.set('gameID', { _stateID: 0 }); |
| 110 | + expect(await db.get('gameID')).toEqual({ _stateID: 1 }); |
| 111 | + |
| 112 | + // Do not override cache on get() if it is fresher than Firebase. |
| 113 | + await db.set('gameID', { _stateID: 0 }); |
| 114 | + db.cache.set('gameID', { _stateID: 1 }); |
| 115 | + await db.get('gameID'); |
| 116 | + expect(db.cache.get('gameID')).toEqual({ _stateID: 1 }); |
| 117 | + |
| 118 | + // Override if it is staler than Firebase. |
| 119 | + await db.set('gameID', { _stateID: 1 }); |
| 120 | + db.cache.reset(); |
| 121 | + expect(await db.get('gameID')).toMatchObject({ _stateID: 1 }); |
| 122 | + expect(db.cache.get('gameID')).toMatchObject({ _stateID: 1 }); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +describe('RTDB', async () => { |
| 127 | + let db = null; |
| 128 | + |
| 129 | + beforeEach(async () => { |
| 130 | + db = NewFirebase({ |
| 131 | + engine: 'RTDB', |
| 132 | + }); |
| 133 | + await db.connect(); |
| 134 | + db.db.autoFlush(); |
| 135 | + }); |
| 136 | + |
| 137 | + test('must return undefined when no game exists', async () => { |
| 138 | + const state = await db.get('gameID'); |
| 139 | + expect(state).toEqual(null); |
| 140 | + }); |
| 141 | + |
| 142 | + test('cache hit', async () => { |
| 143 | + // Create game. |
| 144 | + await db.set('gameID', { a: 1 }); |
| 145 | + |
| 146 | + // Must return created game. |
| 147 | + const state = await db.get('gameID'); |
| 148 | + expect(state).toMatchObject({ a: 1 }); |
| 149 | + |
| 150 | + // Must return true if game exists |
| 151 | + const has = await db.has('gameID'); |
| 152 | + expect(has).toBe(true); |
| 153 | + }); |
| 154 | + |
| 155 | + test('cache miss', async () => { |
| 156 | + // Create game. |
| 157 | + await db.set('gameID', { a: 1 }); |
| 158 | + |
| 159 | + // Must return created game. |
| 160 | + db.cache.reset(); |
| 161 | + const state = await db.get('gameID'); |
| 162 | + expect(state).toMatchObject({ a: 1 }); |
| 163 | + |
| 164 | + // Must return true if game exists |
| 165 | + db.cache.reset(); |
| 166 | + const has = await db.has('gameID'); |
| 167 | + expect(has).toBe(true); |
| 168 | + }); |
| 169 | + |
| 170 | + test('cache size', async () => { |
| 171 | + const db = NewFirebase({ |
| 172 | + cacheSize: 1, |
| 173 | + engine: 'Firestore', |
| 174 | + }); |
| 175 | + await db.connect(); |
| 176 | + db.db.autoFlush(); |
| 177 | + await db.set('gameID', { a: 1 }); |
| 178 | + await db.set('another', { b: 1 }); |
| 179 | + const state = await db.get('gameID'); |
| 180 | + // Check that it came from Firebase and not the cache. |
| 181 | + expect(state._id).toBeDefined(); |
| 182 | + }); |
| 183 | + |
| 184 | + test('race conditions', async () => { |
| 185 | + // Out of order set()'s. |
| 186 | + await db.set('gameID', { _stateID: 1 }); |
| 187 | + await db.set('gameID', { _stateID: 0 }); |
| 188 | + expect(await db.get('gameID')).toEqual({ _stateID: 1 }); |
| 189 | + |
| 190 | + // Do not override cache on get() if it is fresher than Firebase. |
| 191 | + await db.set('gameID', { _stateID: 0 }); |
| 192 | + db.cache.set('gameID', { _stateID: 1 }); |
| 193 | + await db.get('gameID'); |
| 194 | + expect(db.cache.get('gameID')).toEqual({ _stateID: 1 }); |
| 195 | + |
| 196 | + // Override if it is staler than Firebase. |
| 197 | + await db.set('gameID', { _stateID: 1 }); |
| 198 | + db.cache.reset(); |
| 199 | + expect(await db.get('gameID')).toMatchObject({ _stateID: 1 }); |
| 200 | + expect(db.cache.get('gameID')).toMatchObject({ _stateID: 1 }); |
| 201 | + }); |
| 202 | +}); |
0 commit comments