Skip to content

Commit 925920c

Browse files
Qaiser AbbasQaiser Abbas
Qaiser Abbas
authored and
Qaiser Abbas
committed
Mock API for tests using Dependency Injection in MobX State Tree
1 parent ad23749 commit 925920c

File tree

5 files changed

+280
-6
lines changed

5 files changed

+280
-6
lines changed

App.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import { YellowBox } from 'react-native'
12
import { createDrawerNavigator } from 'react-navigation'
23
import BookStack from './src/views/book'
34
import AuthorStack from './src/views/author'
45

6+
YellowBox.ignoreWarnings([
7+
`Warning: isMounted(...) is deprecated`,
8+
'Module RCTImageLoader',
9+
])
10+
511
const App = createDrawerNavigator({
612
Books: BookStack,
713
Authors: AuthorStack,

src/stores/book/__tests__/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { BookStore } from '../index'
2+
import api from '../mock-api/api'
3+
4+
it('bookstore fetches data', async () => {
5+
const store = BookStore.create({ books: [] }, { api })
6+
await store.loadBooks()
7+
expect(store.books.length).toBe(10)
8+
})

src/stores/book/index.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { types as t, flow } from 'mobx-state-tree'
2-
import api from './api'
1+
import { types as t, flow, getEnv } from 'mobx-state-tree'
2+
import bookApi from './api'
33

44
let store = null
55

@@ -12,10 +12,15 @@ const Book = t.model('Book', {
1212
inStock: t.optional(t.boolean, true),
1313
})
1414

15-
const BookStore = t
15+
export const BookStore = t
1616
.model('BookStore', {
1717
books: t.array(Book),
1818
})
19+
.views(self => ({
20+
get api() {
21+
return getEnv(self).api
22+
},
23+
}))
1924
.actions(self => {
2025
function updateBooks(books) {
2126
books.items.forEach(book => {
@@ -32,7 +37,7 @@ const BookStore = t
3237

3338
const loadBooks = flow(function* loadBooks() {
3439
try {
35-
const books = yield api.fetchBooks()
40+
const books = yield self.api.fetchBooks()
3641
updateBooks(books)
3742
} catch (err) {
3843
console.error('Failed to load books ', err)
@@ -44,9 +49,9 @@ const BookStore = t
4449
}
4550
})
4651

47-
export default () => {
52+
export default (api = bookApi) => {
4853
if (store) return store
4954

50-
store = BookStore.create({ books: [] })
55+
store = BookStore.create({ books: [] }, { api })
5156
return store
5257
}

src/stores/book/mock-api/api.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const books = require('./books')
2+
3+
const delayedPromise = (data, delaySecs = 2) =>
4+
new Promise(resolve => setTimeout(() => resolve(data), delaySecs * 1000))
5+
6+
const fetchBooks = () => delayedPromise(books)
7+
8+
export default {
9+
fetchBooks,
10+
}

src/stores/book/mock-api/books.json

+245
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
{
2+
"kind": "books#volumes",
3+
"totalItems": 6382,
4+
"items": [
5+
{
6+
"id": "0_Paklse3XcC",
7+
"selfLink": "https://www.googleapis.com/books/v1/volumes/0_Paklse3XcC",
8+
"volumeInfo": {
9+
"title": "The Waterbug Book",
10+
"subtitle": "A Guide to the Freshwater Macroinvertebrates of Temperate Australia",
11+
"authors": [
12+
"John Gooderham",
13+
"Edward Tsyrlin"
14+
],
15+
"publisher": "CSIRO PUBLISHING",
16+
"publishedDate": "2002",
17+
"description": "Freshwater invertebrates identification guide for both professionals and non-professionals.",
18+
"pageCount": 232,
19+
"printType": "BOOK",
20+
"categories": [
21+
"Nonfiction"
22+
],
23+
"imageLinks": {
24+
"smallThumbnail": "http://books.google.com/books/content?id=0_Paklse3XcC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
25+
"thumbnail": "http://books.google.com/books/content?id=0_Paklse3XcC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
26+
},
27+
"canonicalVolumeLink": "https://books.google.com/books/about/The_Waterbug_Book.html?hl=&id=0_Paklse3XcC"
28+
}
29+
},
30+
{
31+
"id": "V5N1tW8-T7EC",
32+
"selfLink": "https://www.googleapis.com/books/v1/volumes/V5N1tW8-T7EC",
33+
"volumeInfo": {
34+
"title": "The Secret Sharer",
35+
"subtitle": "Easyread Comfort Edition",
36+
"authors": [
37+
"Joseph Conrad"
38+
],
39+
"publisher": "ReadHowYouWant.com",
40+
"publishedDate": "2008-01-14",
41+
"description": "This intriguing short story is narrated by a young man who has recently been promoted as a ship captain.",
42+
"pageCount": 88,
43+
"printType": "BOOK",
44+
"categories": [
45+
"Fiction"
46+
],
47+
"imageLinks": {
48+
"smallThumbnail": "http://books.google.com/books/content?id=V5N1tW8-T7EC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
49+
"thumbnail": "http://books.google.com/books/content?id=V5N1tW8-T7EC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
50+
}
51+
}
52+
},
53+
{
54+
"id": "po4Es1kztr8C",
55+
"selfLink": "https://www.googleapis.com/books/v1/volumes/po4Es1kztr8C",
56+
"volumeInfo": {
57+
"title": "Life on Air",
58+
"subtitle": "Memoirs of a Broadcaster",
59+
"authors": [
60+
"David Attenborough"
61+
],
62+
"publisher": "Random House",
63+
"publishedDate": "2009",
64+
"description": "David Attenborough is one of the most influential and admired figures in English television.",
65+
"pageCount": 416,
66+
"printType": "BOOK",
67+
"categories": [
68+
"Nonfiction"
69+
],
70+
"imageLinks": {
71+
"smallThumbnail": "http://books.google.com/books/content?id=po4Es1kztr8C&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
72+
"thumbnail": "http://books.google.com/books/content?id=po4Es1kztr8C&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
73+
},
74+
"canonicalVolumeLink": "https://books.google.com/books/about/Life_on_Air.html?hl=&id=po4Es1kztr8C"
75+
}
76+
},
77+
{
78+
"id": "FrUYdXKZFZwC",
79+
"selfLink": "https://www.googleapis.com/books/v1/volumes/FrUYdXKZFZwC",
80+
"volumeInfo": {
81+
"title": "The Book",
82+
"subtitle": "Playing the Percentages in Baseball",
83+
"authors": [
84+
"Tom M. Tango",
85+
"Mitchel G. Lichtman",
86+
"Andrew E. Dolphin"
87+
],
88+
"publisher": "Potomac Books, Inc.",
89+
"publishedDate": "2007",
90+
"description": "Written by three esteemed baseball statisticians, The Bookcontinues where the legendary Bill James’s Baseball Abstractsand Palmer and Thorn’s The Hidden Game of Baseballleft off more than twenty years ago.",
91+
"pageCount": 385,
92+
"printType": "BOOK",
93+
"categories": [
94+
"Nonfiction"
95+
],
96+
"imageLinks": {
97+
"smallThumbnail": "http://books.google.com/books/content?id=FrUYdXKZFZwC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
98+
"thumbnail": "http://books.google.com/books/content?id=FrUYdXKZFZwC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
99+
},
100+
"canonicalVolumeLink": "https://books.google.com/books/about/The_Book.html?hl=&id=FrUYdXKZFZwC"
101+
}
102+
},
103+
104+
{
105+
"id": "bkt6CeNye60C",
106+
"selfLink": "https://www.googleapis.com/books/v1/volumes/bkt6CeNye60C",
107+
"volumeInfo": {
108+
"title": "The Radiance of the King",
109+
"authors": [
110+
"Laye Camara",
111+
"James Kirkup"
112+
],
113+
"publisher": "New York Review of Books",
114+
"publishedDate": "2001",
115+
"description": "At the beginning of this masterpiece of African literature, Clarence, a white man, has been shipwrecked on the coast of Africa.",
116+
"pageCount": 279,
117+
"printType": "BOOK",
118+
"categories": [
119+
"Fiction"
120+
],
121+
"imageLinks": {
122+
"smallThumbnail": "http://books.google.com/books/content?id=bkt6CeNye60C&printsec=frontcover&img=1&zoom=5&source=gbs_api",
123+
"thumbnail": "http://books.google.com/books/content?id=bkt6CeNye60C&printsec=frontcover&img=1&zoom=1&source=gbs_api"
124+
},
125+
"canonicalVolumeLink": "https://books.google.com/books/about/The_Radiance_of_the_King.html?hl=&id=bkt6CeNye60C"
126+
}
127+
},
128+
{
129+
"id": "uaKzAAAAQBAJ",
130+
"selfLink": "https://www.googleapis.com/books/v1/volumes/uaKzAAAAQBAJ",
131+
"volumeInfo": {
132+
"title": "By The Book",
133+
"subtitle": "A reader's guide to life",
134+
"authors": [
135+
"Ramona Koval"
136+
],
137+
"publisher": "Text Publishing",
138+
"publishedDate": "2013-10-23",
139+
"description": "By the Book is Ramona Koval's love letter to books and writing, a tribute to the stories that have changed and enriched her life Ramona Koval is one of Australia's best-loved broadcasters.",
140+
"pageCount": 256,
141+
"printType": "BOOK",
142+
"categories": [
143+
"Nonfiction"
144+
],
145+
"imageLinks": {
146+
"smallThumbnail": "http://books.google.com/books/content?id=uaKzAAAAQBAJ&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
147+
"thumbnail": "http://books.google.com/books/content?id=uaKzAAAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
148+
},
149+
"canonicalVolumeLink": "https://books.google.com/books/about/By_The_Book.html?hl=&id=uaKzAAAAQBAJ"
150+
}
151+
},
152+
{
153+
"id": "94wwDwAAQBAJ",
154+
"selfLink": "https://www.googleapis.com/books/v1/volumes/94wwDwAAQBAJ",
155+
"volumeInfo": {
156+
"title": "Tartarugas até lá embaixo",
157+
"subtitle": "",
158+
"authors": [
159+
"John Green"
160+
],
161+
"publisher": "Editora Intrinseca",
162+
"publishedDate": "2017-10-10",
163+
"pageCount": 256,
164+
"printType": "BOOK",
165+
"categories": [
166+
"Fiction"
167+
],
168+
"imageLinks": {
169+
"smallThumbnail": "http://books.google.com/books/content?id=94wwDwAAQBAJ&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
170+
"thumbnail": "http://books.google.com/books/content?id=94wwDwAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
171+
},
172+
"canonicalVolumeLink": "https://market.android.com/details?id=book-94wwDwAAQBAJ"
173+
}
174+
},
175+
{
176+
"id": "dSJQn8egXvUC",
177+
"selfLink": "https://www.googleapis.com/books/v1/volumes/dSJQn8egXvUC",
178+
"volumeInfo": {
179+
"title": "Nudge",
180+
"subtitle": "Improving Decisions about Health, Wealth, and Happiness",
181+
"authors": [
182+
"Richard H. Thaler",
183+
"Cass R. Sunstein"
184+
],
185+
"publisher": "Yale University Press",
186+
"publishedDate": "2008",
187+
"description": "Thaler and Sunstein offer a groundbreaking discussion of how to apply the science of choice to nudge people toward decisions that can improve their lives.",
188+
"pageCount": 293,
189+
"printType": "BOOK",
190+
"categories": [
191+
"Nonfiction"
192+
],
193+
"imageLinks": {
194+
"smallThumbnail": "http://books.google.com/books/content?id=dSJQn8egXvUC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
195+
"thumbnail": "http://books.google.com/books/content?id=dSJQn8egXvUC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
196+
},
197+
"canonicalVolumeLink": "https://books.google.com/books/about/Nudge.html?hl=&id=dSJQn8egXvUC"
198+
}
199+
},
200+
{
201+
"id": "DRoL0NmsrlwC",
202+
"selfLink": "https://www.googleapis.com/books/v1/volumes/DRoL0NmsrlwC",
203+
"volumeInfo": {
204+
"title": "On War",
205+
"authors": [
206+
"Carl von Clausewitz"
207+
],
208+
"publisher": "Princeton University Press",
209+
"publishedDate": "1989-06-21",
210+
"description": "Intends to understand war, both in its internal dynamics and as an instrument of policy.",
211+
"pageCount": 732,
212+
"printType": "BOOK",
213+
"categories": [
214+
"Nonfiction"
215+
],
216+
"imageLinks": {
217+
"smallThumbnail": "http://books.google.com/books/content?id=DRoL0NmsrlwC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
218+
"thumbnail": "http://books.google.com/books/content?id=DRoL0NmsrlwC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
219+
},
220+
"canonicalVolumeLink": "https://books.google.com/books/about/On_War.html?hl=&id=DRoL0NmsrlwC"
221+
}
222+
},
223+
{
224+
"id": "lSMGAAAAQAAJ",
225+
"selfLink": "https://www.googleapis.com/books/v1/volumes/lSMGAAAAQAAJ",
226+
"volumeInfo": {
227+
"title": "Jane Eyre",
228+
"authors": [
229+
"Charlotte Brontë"
230+
],
231+
"publishedDate": "1864",
232+
"description": "Bronte’s novel about a shy, quiet governess who becomes a tutor in a great house and falls in love with its lonely and mysterious master.",
233+
"pageCount": 480,
234+
"categories": [
235+
"Nonfiction"
236+
],
237+
"imageLinks": {
238+
"smallThumbnail": "http://books.google.com/books/content?id=lSMGAAAAQAAJ&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
239+
"thumbnail": "http://books.google.com/books/content?id=lSMGAAAAQAAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
240+
},
241+
"canonicalVolumeLink": "https://market.android.com/details?id=book-lSMGAAAAQAAJ"
242+
}
243+
}
244+
]
245+
}

0 commit comments

Comments
 (0)