Skip to content

Commit

Permalink
convert tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbatson5 committed May 27, 2023
1 parent 7ef5014 commit 1f66dd8
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 142 deletions.
6 changes: 2 additions & 4 deletions __tests__/full-setup-library-firestore.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as FirestoreMock from 'firestore-jest-mock';
import { Timestamp } from '../mocks/timestamp';

import { mocks } from '../mocks/firestore';

const {
import {
mockGet,
mockSelect,
mockAdd,
Expand All @@ -20,7 +18,7 @@ const {
mockOnSnapShot,
mockListCollections,
mockTimestampNow,
} = mocks;
} from '../mocks/firestore';

describe.each([
{ library: '@google-cloud/firestore', mockFunction: 'mockGoogleCloudFirestore' },
Expand Down
82 changes: 39 additions & 43 deletions __tests__/full-setup.test.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
describe.each`
filters
${true}
${false}
`('we can start a firebase application (query filters: $filters)', ({ filters }) => {
// We call `require` inside of a parameterized `describe` so we get
// a fresh mocked Firebase to test cases with query filters turned on and off

import { mockFirebase } from 'firestore-jest-mock';
import { Timestamp } from '../mocks/timestamp';

import { mockInitializeApp } from '../mocks/firebase';

import {
mockGet,
mockSelect,
mockAdd,
mockSet,
mockUpdate,
mockWhere,
mockCollectionGroup,
mockBatch,
mockBatchCommit,
mockBatchDelete,
mockBatchUpdate,
mockBatchSet,
mockSettings,
mockOnSnapShot,
mockUseEmulator,
mockDoc,
mockCollection,
mockWithConverter,
FakeFirestore,
mockQueryOnSnapshot,
mockTimestampNow,
} from '../mocks/firestore';

describe('we can start a firebase application (query filters: $filters)', () => {
jest.resetModules();
const { mockFirebase } = require('firestore-jest-mock');
const { mockInitializeApp } = require('../mocks/firebase');

const flushPromises = () => new Promise(setImmediate);
const { Timestamp } = require('../mocks/timestamp');
const {
mockGet,
mockSelect,
mockAdd,
mockSet,
mockUpdate,
mockWhere,
mockCollectionGroup,
mockBatch,
mockBatchCommit,
mockBatchDelete,
mockBatchUpdate,
mockBatchSet,
mockSettings,
mockOnSnapShot,
mockUseEmulator,
mockDoc,
mockCollection,
mockWithConverter,
FakeFirestore,
mockQueryOnSnapshot,
mockTimestampNow,
} = require('../mocks/firestore');
let firebase;

mockFirebase(
{
Expand All @@ -60,22 +56,23 @@ describe.each`
],
},
},
{ simulateQueryFilters: filters },
{ simulateQueryFilters: false },
);

/** @type {import('firebase').default} */
const firebase = require('firebase');

beforeEach(() => {
beforeEach(async () => {
jest.resetAllMocks();
firebase = await import('firebase');

firebase.initializeApp({
apiKey: '### FIREBASE API KEY ###',
authDomain: '### FIREBASE AUTH DOMAIN ###',
projectId: '### CLOUD FIRESTORE PROJECT ID ###',
});
});

afterEach(() => mockTimestampNow.mockClear());
afterEach(() => {
mockTimestampNow.mockClear();
});

test('We can start an application', async () => {
const db = firebase.firestore();
Expand Down Expand Up @@ -226,9 +223,8 @@ describe.each`
expect(mockCollectionGroup).toHaveBeenCalledWith('cities');
expect(mockGet).toHaveBeenCalledTimes(1);
expect(mockWhere).toHaveBeenCalledWith('country', '==', 'USA');

expect(querySnapshot.forEach).toBeTruthy();
expect(querySnapshot.docs.length).toBe(filters ? 3 : 4);
expect(querySnapshot.docs.length).toBe(4);
expect(querySnapshot.size).toBe(querySnapshot.docs.length);

querySnapshot.forEach(doc => {
Expand Down
11 changes: 6 additions & 5 deletions __tests__/mock-fieldvalue.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const { mockFirebase } = require('firestore-jest-mock');
import { mockFirebase } from 'firestore-jest-mock';
mockFirebase({ database: {} });
const firebase = require('firebase');

const {
import {
mockArrayRemoveFieldValue,
mockArrayUnionFieldValue,
mockDeleteFieldValue,
mockIncrementFieldValue,
mockServerTimestampFieldValue,
} = require('../mocks/firestore');
} from '../mocks/firestore';

describe('Single values transformed by field sentinels', () => {
beforeEach(() => {
let firebase;
beforeEach(async () => {
firebase = await import('firebase');
jest.resetModules();
jest.clearAllMocks();
});
Expand Down
4 changes: 2 additions & 2 deletions __tests__/mock-firestore-mutable.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { FakeFirestore } = require('firestore-jest-mock');
const { mockCollection, mockDoc } = require('firestore-jest-mock/mocks/firestore');
import { FakeFirestore } from 'firestore-jest-mock';
import { mockCollection, mockDoc } from 'firestore-jest-mock/mocks/firestore';

describe('database mutations', () => {
beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/mock-firestore.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { FakeFirestore } = require('firestore-jest-mock');
const { mockCollection, mockDoc } = require('firestore-jest-mock/mocks/firestore');
import { FakeFirestore } from 'firestore-jest-mock';
import { mockCollection, mockDoc } from 'firestore-jest-mock/mocks/firestore';

describe('Queries', () => {
beforeEach(() => {
Expand Down
8 changes: 4 additions & 4 deletions __tests__/mock-timestamp.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { FakeFirestore } = require('firestore-jest-mock');
const {
import { FakeFirestore } from 'firestore-jest-mock';
import {
mockTimestampToDate,
mockTimestampToMillis,
mockTimestampNow,
} = require('firestore-jest-mock/mocks/firestore');
const admin = require('firebase-admin');
} from 'firestore-jest-mock/mocks/firestore';
import admin from 'firebase-admin';
const ref = admin.firestore.Timestamp;

describe('Timestamp mock', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/options.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { FakeFirestore } = require('firestore-jest-mock');
const { mockCollection, mockDoc } = require('firestore-jest-mock/mocks/firestore');
import { FakeFirestore } from 'firestore-jest-mock';
import { mockCollection, mockDoc } from 'firestore-jest-mock/mocks/firestore';

describe('Firestore options', () => {
beforeEach(() => {
Expand Down
23 changes: 14 additions & 9 deletions __tests__/path.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const { mockFirebase } = require('firestore-jest-mock');
import { mockFirebase } from 'firestore-jest-mock';
mockFirebase({ database: {} });
const firebase = require('firebase');
const path = require('../mocks/path');
import { Path } from '../mocks/path';

describe('Single values transformed by field sentinels', () => {
let firebase;

beforeEach(async () => {
firebase = await import('firebase');
});

test('isEqual', () => {
const path1 = new firebase.firestore.FieldPath('collection', 'doc1');
const path2 = new firebase.firestore.FieldPath('collection', 'doc2');
Expand All @@ -13,16 +18,16 @@ describe('Single values transformed by field sentinels', () => {
});

test('compareTo', () => {
const path1 = new path.Path(['abc', 'def', 'ghij']);
const path2 = new path.Path(['abc', 'def', 'ghik']);
const path1 = new Path(['abc', 'def', 'ghij']);
const path2 = new Path(['abc', 'def', 'ghik']);
expect(path1.compareTo(path2)).toEqual(-1);
const path3 = new path.Path(['abc', 'def', 'ghi']);
const path3 = new Path(['abc', 'def', 'ghi']);
expect(path1.compareTo(path3)).toEqual(1);
const path4 = new path.Path(['abc', 'def']);
const path5 = new path.Path(['abc', 'def']);
const path4 = new Path(['abc', 'def']);
const path5 = new Path(['abc', 'def']);
expect(path1.compareTo(path4)).toEqual(1);
expect(path4.compareTo(path5)).toEqual(0);
const path6 = new path.Path(['abc', 'def', 'ghi', 'klm']);
const path6 = new Path(['abc', 'def', 'ghi', 'klm']);
expect(path6.compareTo(path1)).toEqual(-1);
expect(path3.compareTo(path6)).toEqual(-1);
});
Expand Down
24 changes: 14 additions & 10 deletions __tests__/query.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const {
import {
mockCollection,
mockDoc,
mockGet,
mockWhere,
mockOffset,
FakeFirestore,
} = require('../mocks/firestore');
const { mockFirebase } = require('firestore-jest-mock');
} from '../mocks/firestore';
import { mockFirebase } from 'firestore-jest-mock';

describe('Queries', () => {
let firebase, db;

mockFirebase(
{
database: {
Expand Down Expand Up @@ -137,15 +139,17 @@ describe('Queries', () => {
{ simulateQueryFilters: true },
);

const firebase = require('firebase');
firebase.initializeApp({
apiKey: '### FIREBASE API KEY ###',
authDomain: '### FIREBASE AUTH DOMAIN ###',
projectId: '### CLOUD FIRESTORE PROJECT ID ###',
beforeEach(async () => {
firebase = await import('firebase');
firebase.initializeApp({
apiKey: '### FIREBASE API KEY ###',
authDomain: '### FIREBASE AUTH DOMAIN ###',
projectId: '### CLOUD FIRESTORE PROJECT ID ###',
});

db = firebase.firestore();
});

const db = firebase.firestore();

test('it can query a single document', async () => {
const monkey = await db
.collection('animals')
Expand Down
6 changes: 3 additions & 3 deletions __tests__/references.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { FakeFirestore } = require('firestore-jest-mock');
const {
import { FakeFirestore } from 'firestore-jest-mock';
import {
mockCollection,
mockDoc,
mockDelete,
Expand All @@ -8,7 +8,7 @@ const {
mockOrderBy,
mockStartAfter,
mockStartAt,
} = require('firestore-jest-mock/mocks/firestore');
} from 'firestore-jest-mock/mocks/firestore';

describe('Reference Sentinels', () => {
beforeEach(() => {
Expand Down
25 changes: 13 additions & 12 deletions __tests__/transaction.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { mockFirebase, FakeFirestore } = require('firestore-jest-mock');
const {
import { mockFirebase, FakeFirestore } from 'firestore-jest-mock';
import {
mockRunTransaction,
mockDelete,
mockDeleteTransaction,
Expand All @@ -12,23 +12,24 @@ const {
mockGetAll,
mockGetAllTransaction,
mockCreateTransaction,
} = require('firestore-jest-mock/mocks/firestore');
} from 'firestore-jest-mock/mocks/firestore';

describe('Transactions', () => {
let firebase, db;
mockFirebase({
database: {},
});
const firebase = require('firebase');
firebase.initializeApp({
apiKey: '### FIREBASE API KEY ###',
authDomain: '### FIREBASE AUTH DOMAIN ###',
projectId: '### CLOUD FIRESTORE PROJECT ID ###',
});
const db = firebase.firestore();

beforeEach(() => {

beforeEach(async () => {
jest.resetModules();
jest.clearAllMocks();
firebase = await import('firebase');
firebase.initializeApp({
apiKey: '### FIREBASE API KEY ###',
authDomain: '### FIREBASE AUTH DOMAIN ###',
projectId: '### CLOUD FIRESTORE PROJECT ID ###',
});
db = firebase.firestore();
});

test('it returns a Promise', () => {
Expand Down
Loading

0 comments on commit 1f66dd8

Please sign in to comment.