1import React from 'react';
2import { mount } from 'enzyme';
3import moment from 'moment';
4import { BlockDetails, BlockDetailsProps } from './BlockDetails';
5import { sampleAPIResponse } from './__testdata__/testdata';
6
7const sampleBlock = sampleAPIResponse.data.blocks[0];
8const formatTime = (time: number): string => {
9  return moment.unix(time / 1000).format('LLL');
10};
11
12describe('BlockDetails', () => {
13  const defaultProps: BlockDetailsProps = {
14    block: sampleBlock,
15    selectBlock: (): void => {
16      // do nothing
17    },
18  };
19  window.URL.createObjectURL = jest.fn();
20  const blockDetails = mount(<BlockDetails {...defaultProps} />);
21
22  it('renders a heading with block ulid', () => {
23    const title = blockDetails.find({ 'data-testid': 'ulid' });
24    expect(title).toHaveLength(1);
25    expect(title.text()).toEqual(sampleBlock.ulid);
26  });
27
28  it('renders start time of the block', () => {
29    const div = blockDetails.find({ 'data-testid': 'start-time' });
30    expect(div).toHaveLength(1);
31    expect(div.find('span').text()).toBe(formatTime(sampleBlock.minTime));
32  });
33
34  it('renders end time of the block', () => {
35    const div = blockDetails.find({ 'data-testid': 'end-time' });
36    expect(div).toHaveLength(1);
37    expect(div.find('span').text()).toBe(formatTime(sampleBlock.maxTime));
38  });
39
40  it('renders duration of the block', () => {
41    const div = blockDetails.find({ 'data-testid': 'duration' });
42    expect(div).toHaveLength(1);
43    expect(div.find('span').text()).toBe(moment.duration(sampleBlock.maxTime - sampleBlock.minTime, 'ms').humanize());
44  });
45
46  it('renders total number of series in the block', () => {
47    const div = blockDetails.find({ 'data-testid': 'series' });
48    expect(div).toHaveLength(1);
49    expect(div.find('span').text()).toBe(sampleBlock.stats.numSeries.toString());
50  });
51
52  it('renders total number of samples in the block', () => {
53    const div = blockDetails.find({ 'data-testid': 'samples' });
54    expect(div).toHaveLength(1);
55    expect(div.find('span').text()).toBe(sampleBlock.stats.numSamples.toString());
56  });
57
58  it('renders total number of chunks in the block', () => {
59    const div = blockDetails.find({ 'data-testid': 'chunks' });
60    expect(div).toHaveLength(1);
61    expect(div.find('span').text()).toBe(sampleBlock.stats.numChunks.toString());
62  });
63
64  it('renders downsampling resolution of the block', () => {
65    const div = blockDetails.find({ 'data-testid': 'resolution' });
66    expect(div).toHaveLength(1);
67    expect(div.find('span').text()).toBe(sampleBlock.thanos.downsample.resolution.toString());
68  });
69
70  it('renders compaction level of the block', () => {
71    const div = blockDetails.find({ 'data-testid': 'level' });
72    expect(div).toHaveLength(1);
73    expect(div.find('span').text()).toBe(sampleBlock.compaction.level.toString());
74  });
75
76  it('renders source of the block', () => {
77    const div = blockDetails.find({ 'data-testid': 'source' });
78    expect(div).toHaveLength(1);
79    expect(div.find('span').text()).toBe(sampleBlock.thanos.source);
80  });
81
82  it('renders the download button', () => {
83    const div = blockDetails.find({ 'data-testid': 'download' });
84    window.URL.createObjectURL = jest.fn(() => 'details');
85    expect(div).toHaveLength(1);
86    expect(div.find('a').text()).toBe('Download meta.json');
87  });
88
89  it('renders a list of the labels', () => {
90    const div = blockDetails.find({ 'data-testid': 'labels' });
91    const list = div.find('ul');
92    expect(div).toHaveLength(1);
93    expect(list).toHaveLength(1);
94
95    const labels = list.find('li');
96    expect(labels).toHaveLength(Object.keys(sampleBlock.thanos.labels).length);
97  });
98});
99