Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
杨明山 authored and 杨明山 committed May 23, 2017
1 parent 1660fb5 commit 688fdf6
Show file tree
Hide file tree
Showing 7 changed files with 2,226 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
node_modules/
npm-debug.log
*.log
coverage
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# vue-bus
A event bus for Vue.js, support both Vue 1.0 and 2.0. See Vue [documentation](http://vuejs.org/v2/guide/migration.html#Events) for more detail.
# vue-bus [![Build Status](https://img.shields.io/circleci/project/yangmingshan/vue-bus.svg)](https://circleci.com/gh/yangmingshan/vue-bus) [![Coverage Status](https://img.shields.io/codecov/c/github/yangmingshan/vue-bus.svg)](https://codecov.io/gh/yangmingshan/vue-bus) [![Downloads](https://img.shields.io/npm/dt/vue-bus.svg)](https://www.npmjs.com/package/vue-bus) [![Version](https://img.shields.io/npm/v/vue-bus.svg)](https://www.npmjs.com/package/vue-bus) [![License](https://img.shields.io/npm/l/vue-bus.svg)](https://www.npmjs.com/package/vue-bus)

A event bus for Vue.js, support both Vue 1.0 and 2.0. See Vue [documentation](https://vuejs.org/v2/guide/migration.html#Events) for more detail.

## Installation
You can install it via [yarn](https://yarnpkg.com) or [npm](https://npmjs.com).
```
$ npm install vue-bus
$ yarn add vue-bus
$ npm install vue-bus --save
```
When used with a module system, you must explicitly install the bus via Vue.use():
```
Expand Down Expand Up @@ -42,7 +45,14 @@ methods: {
}
}
```
*Note: `$bus.on` `$bus.once` `$bus.off` `$bus.emit` are aliases for `$bus.$on` `$bus.$once` `$bus.$off` `$bus.$emit`. See the [API](http://vuejs.org/v2/api/#Instance-Methods-Events) for more detail.*
#### Another way to use vue-bus
```
// xxx.js
import Vue from 'vue';
Vue.bus.emit('someEvent');
```
*Note: `on` `once` `off` `emit` are aliases for `$on` `$once` `$off` `$emit`. See the [API](https://vuejs.org/v2/api/#Instance-Methods-Events) for more detail.*

## License
[MIT](http://opensource.org/licenses/MIT)
[MIT](https://opensource.org/licenses/MIT)
7 changes: 7 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
machine:
node:
version: 7

test:
override:
- npm test -- -t $CI_CODECOV_TOKEN
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* vue-bus v0.1.0
* vue-bus v1.0.0
* https://github.com/yangmingshan/vue-bus
* @license MIT
*/
Expand Down Expand Up @@ -36,6 +36,8 @@
}
});

Vue.bus = bus;

Object.defineProperty(Vue.prototype, '$bus', {
get: function() {
return bus;
Expand Down
90 changes: 90 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const Vue = require('vue');
const VueBus = require('./index');

Vue.use(VueBus);

test('Vue.bus', () => {
const vm = new Vue({
data() {
return { count: 0 };
},
created() {
this.$bus.on('add', num => { this.count += num });
this.$bus.once('addOnce', num => { this.count += num });
},
methods: {
clean() {
this.$bus.off('add');
}
}
});

const obj = {
fire() {
Vue.bus.emit('add', 1);
},
fireOnce() {
Vue.bus.emit('addOnce', 1);
}
};

obj.fire();
expect(vm.count).toBe(1);

obj.fire();
expect(vm.count).toBe(2);

vm.clean();
obj.fire();
expect(vm.count).toBe(2);

obj.fireOnce();
expect(vm.count).toBe(3);

obj.fireOnce();
expect(vm.count).toBe(3);
});

test('this.$bus', () => {
const vm1 = new Vue({
data() {
return { count: 0 };
},
created() {
this.$bus.on('add', num => { this.count += num });
this.$bus.once('addOnce', num => { this.count += num });
},
methods: {
clean() {
this.$bus.off('add');
}
}
});

const vm2 = new Vue({
methods: {
fire() {
this.$bus.emit('add', 1);
},
fireOnce() {
this.$bus.emit('addOnce', 1);
}
}
});

vm2.fire();
expect(vm1.count).toBe(1);

vm2.fire();
expect(vm1.count).toBe(2);

vm1.clean();
vm2.fire();
expect(vm1.count).toBe(2);

vm2.fireOnce();
expect(vm1.count).toBe(3);

vm2.fireOnce();
expect(vm1.count).toBe(3);
});
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "vue-bus",
"version": "0.3.0",
"version": "1.0.0",
"description": "A event bus for Vue.js",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest && codecov"
},
"repository": {
"type": "git",
Expand All @@ -20,5 +20,14 @@
"bugs": {
"url": "https://github.com/yangmingshan/vue-bus/issues"
},
"homepage": "https://github.com/yangmingshan/vue-bus#readme"
"homepage": "https://github.com/yangmingshan/vue-bus#readme",
"devDependencies": {
"codecov": "^2.2.0",
"jest": "^20.0.3",
"vue": "^2.3.3"
},
"jest": {
"coverageDirectory": "./coverage/",
"collectCoverage": true
}
}
Loading

0 comments on commit 688fdf6

Please sign in to comment.