Skip to content

Commit

Permalink
feat(FirestoreCollection and FirestoreDocument): add the 'children' p…
Browse files Browse the repository at this point in the history
…rop as another rendering method (#15)

fix #14
  • Loading branch information
sampl authored and green-arrow committed May 7, 2018
1 parent 48cb1bf commit 0c3be00
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 18 deletions.
16 changes: 8 additions & 8 deletions src/FirestoreCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class FirestoreCollection extends Component {
PropTypes.arrayOf(PropTypes.string),
PropTypes.arrayOf(PropTypes.array),
]),
render: PropTypes.func.isRequired,
children: PropTypes.func,
render: PropTypes.func,
};

static contextTypes = {
Expand Down Expand Up @@ -106,14 +107,13 @@ class FirestoreCollection extends Component {
};

render() {
const { isLoading, data, snapshot } = this.state;
const { render } = this.props;
const { children, render } = this.props;

return render({
isLoading,
data,
snapshot,
});
if (render) return render(this.state);

if (typeof children === 'function') return children(this.state);

return null;
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/FirestoreDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import PropTypes from 'prop-types';
class FirestoreDocument extends Component {
static propTypes = {
path: PropTypes.string.isRequired,
render: PropTypes.func.isRequired,
children: PropTypes.func,
render: PropTypes.func,
};

static contextTypes = {
Expand Down Expand Up @@ -62,14 +63,13 @@ class FirestoreDocument extends Component {
};

render() {
const { isLoading, data, snapshot } = this.state;
const { render } = this.props;
const { children, render } = this.props;

return render({
isLoading,
data,
snapshot,
});
if (render) return render(this.state);

if (typeof children === 'function') return children(this.state);

return null;
}
}

Expand Down
49 changes: 48 additions & 1 deletion src/__tests__/collection.initial-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,53 @@ test('initial state set up correctly', () => {
expect.objectContaining({
isLoading: true,
data: [],
})
}),
);
});

test('renders child prop', () => {
const {
firestoreMock,
collectionMock,
onSnapshotMock,
} = createMocksForCollection();
const renderMock = jest.fn().mockReturnValue(<div />);
const collectionName = 'users';

mount(
<FirestoreCollection path={collectionName}>
{renderMock}
</FirestoreCollection>,
{
context: { firestoreDatabase: firestoreMock, firestoreCache: {} },
},
);

expect(collectionMock).toHaveBeenCalledTimes(1);
expect(collectionMock).toHaveBeenCalledWith(collectionName);
expect(onSnapshotMock).toHaveBeenCalledTimes(1);
expect(renderMock).toHaveBeenCalledTimes(1);
expect(renderMock).toHaveBeenCalledWith(
expect.objectContaining({
isLoading: true,
data: [],
}),
);
});

test('renders nothing if passed no render prop or children', () => {
const {
firestoreMock,
collectionMock,
onSnapshotMock,
} = createMocksForCollection();
const collectionName = 'users';

mount(<FirestoreCollection path={collectionName} />, {
context: { firestoreDatabase: firestoreMock, firestoreCache: {} },
});

expect(collectionMock).toHaveBeenCalledTimes(1);
expect(collectionMock).toHaveBeenCalledWith(collectionName);
expect(onSnapshotMock).toHaveBeenCalledTimes(1);
});
47 changes: 46 additions & 1 deletion src/__tests__/document.initial-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,51 @@ test('initial state set up correctly', () => {
expect.objectContaining({
isLoading: true,
data: null,
})
}),
);
});

test('renders child prop', () => {
const {
firestoreMock,
documentMock,
onSnapshotMock,
} = createMocksForDocument();
const renderMock = jest.fn().mockReturnValue(<div />);
const documentPath = 'users/1';

mount(
<FirestoreDocument path={documentPath}>{renderMock}</FirestoreDocument>,
{
context: { firestoreDatabase: firestoreMock, firestoreCache: {} },
},
);

expect(documentMock).toHaveBeenCalledTimes(1);
expect(documentMock).toHaveBeenCalledWith(documentPath);
expect(onSnapshotMock).toHaveBeenCalledTimes(1);
expect(renderMock).toHaveBeenCalledTimes(1);
expect(renderMock).toHaveBeenCalledWith(
expect.objectContaining({
isLoading: true,
data: null,
}),
);
});

test('renders nothing if passed no render prop or children', () => {
const {
firestoreMock,
documentMock,
onSnapshotMock,
} = createMocksForDocument();
const documentPath = 'users/1';

mount(<FirestoreDocument path={documentPath} />, {
context: { firestoreDatabase: firestoreMock, firestoreCache: {} },
});

expect(documentMock).toHaveBeenCalledTimes(1);
expect(documentMock).toHaveBeenCalledWith(documentPath);
expect(onSnapshotMock).toHaveBeenCalledTimes(1);
});

0 comments on commit 0c3be00

Please sign in to comment.