Passed
Pull Request — main (#343)
by Alejandro
03:59
created

test/servers/Overview.test.tsx   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 80
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 70
mnd 1
bc 1
fnc 0
dl 0
loc 80
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import { shallow, ShallowWrapper } from 'enzyme';
2
import { Mock } from 'ts-mockery';
3
import { CardText } from 'reactstrap';
4
import { Link } from 'react-router-dom';
5
import { ShortUrlsList as ShortUrlsListState } from '../../src/short-urls/reducers/shortUrlsList';
6
import { Overview as overviewCreator } from '../../src/servers/Overview';
7
import { TagsList } from '../../src/tags/reducers/tagsList';
8
import { VisitsOverview } from '../../src/visits/reducers/visitsOverview';
9
import { MercureInfo } from '../../src/mercure/reducers/mercureInfo';
10
import { ReachableServer } from '../../src/servers/data';
11
import { prettify } from '../../src/utils/helpers/numbers';
12
13
describe('<Overview />', () => {
14
  let wrapper: ShallowWrapper;
15
  const ShortUrlsTable = () => null;
16
  const CreateShortUrl = () => null;
17
  const listShortUrls = jest.fn();
18
  const listTags = jest.fn();
19
  const loadVisitsOverview = jest.fn();
20
  const Overview = overviewCreator(ShortUrlsTable, CreateShortUrl);
21
  const shortUrls = {
22
    pagination: { totalItems: 83710 },
23
  };
24
  const serverId = '123';
25
  const createWrapper = (loading = false) => {
26
    wrapper = shallow(
27
      <Overview
28
        listShortUrls={listShortUrls}
29
        listTags={listTags}
30
        loadVisitsOverview={loadVisitsOverview}
31
        shortUrlsList={Mock.of<ShortUrlsListState>({ loading, shortUrls })}
32
        tagsList={Mock.of<TagsList>({ loading, tags: [ 'foo', 'bar', 'baz' ] })}
33
        visitsOverview={Mock.of<VisitsOverview>({ loading, visitsCount: 3456 })}
34
        selectedServer={Mock.of<ReachableServer>({ id: serverId })}
35
        createNewVisits={jest.fn()}
36
        loadMercureInfo={jest.fn()}
37
        mercureInfo={Mock.all<MercureInfo>()}
38
      />,
39
    ).dive(); // Dive is needed as this component is wrapped in a HOC
40
41
    return wrapper;
42
  };
43
44
  afterEach(() => wrapper?.unmount());
45
46
  test('cards display loading messages when still loading', () => {
47
    const wrapper = createWrapper(true);
48
    const cards = wrapper.find(CardText);
49
50
    expect(cards).toHaveLength(3);
51
    cards.forEach((card) => expect(card.html()).toContain('Loading...'));
52
  });
53
54
  test('amounts are displayed in cards after finishing loading', () => {
55
    const wrapper = createWrapper();
56
    const cards = wrapper.find(CardText);
57
58
    expect(cards).toHaveLength(3);
59
    expect(cards.at(0).html()).toContain(prettify(3456));
60
    expect(cards.at(1).html()).toContain(prettify(83710));
61
    expect(cards.at(2).html()).toContain(prettify(3));
62
  });
63
64
  test('nests complex components', () => {
65
    const wrapper = createWrapper();
66
67
    expect(wrapper.find(CreateShortUrl)).toHaveLength(1);
68
    expect(wrapper.find(ShortUrlsTable)).toHaveLength(1);
69
  });
70
71
  test('links to other sections are displayed', () => {
72
    const wrapper = createWrapper();
73
    const links = wrapper.find(Link);
74
75
    expect(links).toHaveLength(2);
76
    expect(links.at(0).prop('to')).toEqual(`/server/${serverId}/create-short-url`);
77
    expect(links.at(1).prop('to')).toEqual(`/server/${serverId}/list-short-urls/1`);
78
  });
79
});
80