Commit 26cbc463 authored by Martin Wortschack's avatar Martin Wortschack

Merge branch '36382-improve-tracking-mixin' into 'master'

Resolve "Improve Tracking.mixin"

Closes #36382

See merge request gitlab-org/gitlab!20232
parents aea737d5 9b9a4790
...@@ -48,6 +48,12 @@ export default { ...@@ -48,6 +48,12 @@ export default {
}, },
}, },
computed: { computed: {
tracking() {
return {
// eslint-disable-next-line no-underscore-dangle
category: this.$options._componentTag,
};
},
showLoadingState() { showLoadingState() {
return this.subscribed === null; return this.subscribed === null;
}, },
......
...@@ -73,20 +73,25 @@ export default class Tracking { ...@@ -73,20 +73,25 @@ export default class Tracking {
return handlers; return handlers;
} }
static mixin(opts) { static mixin(opts = {}) {
return { return {
data() { computed: {
return { trackingCategory() {
tracking: { const localCategory = this.tracking ? this.tracking.category : null;
// eslint-disable-next-line no-underscore-dangle return localCategory || opts.category;
category: this.$options.name || this.$options._componentTag, },
}, trackingOptions() {
}; return { ...opts, ...this.tracking };
},
}, },
methods: { methods: {
track(action, data) { track(action, data = {}) {
const category = opts.category || data.category || this.tracking.category; const category = data.category || this.trackingCategory;
Tracking.event(category || 'unspecified', action, { ...opts, ...this.tracking, ...data }); const options = {
...this.trackingOptions,
...data,
};
Tracking.event(category, action, options);
}, },
}, },
}; };
......
...@@ -56,6 +56,12 @@ export default { ...@@ -56,6 +56,12 @@ export default {
}; };
}, },
computed: { computed: {
tracking() {
return {
// eslint-disable-next-line no-underscore-dangle
category: this.$options._componentTag,
};
},
isNoValue() { isNoValue() {
return this.checkIfNoValue(this.weight); return this.checkIfNoValue(this.weight);
}, },
......
...@@ -12,9 +12,11 @@ const DEFAULT_PROPS = { ...@@ -12,9 +12,11 @@ const DEFAULT_PROPS = {
describe('Weight', function() { describe('Weight', function() {
let vm; let vm;
let Weight; let Weight;
let trackingSpy;
beforeEach(() => { beforeEach(() => {
Weight = Vue.extend(weight); Weight = Vue.extend(weight);
trackingSpy = mockTracking('_category_', null, spyOn);
}); });
afterEach(() => { afterEach(() => {
...@@ -116,12 +118,11 @@ describe('Weight', function() { ...@@ -116,12 +118,11 @@ describe('Weight', function() {
editable: true, editable: true,
}); });
const spy = mockTracking('_category_', vm.$el, spyOn, afterEach); triggerEvent(vm.$el.querySelector('.js-weight-edit-link'));
triggerEvent('.js-weight-edit-link');
vm.$nextTick() vm.$nextTick()
.then(() => { .then(() => {
expect(spy).toHaveBeenCalled(); expect(trackingSpy).toHaveBeenCalled();
}) })
.then(done) .then(done)
.catch(done.fail); .catch(done.fail);
......
...@@ -177,4 +177,70 @@ describe('Tracking', () => { ...@@ -177,4 +177,70 @@ describe('Tracking', () => {
expect(eventSpy).toHaveBeenCalledWith('_category_', 'nested_event', {}); expect(eventSpy).toHaveBeenCalledWith('_category_', 'nested_event', {});
}); });
}); });
describe('tracking mixin', () => {
describe('trackingOptions', () => {
it('return the options defined on initialisation', () => {
const mixin = Tracking.mixin({ foo: 'bar' });
expect(mixin.computed.trackingOptions()).toEqual({ foo: 'bar' });
});
it('local tracking value override and extend options', () => {
const mixin = Tracking.mixin({ foo: 'bar' });
// the value of this in the vue lifecyle is different, but this serve the tests purposes
mixin.computed.tracking = { foo: 'baz', baz: 'bar' };
expect(mixin.computed.trackingOptions()).toEqual({ foo: 'baz', baz: 'bar' });
});
});
describe('trackingCategory', () => {
it('return the category set in the component properties first', () => {
const mixin = Tracking.mixin({ category: 'foo' });
mixin.computed.tracking = {
category: 'bar',
};
expect(mixin.computed.trackingCategory()).toBe('bar');
});
it('return the category set in the options', () => {
const mixin = Tracking.mixin({ category: 'foo' });
expect(mixin.computed.trackingCategory()).toBe('foo');
});
it('if no category is selected returns undefined', () => {
const mixin = Tracking.mixin();
expect(mixin.computed.trackingCategory()).toBe(undefined);
});
});
describe('track', () => {
let eventSpy;
let mixin;
beforeEach(() => {
eventSpy = jest.spyOn(Tracking, 'event').mockReturnValue();
mixin = Tracking.mixin();
mixin = {
...mixin.computed,
...mixin.methods,
};
});
it('calls the event method', () => {
mixin.trackingCategory = mixin.trackingCategory();
mixin.trackingOptions = mixin.trackingOptions();
mixin.track('foo');
expect(eventSpy).toHaveBeenCalledWith(undefined, 'foo', {});
});
it('give precedence to data for category and options', () => {
mixin.trackingCategory = mixin.trackingCategory();
mixin.trackingOptions = mixin.trackingOptions();
const data = { category: 'foo', label: 'baz' };
mixin.track('foo', data);
expect(eventSpy).toHaveBeenCalledWith('foo', 'foo', data);
});
});
});
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment