Skip to content

Commit

Permalink
Tweak testing docs to make consistent with code base
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn committed Feb 24, 2017
1 parent a0c21da commit aa9c213
Showing 1 changed file with 49 additions and 36 deletions.
85 changes: 49 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,49 +94,62 @@ vm.increment() // increment immediately
```

## Testing with vuex-local

Testing vuex-local module can be as easy as ordinary vuex module. Just getting the module by Component.local().

```js
const module = App.local.call({name:'counterApp'});//use .call to inject a mock component
let {state,getters,actions,mutations} = module;
// Use `call` method to inject a mock component
const module = App.local.call({ name: 'counterApp' })
const { state, getters, actions, mutations } = module
```

A full example here:

```js
import App from '../App.vue';

describe('App Local Module',()=>{
const module = App.local.call({name:'counterApp'});//use .call to inject a mock component
let {state, getters, actions, mutations} = module;
it('counter to be 0 initially',()=>{
expect(state.count).to.be.equal(0);
const anotherModule = App.local.call({name:'counterApp'});
expect(anotherModule.state.count).to.be.equal(0);
});
it('return half',()=>{
expect(getters.half({count:0})).to.be.equal(0);
expect(getters.half({count:-1})).to.be.equal(-0.5);
expect(getters.half({count:1})).to.be.equal(0.5);
});
it('increment the counter in state',()=>{
let state = {count:0};
mutations.increment(state);
expect(state.count).to.be.equal(1);
mutations.increment(state);
expect(state.count).to.be.equal(2);
});
it('increment after 1 second',function(done){
this.timeout(1100);
let begin = Date.now();
let commit = (mutationName)=>{
expect(mutationName).to.be.equal('increment');
expect(Date.now() - begin).to.be.above(900);
expect(Date.now() - begin).to.be.below(1100);
done();
};
actions.asyncIncrement({commit});
});
});
import App from '../App.vue'

describe('App Local Module', () => {
// Use `call` method to inject a mock component
const module = App.local.call({ name: 'counterApp' })
const { state, getters, actions, mutations } = module

it('counter to be 0 initially', () => {
expect(state.count).to.be.equal(0)

const anotherModule = App.local.call({ name: 'counterApp' })
expect(anotherModule.state.count).to.be.equal(0)
})

it('return half', () => {
expect(getters.half({ count: 0 })).to.be.equal(0)
expect(getters.half({ count: -1 })).to.be.equal(-0.5)
expect(getters.half({ count: 1 })).to.be.equal(0.5)
})

it('increment the counter in state', () => {
const state = { count: 0 }

mutations.increment(state)
expect(state.count).to.be.equal(1)

mutations.increment(state)
expect(state.count).to.be.equal(2)
})

it('increment after 1 second', done => {
this.timeout(1100)
const begin = Date.now()

const commit = mutationName => {
expect(mutationName).to.be.equal('increment')
expect(Date.now() - begin).to.be.above(900)
expect(Date.now() - begin).to.be.below(1100)
done()
}

actions.asyncIncrement({ commit })
})
})
```

## License
Expand Down

0 comments on commit aa9c213

Please sign in to comment.