Skip to content

Commit

Permalink
fix(FirestoreProvider): Fix error with applying settings to firestore
Browse files Browse the repository at this point in the history
Setting the 'timestampsInSnapshots' setting needs to be in a call to 'settings', not passed to the 'firestore' function.

Fixes #33
  • Loading branch information
green-arrow committed Nov 13, 2018
1 parent c5dbbbc commit b5e65d3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/FirestoreProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export default class FirestoreProvider extends Component {
static propTypes = {
firebase: PropTypes.object.isRequired,
children: PropTypes.node.isRequired,
useTimestampsInSnapshots: PropTypes.bool.isRequired
useTimestampsInSnapshots: PropTypes.bool.isRequired,
};

static defaultProps = {
useTimestampsInSnapshots: false
}
useTimestampsInSnapshots: false,
};

static childContextTypes = {
firestoreDatabase: PropTypes.object.isRequired,
Expand All @@ -21,10 +21,11 @@ export default class FirestoreProvider extends Component {
constructor(props) {
super(props);

const { firebase } = props;
const { firebase, useTimestampsInSnapshots } = props;
const settings = { timestampsInSnapshots: useTimestampsInSnapshots };

this.state = {
firestoreDatabase: firebase.firestore({timestampsInSnapshots: this.props.useTimestampsInSnapshots}),
firestoreDatabase: firebase.firestore().settings(settings),
firestoreCache: new FirestoreCache(),
};
}
Expand Down
6 changes: 5 additions & 1 deletion src/__tests__/helpers/firestore-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ function createBaseMocks(snapshot, options) {
const documentMock = jest
.fn()
.mockReturnValue({ onSnapshot: onSnapshotMock });
const firestoreMock = { collection: collectionMock, doc: documentMock };
const firestoreMock = {
settings: jest.fn(),
collection: collectionMock,
doc: documentMock,
};

return {
firestoreMock,
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/provider.adds-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ const createChild = () => {
test('database and cache are added to child context', () => {
const Child = createChild();
const firestore = {};
firestore.settings = jest.fn().mockReturnValue(firestore);
const firebase = {
firestore: jest.fn().mockReturnValueOnce(firestore),
};

const component = mount(
<FirestoreProvider firebase={firebase}>
<Child />
</FirestoreProvider>
</FirestoreProvider>,
);
const child = component.find('Child');

Expand Down
33 changes: 33 additions & 0 deletions src/__tests__/provider.firestore-settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { mount } from 'enzyme';
import { FirestoreProvider } from '../';

test('sets timestampsInSnapshots correctly', () => {
const firestore = {};
firestore.settings = jest.fn().mockReturnValue(firestore);
const firebase = {
firestore: jest.fn().mockReturnValue(firestore),
};

mount(
<FirestoreProvider firebase={firebase}>
<div>Test</div>
</FirestoreProvider>,
);

expect(firestore.settings).toHaveBeenCalledTimes(1);
expect(firestore.settings).toHaveBeenCalledWith({
timestampsInSnapshots: false,
});

mount(
<FirestoreProvider firebase={firebase} useTimestampsInSnapshots>
<div>Test</div>
</FirestoreProvider>,
);

expect(firestore.settings).toHaveBeenCalledTimes(2);
expect(firestore.settings).toHaveBeenCalledWith({
timestampsInSnapshots: true,
});
});

0 comments on commit b5e65d3

Please sign in to comment.