Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fc8a32d

Browse files
author
Evan You
committed
test example
1 parent 47e743a commit fc8a32d

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

‎source/guide/application.md‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,53 @@ All Vue.js ViewModels and their raw `$data` can be serialized with `JSON.stringi
3535

3636
By using Component, Vue.js entities (ViewModel constructors, directives, filters) within a large project can be split into separate CommonJS modules. When a Component-based project is built without the `standalone` flag, it will expose its `require()` method, granting access to all these internal modules. This makes it quite easy to write browser unit tests - just include the test build and require the module you want to test.
3737

38+
The best practice is to export raw options / functions inside modules. Consider this example:
39+
40+
``` js
41+
// my-component.js
42+
module.exports = {
43+
created: function () {
44+
this.message = 'hello!'
45+
}
46+
}
47+
```
48+
49+
You can use that file in your entry module like this:
50+
51+
``` js
52+
// main.js
53+
var Vue = require('vue')
54+
55+
var app = new Vue({
56+
el: '#app',
57+
data: { /* ... */ },
58+
components: {
59+
'my-component': require('./my-component')
60+
}
61+
})
62+
```
63+
64+
And you can test that module like this:
65+
66+
``` js
67+
// Some Mocha tests
68+
// using a non-standalone build of the project
69+
describe('my-component', function () {
70+
71+
// require exposed internal module
72+
var myComponent = require('my-project/src/my-component')
73+
74+
it('should have a created hook', function () {
75+
assert.equal(typeof myComponent.created, 'function')
76+
})
77+
78+
it('should set message in the created hook', function () {
79+
var mock = {}
80+
myComponent.created.call(mock)
81+
assert.equal(mock.message, 'hello!')
82+
})
83+
84+
})
85+
```
86+
3887
<p class="tip">Since Vue.js bindings update asynchronously, you should use `Vue.nextTick()` when asserting DOM updates after changing the data.</p>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /