Skip to content

Commit 2c3a949

Browse files
docs(website): add testing docs to separate out config
1 parent f8e190d commit 2c3a949

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import { tsAppIndex } from '../../templates'
2+
3+
const javascript = {
4+
react: [
5+
{
6+
fileName: 'overmind/index.js',
7+
code: `
8+
import { createHook } from 'overmind-react'
9+
10+
export const config = {
11+
state: {
12+
isLoadingPosts: false
13+
}
14+
}
15+
16+
export const useOvermind = createHook()
17+
`,
18+
},
19+
{
20+
fileName: 'index.js',
21+
code: `
22+
import React from 'react'
23+
import { render } from 'react-dom'
24+
import { Overmind } from 'overmind'
25+
import { Provider } from 'overmind-react'
26+
import { config } from './overmind'
27+
import App from './components/App'
28+
29+
const overmind = new Overmind(config)
30+
31+
render(
32+
<Provider value={overmind}>
33+
<App />
34+
</Provider>,
35+
document.querySelector('#app')
36+
)
37+
`,
38+
},
39+
],
40+
vue: [
41+
{
42+
fileName: 'overmind/index.js',
43+
code: `
44+
export const config = {
45+
state: {
46+
isLoadingPosts: false
47+
}
48+
}
49+
`,
50+
},
51+
{
52+
fileName: 'overmind/index.js',
53+
code: `
54+
import Vue from 'vue'
55+
import { Overmind } from 'overmind'
56+
import { createPlugin } from 'overmind-vue'
57+
import { config } from './overmind'
58+
import App from './components/App'
59+
60+
const overmind = new Overmind(config)
61+
62+
Vue.use(createPlugin(overmind))
63+
64+
new Vue({
65+
el: document.querySelector('#app'),
66+
render: h => h(App)
67+
})
68+
`,
69+
},
70+
],
71+
}
72+
73+
const typescript = {
74+
react: [
75+
{
76+
fileName: 'overmind/index.ts',
77+
code: `
78+
import { IConfig } from 'overmind'
79+
import { createHook } from 'overmind-react'
80+
import { state } from './state'
81+
82+
export const config = {
83+
state
84+
}
85+
86+
declare module 'overmind' {
87+
interface Config extends IConfig<typeof config> {}
88+
}
89+
90+
export const useOvermind = createHook<typeof config>()
91+
`,
92+
},
93+
{
94+
fileName: 'index.ts',
95+
code: `
96+
import * as React from 'react'
97+
import { render } from 'react-dom'
98+
import { Overmind } from 'overmind'
99+
import { Provider } from 'overmind-react'
100+
import { config } from './overmind'
101+
import { App } from './components/App'
102+
103+
const overmind = new Overmind(config)
104+
105+
render(
106+
<Provider value={overmind}>
107+
<App />
108+
</Provider>,
109+
document.querySelector('#app')
110+
)
111+
`,
112+
},
113+
],
114+
vue: javascript.vue,
115+
angular: [
116+
{
117+
fileName: 'overmind/index.ts',
118+
code: tsAppIndex(
119+
'angular',
120+
`
121+
import { state } from './state'
122+
123+
const config = {
124+
state,
125+
}
126+
`
127+
),
128+
},
129+
],
130+
}
131+
132+
export default (ts, view) => (ts ? typescript[view] : javascript[view])

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
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.
44

5+
## Structuring the app
6+
7+
When you write tests you will create many instances of a mocked version of Overmind with the configuration you have created. To ensure that this configuration can be used many times we have to separate our configuration from the instantiation of the actual app.
8+
9+
```marksy
10+
h(Example, { name: "guide/writingtests/structuringtheapp.ts" })
11+
```
12+
13+
Now we are free to import our configuration without touching the application instance. Lets go!
14+
515
## Testing actions
616

717
When testing an action you want to verify that changes to state are performed as expected. To give you the best possible testing experience Overmind has mocking tool called **createOvermindMock**. It takes your application configuration and allows you to run actions as if they were run from components.

packages/overmind-website/src/components/ViewSelector/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const ViewSelector: SFC = () => {
1515
const selectorRef = useRef(null)
1616

1717
function onSelectorClick() {
18+
if (isOpen) {
19+
return setOpen(false)
20+
}
21+
1822
setOpen(true)
1923

2024
const onDocumentClick = function() {

0 commit comments

Comments
 (0)