SpeciesSelectorAppBar.test.tsx 2.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Andrey Azov's avatar
Andrey Azov committed
17
import React from 'react';
18 19
import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
Andrey Azov's avatar
Andrey Azov committed
20
import times from 'lodash/times';
21 22 23
import { push } from 'connected-react-router';

import * as urlFor from 'src/shared/helpers/urlHelper';
Andrey Azov's avatar
Andrey Azov committed
24 25 26 27 28

import { createSelectedSpecies } from 'tests/fixtures/selected-species';

import {
  SpeciesSelectorAppBar,
29
  placeholderMessage
Andrey Azov's avatar
Andrey Azov committed
30 31
} from './SpeciesSelectorAppBar';

32 33 34 35
jest.mock('react-router-dom', () => ({
  Link: (props: any) => <div>{props.children}</div>
}));

36 37 38
jest.mock('connected-react-router', () => ({
  push: jest.fn(() => ({ type: 'push' }))
}));
Andrey Azov's avatar
Andrey Azov committed
39

40 41 42 43 44
jest.mock(
  'ensemblRoot/src/shared/components/communication-framework/ConversationIcon',
  () => () => <div>ConversationIcon</div>
);

Andrey Azov's avatar
Andrey Azov committed
45 46
const defaultProps = {
  selectedSpecies: times(5, () => createSelectedSpecies()),
47
  push
Andrey Azov's avatar
Andrey Azov committed
48 49 50 51 52 53 54 55 56
};

describe('<SpeciesSelectorAppBar />', () => {
  afterEach(() => {
    jest.resetAllMocks();
  });

  it('shows placeholder message if no species are selected', () => {
    const props = { ...defaultProps, selectedSpecies: [] };
57 58 59 60
    const { container } = render(<SpeciesSelectorAppBar {...props} />);
    expect(container.querySelector('.placeholderMessage')?.textContent).toBe(
      placeholderMessage
    );
Andrey Azov's avatar
Andrey Azov committed
61 62 63
  });

  it('does not show placeholder message if there are selected species', () => {
64 65
    const { container } = render(<SpeciesSelectorAppBar {...defaultProps} />);
    expect(container.querySelector('.placeholderMessage')).toBeFalsy();
Andrey Azov's avatar
Andrey Azov committed
66 67 68
  });

  it('renders the list of selected species if there are some', () => {
69
    const { container } = render(<SpeciesSelectorAppBar {...defaultProps} />);
70

71
    expect(container.querySelectorAll('.species').length).toBe(
Andrey Azov's avatar
Andrey Azov committed
72 73 74
      defaultProps.selectedSpecies.length
    );
  });
75 76

  it('opens the species page when a SelectedSpecies tab button is clicked', () => {
77 78 79 80
    const { container } = render(<SpeciesSelectorAppBar {...defaultProps} />);
    const firstSelectedSpecies = container.querySelector(
      '.species'
    ) as HTMLElement;
81

82
    userEvent.click(firstSelectedSpecies);
83 84 85 86 87 88 89 90

    const firstSpeciesGenomeId = defaultProps.selectedSpecies[0].genome_id;
    const speciesPageUrl = urlFor.speciesPage({
      genomeId: firstSpeciesGenomeId
    });

    expect(push).toBeCalledWith(speciesPageUrl);
  });
Andrey Azov's avatar
Andrey Azov committed
91
});