Skip to content

Commit 86c3db9

Browse files
committed
Merge pull request #20 from Legitcode/feature/store-tests
Add testing helpers for alt stores
2 parents 2e59f50 + 25f7039 commit 86c3db9

8 files changed

Lines changed: 122 additions & 2 deletions

File tree

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ Test(<TestComponent />)
116116

117117
~~~
118118

119+
You can see more examples in the tests directory.
119120

121+
##Testing Alt Stores
120122

121-
You can see more examples in the tests directory.
123+
You can now test [Alt](http://alt.js.org/) stores using the same API.
124+
125+
~~~js
126+
import TestStore from 'legit-tests/alt/store'
127+
128+
TestStore(MyStore, MyActions)
129+
.setInitialState({ todos: todos })
130+
.addTodo({ title: "Get Beer", complete: false })
131+
.test(({ state }) => {
132+
expect(state.todos).to.eql(expected);
133+
})
134+
~~~
135+
136+
You can see the full documentation on the Wiki

mocha.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
--full-trace
22
--compilers js:babel/register
3+
--harmony-proxies
34
--recursive ./tests/**/*.jsx

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"homepage": "https://github.com/legitcode/tests#readme",
3030
"devDependencies": {
31+
"alt": "^0.17.3",
3132
"babel": "^5.8.23",
3233
"babel-core": "^5.8.25",
3334
"babel-eslint": "^4.1.3",
@@ -37,6 +38,7 @@
3738
"eslint": "^1.5.1",
3839
"eslint-plugin-react": "^3.4.2",
3940
"expect": "^1.10.0",
41+
"harmony-reflect": "^1.4.2",
4042
"mocha": "^2.3.3",
4143
"mocha-babel": "^3.0.0",
4244
"react-hot-loader": "^1.3.0",

src/alt/alt.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Alt from 'alt'
2+
3+
export default new Alt()

src/alt/store.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'harmony-reflect'
2+
3+
class TestStore {
4+
constructor(store, actions) {
5+
this.store = store
6+
this.actions = actions
7+
}
8+
9+
test(callback) {
10+
callback.call(this, this.store)
11+
return this
12+
}
13+
}
14+
15+
export default function TestStoreWrapper(store, actions) {
16+
var proxy = new Proxy(new TestStore(store, actions), {
17+
get: function(target, name) {
18+
if (name in target) {
19+
return target[name]
20+
}
21+
else if (name in target.actions) {
22+
return (params) => {
23+
target.actions[name](params)
24+
return proxy
25+
}
26+
}
27+
}
28+
})
29+
30+
return proxy
31+
}

tests/find.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { expect } from 'chai'
77
import { TestComponent, TinyComponent } from './components'
88

99
describe('Find middleware', () => {
10-
1110
it('should find div', () => {
1211
Test(<TestComponent/>)
1312
.find('div')

tests/store.jsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { expect } from 'chai'
2+
import TestStore from '../src/alt/store'
3+
4+
import { MyStore, MyActions } from './testStore'
5+
6+
describe('TestStore', () => {
7+
let todos = [
8+
{
9+
title: "Get Milk",
10+
complete: false
11+
},
12+
{
13+
title: "Get Bread",
14+
complete: false
15+
}
16+
]
17+
18+
let expected = [
19+
{
20+
title: "Get Milk",
21+
complete: false
22+
},
23+
{
24+
title: "Get Bread",
25+
complete: false
26+
},
27+
{
28+
title: "Get Beer",
29+
complete: false
30+
}
31+
]
32+
33+
describe('proxy', () => {
34+
it('should proxy missing method calls to the call function', () => {
35+
TestStore(MyStore, MyActions)
36+
.setInitialState({ todos: todos })
37+
.addTodo({ title: "Get Beer", complete: false })
38+
.test(({ state }) => {
39+
expect(state.todos).to.eql(expected)
40+
})
41+
})
42+
})
43+
})

tests/testStore.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import alt from '../src/alt/alt'
2+
3+
class myActions {
4+
constructor() {
5+
this.generateActions('addTodo', 'setInitialState')
6+
}
7+
}
8+
9+
const MyActions = alt.createActions(myActions)
10+
11+
class myStore {
12+
constructor() {
13+
this.bindActions(MyActions)
14+
}
15+
16+
setInitialState(params) {
17+
this.setState(params)
18+
}
19+
20+
addTodo(todo) {
21+
this.setState({ todos: this.todos.concat(todo) })
22+
}
23+
}
24+
25+
const MyStore = alt.createStore(myStore, 'MyStore')
26+
export { MyStore, MyActions }

0 commit comments

Comments
 (0)