Skip to content

Commit b6019a4

Browse files
patinthehatPatrick
authored andcommitted
add tests for vue 3 event intercepts
1 parent 70a7c4e commit b6019a4

File tree

3 files changed

+226
-82
lines changed

3 files changed

+226
-82
lines changed

tests/__snapshots__/vue3.test.ts.snap

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Vue 3 Mixin: it conditionally displays the beforeMount event 1`] = `
4+
Array [
5+
Object {
6+
"content": Array [
7+
"component before-mount: <code>test</code>",
8+
"filename: <code>&lt;project root&gt;/test.js</code>",
9+
],
10+
"label": undefined,
11+
"name": "table",
12+
},
13+
]
14+
`;
15+
16+
exports[`Vue 3 Mixin: it conditionally displays the created & mounted events 1`] = `
17+
Array [
18+
Object {
19+
"content": Array [
20+
"component created: <code>test</code>",
21+
"filename: <code>&lt;project root&gt;/test.js</code>",
22+
],
23+
"label": undefined,
24+
"name": "table",
25+
},
26+
Object {
27+
"content": Array [
28+
"component mounted: <code>test</code>",
29+
"filename: <code>&lt;project root&gt;/test.js</code>",
30+
],
31+
"label": undefined,
32+
"name": "table",
33+
},
34+
]
35+
`;
36+
37+
exports[`Vue 3 Mixin: it conditionally displays the unmounted event 1`] = `
38+
Array [
39+
Object {
40+
"content": Array [
41+
"component unmounted: <code>test</code>",
42+
"filename: <code>&lt;project root&gt;/test.js</code>",
43+
],
44+
"label": undefined,
45+
"name": "table",
46+
},
47+
]
48+
`;
49+
50+
exports[`Vue 3 Mixin: it conditionally displays the updated event 1`] = `
51+
Array [
52+
Object {
53+
"content": Array [
54+
"component updated: <code>test</code>",
55+
"filename: <code>&lt;project root&gt;/test.js</code>",
56+
],
57+
"label": undefined,
58+
"name": "table",
59+
},
60+
]
61+
`;

tests/vue3.test.js

Lines changed: 0 additions & 82 deletions
This file was deleted.

tests/vue3.test.ts

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/* eslint-disable no-unused-vars */
2+
/* eslint-disable no-undef */
3+
4+
import RayVue3Plugin from '../src/RayVue3';
5+
import { FakeRay } from './TestClasses/FakeRay';
6+
import { FakeVm } from './TestClasses/FakeVm';
7+
import { Vue3RayMixin } from '../src/v3/Vue3RayMixin';
8+
import { VueRay } from '../src/shared/VueRay';
9+
10+
let ray: FakeRay, fakeApp: FakeApp;
11+
12+
class FakeApp {
13+
public providedItems: any[] = [];
14+
public config: any;
15+
public mixins: any = {};
16+
17+
constructor() {
18+
this.providedItems = [];
19+
this.config = {
20+
globalProperties: {
21+
$ray: null,
22+
},
23+
errorHandler: null,
24+
};
25+
26+
this.mixins = {};
27+
this.mixins.methods = {
28+
$ray: null,
29+
};
30+
}
31+
32+
provide(name: any, options: any) {
33+
this.providedItems.push(name);
34+
}
35+
36+
getProvided() {
37+
return this.providedItems;
38+
}
39+
40+
getLastProvided() {
41+
return this.getProvided().slice(0).pop();
42+
}
43+
44+
mixin(obj: any) {
45+
this.mixins = Object.assign({}, this.mixins, obj);
46+
}
47+
}
48+
49+
beforeEach(() => {
50+
ray = new FakeRay();
51+
fakeApp = new FakeApp();
52+
});
53+
54+
const prepareMixin = () => {
55+
// @ts-ignore
56+
Vue3RayMixin.$options = {
57+
name: 'test',
58+
__file: 'test.js',
59+
};
60+
61+
// @ts-ignore
62+
Vue3RayMixin.$ray = function (...args: any[]) {
63+
return ray.send(...args);
64+
};
65+
66+
Vue3RayMixin.methods = {
67+
$ray(...args: any[]) {
68+
return ray.send(...args);
69+
},
70+
};
71+
};
72+
73+
describe('Vue 3 Ray Plugin:', () => {
74+
test('it installs successfully', () => {
75+
RayVue3Plugin.install(fakeApp, {});
76+
77+
expect(fakeApp.getLastProvided()).toBe('ray');
78+
expect(fakeApp.getProvided().length).toBe(1);
79+
//expect(fakeApp.config.globalProperties['$ray']).not.toBe(null);
80+
expect(fakeApp.mixins.methods.$ray).not.toBe(null);
81+
82+
if (fakeApp.mixins.methods.$ray !== null) {
83+
const testRay = fakeApp.mixins.methods.$ray;
84+
85+
expect(typeof testRay).not.toBe('undefined');
86+
expect(testRay().constructor.name).toBe('VueRay');
87+
}
88+
});
89+
90+
test('it installs successfully with the interceptErrors option', () => {
91+
RayVue3Plugin.install(fakeApp, { interceptErrors: true });
92+
93+
expect(fakeApp.getLastProvided()).toBe('ray');
94+
expect(fakeApp.getProvided().length).toBe(1);
95+
expect(fakeApp.mixins.methods.$ray).not.toBe(null);
96+
expect(fakeApp.config.errorHandler).not.toBeNull();
97+
expect(typeof fakeApp.config.errorHandler).toBe('function');
98+
99+
const fakeVm = new FakeVm(ray);
100+
fakeApp.config.errorHandler({ stack: 'test error' }, fakeVm);
101+
102+
expect(ray.payloads.length).toBe(3);
103+
expect(ray.payloads[0].content).toContain('test error');
104+
expect(ray.payloads[1].content).toBe('small');
105+
expect(ray.payloads[2].content).toBe('red');
106+
});
107+
});
108+
109+
describe('Vue 3 Mixin:', () => {
110+
test('it does not conditionally display events if not defined', () => {
111+
VueRay.show_component_lifecycles = [];
112+
113+
prepareMixin();
114+
115+
//Vue3RayMixin.beforeCreate();
116+
Vue3RayMixin.beforeMount();
117+
Vue3RayMixin.created();
118+
Vue3RayMixin.mounted();
119+
Vue3RayMixin.unmounted();
120+
Vue3RayMixin.updated();
121+
122+
expect(ray.payloads.length).toBe(0);
123+
});
124+
125+
test('it conditionally displays the beforeMount event', () => {
126+
VueRay.show_component_lifecycles = ['before-mount'];
127+
128+
prepareMixin();
129+
Vue3RayMixin.beforeMount();
130+
131+
expect(ray.payloads.length).toBe(1);
132+
expect(ray.payloads).toMatchSnapshot();
133+
});
134+
135+
test('it conditionally displays the created & mounted events', () => {
136+
VueRay.show_component_lifecycles = ['created', 'mounted'];
137+
138+
prepareMixin();
139+
Vue3RayMixin.created();
140+
Vue3RayMixin.mounted();
141+
142+
expect(ray.payloads.length).toBe(2);
143+
expect(ray.payloads).toMatchSnapshot();
144+
});
145+
146+
test('it conditionally displays the unmounted event', () => {
147+
VueRay.show_component_lifecycles = ['unmounted'];
148+
149+
prepareMixin();
150+
Vue3RayMixin.unmounted();
151+
152+
expect(ray.payloads.length).toBe(1);
153+
expect(ray.payloads).toMatchSnapshot();
154+
});
155+
156+
test('it conditionally displays the updated event', () => {
157+
VueRay.show_component_lifecycles = ['updated'];
158+
159+
prepareMixin();
160+
Vue3RayMixin.updated();
161+
162+
expect(ray.payloads.length).toBe(1);
163+
expect(ray.payloads).toMatchSnapshot();
164+
});
165+
});

0 commit comments

Comments
 (0)