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

Migrate to exports #167

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]
node-version: [15.x, 16.x, 17.x, 18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
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
45 changes: 23 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,50 @@ describe('we can start a firebase application', () => {
currentUser: { uid: 'abc123', displayName: 'Bob' },
});

beforeEach(() => {
beforeEach(async () => {
jest.clearAllMocks();
this.firebase = require('firebase');
this.admin = require('firebase-admin');
this.firebase.initializeApp({
firebase = await import('firebase');
admin = await import('firebase-admin');
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 +90,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 +118,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 +128,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 +154,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 +163,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
85 changes: 44 additions & 41 deletions __tests__/full-setup-library-firestore.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import * as FirestoreMock from 'firestore-jest-mock';
import { Timestamp } from '../mocks/timestamp';

import {
mockGet,
mockSelect,
mockAdd,
mockSet,
mockUpdate,
mockWhere,
mockCollectionGroup,
mockBatch,
mockBatchCommit,
mockBatchDelete,
mockBatchUpdate,
mockBatchSet,
mockSettings,
mockOnSnapShot,
mockListCollections,
mockTimestampNow,
} from '../mocks/firestore';

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 +51,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 +82,7 @@ describe.each([
});

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

return firestore
.collection('users')
Expand All @@ -97,7 +100,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 +117,7 @@ describe.each([
});

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

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

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

return firestore
.collectionGroup('users')
Expand All @@ -153,7 +156,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 +179,7 @@ describe.each([
});

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

return firestore
.collection('cities')
Expand All @@ -196,7 +199,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 +216,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 +244,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 +255,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 +264,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 +281,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 +292,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 +316,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 +346,7 @@ describe.each([
});

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

const unsubscribe = firestore
.collection('cities')
Expand Down
Loading
Loading