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

test/short-urls/ShortUrlsTable.test.tsx   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 60
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 49
mnd 1
bc 1
fnc 0
dl 0
loc 60
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 { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
4
import { ShortUrlsTable as shortUrlsTableCreator } from '../../src/short-urls/ShortUrlsTable';
5
import { SORTABLE_FIELDS } from '../../src/short-urls/reducers/shortUrlsListParams';
6
import { ShortUrlsList } from '../../src/short-urls/reducers/shortUrlsList';
7
8
describe('<ShortUrlsTable />', () => {
9
  let wrapper: ShallowWrapper;
10
  const shortUrlsList = Mock.all<ShortUrlsList>();
11
  const orderByColumn = jest.fn();
12
  const ShortUrlsRow = () => null;
13
14
  const ShortUrlsTable = shortUrlsTableCreator(ShortUrlsRow);
15
16
  beforeEach(() => {
17
    wrapper = shallow(
18
      <ShortUrlsTable shortUrlsList={shortUrlsList} selectedServer={null} orderByColumn={() => orderByColumn} />,
19
    );
20
  });
21
22
  afterEach(jest.resetAllMocks);
23
  afterEach(() => wrapper?.unmount());
24
25
  it('should render inner table by default', () => {
26
    expect(wrapper.find('table')).toHaveLength(1);
27
  });
28
29
  it('should render table header by default', () => {
30
    expect(wrapper.find('table').find('thead')).toHaveLength(1);
31
  });
32
33
  it('should render 6 table header cells by default', () => {
34
    expect(wrapper.find('table').find('thead').find('tr').find('th')).toHaveLength(6);
35
  });
36
37
  it('should render 6 table header cells without order by icon by default', () => {
38
    const thElements = wrapper.find('table').find('thead').find('tr').find('th');
39
40
    thElements.forEach((thElement) => {
41
      expect(thElement.find(FontAwesomeIcon)).toHaveLength(0);
42
    });
43
  });
44
45
  it('should render 6 table header cells with conditional order by icon', () => {
46
    const getThElementForSortableField = (sortableField: string) => wrapper.find('table')
47
      .find('thead')
48
      .find('tr')
49
      .find('th')
50
      .filterWhere((e) => e.text().includes(SORTABLE_FIELDS[sortableField as keyof typeof SORTABLE_FIELDS]));
51
    const sortableFields = Object.keys(SORTABLE_FIELDS);
52
53
    expect.assertions(sortableFields.length);
54
    sortableFields.forEach((sortableField) => {
55
      getThElementForSortableField(sortableField).simulate('click');
56
      expect(orderByColumn).toHaveBeenCalled();
57
    });
58
  });
59
});
60