Skip to content

Commit db70828

Browse files
authoredNov 4, 2022
Merge pull request #274 from levino/feature/test-use-document
chore: add testing
2 parents 0d75ab2 + bcab2b7 commit db70828

13 files changed

+29216
-3102
lines changed
 

‎.firebaserc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "demo-noop"
4+
}
5+
}

‎.github/workflows/checks.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: build
2+
on:
3+
pull_request:
4+
jobs:
5+
build:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v3
9+
- uses: actions/setup-node@v3
10+
with:
11+
node-version: 16
12+
cache: npm
13+
- name: Install
14+
run: npm install
15+
- name: Build
16+
run: npm run build
17+
- name: Test
18+
run: npm run test:ci
19+
test:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v3
23+
- uses: actions/setup-node@v3
24+
with:
25+
node-version: 16
26+
cache: npm
27+
- name: Install
28+
run: npm install
29+
- name: Test
30+
run: npm run test:ci

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ coverage
1616

1717
#test files
1818
/example
19+
fire*.log
20+
/.idea/

‎babel.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
['@babel/preset-env', { targets: { node: 'current' } }],
4+
'@babel/preset-typescript',
5+
],
6+
};

‎firebase.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"functions": {
3+
"runtime": "node16"
4+
},
5+
"firestore": {
6+
"rules": "firestore.rules",
7+
"indexes": "firestore.indexes.json"
8+
},
9+
"emulators": {
10+
"firestore": {
11+
"port": 8080
12+
},
13+
"ui": {
14+
"enabled": false
15+
},
16+
"singleProjectMode": true
17+
}
18+
}

‎firestore.indexes.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
// Example:
3+
//
4+
// "indexes": [
5+
// {
6+
// "collectionGroup": "widgets",
7+
// "queryScope": "COLLECTION",
8+
// "fields": [
9+
// { "fieldPath": "foo", "arrayConfig": "CONTAINS" },
10+
// { "fieldPath": "bar", "mode": "DESCENDING" }
11+
// ]
12+
// },
13+
//
14+
// "fieldOverrides": [
15+
// {
16+
// "collectionGroup": "widgets",
17+
// "fieldPath": "baz",
18+
// "indexes": [
19+
// { "order": "ASCENDING", "queryScope": "COLLECTION" }
20+
// ]
21+
// },
22+
// ]
23+
// ]
24+
"indexes": [],
25+
"fieldOverrides": []
26+
}

‎firestore.rules

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
rules_version = '2';
2+
service cloud.firestore {
3+
match /databases/{database}/documents {
4+
match /{document=**} {
5+
allow read, write: if true;
6+
}
7+
}
8+
}

‎firestore/useDocument.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { addDoc, collection, doc } from 'firebase/firestore';
2+
3+
import { db } from '../test/firebase';
4+
import { renderHook } from '@testing-library/react-hooks';
5+
import { useDocument } from './useDocument';
6+
7+
describe('useDocument hook', () => {
8+
test('begins in loading state', async () => {
9+
// arrange
10+
const { id } = await addDoc(collection(db, 'test'), {});
11+
12+
// act
13+
const { result, unmount } = renderHook(() =>
14+
useDocument(doc(collection(db, 'test'), id))
15+
);
16+
17+
//assert
18+
expect(result.current[1]).toBeTruthy();
19+
20+
// clean up
21+
unmount();
22+
});
23+
24+
test('loads and returns data from server', async () => {
25+
// arrange
26+
const { id } = await addDoc(collection(db, 'test'), { name: 'bo' });
27+
28+
// act
29+
const { result, waitFor, unmount } = renderHook(() =>
30+
useDocument(doc(collection(db, 'test'), id))
31+
);
32+
await waitFor(() => result.current[1] === false);
33+
34+
// assert
35+
expect(result.current[0]?.data()).toEqual({ name: 'bo' });
36+
37+
// clean up
38+
unmount();
39+
});
40+
});

‎jest.config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { Config } from 'jest';
2+
3+
const config: Config = {
4+
testEnvironment: 'node',
5+
};
6+
7+
export default config;

0 commit comments

Comments
 (0)
Please sign in to comment.