Skip to content

Commit

Permalink
initial port and setup for both cjs and es
Browse files Browse the repository at this point in the history
  • Loading branch information
sbatson5 committed May 27, 2023
1 parent f2158d0 commit 7ef5014
Show file tree
Hide file tree
Showing 25 changed files with 24,517 additions and 19,894 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DS_Store
.vscode/launch.json
node_modules
/dist
dist

# Log files
npm-debug.log*
Expand Down
47 changes: 25 additions & 22 deletions __tests__/auth.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { mockFirebase } = require('firestore-jest-mock');
const { mockInitializeApp } = require('../mocks/firebase');
const {
import { mockFirebase } from 'firestore-jest-mock';
import { mockInitializeApp } from '../mocks/firebase';
import {
mockCreateUserWithEmailAndPassword,
mockSignInWithEmailAndPassword,
mockSignOut,
Expand All @@ -11,9 +11,10 @@ const {
mockCreateCustomToken,
mockSetCustomUserClaims,
mockUseEmulator,
} = require('../mocks/auth');
} from '../mocks/auth';

describe('we can start a firebase application', () => {
let admin, firebase;
mockFirebase({
database: {
users: [
Expand All @@ -36,50 +37,52 @@ describe('we can start a firebase application', () => {
currentUser: { uid: 'abc123', displayName: 'Bob' },
});

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

test('We can start an application', async () => {
this.firebase.auth();
firebase.auth();
expect(mockInitializeApp).toHaveBeenCalled();
});

test('We can use emulator', () => {
this.firebase.auth().useEmulator('http://localhost:9099');
firebase.auth().useEmulator('http://localhost:9099');
expect(mockUseEmulator).toHaveBeenCalledWith('http://localhost:9099');
});

describe('Client Auth Operations', () => {
describe('Examples from documentation', () => {
test('add a user', async () => {
expect.assertions(1);
await this.firebase.auth().createUserWithEmailAndPassword('sam', 'hill');
await firebase.auth().createUserWithEmailAndPassword('sam', 'hill');
expect(mockCreateUserWithEmailAndPassword).toHaveBeenCalledWith('sam', 'hill');
});

test('sign in', async () => {
expect.assertions(1);
await this.firebase.auth().signInWithEmailAndPassword('sam', 'hill');
await firebase.auth().signInWithEmailAndPassword('sam', 'hill');
expect(mockSignInWithEmailAndPassword).toHaveBeenCalledWith('sam', 'hill');
});

test('sign out', async () => {
expect.assertions(1);
await this.firebase.auth().signOut();
await firebase.auth().signOut();
expect(mockSignOut).toHaveBeenCalled();
});

test('send password reset email', async () => {
expect.assertions(1);
await this.firebase.auth().sendPasswordResetEmail('sam', null);
await firebase.auth().sendPasswordResetEmail('sam', null);
expect(mockSendPasswordResetEmail).toHaveBeenCalledWith('sam', null);
});
});
Expand All @@ -89,25 +92,25 @@ describe('we can start a firebase application', () => {
describe('Examples from documentation', () => {
test('delete a user', async () => {
expect.assertions(1);
await this.admin.auth().deleteUser('some-uid');
await admin.auth().deleteUser('some-uid');
expect(mockDeleteUser).toHaveBeenCalledWith('some-uid');
});

test('verify an ID token', async () => {
expect.assertions(1);
await this.admin.auth().verifyIdToken('token_string', true);
await admin.auth().verifyIdToken('token_string', true);
expect(mockVerifyIdToken).toHaveBeenCalledWith('token_string', true);
});

test('get user object', async () => {
expect.assertions(1);
await this.admin.auth().getUser('some-uid');
await admin.auth().getUser('some-uid');
expect(mockGetUser).toHaveBeenCalledWith('some-uid');
});

test('get currentUser object', async () => {
expect.assertions(2);
const currentUser = await this.admin.auth().currentUser;
const currentUser = await admin.auth().currentUser;
expect(currentUser.uid).toEqual('abc123');
expect(currentUser.data.displayName).toBe('Bob');
});
Expand All @@ -117,7 +120,7 @@ describe('we can start a firebase application', () => {
const claims = {
custom: true,
};
const token = await this.admin.auth().createCustomToken('some-uid', claims);
const token = await admin.auth().createCustomToken('some-uid', claims);
expect(mockCreateCustomToken).toHaveBeenCalledWith('some-uid', claims);
expect(token).toEqual('');
});
Expand All @@ -127,7 +130,7 @@ describe('we can start a firebase application', () => {
const claims = {
do: 'the thing',
};
await this.admin.auth().setCustomUserClaims('some-uid', claims);
await admin.auth().setCustomUserClaims('some-uid', claims);
expect(mockSetCustomUserClaims).toHaveBeenCalledWith('some-uid', claims);
});
});
Expand All @@ -153,7 +156,7 @@ describe('we can start a firebase application', () => {
};
mockGetUser.mockReturnValueOnce(userRecord);
expect.assertions(2);
const result = await this.admin.auth().getUser(uid);
const result = await admin.auth().getUser(uid);
expect(mockGetUser).toHaveBeenCalledWith(uid);
expect(result).toStrictEqual(userRecord);
});
Expand All @@ -162,7 +165,7 @@ describe('we can start a firebase application', () => {
const error = new Error('test');
expect.assertions(1);
mockVerifyIdToken.mockRejectedValueOnce(error);
const result = await this.admin
const result = await admin
.auth()
.verifyIdToken('token_string', true)
.catch(err => err);
Expand Down
87 changes: 46 additions & 41 deletions __tests__/full-setup-library-firestore.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import * as FirestoreMock from 'firestore-jest-mock';
import { Timestamp } from '../mocks/timestamp';

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

const {
mockGet,
mockSelect,
mockAdd,
mockSet,
mockUpdate,
mockWhere,
mockCollectionGroup,
mockBatch,
mockBatchCommit,
mockBatchDelete,
mockBatchUpdate,
mockBatchSet,
mockSettings,
mockOnSnapShot,
mockListCollections,
mockTimestampNow,
} = mocks;

describe.each([
{ library: '@google-cloud/firestore', mockFunction: 'mockGoogleCloudFirestore' },
{ library: '@react-native-firebase/firestore', mockFunction: 'mockReactNativeFirestore' },
])('mocking %i with %i', ({ library, mockFunction }) => {
const FirestoreMock = require('firestore-jest-mock');

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,
mockListCollections,
mockTimestampNow,
} = require('../mocks/firestore');

describe('we can start a firestore application', () => {
let Firestore;
FirestoreMock[mockFunction]({
database: {
users: [
Expand All @@ -49,21 +53,22 @@ describe.each([
},
});

beforeEach(() => {
this.Firestore = require(library).Firestore;
beforeEach(async () => {
const lib = await import(library);
Firestore = lib.Firestore;
});

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

test('We can start an application', async () => {
const firestore = new this.Firestore();
const firestore = new Firestore();
firestore.settings({ ignoreUndefinedProperties: true });
expect(mockSettings).toHaveBeenCalledWith({ ignoreUndefinedProperties: true });
});

describe('Examples from documentation', () => {
test('add a user', () => {
const firestore = new this.Firestore();
const firestore = new Firestore();

return firestore
.collection('users')
Expand All @@ -79,7 +84,7 @@ describe.each([
});

test('get all users', () => {
const firestore = new this.Firestore();
const firestore = new Firestore();

return firestore
.collection('users')
Expand All @@ -97,7 +102,7 @@ describe.each([
});

test('select specific fields only', () => {
const firestore = new this.Firestore();
const firestore = new Firestore();

return firestore
.collection('users')
Expand All @@ -114,7 +119,7 @@ describe.each([
});

test('select refs only', () => {
const firestore = new this.Firestore();
const firestore = new Firestore();

return firestore
.collection('users')
Expand All @@ -129,7 +134,7 @@ describe.each([
});

test('collectionGroup at root', () => {
const firestore = new this.Firestore();
const firestore = new Firestore();

return firestore
.collectionGroup('users')
Expand All @@ -153,7 +158,7 @@ describe.each([

test('collectionGroup with subcollections', () => {
jest.clearAllMocks();
const firestore = new this.Firestore();
const firestore = new Firestore();

return firestore
.collectionGroup('cities')
Expand All @@ -176,7 +181,7 @@ describe.each([
});

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

return firestore
.collection('cities')
Expand All @@ -196,7 +201,7 @@ describe.each([
});

test('updating a city', () => {
const firestore = new this.Firestore();
const firestore = new Firestore();
const now = Timestamp._fromMillis(new Date().getTime());
const washingtonRef = firestore.collection('cities').doc('DC');

Expand All @@ -213,7 +218,7 @@ describe.each([
});

test('batch writes', () => {
const firestore = new this.Firestore();
const firestore = new Firestore();

// Get a new write batch
const batch = firestore.batch();
Expand Down Expand Up @@ -241,7 +246,7 @@ describe.each([
});

test('listCollections returns a promise', async () => {
const firestore = new this.Firestore();
const firestore = new Firestore();

const listCollectionsPromise = firestore
.collection('cities')
Expand All @@ -252,7 +257,7 @@ describe.each([
});

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

const result = await firestore
.collection('users')
Expand All @@ -261,12 +266,12 @@ describe.each([

expect(result).toEqual(expect.any(Array));
expect(result).toHaveLength(1);
expect(result[0]).toEqual(expect.any(this.Firestore.CollectionReference));
expect(result[0]).toEqual(expect.any(Firestore.CollectionReference));
expect(result[0].id).toBe('cities');
});

test('listCollections resolves with empty array if there are no collections in document', async () => {
const firestore = new this.Firestore();
const firestore = new Firestore();

const result = await firestore
.collection('users')
Expand All @@ -278,7 +283,7 @@ describe.each([
});

test('listCollections calls mockListCollections', async () => {
const firestore = new this.Firestore();
const firestore = new Firestore();

await firestore
.collection('users')
Expand All @@ -289,7 +294,7 @@ describe.each([
});

test('onSnapshot single doc', async () => {
const firestore = new this.Firestore();
const firestore = new Firestore();
const now = Timestamp._fromMillis(new Date().getTime());

mockTimestampNow.mockReturnValue(now);
Expand All @@ -313,7 +318,7 @@ describe.each([
});

test('onSnapshot can work with options', async () => {
const firestore = new this.Firestore();
const firestore = new Firestore();
const now = Timestamp._fromMillis(new Date().getTime());

mockTimestampNow.mockReturnValue(now);
Expand Down Expand Up @@ -343,7 +348,7 @@ describe.each([
});

test('onSnapshot with query', async () => {
const firestore = new this.Firestore();
const firestore = new Firestore();

const unsubscribe = firestore
.collection('cities')
Expand Down
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { presets: ['@babel/preset-env'] }
Loading

0 comments on commit 7ef5014

Please sign in to comment.