-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* simple implementation of set allows tests to mutate DB * sandboxed unit tests to prevent mutability leaking between tests * Implemented collection.add, enforced unique IDs * ensure random Id id always string * fixed build issue reported by babel project (unrecognized token) * implement 'set' on batch. Update to use 'mutable' option on DB creation * Fixed batching & tests * reverted no-longer-necessary test changes * PR changes requested * fixed missing collection bug
- Loading branch information
1 parent
2d98d57
commit e5b0bc2
Showing
6 changed files
with
229 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
const { FakeFirestore } = require('firestore-jest-mock'); | ||
const { mockCollection, mockDoc } = require('firestore-jest-mock/mocks/firestore'); | ||
|
||
describe('database mutations', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
// db is a fn, instead a shared variable to enforce sandboxing data on each test. | ||
const db = () => | ||
new FakeFirestore( | ||
{ | ||
characters: [ | ||
{ | ||
id: 'homer', | ||
name: 'Homer', | ||
occupation: 'technician', | ||
address: { street: '742 Evergreen Terrace' }, | ||
}, | ||
{ id: 'krusty', name: 'Krusty', occupation: 'clown' }, | ||
{ | ||
id: 'bob', | ||
name: 'Bob', | ||
occupation: 'insurance agent', | ||
_collections: { | ||
family: [ | ||
{ id: 'violet', name: 'Violet', relation: 'daughter' }, | ||
{ id: 'dash', name: 'Dash', relation: 'son' }, | ||
{ id: 'jackjack', name: 'Jackjack', relation: 'son' }, | ||
{ id: 'helen', name: 'Helen', relation: 'wife' }, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
{ mutable: true }, | ||
); | ||
|
||
test('it can set simple record data', async () => { | ||
const mdb = db(); | ||
await mdb | ||
.collection('animals') | ||
.doc('fantasy') | ||
.collection('dragons') | ||
.doc('whisperingDeath') | ||
.set({ | ||
age: 15, | ||
food: 'omnivore', | ||
special: 'tunneling', | ||
}); | ||
expect(mockCollection).toHaveBeenCalledWith('dragons'); | ||
expect(mockDoc).toHaveBeenCalledWith('whisperingDeath'); | ||
|
||
const doc = await mdb.doc('animals/fantasy/dragons/whisperingDeath').get(); | ||
expect(doc.exists).toBe(true); | ||
expect(doc.id).toBe('whisperingDeath'); | ||
}); | ||
|
||
test('it correctly merges data on update', async () => { | ||
const mdb = db(); | ||
const homer = mdb.collection('characters').doc('homer'); | ||
await homer.set({ occupation: 'Astronaut' }, { merge: true }); | ||
const doc = await homer.get(); | ||
expect(doc.data().name).toEqual('Homer'); | ||
expect(doc.data().occupation).toEqual('Astronaut'); | ||
}); | ||
|
||
test('it correctly overwrites data on set', async () => { | ||
const mdb = db(); | ||
const homer = mdb.collection('characters').doc('homer'); | ||
await homer.set({ occupation: 'Astronaut' }); | ||
const doc = await homer.get(); | ||
expect(doc.data().name).toBeUndefined(); | ||
expect(doc.data().occupation).toEqual('Astronaut'); | ||
}); | ||
|
||
test('it can batch appropriately', async () => { | ||
const mdb = db(); | ||
const homer = mdb.collection('characters').doc('homer'); | ||
const krusty = mdb.collection('characters').doc('krusty'); | ||
await mdb | ||
.batch() | ||
.update(homer, { drink: 'duff' }) | ||
.set(krusty, { causeOfDeath: 'Simian homicide' }) | ||
.commit(); | ||
|
||
const homerData = (await homer.get()).data(); | ||
expect(homerData.name).toEqual('Homer'); | ||
expect(homerData.drink).toEqual('duff'); | ||
const krustyData = (await krusty.get()).data(); | ||
expect(krustyData.name).toBeUndefined(); | ||
expect(krustyData.causeOfDeath).toEqual('Simian homicide'); | ||
}); | ||
|
||
test('it can add to collection', async () => { | ||
const col = db().collection('characters'); | ||
const newDoc1 = await col.add({ | ||
name: 'Lisa', | ||
occupation: 'President-in-waiting', | ||
address: { street: '742 Evergreen Terrace' }, | ||
}); | ||
|
||
const test = await newDoc1.get(); | ||
expect(test.exists).toBe(true); | ||
|
||
const newDoc2 = await col.add({ | ||
name: 'Lisa', | ||
occupation: 'President-in-waiting', | ||
address: { street: '742 Evergreen Terrace' }, | ||
}); | ||
expect(newDoc2.id).not.toEqual(newDoc1.id); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters