Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create method to FakeFirestore.DocumentReference #181

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading