Skip to content

Commit

Permalink
Merge pull request #13 from l-hammer/block_show_support_args
Browse files Browse the repository at this point in the history
block show track support arguments
  • Loading branch information
LHammer authored Aug 5, 2019
2 parents d0be03f + 7bfbff1 commit 9c1d4bd
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "v-track",
"version": "0.8.5",
"version": "0.8.6",
"description": "一个基于Vue指令的埋点插件",
"author": "LHammer <[email protected]>",
"scripts": {
Expand Down
7 changes: 4 additions & 3 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: 宋慧武
* @Date: 2019-03-06 17:49:29
* @Last Modified by: 宋慧武
* @Last Modified time: 2019-05-28 17:05:58
* @Last Modified time: 2019-08-05 17:43:57
*/
import {
zipArray,
Expand Down Expand Up @@ -107,14 +107,15 @@ export function bind(
}
// 区域曝光埋点
else if (partialMatch("show")) {
const fn = () => events[id](context);
const [args] = zipArray(value);
const tck = events[id].bind(null, context, ...args);
const once = partialMatch("once");
const custom = partialMatch("custom");

if (!el.$visMonitor) {
const vm = new VisMonitor(el, custom && context.$refs[value.ref]);

(once ? vm.$once : vm.$on).call(vm, "fullyvisible", fn);
(once ? vm.$once : vm.$on).call(vm, "fullyvisible", tck);
el.$visMonitor = vm;
}
} else if (
Expand Down
4 changes: 2 additions & 2 deletions src/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: 宋慧武
* @Date: 2019-04-08 11:13:34
* @Last Modified by: 宋慧武
* @Last Modified time: 2019-04-20 18:06:43
* @Last Modified time: 2019-08-05 15:31:00
*/

/**
Expand Down Expand Up @@ -33,7 +33,7 @@ export const isDef = v => v !== undefined && v !== null;
* @param {Object} value
* @returns {Array} [keys, values]
*/
export function zipArray(value) {
export function zipArray(value = {}) {
return [Object.values(value), Object.keys(value)];
}

Expand Down
51 changes: 51 additions & 0 deletions tests/unit/track-show-param.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Created Date: 2019-08-05
* Author: 宋慧武
* ------
* Last Modified: Monday 2019-08-05 17:01:32 pm
* Modified By: the developer formerly known as 宋慧武 at <[email protected]>
* ------
* HISTORY:
* ------
* Javascript will save your soul!
*/
import Vue from "vue";
import VTrack from "@/";
import { mount, createLocalVue } from "@vue/test-utils";
import { mockParentNode, mockRect } from "../helper";

const localVue = createLocalVue();
const mockTrackAction = jest.fn((_, id) => id);
const trackEvents = {
18015: mockTrackAction
};
const TrackShow = Vue.extend({
template: `
<div v-track:18015.show="{ id }" />
`,
data() {
return {
id: "2019"
};
}
});

localVue.use(VTrack, {
trackEvents
});

jest.useFakeTimers();

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

mockParentNode(vm.$el);
mockRect(vm.$el);
jest.runAllTimers();

expect(mockTrackAction.mock.results[0].value).toBe("2019");
expect(mockTrackAction).toBeCalledTimes(1);
});
});

0 comments on commit 9c1d4bd

Please sign in to comment.