import React, { Fragment } from 'react';
import { render } from '@testing-library/react';
import 'jest-styled-components';
import { Grommet } from '../../Grommet';
import { Box } from '../../Box';
import { Text } from '../../Text';
import { DataChart, DataChartProps } from '..';
import { ChartProps } from '../../Chart';
const data = [
{ a: 1, b: 'one', c: 111111, d: '2020-06-24' },
{ a: 2, b: 'two', c: 222222, d: '2020-06-23' },
];
describe('DataChart', () => {
let warnSpy: jest.SpyInstance;
beforeEach(() => {
warnSpy = jest.spyOn(console, 'warn').mockImplementation();
});
afterEach(() => {
warnSpy.mockRestore();
});
test('default', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('nothing', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('single', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('gap', () => {
const { container } = render(
{['small', 'medium', 'large'].map((gap) => (
))}
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('pad', () => {
const { container } = render(
{['small', 'medium', 'large'].map((pad) => (
))}
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('size', () => {
const { container } = render(
{['fill', { width: 'fill' }, { width: 'auto' }].map((size, i) => (
// eslint-disable-next-line react/no-array-index-key
))}
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('axis', () => {
const values: DataChartProps['axis'][] = [
true,
false,
{ x: { property: 'd' } },
{ y: { property: 'a' } },
{ x: { property: 'd', granularity: 'fine' } },
{ y: { property: 'a', granularity: 'fine' } },
];
const { container } = render(
{values.map((axis, i) => (
// eslint-disable-next-line react/no-array-index-key
))}
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('dates', () => {
const dateData: DataChartProps['data'] = [];
for (let i = 0; i < 4; i += 1) {
const digits = ((i % 12) + 1).toString().padStart(2, '0');
dateData.push({
second: `2020-05-15T08:04:${digits}`,
minute: `2020-05-15T08:${digits}:00`,
hour: `2020-05-15T${digits}:00:00`,
day: `2020-05-${digits}T08:00:00`,
month: `2020-${digits}-15`,
year: `20${digits}-01-15`,
percent: Math.abs(i * 10),
amount: i * 111111,
});
}
const { container } = render(
{['second', 'minute', 'hour', 'day', 'month', 'year'].map((key) => (
))}
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('guide', () => {
const values: DataChartProps['guide'][] = [
true,
false,
{ x: { granularity: 'fine' } },
{ y: { granularity: 'fine' } },
];
const { container } = render(
{values.map((guide, i) => (
// eslint-disable-next-line react/no-array-index-key
))}
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('legend', () => {
const { container } = render(
{[true, false].map((legend, i) => (
// eslint-disable-next-line react/no-array-index-key
))}
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('detail', () => {
const { container } = render(
{[true, false].map((detail, i) => (
// eslint-disable-next-line react/no-array-index-key
))}
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('detail pad + thickness', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('axis x granularity', () => {
const { container } = render(
{[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13].map((count) => (
({ a: i }))}
series="a"
axis={{ x: { granularity: 'medium' } }}
/>
))}
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('type', () => {
const values: ChartProps['type'][] = ['bar', 'line', 'area'];
const { container } = render(
{values.map((type) => (
))}
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('bars', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('bars colors', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('bars invalid', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('bars empty', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('areas', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('lines', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('horizontal', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('offset', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('offset gap', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('bounds align', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('bounds explicit', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('negative values', () => {
const { container } = render(
{[undefined, 'coarse', 'medium', 'find'].map((granularity) => (
))}
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('placeholder text', () => {
const { container } = render(
({ d }))} // date only
series={['d', 'a']}
bounds={{ y: [0, 100] }}
placeholder="no data"
/>
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('placeholder node', () => {
const { container } = render(
({ d }))} // date only
series={['d', 'a']}
bounds={{ y: [0, 100] }}
placeholder={
no data
}
/>
,
);
expect(container.firstChild).toMatchSnapshot();
});
});