Skip to content

Commit

Permalink
[Fix] Stub lifecycle events in shallow method
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyerburgh committed Jun 20, 2017
1 parent 27f458b commit c39ed7f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/shallow.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import Vue from 'vue';
import mount from './mount';

const LIFECYCLE_HOOKS = [
'beforeCreate',
'created',
'beforeMount',
'mounted',
'beforeUpdate',
'updated',
'beforeDestroy',
'destroyed',
'activated',
'deactivated',
];

function stubLifeCycleEvents(component) {
LIFECYCLE_HOOKS.forEach((hook) => {
component[hook] = () => {}; // eslint-disable-line no-param-reassign
});
}

function replaceComponents(component) {
Object.keys(component.components).forEach((c) => {
// Remove cached constructor
Expand All @@ -10,6 +29,7 @@ function replaceComponents(component) {
render: () => {},
};
Vue.config.ignoredElements.push(c);
stubLifeCycleEvents(component.components[c]);
});
}

Expand All @@ -18,6 +38,8 @@ export default function shallow(component, options) {
replaceComponents(component);
}

stubLifeCycleEvents(component);

return mount(component, options);
}

30 changes: 30 additions & 0 deletions test/resources/components/nested-components/FirstChild.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,35 @@ export default{
components: {
SecondChild,
},
beforeCreate() {
console.log('beforeCreate'); // eslint-disable-line no-console
},
created() {
console.log('created'); // eslint-disable-line no-console
},
beforeMount() {
console.log('beforeMount'); // eslint-disable-line no-console
},
mounted() {
console.log('mounted'); // eslint-disable-line no-console
},
beforeUpdate() {
console.log('beforeUpdate'); // eslint-disable-line no-console
},
updated() {
console.log('updated'); // eslint-disable-line no-console
},
beforeDestroy() {
console.log('beforeDestroy'); // eslint-disable-line no-console
},
destroyed() {
console.log('destroyed'); // eslint-disable-line no-console
},
activated() {
console.log('beforeDestroy'); // eslint-disable-line no-console
},
deactivated() {
console.log('destroyed'); // eslint-disable-line no-console
},
};
</script>
14 changes: 12 additions & 2 deletions test/unit/specs/shallow.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import shallow from '../../../src/shallow';
import ParentComponent from '../../resources/components/nested-components/Parent.vue';
import Parent from '../../resources/components/nested-components/Parent.vue';
import FirstChild from '../../resources/components/nested-components/FirstChild.vue';
import SecondChild from '../../resources/components/nested-components/SecondChild.vue';

Expand All @@ -9,8 +9,18 @@ describe('shallow', () => {
if (navigator.userAgent.includes && navigator.userAgent.includes('node.js')) {
return;
}
const wrapper = shallow(ParentComponent);
const wrapper = shallow(Parent);
expect(wrapper.find(FirstChild).length).to.equal(2);
expect(wrapper.find(SecondChild).length).to.equal(0);
});

it('stubs lifecycle methods', () => {
if (navigator.userAgent.includes && navigator.userAgent.includes('node.js')) {
return;
}
const log = sinon.stub(console, 'log');
shallow(Parent);
expect(log.called).to.equal(false);
log.restore();
});
});

0 comments on commit c39ed7f

Please sign in to comment.