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 recursiveDelete & batch.create mocks #186

Merged
merged 9 commits into from
Jan 28, 2024
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
Loading