Skip to content

Commit 4f5b3d6

Browse files
docs(website): several doc fixes
1 parent d90fefa commit 4f5b3d6

File tree

8 files changed

+170
-18
lines changed

8 files changed

+170
-18
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Mock
2+
3+
The **createOvermindMock** factory creates an instance of Overmind which can be used to test actions. You can mock out effects and evaluate mutations performed during action execution.
4+
5+
6+
```marksy
7+
h(Example, { name: "guide/writingtests/actionsnapshot" })
8+
```

packages/overmind-website/api/overmind.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Overmind
22

3-
The **Overmind** class is used to create the application instance. You need to create and export a mechanism to connect your instance to the components. Please look at the guides for each view layer for more information.
3+
The **createOvermind** factory is used to create the application instance. You need to create and export a mechanism to connect your instance to the components. Please look at the guides for each view layer for more information.
44

55
```marksy
66
h(Example, { name: "api/app_initialize" })
77
```
88

9-
You can pass a second argument to the **Overmind** constructor. This is an options object with the following properties:
9+
You can pass a second argument to the **createOvermind** factory. This is an options object with the following properties:
1010

1111
## addMutationListener
1212

@@ -24,7 +24,6 @@ The **addMutationListener** triggers whenever there is a mutation. The **addFlus
2424
h(Example, { name: "api/app_addFlushListener" })
2525
```
2626

27-
2827
## options.devtools
2928
If you develop your app on localhost the application connects to the devtools on **localhost:3031**. You can change this in case you need to use an IP address, the devtools is configured with a different port or you want to connect to localhost (with default port) even though the app is not on localhost.
3029

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SSR
2+
3+
The **createOvermindSSR** factory creates an instance of Overmind which can be used on the server with server side rendering. It allows you to change state and extract the mutations performed, which can then be rehydrated on the client.
4+
5+
```marksy
6+
h(Example, { name: "guide/serversiderendering/renderonserver" })
7+
```
8+
9+
## rehydrate
10+
11+
```marksy
12+
h(Example, { name: "guide/serversiderendering/renderonclient" })
13+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
export default (ts) =>
2+
ts
3+
? [
4+
{
5+
fileName: 'overmind/state.ts',
6+
code: `
7+
export type User = {
8+
id: string
9+
username: string
10+
}
11+
12+
export type State = {
13+
users: {
14+
[id: string]: User
15+
}
16+
currentUser: User
17+
}
18+
19+
export const state: State = {
20+
users: {},
21+
currentUser: null
22+
}
23+
`,
24+
},
25+
{
26+
fileName: 'overmind/actions.ts',
27+
code: `
28+
import { Action } from 'overmind'
29+
30+
export const setUser: Action<string> = ({ state }, id) => {
31+
state.currentUser = state.users[id]
32+
}
33+
`,
34+
},
35+
]
36+
: [
37+
{
38+
fileName: 'overmind/state.js',
39+
code: `
40+
export default {
41+
users: {},
42+
currentUser: null
43+
}
44+
`,
45+
},
46+
{
47+
fileName: 'overmind/actions.ts',
48+
code: `
49+
export const setUser = ({ state }, id) => {
50+
state.currentUser = state.users[id]
51+
}
52+
`,
53+
},
54+
]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
export default (ts) =>
2+
ts
3+
? [
4+
{
5+
fileName: 'overmind/state.ts',
6+
code: `
7+
export type User = {
8+
id: string
9+
username: string
10+
}
11+
12+
export type State = {
13+
users: {
14+
[id: string]: User
15+
}
16+
currentUserId: string
17+
currentUser: User
18+
}
19+
20+
export const state: State = {
21+
users: {},
22+
currentUserId: null,
23+
get currentUser(this: State) {
24+
return this.users[this.currentUserId]
25+
}
26+
}
27+
`,
28+
},
29+
{
30+
fileName: 'overmind/actions.ts',
31+
code: `
32+
import { Action } from 'overmind'
33+
34+
export const setUser: Action<string> = ({ state }, id) => {
35+
state.currentUserId = id
36+
}
37+
`,
38+
},
39+
]
40+
: [
41+
{
42+
fileName: 'overmind/state.js',
43+
code: `
44+
export default {
45+
users: {},
46+
currentUserId: null
47+
get currentUser() {
48+
return this.users[this.currentUserId]
49+
}
50+
}
51+
`,
52+
},
53+
{
54+
fileName: 'overmind/actions.ts',
55+
code: `
56+
export const setUser = ({ state }, id) => {
57+
state.currentUserId = id
58+
}
59+
`,
60+
},
61+
]

packages/overmind-website/examples/guide/serversiderendering/renderonserver.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { tsAppIndex } from '../../templates'
2-
31
const javascript = {
42
react: [
53
{
@@ -13,10 +11,10 @@ import App from '../client/components/App'
1311
import db from './db'
1412
1513
module.exports = async (req, res) => {
16-
const overmind = createOvermindSSR(overmind)
14+
const overmind = createOvermindSSR(config)
1715
18-
overmind.currentPage = 'posts'
19-
overmind.posts = await db.getPosts()
16+
overmind.state.currentPage = 'posts'
17+
overmind.state.posts = await db.getPosts()
2018
2119
const html = renderToString(
2220
<Provider value={overmind}>
@@ -72,10 +70,10 @@ import App from '../client/components/App'
7270
import db from './db'
7371
7472
export default async (req, res) => {
75-
const overmind = createOvermindSSR(overmind)
73+
const overmind = createOvermindSSR(config)
7674
77-
overmind.currentPage = 'posts'
78-
overmind.posts = await db.getPosts()
75+
overmind.state.currentPage = 'posts'
76+
overmind.state.posts = await db.getPosts()
7977
8078
const html = renderToString(
8179
<Provider value={overmind}>
@@ -188,8 +186,8 @@ const { AppServerModuleNgFactory } = require('./server/main')
188186
export default async (req, res) => {
189187
const overmind = createOvermindSSR(config)
190188
191-
overmind.currentPage = 'posts'
192-
overmind.posts = await db.getPosts()
189+
overmind.state.currentPage = 'posts'
190+
overmind.state.posts = await db.getPosts()
193191
194192
const html = await renderModuleFactory(AppServerModuleNgFactory, {
195193
extraProviders: [{

packages/overmind-website/guides/beginner/02_definingstate.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,17 @@ Are things loading or not, is the user logged in or not ? These are typical usag
4848

4949
All values, with the exception of booleans, can also be **null**. Non existing. You can have a non existing object, array, string or number. That means if we have not selected a tab both the string version and number version would have the value **null**.
5050

51+
## Getter
52+
53+
A concept in Javascript called a [getter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get) allows you to intercept accessing a property in an object. A getter is just like a plain value, it can be added an removed at any point. Getters does **not** cache the result for that very reason, but whatever state they access is tracked.
54+
55+
```marksy
56+
h(Example, { name: "guide/definingstate/getter" })
57+
```
58+
5159
## Deriving state
5260

53-
Not all state should be explicitly defined. Some state are values based off of other state. This is what we call **derived state**. A simple example of this would be:
61+
When you need to do more heavy calculation or combine state from different parts ot the tree you can use **derived state**. A simple example of this would be:
5462

5563
```marksy
5664
h(Example, { name: "guide/definingstate/derived" })
@@ -87,14 +95,25 @@ So to solve this specific scenario you would rather create a new state called **
8795

8896
Also remember that **derived** is a concept for computation heavy derived state, you most commonly want to use a **getter**.
8997

90-
## Getter
98+
```marksy
99+
h(Notice, null, "Derived state can not be dynamically added. They have to be defined and live in the tree from start to end of your application lifecycle.")
100+
```
101+
102+
## References
103+
104+
When you add objects and arrays to your state tree, they are labeled with an "address" in the tree. That means if you try to add the same object or array in multiple spots in the tree you will get an error, as they can not have multiple addresses. Typically this indicates that you rather want to create a references to an existing object or array.
91105

92-
A concept in Javascript called a [getter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get) allows you to intercept accessing a property in an object. This is similar to deriving state, though they are dynamic. That means derived state can only be defined once and needs to stay in the state tree. A getter is just like a plain value, it can be added an removed at any point. Getters does **not** cache the result for that very reason, but whatever state they access is tracked.
106+
So this is an example of how you would **not** want to do it:
93107

94108
```marksy
95-
h(Example, { name: "guide/definingstate/getter" })
109+
h(Example, { name: "guide/definingstate/reference" })
96110
```
97111

112+
You rather change a reference to the user and for example use a **getter** to grab the actual user:
113+
114+
```marksy
115+
h(Example, { name: "guide/definingstate/reference_correct" })
116+
```
98117

99118
## Exposing the state
100119

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Writing tests
22

3-
Testing is a broad subject and everybody has an opinion on it. We can only show you how we think about testing in general and how to effectively write those tests for your Overmind app. It is encouraged to think **unit testing** of actions and effects. This will cover expected changes in state and that your side effects behaves in a predictable manner. If you want to test how your application works when it is all put together we recommend doing integration tests as close to the user experience as possible. Testing solutions like [Cypress.io](https://www.cypress.io/) is a great way to do exactly that.
3+
Testing is a broad subject and everybody has an opinion on it. We can only show you how we think about testing in general and how to effectively write those tests for your Overmind app. It is encouraged to think **unit testing** of actions and effects. This will cover expected changes in state and that your side effects behaves in a predictable manner. If you want to test how your application works when it is all put together we recommend doing integration tests as close to the user experience as possible. Testing solutions like [Cypress.io](https://www.cypress.io/) is a great way to do exactly that. You can read more about Cypress and integration testing with Overmind in [this article](https://www.cypress.io/blog/2019/02/28/shrink-the-untestable-code-with-app-actions-and-effects/#).
44

55
## Structuring the app
66

0 commit comments

Comments
 (0)