Skip to content

Commit

Permalink
✅update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
宋慧武 committed Apr 13, 2019
1 parent f1404ef commit c06e7e6
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 19 deletions.
4 changes: 2 additions & 2 deletions tests/unit/track-click-async.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: 宋慧武
* @Date: 2019-04-09 19:08:17
* @Last Modified by: 宋慧武
* @Last Modified time: 2019-04-09 21:40:16
* @Last Modified time: 2019-04-13 22:30:28
*/
import Vue from "vue";
import VTrack from "@/";
Expand Down Expand Up @@ -35,7 +35,7 @@ localVue.use(VTrack, {
});

describe("TrackClickAsync", () => {
it("确保异步事件返回成功之后再触发埋点", done => {
it("确保异步的事件返回成功之后再上报埋点", done => {
const wrapper = mount(TrackClickAsync, {
localVue
});
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/track-click-delay.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: 宋慧武
* @Date: 2019-04-09 21:41:47
* @Last Modified by: 宋慧武
* @Last Modified time: 2019-04-09 21:59:12
* @Last Modified time: 2019-04-13 22:31:03
*/
import Vue from "vue";
import VTrack from "@/";
Expand Down Expand Up @@ -34,7 +34,7 @@ localVue.use(VTrack, {
});

describe("TrackClick", () => {
it("确保埋点在click事件之后执行", () => {
it("确保埋点在click事件之后上报", () => {
const wrapper = mount(TrackClick, {
localVue
});
Expand Down
17 changes: 9 additions & 8 deletions tests/unit/track-click-param.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: 宋慧武
* @Date: 2019-04-09 18:23:04
* @Last Modified by: 宋慧武
* @Last Modified time: 2019-04-09 21:40:29
* @Last Modified time: 2019-04-13 21:56:46
*/
import Vue from "vue";
import VTrack from "@/";
Expand All @@ -22,7 +22,7 @@ const TrackClickHasParam = Vue.extend({
item: {
id: 1
},
id: 1
id: 0
};
},
methods: {
Expand All @@ -37,18 +37,19 @@ localVue.use(VTrack, {
});

describe("TrackClickHasParam", () => {
it("确保传参正确,点击多次会上报多次埋点", () => {
it("确保事件传参正确,并且点击事件次数等于埋点上报次数", () => {
const wrapper = mount(TrackClickHasParam, {
localVue
});
const vm = wrapper.vm;

expect(wrapper.vm.id).toBe(1);
wrapper.setData({ item: { id: 2 } });
expect(vm.id).toBe(0);
wrapper.setData({ item: { id: 1 } });
wrapper.trigger("click");
expect(wrapper.vm.id).toBe(2);
wrapper.setData({ item: { id: 3 } });
expect(vm.id).toBe(1);
wrapper.setData({ item: { id: 2 } });
wrapper.trigger("click");
expect(wrapper.vm.id).toBe(3);
expect(vm.id).toBe(2);
expect(mockTrackAction).toBeCalledTimes(2);
});
});
61 changes: 61 additions & 0 deletions tests/unit/track-custom-event-async.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* @Author: 宋慧武
* @Date: 2019-04-13 22:00:04
* @Last Modified by: 宋慧武
* @Last Modified time: 2019-04-13 22:54:09
*/
import Vue from "vue";
import VTrack from "@/";
import { mount, createLocalVue } from "@vue/test-utils";

const localVue = createLocalVue();
const mockTrackAction = jest.fn(({ status }) => status);
const trackEvents = {
18015: mockTrackAction
};
const Child = Vue.extend({
template: `
<div @click="$emit('search')" />
`
});
const TrackCustomEventAsync = Vue.extend({
template: `
<child v-track:18015.search.async="{ fetchRest, rest }" />
`,
components: {
child: Child
},
data() {
return {
rest: null
};
},
methods: {
async fetchRest() {
const response = await Promise.resolve({ data: "success" });
this.rest = response.data;
}
}
});

localVue.use(VTrack, {
trackEvents
});

describe("TrackCustomEventAsync", () => {
it("确保异步的自定义事件返回成功之后再上报埋点", done => {
const wrapper = mount(TrackCustomEventAsync, {
localVue
});
const vm = wrapper.vm;

wrapper.find(Child).trigger("click");
expect(vm.rest).toBeNull();
expect(mockTrackAction).toBeCalledTimes(0);
vm.$nextTick(() => {
expect(vm.rest).toBe("success");
expect(mockTrackAction).toBeCalledTimes(1);
done();
});
});
});
59 changes: 59 additions & 0 deletions tests/unit/track-custom-event-delay.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* @Author: 宋慧武
* @Date: 2019-04-13 22:00:04
* @Last Modified by: 宋慧武
* @Last Modified time: 2019-04-13 22:30:58
*/
import Vue from "vue";
import VTrack from "@/";
import { mount, createLocalVue } from "@vue/test-utils";

const localVue = createLocalVue();
const mockTrackAction = jest.fn(({ status }) => status);
const trackEvents = {
18015: mockTrackAction
};
const Child = Vue.extend({
template: `
<div @click="$emit('expand', { status })" />
`,
data() {
return {
status: true
};
}
});
const TrackCustomEventDelay = Vue.extend({
template: `
<child v-track:18015.expand.delay="handleExpand" />
`,
components: {
child: Child
},
data() {
return {
status: false
};
},
methods: {
handleExpand({ status }) {
this.status = status;
}
}
});

localVue.use(VTrack, {
trackEvents
});

describe("TrackCustomEventDelay", () => {
it("确保埋点在自定义事件之后上报", () => {
const wrapper = mount(TrackCustomEventDelay, {
localVue
});

wrapper.find(Child).trigger("click");
expect(mockTrackAction).toBeTruthy();
expect(mockTrackAction).toBeCalledTimes(1);
});
});
68 changes: 68 additions & 0 deletions tests/unit/track-custom-event-param.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* @Author: 宋慧武
* @Date: 2019-04-13 21:03:55
* @Last Modified by: 宋慧武
* @Last Modified time: 2019-04-13 22:56:41
*/
import Vue from "vue";
import VTrack from "@/";
import { mount, createLocalVue } from "@vue/test-utils";

const localVue = createLocalVue();
const mockTrackAction = jest.fn();
const trackEvents = {
18015: mockTrackAction
};
const Child = Vue.extend({
template: `
<div @click="$emit('search', { item })" />
`,
data() {
return {
item: {
id: 0
}
};
}
});
const TrackCustomEventParam = Vue.extend({
template: `
<child v-track:18015.search="handleClick" />
`,
components: {
child: Child
},
data() {
return {
id: 0
};
},
methods: {
handleClick({ id }) {
this.id = id;
}
}
});

localVue.use(VTrack, {
trackEvents
});

describe("TrackCustomEventParam", () => {
it("确保自定义事件传参正确,并且点击事件次数等于埋点上报次数", () => {
const wrapper = mount(TrackCustomEventParam, {
localVue
});
const vm = wrapper.vm;
const childWrapper = wrapper.find(Child);

expect(vm.id).toBe(0);
childWrapper.setData({ item: { id: 1 } });
childWrapper.trigger("click");
expect(vm.id).toBe(1);
childWrapper.setData({ item: { id: 2 } });
childWrapper.trigger("click");
expect(vm.id).toBe(2);
expect(mockTrackAction).toBeCalledTimes(2);
});
});
55 changes: 55 additions & 0 deletions tests/unit/track-custom-event.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* @Author: 宋慧武
* @Date: 2019-04-13 21:03:55
* @Last Modified by: 宋慧武
* @Last Modified time: 2019-04-13 22:17:26
*/
import Vue from "vue";
import VTrack from "@/";
import { mount, createLocalVue } from "@vue/test-utils";

const localVue = createLocalVue();
const mockTrackAction = jest.fn();
const trackEvents = {
18015: mockTrackAction
};
const Child = Vue.extend({
template: `
<div @click="$emit('click')" />
`
});
const TrackCustomEvent = Vue.extend({
template: `
<child v-track:18015.click="handleClick" />
`,
components: {
child: Child
},
data() {
return {
count: 0
};
},
methods: {
handleClick() {
this.count += 1;
}
}
});

localVue.use(VTrack, {
trackEvents
});

describe("TrackCustomEvent", () => {
it("确保自定义事件、埋点事件正常触发,且只触发一次", () => {
const wrapper = mount(TrackCustomEvent, {
localVue
});

expect(wrapper.vm.count).toBe(0);
wrapper.find(Child).trigger("click");
expect(wrapper.vm.count).toBe(1);
expect(mockTrackAction).toBeCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: 宋慧武
* @Date: 2019-04-12 21:03:46
* @Last Modified by: 宋慧武
* @Last Modified time: 2019-04-13 15:01:44
* @Last Modified time: 2019-04-13 22:58:00
*/
import Vue from "vue";
import VTrack from "@/";
Expand All @@ -15,7 +15,7 @@ const trackEvents = {
18015: mockTrackAction
};
const Cmp = Vue.extend({
template: `<div class="test"></div>`
template: `<div></div>`
});
const CmpTrackShowCustomOnce = Vue.extend({
template: `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: 宋慧武
* @Date: 2019-04-12 21:03:46
* @Last Modified by: 宋慧武
* @Last Modified time: 2019-04-13 15:02:02
* @Last Modified time: 2019-04-13 22:57:53
*/
import Vue from "vue";
import VTrack from "@/";
Expand All @@ -15,9 +15,9 @@ const trackEvents = {
18015: mockTrackAction
};
const Cmp = Vue.extend({
template: `<div class="test"></div>`
template: `<div></div>`
});
const CmpTrackShowCustom = Vue.extend({
const TrackShowCustomScroll = Vue.extend({
template: `
<span>
<cmp ref="scroll" v-track:18015.show.custom="{ref: 'scroll'}" />
Expand All @@ -34,9 +34,9 @@ localVue.use(VTrack, {

jest.useFakeTimers();

describe("CmpTrackShowCustom", () => {
describe("TrackShowCustomScroll", () => {
it("确保组件完全可见之后触发埋点,且至少间隔200ms执行一下", () => {
const wrapper = mount(CmpTrackShowCustom, { localVue });
const wrapper = mount(TrackShowCustomScroll, { localVue });
const vm = wrapper.find(Cmp).vm;

mockParentNode(vm.$el);
Expand Down

0 comments on commit c06e7e6

Please sign in to comment.