Skip to content

Commit b5706e3

Browse files
committed
refactor test cases for fix codeclimate warnings
1 parent 4805702 commit b5706e3

3 files changed

Lines changed: 62 additions & 77 deletions

File tree

public/js/tests/advanced_parameters.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ describe('ADVANCED PARAMETERS', () => {
1515
const getInputElement = () => screen.getByRole('textbox', { name: '' });
1616
test('should not render the link to advanced parameters modal if blast algorithm is unknown', () => {
1717
setMockJSONResult(data);
18-
const {container } =render(<Form onSequenceTypeChanged={() => { }
18+
const {container } =render(<Form onSequenceTypeChanged={() => { }
1919
} />);
2020
const modalButton = container.querySelector('[data-target="#help"]');
2121
expect(modalButton).toBeNull();
2222
});
2323
test('should render the link to advanced parameters modal if blast algorithm is known', () => {
2424
setMockJSONResult(data);
25-
const {container } =render(<Form onSequenceTypeChanged={() => { }
25+
const {container } =render(<Form onSequenceTypeChanged={() => { }
2626
} />);
2727

2828
const inputEl = getInputElement();

public/js/tests/report.spec.js

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ const TestSidebar = ({ long }) => {
2525
/>;
2626
};
2727

28+
const clickCheckboxes = (checkboxes, count) => {
29+
Array.from(checkboxes).slice(0, count).forEach((checkbox) => {
30+
fireEvent.click(checkbox);
31+
});
32+
};
2833
describe('REPORT PAGE', () => {
2934
global.URL.createObjectURL = jest.fn();//.mockReturnValue('xyz.test');
3035
global.setTimeout = (cb) => cb();
@@ -65,18 +70,18 @@ describe('REPORT PAGE', () => {
6570
});
6671

6772
describe('LONG QUERIES (>12)', () => {
68-
73+
let container;
74+
beforeEach(() => {
75+
container = render(<TestSidebar long />).container;
76+
});
6977
it('should not show navigation links for long queries', () => {
70-
const { container } = render(<TestSidebar long />);
7178
expect(container.querySelectorAll('a[href^="#Query_"]').length).toBe(0);
7279
});
7380
it('should show only next button if on first query ', () => {
74-
render(<TestSidebar long />);
7581
expect(nextQueryButton()).toBeInTheDocument();
7682
expect(previousQueryButton()).not.toBeInTheDocument();
7783
});
7884
it('should show both previous and next buttons if not on first query', () => {
79-
render(<TestSidebar long />);
8085
const nextBtn = nextQueryButton();
8186
expect(nextBtn).toBeInTheDocument();
8287
fireEvent.click(nextBtn);
@@ -86,7 +91,6 @@ describe('REPORT PAGE', () => {
8691
});
8792
it('should show only previous button if on last query', () => {
8893
const { queries } = longResponseJSON;
89-
render(<TestSidebar long />);
9094
expect(nextQueryButton()).toBeInTheDocument();
9195
expect(previousQueryButton()).not.toBeInTheDocument();
9296

@@ -98,61 +102,52 @@ describe('REPORT PAGE', () => {
98102
});
99103
});
100104

101-
describe('ALIGNMENT DOWNLOAD', () => {
102-
it('should generate a blob url and filename for downloading alignment of all hits on render', () => {
103-
setMockJSONResult({ status: 200, responseJSON: shortResponseJSON });
104-
const { container } = render(<Report getCharacterWidth={jest.fn()} />);
105-
const alignment_download_link = container.querySelector('.download-alignment-of-all');
106-
const expected_num_hits = container.querySelectorAll('.hit-links input[type="checkbox"]').length;
107-
const file_name = `alignment-${expected_num_hits}_hits.txt`;
108-
expect(alignment_download_link.download).toEqual(file_name);
109-
expect(alignment_download_link.hred).not.toEqual('#');
110-
});
111-
it('link for downloading alignment of specific number of selected hits should be disabled on initial load', () => {
105+
describe('DOWNLOAD LINKS', () => {
106+
let container;
107+
beforeEach(() => {
112108
setMockJSONResult({ status: 200, responseJSON: shortResponseJSON });
113-
const { container } = render(<Report getCharacterWidth={jest.fn()} />);
114-
const alignment_download_link = container.querySelector('.download-alignment-of-selected');
115-
expect(alignment_download_link.classList.contains('disabled')).toBeTruthy();
116-
109+
container = render(<Report getCharacterWidth={jest.fn()} />).container;
117110
});
118-
it('should generate a blob url and filename for downloading alignment of specific number of selected hits', () => {
119-
setMockJSONResult({ status: 200, responseJSON: shortResponseJSON });
120-
const { container } = render(<Report getCharacterWidth={jest.fn()} />);
121-
const alignment_download_link = container.querySelector('.download-alignment-of-selected');
122-
123-
// QUERY ALL HIT LINKS CHECKBOXES
124-
const checkboxes = container.querySelectorAll('.hit-links input[type="checkbox"]');
125-
// SELECT 4 CHECKBOXES
126-
Array.from(checkboxes).slice(0, 4).forEach((checkbox) => {
127-
fireEvent.click(checkbox);
111+
describe('ALIGNMENT DOWNLOAD', () => {
112+
it('should generate a blob url and filename for downloading alignment of all hits on render', () => {
113+
const alignment_download_link = container.querySelector('.download-alignment-of-all');
114+
const expected_num_hits = container.querySelectorAll('.hit-links input[type="checkbox"]').length;
115+
const file_name = `alignment-${expected_num_hits}_hits.txt`;
116+
expect(alignment_download_link.download).toEqual(file_name);
117+
expect(alignment_download_link.hred).not.toEqual('#');
118+
});
119+
it('link for downloading alignment of specific number of selected hits should be disabled on initial load', () => {
120+
const alignment_download_link = container.querySelector('.download-alignment-of-selected');
121+
expect(alignment_download_link.classList.contains('disabled')).toBeTruthy();
122+
123+
});
124+
it('should generate a blob url and filename for downloading alignment of specific number of selected hits', () => {
125+
const alignment_download_link = container.querySelector('.download-alignment-of-selected');
126+
// QUERY ALL HIT LINKS CHECKBOXES
127+
const checkboxes = container.querySelectorAll('.hit-links input[type="checkbox"]');
128+
// SELECT 4 CHECKBOXES
129+
clickCheckboxes(checkboxes, 4);
130+
const file_name = 'alignment-4_hits.txt';
131+
expect(alignment_download_link.textContent).toEqual('Alignment of 4 selected hit(s)');
132+
expect(alignment_download_link.download).toEqual(file_name);
128133
});
129-
const file_name = 'alignment-4_hits.txt';
130-
expect(alignment_download_link.textContent).toEqual('Alignment of 4 selected hit(s)');
131-
expect(alignment_download_link.download).toEqual(file_name);
132-
});
133-
});
134-
135-
describe('FASTA DOWNLOAD', () => {
136-
it('link for downloading fasta of specific number of selected hits should be disabled on initial load', () => {
137-
setMockJSONResult({ status: 200, responseJSON: shortResponseJSON });
138-
const { container } = render(<Report getCharacterWidth={jest.fn()} />);
139-
const fasta_download_link = container.querySelector('.download-fasta-of-selected');
140-
expect(fasta_download_link.classList.contains('disabled')).toBeTruthy();
141-
142134
});
143-
144-
it('link for downloading fasta of specific number of selected hits should be active after selection', () => {
145-
setMockJSONResult({ status: 200, responseJSON: shortResponseJSON });
146-
const { container } = render(<Report getCharacterWidth={jest.fn()} />);
147-
const fasta_download_link = container.querySelector('.download-fasta-of-selected');
148-
149-
// QUERY ALL HIT LINKS CHECKBOXES
150-
const checkboxes = container.querySelectorAll('.hit-links input[type="checkbox"]');
151-
// SELECT 5 CHECKBOXES
152-
Array.from(checkboxes).slice(0, 5).forEach((checkbox) => {
153-
fireEvent.click(checkbox);
135+
136+
describe('FASTA DOWNLOAD', () => {
137+
let fasta_download_link;
138+
beforeEach(() => {
139+
fasta_download_link = container.querySelector('.download-fasta-of-selected');
140+
});
141+
it('link for downloading fasta of selected number of hits should be disabled on initial load', () => {
142+
expect(fasta_download_link.classList.contains('disabled')).toBeTruthy();
143+
});
144+
145+
it('link for downloading fasta of specific number of selected hits should be active after selection', () => {
146+
const checkboxes = container.querySelectorAll('.hit-links input[type="checkbox"]');
147+
// SELECT 5 CHECKBOXES
148+
clickCheckboxes(checkboxes, 5);
149+
expect(fasta_download_link.textContent).toEqual('FASTA of 5 selected hit(s)');
154150
});
155-
expect(fasta_download_link.textContent).toEqual('FASTA of 5 selected hit(s)');
156151
});
157152
});
158153
});

public/js/tests/search_query.spec.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,30 @@ import { AMINO_ACID_SEQUENCE, NUCLEOTIDE_SEQUENCE } from './mock_data/sequences'
77
import '@testing-library/jest-dom/extend-expect';
88
import '@testing-library/react/dont-cleanup-after-each';
99

10+
let container;
11+
let inputEl;
1012

1113
describe('SEARCH COMPONENT', () => {
12-
const getInputElement = () => screen.getByRole('textbox', { name: '' });
14+
beforeEach(() => {
15+
container = render(<Form onSequenceTypeChanged={() => { }
16+
} />).container;
17+
inputEl = screen.getByRole('textbox', { name: '' });
18+
});
19+
1320
test('should render the search component textarea', () => {
14-
render(<SearchQueryWidget onSequenceTypeChanged={() => { }
15-
} />);
16-
const el = getInputElement();
17-
expect(el).toHaveClass('form-control');
21+
expect(inputEl).toHaveClass('form-control');
1822
});
1923

2024
test('clear button should only become visible if textarea is not empty', () => {
21-
render(<SearchQueryWidget onSequenceTypeChanged={() => { }
22-
} />);
2325
const getButtonWrapper = () => screen.getByRole('button', { name: /clear query sequence\(s\)\./i }).parentElement;
2426
expect(getButtonWrapper()).toHaveClass('hidden');
25-
const inputEl = getInputElement();
2627
fireEvent.change(inputEl, { target: { value: AMINO_ACID_SEQUENCE } });
2728
expect(getButtonWrapper()).not.toHaveClass('hidden');
2829
fireEvent.change(inputEl, { target: { value: '' } });
2930
expect(getButtonWrapper()).toHaveClass('hidden');
30-
3131
});
3232

3333
test('should correctly detect the amino-acid sequence type and show notification', () => {
34-
const { container } = render(<Form onSequenceTypeChanged={() => { }
35-
} />);
36-
const inputEl = getInputElement();
3734
// populate search
3835
fireEvent.change(inputEl, { target: { value: AMINO_ACID_SEQUENCE } });
3936
const activeNotification = container.querySelector('.notification.active');
@@ -43,22 +40,15 @@ describe('SEARCH COMPONENT', () => {
4340
});
4441

4542
test('should correctly detect the nucleotide sequence type and show notification', () => {
46-
const { container } = render(<Form onSequenceTypeChanged={() => { }
47-
} />);
48-
const inputEl = getInputElement();
4943
// populate search
5044
fireEvent.change(inputEl, { target: { value: NUCLEOTIDE_SEQUENCE } });
5145
const activeNotification = container.querySelector('.notification.active');
52-
expect(activeNotification.id).toBe('nucleotide-sequence-notification');
5346
const alertWrapper = activeNotification.children[0];
47+
expect(activeNotification.id).toBe('nucleotide-sequence-notification');
5448
expect(alertWrapper).toHaveTextContent('Detected: nucleotide sequence(s).');
5549
});
5650

5751
test('should correctly detect the mixed sequences and show error notification', () => {
58-
const { container } = render(<Form onSequenceTypeChanged={() => { }
59-
} />);
60-
const inputEl = getInputElement();
61-
// populate search
6252
fireEvent.change(inputEl, { target: { value: `${NUCLEOTIDE_SEQUENCE}${AMINO_ACID_SEQUENCE}` } });
6353
const activeNotification = container.querySelector('.notification.active');
6454
expect(activeNotification.id).toBe('mixed-sequence-notification');

0 commit comments

Comments
 (0)