|
| 1 | +import React from 'react'; |
| 2 | +import { render, unmountComponentAtNode } from 'react-dom'; |
| 3 | +import ScanList from './ScanList'; |
| 4 | +import { act } from 'react-dom/test-utils'; |
| 5 | + |
| 6 | +describe('ScanList', () => { |
| 7 | + let container = null; |
| 8 | + beforeEach(() => { |
| 9 | + container = document.createElement('div'); |
| 10 | + document.body.appendChild(container); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + unmountComponentAtNode(container); |
| 15 | + container.remove(); |
| 16 | + container = null; |
| 17 | + }); |
| 18 | + |
| 19 | + it('displays all the element of the list', () => { |
| 20 | + const mockScans = [ |
| 21 | + { repositoryName: 'One', status: 'pending', queuedAt: '01/01/2020', findings: [] }, |
| 22 | + { repositoryName: 'Two', status: 'pending', queuedAt: '01/01/2020', findings: [] }, |
| 23 | + { repositoryName: 'Three', status: 'pending', queuedAt: '01/01/2020', findings: [] }, |
| 24 | + ]; |
| 25 | + |
| 26 | + act(() => { |
| 27 | + render(<ScanList list={mockScans} />, container); |
| 28 | + }); |
| 29 | + |
| 30 | + expect(container.querySelectorAll('.scan_item').length).toEqual(3); |
| 31 | + }); |
| 32 | + |
| 33 | + it('displays the correct timestamp depending on status', () => { |
| 34 | + const queuedAt = '01/01/2020'; |
| 35 | + const scanningAt = '02/01/2020'; |
| 36 | + const finishedAt = '03/01/2020'; |
| 37 | + const mockScans = [ |
| 38 | + { repositoryName: 'One', status: 'Pending', queuedAt, scanningAt, finishedAt, findings: [] }, |
| 39 | + { repositoryName: 'Two', status: 'In Progress', queuedAt, scanningAt, finishedAt, findings: [] }, |
| 40 | + { repositoryName: 'Three', status: 'Failure', queuedAt, scanningAt, finishedAt, findings: [] }, |
| 41 | + { repositoryName: 'Four', status: 'Success', queuedAt, scanningAt, finishedAt, findings: [] }, |
| 42 | + ]; |
| 43 | + |
| 44 | + act(() => { |
| 45 | + render(<ScanList list={mockScans} />, container); |
| 46 | + }); |
| 47 | + |
| 48 | + const [pending, scanning, failure, success] = container.querySelectorAll('.scan_item'); |
| 49 | + expect(pending.querySelector('.timestamp').textContent).toEqual(queuedAt); |
| 50 | + expect(scanning.querySelector('.timestamp').textContent).toEqual(scanningAt); |
| 51 | + expect(failure.querySelector('.timestamp').textContent).toEqual(finishedAt); |
| 52 | + expect(success.querySelector('.timestamp').textContent).toEqual(finishedAt); |
| 53 | + }); |
| 54 | + |
| 55 | + it('displays the Badges with a number of findings', () => { |
| 56 | + const mockFinding = { |
| 57 | + ruleId: 1, |
| 58 | + metadata: { description: 'description', severity: 'HIGH' }, |
| 59 | + location: { path: '/somwhere', positions: { begin: { line: 666 } } }, |
| 60 | + }; |
| 61 | + |
| 62 | + const finishedAt = '03/01/2020'; |
| 63 | + const mockScans = [ |
| 64 | + { repositoryName: 'one', status: 'Success', finishedAt, findings: [mockFinding] }, |
| 65 | + { repositoryName: 'two', status: 'Success', finishedAt, findings: [mockFinding, mockFinding] }, |
| 66 | + { |
| 67 | + repositoryName: 'three', |
| 68 | + status: 'Success', |
| 69 | + finishedAt, |
| 70 | + findings: [mockFinding, mockFinding, mockFinding], |
| 71 | + }, |
| 72 | + ]; |
| 73 | + |
| 74 | + act(() => { |
| 75 | + render(<ScanList list={mockScans} />, container); |
| 76 | + }); |
| 77 | + |
| 78 | + const [one, two, three] = container.querySelectorAll('.scan_item'); |
| 79 | + expect(one.querySelector('.findings').childNodes[0].textContent).toEqual('1'); |
| 80 | + expect(two.querySelector('.findings').childNodes[0].textContent).toEqual('2'); |
| 81 | + expect(three.querySelector('.findings').childNodes[0].textContent).toEqual('3'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('does not display the Badge when there is no findings', () => { |
| 85 | + const mockScans = [{ repositoryName: 'One', status: 'pending', queuedAt: '01/01/2020', findings: [] }]; |
| 86 | + |
| 87 | + act(() => { |
| 88 | + render(<ScanList list={mockScans} />, container); |
| 89 | + }); |
| 90 | + |
| 91 | + const scan = container.querySelector('.scan_item'); |
| 92 | + expect(scan.querySelector('.findings').childNodes.length).toEqual(0); |
| 93 | + expect(scan.querySelector('.findings').textContent).toEqual(''); |
| 94 | + }); |
| 95 | +}); |
0 commit comments