Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: funnel #1916

Merged
merged 7 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions __tests__/unit/plots/funnel/basic-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Funnel } from '../../../../src';
import { PV_DATA } from '../../../data/conversion';
import { createDiv } from '../../../utils/dom';
import { FUNNEL_CONVERSATION, FUNNEL_PERCENT } from '../../../../src/plots/funnel/constant';

describe('basic funnel', () => {
let funnel;

const funnelOption = {
width: 400,
height: 400,
data: PV_DATA,
xField: 'action',
yField: 'pv',
};

beforeAll(() => {
funnel = new Funnel(createDiv('basic funnel'), funnelOption);
funnel.render();
});

afterAll(() => {
funnel.destroy();
});

describe('geometry', () => {
test('geometry test', () => {
const geometry = funnel.chart.geometries[0];
// 数据量
expect(geometry.elements.length).toBe(PV_DATA.length);

// geometry
expect(geometry.type).toBe('interval');
// @ts-ignore
expect(geometry.adjustOption[0].type).toBe('symmetric');

// position
const positionFields = geometry.getAttribute('position').getFields();
expect(positionFields).toHaveLength(2);
expect(positionFields[0]).toBe('action');
expect(positionFields[1]).toBe('pv');

// shape
const shapeFields = geometry.getAttribute('shape').getFields();
expect(shapeFields[0]).toBe('funnel');

// color
const colorFields = geometry.getAttribute('color').getFields();
expect(colorFields[0]).toBe('action');

// transpose
const coordinate = funnel.chart.getCoordinate();
expect(coordinate.isRect).toBe(true);
expect(coordinate.isTransposed).toBe(true);

// 判断数据是否正确
const { data } = funnel.chart.getOptions();
data.forEach((item, index) => {
expect(item[FUNNEL_PERCENT]).toEqual(item.pv / data[0].pv);
expect(item[FUNNEL_CONVERSATION]).toEqual(index === 0 ? 1 : item.pv / data[index - 1].pv);
});
});
});

describe('transpose', () => {
test('transpose', () => {
funnel.update({
...funnelOption,
isTransposed: true,
});
// transpose
const coordinate = funnel.chart.getCoordinate();
expect(coordinate.isRect).toBe(true);
expect(coordinate.isTransposed).toBe(false);
});
});
});
111 changes: 13 additions & 98 deletions __tests__/unit/plots/funnel/compare-spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Funnel } from '../../../../src';
import { PV_DATA_COMPARE } from '../../../data/conversion';
import { createDiv } from '../../../utils/dom';
import { FUNNEL_PERCENT } from '../../../../src/plots/funnel/constant';
import { FUNNEL_PERCENT, FUNNEL_CONVERSATION } from '../../../../src/plots/funnel/constant';

describe('compare funnel', () => {
let funnel;
Expand Down Expand Up @@ -44,115 +44,30 @@ describe('compare funnel', () => {

// position
const positionFields = geometry.getAttribute('position').getFields();
expect(positionFields).toHaveLength(3);
expect(positionFields).toHaveLength(2);
expect(positionFields[0]).toBe('action');
expect(positionFields[1]).toBe('pv');
expect(positionFields[2]).toBe(FUNNEL_PERCENT);

const shapeFields = geometry.getAttribute('shape').getFields();
expect(shapeFields[0]).toBe('funnel');

const colorFields = geometry.getAttribute('color').getFields();
expect(colorFields[0]).toBe('action');
});
});
});

describe('geometry label', () => {
test('geometry label default', () => {
funnel.chart.views.forEach((funnelView) => {
const geometry = funnelView.geometries[0];
const { labelOption, labelsContainer } = geometry;
expect(labelOption.fields).toEqual(['action', 'pv', FUNNEL_PERCENT]);
expect(labelsContainer.cfg.children.length).toBe(5);
expect(labelOption.cfg.style.fill).toBe('#fff');
expect(labelOption.cfg.style.fontSize).toBe(12);
});
});

test('geometry label custom', () => {
funnel.update({
...funnelOption,
label: {
style: {
fill: '#f00',
fontSize: 14,
},
callback: (xField, yField) => ({
content: `${yField}`,
}),
},
});

funnel.chart.views.forEach((funnelView) => {
expect(funnelView.geometries[0].labelOption.cfg.style.fill).toBe('#f00');
expect(funnelView.geometries[0].labelOption.cfg.style.fontSize).toBe(14);
});
});

test('geometry label close', () => {
// 关闭 label
funnel.update({
...funnelOption,
label: false,
});

funnel.chart.views.forEach((funnelView) => {
expect(funnelView.geometries[0].labelsContainer.cfg.children.length).toBe(0);
});
});
});

describe('conversionTag', () => {
// test('conversionTag default', () => {
// funnel.chart.views.forEach((funnelView) => {
// const { data } = funnelView.getOptions();
// const annotation = funnelView.getController('annotation').getComponents();
// expect(annotation.length).toEqual(5);
// expect(annotation[0].component.cfg.content).toBe(data[0].quarter);
// data.forEach((pvItem, index) => {
// if (index === 0) return;
// expect(annotation[index].component.get('text').content).toBe(`转化率${pvItem[FUNNEL_PERCENT] * 100}%`);
// });
// });
// });

test('conversionTag custom', () => {
// 自定义转化率组件
funnel.update({
...funnelOption,
conversionTag: {
style: {
fill: '#f00',
fontSize: 18,
},
formatter: (datum) => `${datum.$$percentage$$}转化`,
},
});
funnel.chart.views.forEach((funnelView) => {
const { data } = funnelView.getOptions();
const customAnnotation = funnelView.getController('annotation').getComponents();
expect(customAnnotation.length).toEqual(5);
data.forEach((pvItem, index) => {
if (index === 0) return;
const text = customAnnotation[index].component.get('text');
expect(text.content).toBe(`${pvItem[FUNNEL_PERCENT]}转化`);
expect(text.style.fill).toBe('#f00');
expect(text.style.fontSize).toBe(18);
const origin = {
'2020Q1': PV_DATA_COMPARE.filter((item) => item.quarter === '2020Q1'),
'2020Q2': PV_DATA_COMPARE.filter((item) => item.quarter === '2020Q2'),
};

const { data } = funnel.chart.getOptions();
data.forEach((item, index) => {
const originData = origin[item.quarter];
const originIndex = originData.findIndex((jtem) => jtem.pv === item.pv);
expect(item[FUNNEL_PERCENT]).toEqual(item.pv / originData[0].pv);
expect(item[FUNNEL_CONVERSATION]).toEqual(originIndex === 0 ? 1 : item.pv / originData[originIndex - 1].pv);
});
});
});

test('conversionTag close', () => {
funnel.update({
...funnelOption,
conversionTag: false,
});

funnel.chart.views.forEach((funnelView) => {
expect(funnelView.getController('annotation').getComponents().length).toEqual(1);
});
});
});

describe('transpose', () => {
Expand Down
88 changes: 88 additions & 0 deletions __tests__/unit/plots/funnel/conversion-tag-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Funnel } from '../../../../src';
import { PV_DATA, PV_DATA_COMPARE } from '../../../data/conversion';
import { createDiv } from '../../../utils/dom';
import { FUNNEL_CONVERSATION, FUNNEL_PERCENT } from '../../../../src/plots/funnel/constant';

describe('conversition tag', () => {
test('conversition tag: basic & dynamicHeight', () => {
const funnelOption = {
width: 400,
height: 400,
data: PV_DATA,
xField: 'action',
yField: 'pv',
};
const funnel = new Funnel(createDiv('conversition tag: basic & dynamicHeight'), funnelOption);
funnel.render();

const annotation = funnel.chart.getController('annotation').getComponents();
expect(annotation.length).toEqual(4);
PV_DATA.forEach((pvItem, index) => {
if (index === 0) return;
const content = annotation[index - 1].component.get('text').content;
expect(content).toBe(`转化率${(pvItem[FUNNEL_CONVERSATION] * 100).toFixed(2)}%`);
});

// 自定义 label
funnel.update({
...funnelOption,
dynamicHeight: true,
conversionTag: {
offsetX: 50,
offsetY: 20,
style: {
fontSize: 18,
},
formatter: (datum) => `${datum.$$percentage$$}占比`,
},
});

const customAnnotation = funnel.chart.getController('annotation').getComponents();
expect(customAnnotation.length).toEqual(4);
PV_DATA.forEach((pvItem, index) => {
if (index === 0) return;
const text = customAnnotation[index - 1].component.get('text');
expect(text.content).toBe(`${pvItem[FUNNEL_PERCENT]}占比`);
expect(text.offsetX).toBe(50);
expect(text.offsetY).toBe(20);
expect(text.style.fontSize).toBe(18);
});

// 关闭转化率组件
funnel.update({
...funnelOption,
conversionTag: false,
});
expect(funnel.chart.getController('annotation').getComponents().length).toBe(0);

funnel.destroy();
});

test('conversation compare', () => {
// 自定义 label
const funnel = new Funnel(createDiv('conversation compare'), {
width: 400,
height: 400,
data: PV_DATA_COMPARE,
compareField: 'quarter',
xField: 'action',
yField: 'pv',
});
funnel.render();

funnel.chart.views.forEach((funnelView) => {
const { data } = funnelView.getOptions();
const annotation = funnelView.getController('annotation').getComponents();
expect(annotation.length).toEqual(5);
expect(annotation[0].component.cfg.content).toBe(data[0].quarter);
data.forEach((pvItem, index) => {
if (index === 0) return;
expect(annotation[index].component.get('text').content).toBe(
`转化率${(pvItem[FUNNEL_CONVERSATION] * 100).toFixed(2)}%`
);
});
});

funnel.destroy();
});
});
Loading