Skip to content

Commit 23ed8bf

Browse files
docs(website): use async methods and improve testing docs
1 parent 1d410cb commit 23ed8bf

File tree

6 files changed

+64
-34
lines changed

6 files changed

+64
-34
lines changed

packages/overmind-website/examples/api/effects.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ import axios from 'axios'
88
import { User, Item } from './state'
99
1010
export const api = {
11-
getUser(): Promise<User> {
12-
return fetch('/user').then(response => response.json())
11+
async getUser(): Promise<User> {
12+
const response = await fetch('/user')
13+
14+
return response.json()
1315
},
14-
getItem(id: number): Promise<Item> {
15-
return fetch(\`/items/\${id}\`).then(response => response.json())
16+
async getItem(id: number): Promise<Item> {
17+
const response = await fetch(\`/items/\${id}\`)
18+
19+
return response.json()
1620
}
1721
}
1822
`,
@@ -25,11 +29,15 @@ export const api = {
2529
import axios from 'axios'
2630
2731
export const api = {
28-
getUser() {
29-
return fetch('/user').then(response => response.json())
32+
async getUser() {
33+
const response = await fetch('/user')
34+
35+
return response.json()
3036
},
31-
getItem(id) {
32-
return fetch(\`/items/\${id}\`).then(response => response.json())
37+
async getItem(id) {
38+
const response = fetch(\`/items/\${id}\`)
39+
40+
return response.json()
3341
}
3442
}
3543
`,

packages/overmind-website/examples/frontpage/effects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export default () => [
22
{
33
code: `
44
export const api = {
5-
fetchItems: async () => {
5+
async fetchItems() => {
66
const response = await fetch('/api/items')
77
88
return response.json()

packages/overmind-website/examples/guide/getstarted/actions.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ export const overmind = new Overmind({
4747
},
4848
effects: {
4949
jsonPlaceholder: {
50-
getPosts() {
51-
return fetch('https://jsonplaceholder.typicode.com/posts')
52-
.then(response => response.json())
50+
async getPosts() {
51+
const response = await fetch('https://jsonplaceholder.typicode.com/posts')
52+
53+
return response.json()
5354
}
5455
}
5556
},
5657
actions: {
57-
getPosts: async ({ state, effects }) => {
58+
async getPosts({ state, effects }) => {
5859
state.isLoadingPosts = true
5960
state.posts = await effects.jsonPlaceholder.getPosts()
6061
state.isLoadingPosts = false

packages/overmind-website/examples/guide/getstarted/effects.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import { Post } from './state'
1010
1111
export const jsonPlaceholder = {
1212
async getPosts(): Promise<Post[]> {
13-
return fetch('https://jsonplaceholder.typicode.com/posts')
14-
.then(response => response.json())
13+
const response = await fetch('https://jsonplaceholder.typicode.com/posts')
14+
15+
return response.json()
1516
}
1617
}
1718
`,
@@ -45,9 +46,10 @@ export const overmind = new Overmind({
4546
posts: []
4647
},
4748
effects: {
48-
getPosts() {
49-
return fetch('https://jsonplaceholder.typicode.com/posts')
50-
.then(response => response.json())
49+
async getPosts() {
50+
const response = await fetch('https://jsonplaceholder.typicode.com/posts')
51+
52+
return response.json()
5153
}
5254
}
5355
})

packages/overmind-website/examples/guide/writingtests/effect.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,31 @@ interface IOptions {
1616
baseUrl: string
1717
}
1818
19+
// This is the class we can create new instances of when testing
1920
export class Api {
2021
request: IRequest
2122
options: IOptions
2223
constructor(request: IRequest, options: IOptions) {
2324
this.request = request
2425
this.options = options
2526
}
26-
getPost(id: string): Promise<Post> {
27-
return this.request.get(this.options.baseUrl + '/posts/' + id, {
28-
headers: {
29-
'Auth-Token': this.options.authToken
30-
}
31-
})
32-
.then(response => response.data)
33-
.catch(() => throw new Error('Could not grab post with id ' + id))
27+
async getPost(id: string): Promise<Post> {
28+
try {
29+
const response = await this.request.get(this.options.baseUrl + '/posts/' + id, {
30+
headers: {
31+
'Auth-Token': this.options.authToken
32+
}
33+
})
34+
35+
return response.data
36+
} catch (error) {
37+
throw new Error('Could not grab post with id ' + id)
38+
}
3439
}
3540
}
3641
42+
// We export the default instance that we actually use with our
43+
// application
3744
export const api = new Api(axios, {
3845
authToken: '134981091031hfh31',
3946
baseUrl: '/api'
@@ -47,21 +54,29 @@ export const api = new Api(axios, {
4754
code: `
4855
import axios from 'axios'
4956
57+
// This is the class we can create new instances of when testing
5058
export class Api {
5159
constructor(request, options) {
5260
this.request = request
5361
this.options = options
5462
}
55-
getPost(id) {
56-
return this.request.get(this.options.baseUrl + '/posts/' + id, {
57-
headers: {
58-
'Auth-Token': this.options.authToken
59-
}
60-
})
61-
.then(response => response.data)
63+
async getPost(id) {
64+
try {
65+
const response = await this.request.get(this.options.baseUrl + '/posts/' + id, {
66+
headers: {
67+
'Auth-Token': this.options.authToken
68+
}
69+
})
70+
71+
return response.data
72+
} catch (error) {
73+
throw new Error('Could not grab post with id ' + id)
74+
}
6275
}
6376
}
6477
78+
// We export the default instance that we actually use with our
79+
// application
6580
export const api = new Api(axios, {
6681
authToken: '134981091031hfh31',
6782
baseUrl: '/api'

packages/overmind-website/guides/intermediate/05_writingtests.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ In this scenario we would also ensure that the **isLoadingPost** state indeed fl
2828

2929
## Testing effects
3030

31-
Where you want to put in your effort is with the effects. This is where you have your chance to build a domain specific API for your actual application logic. The bridge between some generic tool and what your application actually wants to use it for. A simple example of this is doing requests. Maybe you want to use axios to reach your API, but you do not really care about testing that library. What you want to test is that it is used correctly when you use your application specific API. Lets look at an example:
31+
Where you want to put in your effort is with the effects. This is where you have your chance to build a domain specific API for your actual application logic. The bridge between some generic tool and what your application actually wants to use it for.
32+
33+
A simple example of this is doing requests. Maybe you want to use axios to reach your API, but you do not really care about testing that library. What you want to test is that it is used correctly when you use your application specific API.
34+
35+
This is just an example showing you how you can structure your code for optimal testability. You might prefer a different approach or maybe rely on integration tests for this. No worries, you do what makes most sense for your application:
3236

3337
```marksy
3438
h(Example, { name: "guide/writingtests/effect.ts" })

0 commit comments

Comments
 (0)