Skip to content

Commit

Permalink
Add create method to FakeFirestore.DocumentReference (#181)
Browse files Browse the repository at this point in the history
* feat: add create to document API

* fix: package version conflict
  • Loading branch information
jefshe authored Jan 4, 2024
1 parent 57d2241 commit ae4d073
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
41 changes: 25 additions & 16 deletions __tests__/full-setup-library-firestore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe.each([
mockOnSnapShot,
mockListCollections,
mockTimestampNow,
mockCreate,
} = require('firestore-jest-mock/mocks/firestore');

describe('we can start a firestore application', () => {
Expand Down Expand Up @@ -195,6 +196,26 @@ describe.each([
});
});

test('create a city', () => {
const firestore = new this.Firestore();

return firestore
.collection('cities')
.doc('LA')
.create({
name: 'Los Angeles',
state: 'CA',
country: 'USA',
})
.then(function() {
expect(mockCreate).toHaveBeenCalledWith({
name: 'Los Angeles',
state: 'CA',
country: 'USA',
});
});
});

test('updating a city', () => {
const firestore = new this.Firestore();
const now = Timestamp._fromMillis(new Date().getTime());
Expand Down Expand Up @@ -243,21 +264,15 @@ describe.each([
test('listCollections returns a promise', async () => {
const firestore = new this.Firestore();

const listCollectionsPromise = firestore
.collection('cities')
.doc('LA')
.listCollections();
const listCollectionsPromise = firestore.collection('cities').doc('LA').listCollections();

expect(listCollectionsPromise).toEqual(expect.any(Promise));
});

test('listCollections resolves with child collections', async () => {
const firestore = new this.Firestore();

const result = await firestore
.collection('users')
.doc('123abc')
.listCollections();
const result = await firestore.collection('users').doc('123abc').listCollections();

expect(result).toEqual(expect.any(Array));
expect(result).toHaveLength(1);
Expand All @@ -268,10 +283,7 @@ describe.each([
test('listCollections resolves with empty array if there are no collections in document', async () => {
const firestore = new this.Firestore();

const result = await firestore
.collection('users')
.doc('abc123')
.listCollections();
const result = await firestore.collection('users').doc('abc123').listCollections();

expect(result).toEqual(expect.any(Array));
expect(result).toHaveLength(0);
Expand All @@ -280,10 +292,7 @@ describe.each([
test('listCollections calls mockListCollections', async () => {
const firestore = new this.Firestore();

await firestore
.collection('users')
.doc('abc123')
.listCollections();
await firestore.collection('users').doc('abc123').listCollections();

expect(mockListCollections).toHaveBeenCalled();
});
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/mocks/firestore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ declare class DocumentReference {
delete(): Promise<void>;
get(): Promise<MockedDocument>;

create(object: DocumentData): Promise<MockedDocument>;
update(object: DocumentData): Promise<MockedDocument>;
set(object: DocumentData): Promise<MockedDocument>;

Expand Down Expand Up @@ -121,6 +122,7 @@ export const mockRunTransaction: jest.Mock;
export const mockCollection: jest.Mock;
export const mockCollectionGroup: jest.Mock;
export const mockDoc: jest.Mock;
export const mockCreate: jest.Mock;
export const mockUpdate: jest.Mock;
export const mockSet: jest.Mock;
export const mockAdd: jest.Mock;
Expand Down
15 changes: 11 additions & 4 deletions src/mocks/firestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const mockSettings = jest.fn();
const mockUseEmulator = jest.fn();
const mockCollection = jest.fn();
const mockDoc = jest.fn();
const mockCreate = jest.fn();
const mockUpdate = jest.fn();
const mockSet = jest.fn();
const mockAdd = jest.fn();
Expand Down Expand Up @@ -242,10 +243,7 @@ FakeFirestore.DocumentReference = class {
this.id = id;
this.parent = parent;
this.firestore = parent.firestore;
this.path = parent.path
.split('/')
.concat(id)
.join('/');
this.path = parent.path.split('/').concat(id).join('/');
}

collection(collectionName) {
Expand Down Expand Up @@ -308,6 +306,14 @@ FakeFirestore.DocumentReference = class {
return Promise.resolve(data);
}

create(object) {
mockCreate(...arguments);
this.firestore._updateData(this.path, object, false);
return Promise.resolve(
buildDocFromHash({ ...object, _ref: this, _updateTime: timestamp.Timestamp.now() }),
);
}

update(object) {
mockUpdate(...arguments);
if (this._get().exists) {
Expand Down Expand Up @@ -543,6 +549,7 @@ module.exports = {
mockDoc,
mockAdd,
mockDelete,
mockCreate,
mockUpdate,
mockSet,
mockSettings,
Expand Down

0 comments on commit ae4d073

Please sign in to comment.