UNPKG

3.12 kBJavaScriptView Raw
1import test from 'ava';
2import sinon from 'sinon';
3import {mount} from 'vuenit';
4import {createHOC, createHOCc, createRenderFn} from '../dist';
5
6const Component = {
7 template : `<div>
8 <button id="button1" @click="$emit('button1click')">1</button>
9 </div>`,
10 methods : {
11 handleClick(){
12 this.$emit('button1click');
13 }
14 }
15};
16mount(Component);
17
18test('it emits an event', t => {
19 const spy = sinon.spy();
20 const vm = mount(Component, {
21 on : {
22 button1click : spy
23 }
24 });
25
26 vm.$find('#button1').$trigger('click');
27
28 t.true(spy.called);
29});
30
31test('it emits an event through a hoc', t => {
32 const spy = sinon.spy();
33 const hoc = createHOC(Component);
34 const C2 = {
35 components : {hoc},
36 template : '<hoc @button1click="callSpy"/>',
37 methods: {
38 callSpy(){
39 spy();
40 }
41 }
42 };
43 const vm = mount(C2);
44
45 vm.$find('#button1').$trigger('click');
46
47 t.true(spy.called);
48});
49
50test('it can intercept an event', t => {
51 const spy1 = sinon.spy(), spy2 = sinon.spy();
52 const hoc = createHOCc(null, {
53 listeners: {
54 button1click: spy2,
55 }
56 })(Component);
57 const C2 = {
58 components : {hoc},
59 template : '<hoc @button1click="callSpy"/>',
60 methods: {
61 callSpy(){
62 spy1();
63 }
64 }
65 };
66 const vm = mount(C2);
67
68 vm.$find('#button1').$trigger('click');
69
70 t.false(spy1.called);
71 t.true(spy2.called);
72});
73
74test('it can bubble an event (function syntax)', t => {
75 const spy1 = sinon.spy(), spy2 = sinon.spy();
76 const hoc = createHOC(Component, {
77 render : createRenderFn(Component, {
78 listeners(listeners){
79 return {
80 button1click : () => {
81 spy2();
82 this.$emit('button1click');
83 }
84 };
85 }
86 })
87 });
88 const C2 = {
89 components : {hoc},
90 template : '<hoc @button1click="callSpy"/>',
91 methods: {
92 callSpy(){
93 spy1();
94 }
95 }
96 };
97 const vm = mount(C2);
98 t.log(vm.$html);
99 vm.$find('#button1').$trigger('click');
100
101 t.true(spy1.called);
102 t.true(spy2.called);
103});
104
105test('it can bubble an event (object syntax)', t => {
106 const spy1 = sinon.spy(), spy2 = sinon.spy();
107 const hoc = createHOC(Component, {
108 render : createRenderFn(Component, {
109 listeners: {
110 button1click(){
111 spy2();
112 this.$emit('button1click');
113 }
114 }
115 })
116 });
117 const C2 = {
118 components : {hoc},
119 template : '<hoc @button1click="callSpy"/>',
120 methods: {
121 callSpy(){
122 spy1();
123 }
124 }
125 };
126 const vm = mount(C2);
127
128 vm.$find('#button1').$trigger('click');
129
130 t.true(spy1.called);
131 t.true(spy2.called);
132});
133
134test('events bubble through multiple hocs', t => {
135 const spy = sinon.spy();
136 const hoc1 = createHOCc({}, null, Component);
137 const hoc2 = createHOCc({}, null, hoc1);
138 const hoc3 = createHOCc({}, null, hoc2);
139 const C2 = {
140 components : {hoc3},
141 template : '<hoc3 @button1click="callSpy"/>',
142 methods: {
143 callSpy(){
144 spy();
145 }
146 }
147 };
148 const vm = mount(C2);
149
150 vm.$find('#button1').$trigger('click');
151
152 t.true(spy.called);
153});