Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#55 data option #56

Merged
merged 2 commits into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.6.0
- Added a `data` option to specify initial data properties.

## 0.5.0
- `vuenit.http` now uses the `mock-http-client` library.
- `vuenit.store` now uses the `mock-vuex` library.
Expand Down
19 changes: 19 additions & 0 deletions lib/component/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ exports.mergeComponent = function (component, options) {
mergeDirectives(component, options);
mergeProps(component, options);
mergeMixins(component, options);
mergeData(component, options);
}else{
// Register all stubbed components globally
Object.keys(options.components).forEach(function (key) {
Expand Down Expand Up @@ -143,6 +144,24 @@ function mergeMixins(component, options) {
}
}

function mergeData(component, options) {
if (options.data){
var dataFn = component.data;
component.data = function () {
var data = {};
if (typeof dataFn === 'function'){
Object.assign(data, dataFn.call(this));
}
if (typeof options.data === 'function'){
Object.assign(data, options.data.call(this));
}else{
Object.assign(data, options.data);
}
return data;
};
}
}

// Creates a wrapper component that contains the vm component
exports.createComponentWrapper = function (component, options) {
var name = componentName(component, options);
Expand Down
1 change: 1 addition & 0 deletions lib/component/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exports.mergeOptions = function (options, config, Vue, injector) {
name : null,
install : null,
before : null,
data : null,
defaultTemplate : '<div><slot></slot></div>'
};
var mergedOptions = Object.assign(defaultOptions, config, options);
Expand Down
110 changes: 110 additions & 0 deletions spec/component/data.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import test from 'ava-spec';
import sinon from 'sinon';
import vuenit from '../../lib';

test.beforeEach(t => {
let spy1 = sinon.spy();
let spy2 = sinon.spy();
let c = {
name : 'test',
template : `<div>{{value}}</div>`,
data(){
return {
value : 'initial',
uuid : null,
dontUpdate : 'foo'
};
},
watch : {
uuid(newVal, oldVal){
if (oldVal){
spy1();
}
if (newVal){
spy2();
}
}
}
};

t.context = {spy1, spy2, c};
});

test('uses real data values', async t => {
let {c, spy1, spy2} = t.context;
let vm = vuenit.mount(c);

t.is(vm.value, 'initial');
t.is(vm.uuid, null);
t.is(vm.dontUpdate, 'foo');

t.false(spy1.called);
t.false(spy2.called);

vm.uuid = 1234;
await vm.$nextTick();

t.false(spy1.called);
t.true(spy2.called);

vm.uuid = 5678;
await vm.$nextTick();

t.true(spy1.called);
});
test('merges with a data object', t => {
let {c} = t.context;
let vm = vuenit.mount(c, {
data : {
value : 'overwritten',
uuid : 1234
}
});

t.is(vm.value, 'overwritten');
t.is(vm.uuid, 1234);
t.is(vm.dontUpdate, 'foo');
});
test('merges with a data function', t => {
let {c} = t.context;
let vm = vuenit.mount(c, {
data(){
return {
value : 'overwritten',
uuid : 1234
}
}
});

t.is(vm.value, 'overwritten');
t.is(vm.uuid, 1234);
t.is(vm.dontUpdate, 'foo');
});
test('has access to vm context', t => {
let {c} = t.context;
let vm2;
let vm = vuenit.mount(c, {
data(){
vm2 = this;
}
});

t.is(vm2, vm);
});
test('actives both spies as expected', async t => {
let {c, spy1, spy2} = t.context;
let vm = vuenit.mount(c, {
data : {
uuid : 1234
}
});

t.false(spy1.called);
t.false(spy2.called);

vm.uuid = 5678;
await vm.$nextTick();

t.true(spy1.called);
t.true(spy2.called);
});