Skip to content

Commit 4bb560d

Browse files
committed
add full actions and reducers code
1 parent 10b3448 commit 4bb560d

18 files changed

+2605
-60
lines changed

.babelrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"presets": ["react-native"],
3+
"plugins": [
4+
"transform-decorators-legacy"
5+
]
6+
}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/
2+
coverage/

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
# react-native-redux-firebase-patterns
1+
# Some code patterns for working with react native, Firebase and redux-thunk
2+
3+
To run the tests
4+
```
5+
git clone [email protected]:rmrs/react-native-redux-firebase-patterns.git
6+
7+
yarn install
8+
9+
npm run coverage
10+
```

__mocks__/react-native-firebase.js

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'use strict'
2+
3+
export class Database {
4+
ref = (path) => {
5+
if (!this[path]) {
6+
this[path] = new Reference(path)
7+
}
8+
return this[path]
9+
}
10+
}
11+
12+
export class Reference {
13+
constructor(path) {
14+
this.path = path
15+
this.snap = { val: () => this._val()}
16+
this.data = null
17+
}
18+
19+
_val = jest.fn(() => {
20+
return this.data
21+
})
22+
23+
once = jest.fn((param, callback) => {
24+
const promise = new Promise ((resolve, reject) => {
25+
if (callback) {
26+
callback(this.snap)
27+
resolve()
28+
} else {
29+
resolve(this.snap)
30+
}
31+
})
32+
RNFirebase.promises.push(promise)
33+
return promise
34+
})
35+
36+
on = jest.fn((param, callback) => {
37+
const promise = new Promise ((resolve, reject) => {
38+
if (callback) {
39+
callback(this.snap)
40+
resolve()
41+
} else {
42+
resolve(this.snap)
43+
}
44+
})
45+
RNFirebase.promises.push(promise)
46+
return promise
47+
})
48+
49+
off = jest.fn((param, callback) => {
50+
const promise = Promise.resolve()
51+
RNFirebase.promises.push(promise)
52+
return promise
53+
})
54+
55+
update = jest.fn((data) => {
56+
const promise = Promise.resolve()
57+
RNFirebase.promises.push(promise)
58+
return promise
59+
})
60+
61+
remove = jest.fn(() => {
62+
const promise = Promise.resolve()
63+
RNFirebase.promises.push(promise)
64+
return promise
65+
})
66+
}
67+
68+
export class MockFirebase {
69+
constructor() {
70+
this.database = () => {
71+
if (!this.databaseInstance) {
72+
this.databaseInstance = new Database()
73+
}
74+
return this.databaseInstance
75+
}
76+
}
77+
}
78+
79+
export default class RNFirebase {
80+
static initializeApp() {
81+
RNFirebase.firebase = new MockFirebase()
82+
RNFirebase.promises = []
83+
return RNFirebase.firebase
84+
}
85+
86+
static reset() {
87+
RNFirebase.promises = []
88+
RNFirebase.firebase.databaseInstance = null
89+
}
90+
91+
static waitForPromises() {
92+
return Promise.all(RNFirebase.promises)
93+
}
94+
}

0 commit comments

Comments
 (0)