Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit 37bd390

Browse files
committed
test(vuex-middleware): Adds tests for the vuex-middleware module
1 parent d53dc83 commit 37bd390

File tree

1 file changed

+297
-0
lines changed

1 file changed

+297
-0
lines changed

__tests__/vuex-middleware.spec.js

+297
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
jest.mock('load-script')
2+
3+
import Vue from 'vue'
4+
import VueAnalytics from '../src'
5+
import analyticsMiddleware from '../src/vuex-middleware'
6+
window.ga = jest.fn()
7+
8+
let $vm
9+
let store
10+
beforeEach(() => {
11+
window.ga.mockClear()
12+
store = {
13+
subscribers: [],
14+
subscribe: function (fn) {
15+
this.subscribers.push(fn)
16+
},
17+
fire: function (mutation) {
18+
this.subscribers.forEach(x => { x(mutation) })
19+
}
20+
}
21+
22+
analyticsMiddleware(store)
23+
Vue.use(VueAnalytics, {
24+
id: 'UA-1234-5'
25+
})
26+
$vm = new Vue({})
27+
28+
$vm.$mount()
29+
})
30+
31+
describe('input check', () => {
32+
it('should allow meta tags without analytics', () => {
33+
let mutation = {
34+
payload: {
35+
meta: {
36+
37+
}
38+
}
39+
}
40+
expect(() => { store.fire(mutation) }).not.toThrowError()
41+
})
42+
43+
it('should throw an error when analytics is not an array', () => {
44+
let mutation = {
45+
payload: {
46+
meta: {
47+
analytics: {}
48+
}
49+
}
50+
}
51+
expect(() => { store.fire(mutation) }).toThrowError()
52+
})
53+
54+
it('should throw an unknown type error', () => {
55+
let mutation = {
56+
payload: {
57+
meta: {
58+
analytics: [['unknown']]
59+
}
60+
}
61+
}
62+
expect(() => { store.fire(mutation) }).toThrowError()
63+
})
64+
65+
it('should throw an unknown method for type error', () => {
66+
let mutation = {
67+
payload: {
68+
meta: {
69+
analytics: [['event:unknown']]
70+
}
71+
}
72+
}
73+
expect(() => { store.fire(mutation) }).toThrowError()
74+
})
75+
76+
it('should throw an error because ecommerce requires a method call', () => {
77+
let mutation = {
78+
payload: {
79+
meta: {
80+
analytics: [['ecommerce']]
81+
}
82+
}
83+
}
84+
expect(() => { store.fire(mutation) }).toThrowError()
85+
})
86+
})
87+
88+
describe('fire event', () => {
89+
it('should fire a simple tracking event based on the meta payload', () => {
90+
let mutation = {
91+
payload: {
92+
meta: {
93+
analytics: [
94+
[
95+
'event', 'CLICK', 'TEST', 'LABEL', 1
96+
]
97+
]
98+
}
99+
}
100+
}
101+
store.fire(mutation)
102+
expect(window.ga).toBeCalledWith('send', 'event', 'CLICK', 'TEST', 'LABEL', 1)
103+
})
104+
105+
106+
it('should fire a simple event based on the meta payload', () => {
107+
let mutation = {
108+
payload: {
109+
meta: {
110+
analytics: [
111+
[
112+
'event', 'foo', 'bar'
113+
]
114+
]
115+
}
116+
}
117+
}
118+
store.fire(mutation)
119+
expect(window.ga).toBeCalledWith('send', 'event', 'foo', 'bar')
120+
})
121+
122+
it('should fire a simple exception event based on the meta payload', () => {
123+
let mutation = {
124+
payload: {
125+
meta: {
126+
analytics: [
127+
[
128+
'exception', 'CLICK', 'TEST'
129+
]
130+
]
131+
}
132+
}
133+
}
134+
store.fire(mutation)
135+
expect(window.ga).toBeCalledWith('send', 'exception', { exDescription: 'CLICK', exFatal: 'TEST' })
136+
})
137+
138+
it('should fire a simple set event based on the meta payload', () => {
139+
let mutation = {
140+
payload: {
141+
meta: {
142+
analytics: [
143+
[
144+
'set', 'foo', 'bar'
145+
]
146+
]
147+
}
148+
}
149+
}
150+
store.fire(mutation)
151+
expect(window.ga).toBeCalledWith('set', 'foo', 'bar')
152+
})
153+
154+
155+
it('should fire a simple social event based on the meta payload', () => {
156+
let mutation = {
157+
payload: {
158+
meta: {
159+
analytics: [
160+
[
161+
'social', 'Facebook', 'like', 'http://foo.com'
162+
]
163+
]
164+
}
165+
}
166+
}
167+
store.fire(mutation)
168+
expect(window.ga).toBeCalledWith('send', 'social', 'Facebook', 'like', 'http://foo.com')
169+
})
170+
171+
it('should fire a simple timing event based on the meta payload', () => {
172+
let mutation = {
173+
payload: {
174+
meta: {
175+
analytics: [
176+
[
177+
'time', 'category', 'variable', 123, 'label'
178+
]
179+
]
180+
}
181+
}
182+
}
183+
store.fire(mutation)
184+
expect(window.ga).toBeCalledWith('send', 'timing', 'category', 'variable', 123, 'label')
185+
})
186+
187+
188+
it('should fire a simple timing event based on the meta payload', () => {
189+
let mutation = {
190+
payload: {
191+
meta: {
192+
analytics: [
193+
[
194+
'time', 'category', 'variable', 123, 'label'
195+
]
196+
]
197+
}
198+
}
199+
}
200+
store.fire(mutation)
201+
expect(window.ga).toBeCalledWith('send', 'timing', 'category', 'variable', 123, 'label')
202+
})
203+
204+
it('should fire a simple page event based on the meta payload', () => {
205+
let mutation = {
206+
payload: {
207+
meta: {
208+
analytics: [
209+
[
210+
'page', '/'
211+
]
212+
]
213+
}
214+
}
215+
}
216+
store.fire(mutation)
217+
expect(window.ga).toBeCalledWith('set', 'page', '/')
218+
expect(window.ga).toBeCalledWith('send', 'pageview', '/')
219+
})
220+
221+
it('should fire a simple require event based on the meta payload', () => {
222+
let mutation = {
223+
payload: {
224+
meta: {
225+
analytics: [
226+
[
227+
'require', 'myplugin', { foo: 'bar' }
228+
]
229+
]
230+
}
231+
}
232+
}
233+
store.fire(mutation)
234+
expect(window.ga).toBeCalledWith('require', 'myplugin', {
235+
foo: 'bar'
236+
})
237+
})
238+
239+
it('should fire a simple query event based on the meta payload', () => {
240+
let mutation = {
241+
payload: {
242+
meta: {
243+
analytics: [
244+
[
245+
'query', 'test'
246+
]
247+
]
248+
}
249+
}
250+
}
251+
store.fire(mutation)
252+
expect(window.ga).toBeCalledWith('test')
253+
})
254+
255+
it('should fire a ecommerce query event based on the meta payload', () => {
256+
let item = {
257+
id: '1234',
258+
name: 'Fluffy Pink Bunnies',
259+
sku: 'DD23444',
260+
category: 'Party Toys',
261+
price: '11.99',
262+
quantity: '1'
263+
}
264+
let mutation = {
265+
payload: {
266+
meta: {
267+
analytics: [
268+
['ecommerce:addItem', item]
269+
]
270+
}
271+
}
272+
}
273+
store.fire(mutation)
274+
expect(window.ga).toBeCalledWith('ecommerce:addItem', item)
275+
})
276+
277+
it('should allow multiple events to fire', () => {
278+
let mutation = {
279+
payload: {
280+
meta: {
281+
analytics: [
282+
[
283+
'event', 'foo', 'bar'
284+
],
285+
[
286+
'exception', 'CLICK', 'TEST', 'LABEL', 1
287+
]
288+
]
289+
}
290+
}
291+
}
292+
store.fire(mutation)
293+
expect(window.ga).toBeCalledWith('send', 'event', 'foo', 'bar')
294+
expect(window.ga).toBeCalledWith('send', 'exception', { exDescription: 'CLICK', exFatal: 'TEST' })
295+
296+
})
297+
})

0 commit comments

Comments
 (0)