Skip to content

Commit

Permalink
Add recursiveDelete & batch.create mocks (#186)
Browse files Browse the repository at this point in the history
* Add mockBatchCreate

* Add mockBatchCreate type def

* Add mockBatchCreate test to full-setup.test.js

* Add mockBatchCreate test to full-setup-library-firestore.test.js

* Add mockRecursiveDelete, missing export of mockBatchCreate

* Add mockRecursiveDelete type def, missing create def from FirestoreBatch

* Add recursiveDelete test to full-setup.test.js

* Add recursiveDelete test to full-setup-library-firestore.test.js

* Fix tests full-setup-library-firestore.test.js
  • Loading branch information
netdown authored Jan 28, 2024
1 parent 36151cc commit 142e847
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
16 changes: 16 additions & 0 deletions __tests__/full-setup-library-firestore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ describe.each([
mockBatchDelete,
mockBatchUpdate,
mockBatchSet,
mockBatchCreate,
mockSettings,
mockOnSnapShot,
mockListCollections,
mockTimestampNow,
mockCreate,
mockRecursiveDelete,
} = require('firestore-jest-mock/mocks/firestore');

describe('we can start a firestore application', () => {
Expand Down Expand Up @@ -243,6 +245,10 @@ describe.each([
const nycRef = firestore.collection('cities').doc('NYC');
batch.set(nycRef, { name: 'New York City' });

// Create new city 'CHI'
const chiRef = firestore.collection('cities').doc('CHI');
batch.create(chiRef, { name: 'Chicago', state: 'IL', country: 'USA' });

// Update the population of 'SF'
const sfRef = firestore.collection('cities').doc('SF');
batch.update(sfRef, { population: 1000000 });
Expand All @@ -257,6 +263,7 @@ describe.each([
expect(mockBatchDelete).toHaveBeenCalledWith(laRef);
expect(mockBatchUpdate).toHaveBeenCalledWith(sfRef, { population: 1000000 });
expect(mockBatchSet).toHaveBeenCalledWith(nycRef, { name: 'New York City' });
expect(mockBatchCreate).toHaveBeenCalledWith(chiRef, { name: 'Chicago', state: 'IL', country: 'USA' });
expect(mockBatchCommit).toHaveBeenCalled();
});
});
Expand Down Expand Up @@ -375,6 +382,15 @@ describe.each([
expect(mockWhere).toHaveBeenCalled();
expect(mockOnSnapShot).toHaveBeenCalled();
});

test('recursiveDelete', async () => {
const firestore = new this.Firestore();
const citiesRef = firestore.collection('cities');

firestore.recursiveDelete(citiesRef)

expect(mockRecursiveDelete).toHaveBeenCalledWith(citiesRef);
});
});
});
});
16 changes: 16 additions & 0 deletions __tests__/full-setup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe.each`
mockBatchDelete,
mockBatchUpdate,
mockBatchSet,
mockBatchCreate,
mockSettings,
mockOnSnapShot,
mockUseEmulator,
Expand All @@ -34,6 +35,7 @@ describe.each`
FakeFirestore,
mockQueryOnSnapshot,
mockTimestampNow,
mockRecursiveDelete,
} = require('firestore-jest-mock/mocks/firestore');

mockFirebase(
Expand Down Expand Up @@ -292,6 +294,10 @@ describe.each`
const nycRef = db.collection('cities').doc('NYC');
batch.set(nycRef, { name: 'New York City' });

// Create new city 'CHI'
const chiRef = db.collection('cities').doc('CHI');
batch.create(chiRef, { name: 'Chicago', state: 'IL', country: 'USA' });

// Update the population of 'SF'
const sfRef = db.collection('cities').doc('SF');
batch.update(sfRef, { population: 1000000 });
Expand All @@ -307,6 +313,7 @@ describe.each`
expect(mockBatchDelete).toHaveBeenCalledWith(laRef);
expect(mockBatchUpdate).toHaveBeenCalledWith(sfRef, { population: 1000000 });
expect(mockBatchSet).toHaveBeenCalledWith(nycRef, { name: 'New York City' });
expect(mockBatchCreate).toHaveBeenCalledWith(chiRef, { name: 'Chicago', state: 'IL', country: 'USA' });
expect(mockBatchCommit).toHaveBeenCalled();
});
});
Expand Down Expand Up @@ -410,6 +417,15 @@ describe.each`
expect(mockQueryOnSnapshot).toHaveBeenCalled();
});

test('recursiveDelete', async () => {
const db = firebase.firestore();
const citiesRef = db.collection('cities');

db.recursiveDelete(citiesRef)

expect(mockRecursiveDelete).toHaveBeenCalledWith(citiesRef);
});

describe('withConverter', () => {
const converter = {
fromFirestore: () => {},
Expand Down
4 changes: 4 additions & 0 deletions src/mocks/firestore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface FirestoreBatch {
delete(): FirestoreBatch;
set(doc: DocumentReference, data: DocumentData, options?: SetOptions): FirestoreBatch;
update(doc: DocumentReference, data: DocumentData): FirestoreBatch;
create(doc: DocumentReference, data: DocumentData): FirestoreBatch;
commit(): Promise<void>;
}

Expand Down Expand Up @@ -54,6 +55,7 @@ export class FakeFirestore {
collectionGroup(collectionName: string): Query;
doc(path: string): DocumentReference;
runTransaction<T>(updateFunction: (transaction: Transaction) => Promise<T>): Promise<T>;
recursiveDelete(ref: DocumentReference|CollectionReference): Promise<void>;
}

declare class DocumentReference {
Expand Down Expand Up @@ -118,6 +120,7 @@ declare class CollectionReference extends FakeFirestore.Query {
// Mocks exported from this module
export const mockBatch: jest.Mock;
export const mockRunTransaction: jest.Mock;
export const mockRecursiveDelete: jest.Mock;

export const mockCollection: jest.Mock;
export const mockCollectionGroup: jest.Mock;
Expand All @@ -137,6 +140,7 @@ export const mockBatchDelete: jest.Mock;
export const mockBatchCommit: jest.Mock;
export const mockBatchUpdate: jest.Mock;
export const mockBatchSet: jest.Mock;
export const mockBatchCreate: jest.Mock;

export const mockOnSnapShot: jest.Mock;

Expand Down
14 changes: 14 additions & 0 deletions src/mocks/firestore.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const mockCollectionGroup = jest.fn();
const mockBatch = jest.fn();
const mockRunTransaction = jest.fn();
const mockRecursiveDelete = jest.fn();

const mockSettings = jest.fn();
const mockUseEmulator = jest.fn();
Expand All @@ -18,6 +19,7 @@ const mockBatchDelete = jest.fn();
const mockBatchCommit = jest.fn();
const mockBatchUpdate = jest.fn();
const mockBatchSet = jest.fn();
const mockBatchCreate = jest.fn();

const mockOnSnapShot = jest.fn();

Expand Down Expand Up @@ -73,6 +75,11 @@ class FakeFirestore {
this._ref._updateData(doc.path, data, true);
return this;
},
create(doc, data) {
mockBatchCreate(...arguments);
this._ref._updateData(doc.path, data, false);
return this;
},
commit() {
mockBatchCommit(...arguments);
return Promise.resolve([]);
Expand Down Expand Up @@ -224,6 +231,11 @@ class FakeFirestore {
id: docId,
};
}

recursiveDelete(ref, bulkWriter) {
mockRecursiveDelete(...arguments);
return Promise.resolve();
}
}

FakeFirestore.Query = query.Query;
Expand Down Expand Up @@ -544,6 +556,7 @@ module.exports = {
FakeFirestore,
mockBatch,
mockRunTransaction,
mockRecursiveDelete,
mockCollection,
mockCollectionGroup,
mockDoc,
Expand All @@ -558,6 +571,7 @@ module.exports = {
mockBatchCommit,
mockBatchUpdate,
mockBatchSet,
mockBatchCreate,
mockOnSnapShot,
mockListDocuments,
mockListCollections,
Expand Down

0 comments on commit 142e847

Please sign in to comment.