From c06e7e6360f6f8b010a13e42086d16710a5dd2ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E6=85=A7=E6=AD=A6?= Date: Sat, 13 Apr 2019 23:01:03 +0800 Subject: [PATCH] :white_check_mark:update tests --- tests/unit/track-click-async.spec.js | 4 +- tests/unit/track-click-delay.spec.js | 4 +- tests/unit/track-click-param.spec.js | 17 ++--- tests/unit/track-custom-event-async.spec.js | 61 +++++++++++++++++ tests/unit/track-custom-event-delay.spec.js | 59 ++++++++++++++++ tests/unit/track-custom-event-param.spec.js | 68 +++++++++++++++++++ tests/unit/track-custom-event.spec.js | 55 +++++++++++++++ ... => track-show-custom-scroll-once.spec.js} | 4 +- ...ec.js => track-show-custom-scroll.spec.js} | 10 +-- 9 files changed, 263 insertions(+), 19 deletions(-) create mode 100644 tests/unit/track-custom-event-async.spec.js create mode 100644 tests/unit/track-custom-event-delay.spec.js create mode 100644 tests/unit/track-custom-event-param.spec.js create mode 100644 tests/unit/track-custom-event.spec.js rename tests/unit/{cmp-track-show-custom-once.spec.js => track-show-custom-scroll-once.spec.js} (94%) rename tests/unit/{cmp-track-show-custom.spec.js => track-show-custom-scroll.spec.js} (84%) diff --git a/tests/unit/track-click-async.spec.js b/tests/unit/track-click-async.spec.js index 619247d..cc3c1c1 100644 --- a/tests/unit/track-click-async.spec.js +++ b/tests/unit/track-click-async.spec.js @@ -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 "@/"; @@ -35,7 +35,7 @@ localVue.use(VTrack, { }); describe("TrackClickAsync", () => { - it("确保异步事件返回成功之后再触发埋点", done => { + it("确保异步的事件返回成功之后再上报埋点", done => { const wrapper = mount(TrackClickAsync, { localVue }); diff --git a/tests/unit/track-click-delay.spec.js b/tests/unit/track-click-delay.spec.js index 195ea5e..db86c03 100644 --- a/tests/unit/track-click-delay.spec.js +++ b/tests/unit/track-click-delay.spec.js @@ -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 "@/"; @@ -34,7 +34,7 @@ localVue.use(VTrack, { }); describe("TrackClick", () => { - it("确保埋点在click事件之后执行", () => { + it("确保埋点在click事件之后上报", () => { const wrapper = mount(TrackClick, { localVue }); diff --git a/tests/unit/track-click-param.spec.js b/tests/unit/track-click-param.spec.js index c98a6f5..3be4d93 100644 --- a/tests/unit/track-click-param.spec.js +++ b/tests/unit/track-click-param.spec.js @@ -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 "@/"; @@ -22,7 +22,7 @@ const TrackClickHasParam = Vue.extend({ item: { id: 1 }, - id: 1 + id: 0 }; }, methods: { @@ -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); }); }); diff --git a/tests/unit/track-custom-event-async.spec.js b/tests/unit/track-custom-event-async.spec.js new file mode 100644 index 0000000..7c8662f --- /dev/null +++ b/tests/unit/track-custom-event-async.spec.js @@ -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: ` +
+ ` +}); +const TrackCustomEventAsync = Vue.extend({ + template: ` + + `, + 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(); + }); + }); +}); diff --git a/tests/unit/track-custom-event-delay.spec.js b/tests/unit/track-custom-event-delay.spec.js new file mode 100644 index 0000000..b14a183 --- /dev/null +++ b/tests/unit/track-custom-event-delay.spec.js @@ -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: ` +
+ `, + data() { + return { + status: true + }; + } +}); +const TrackCustomEventDelay = Vue.extend({ + template: ` + + `, + 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); + }); +}); diff --git a/tests/unit/track-custom-event-param.spec.js b/tests/unit/track-custom-event-param.spec.js new file mode 100644 index 0000000..3389618 --- /dev/null +++ b/tests/unit/track-custom-event-param.spec.js @@ -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: ` +
+ `, + data() { + return { + item: { + id: 0 + } + }; + } +}); +const TrackCustomEventParam = Vue.extend({ + template: ` + + `, + 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); + }); +}); diff --git a/tests/unit/track-custom-event.spec.js b/tests/unit/track-custom-event.spec.js new file mode 100644 index 0000000..fb4cb40 --- /dev/null +++ b/tests/unit/track-custom-event.spec.js @@ -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: ` +
+ ` +}); +const TrackCustomEvent = Vue.extend({ + template: ` + + `, + 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); + }); +}); diff --git a/tests/unit/cmp-track-show-custom-once.spec.js b/tests/unit/track-show-custom-scroll-once.spec.js similarity index 94% rename from tests/unit/cmp-track-show-custom-once.spec.js rename to tests/unit/track-show-custom-scroll-once.spec.js index f97767e..59d2c90 100644 --- a/tests/unit/cmp-track-show-custom-once.spec.js +++ b/tests/unit/track-show-custom-scroll-once.spec.js @@ -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 "@/"; @@ -15,7 +15,7 @@ const trackEvents = { 18015: mockTrackAction }; const Cmp = Vue.extend({ - template: `
` + template: `
` }); const CmpTrackShowCustomOnce = Vue.extend({ template: ` diff --git a/tests/unit/cmp-track-show-custom.spec.js b/tests/unit/track-show-custom-scroll.spec.js similarity index 84% rename from tests/unit/cmp-track-show-custom.spec.js rename to tests/unit/track-show-custom-scroll.spec.js index 0097f51..304e1d3 100644 --- a/tests/unit/cmp-track-show-custom.spec.js +++ b/tests/unit/track-show-custom-scroll.spec.js @@ -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 "@/"; @@ -15,9 +15,9 @@ const trackEvents = { 18015: mockTrackAction }; const Cmp = Vue.extend({ - template: `
` + template: `
` }); -const CmpTrackShowCustom = Vue.extend({ +const TrackShowCustomScroll = Vue.extend({ template: ` @@ -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);