|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
| 4 | +import Build from '@pages/sites/$siteId/builds/Build'; |
| 5 | +import { createTestQueryClient } from '@support/queryClient'; |
| 6 | + |
| 7 | +// Mocking react-router-dom's <Link> component |
| 8 | +// to avoid rendering actual routing elements in tests. |
| 9 | +// This prevents warnings when <Link> is used outside a Router |
| 10 | +jest.mock('react-router-dom', () => { |
| 11 | + const original = jest.requireActual('react-router-dom'); |
| 12 | + return { |
| 13 | + ...original, |
| 14 | + Link: ({ to, children }) => { |
| 15 | + return `Link[to="${to}"] ${children}`; |
| 16 | + }, |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +describe('<Build />', () => { |
| 21 | + const props = { |
| 22 | + build: { state: '', id: 1 }, |
| 23 | + containerRef: { current: { offsetTop: 0 } }, |
| 24 | + latestForBranch: true, |
| 25 | + showBuildTasks: true, |
| 26 | + site: { id: 123 }, |
| 27 | + }; |
| 28 | + |
| 29 | + const createWrapper = createTestQueryClient(); |
| 30 | + |
| 31 | + function testValidBuild(container) { |
| 32 | + expect(container.querySelector('.build-info-prefix')).toHaveTextContent('#1'); |
| 33 | + expect(screen.queryByRole('button', { name: 'Rebuild' })).toBeInTheDocument(); |
| 34 | + expect(screen.getByText(/View build logs/)).toHaveTextContent( |
| 35 | + 'Link[to="/sites/123/builds/1/logs"]', |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + test('it renders successful build', () => { |
| 40 | + const { container } = render( |
| 41 | + <Build |
| 42 | + {...props} |
| 43 | + build={{ |
| 44 | + ...props.build, |
| 45 | + state: 'success', |
| 46 | + startedAt: 1770775800000, |
| 47 | + branch: 'main', |
| 48 | + }} |
| 49 | + />, |
| 50 | + { wrapper: createWrapper() }, |
| 51 | + ); |
| 52 | + testValidBuild(container); |
| 53 | + }); |
| 54 | + |
| 55 | + test('it renders failed build', () => { |
| 56 | + const { container } = render( |
| 57 | + <Build |
| 58 | + {...props} |
| 59 | + build={{ |
| 60 | + ...props.build, |
| 61 | + state: 'error', |
| 62 | + startedAt: 1770775800000, |
| 63 | + branch: 'main', |
| 64 | + }} |
| 65 | + />, |
| 66 | + { wrapper: createWrapper() }, |
| 67 | + ); |
| 68 | + testValidBuild(container); |
| 69 | + }); |
| 70 | + |
| 71 | + test('it renders invalid build', () => { |
| 72 | + const { container } = render( |
| 73 | + <Build |
| 74 | + {...props} |
| 75 | + build={{ ...props.build, state: 'invalid', branch: 'invalid branch ~!@#$%^&*()' }} |
| 76 | + />, |
| 77 | + { wrapper: createWrapper() }, |
| 78 | + ); |
| 79 | + expect(container.querySelector('.build-info-prefix')).toHaveTextContent('#1'); |
| 80 | + const logsLink = screen.getByText(/View build logs/); |
| 81 | + expect(logsLink).toHaveTextContent('Link[to="/sites/123/builds/1/logs"]'); |
| 82 | + expect(screen.queryByRole('button', { name: 'Rebuild' })).not.toBeInTheDocument(); |
| 83 | + const link = screen.getByRole('link', { name: /invalid branch ~!@#\$%\^&\*\(\)/i }); |
| 84 | + expect(link).toHaveAttribute('href', 'https://github.com/undefined/undefined/tree/invalid%20branch%20~!%40%23%24%25%5E%26*()'); |
| 85 | + expect(link).toHaveAttribute('title', 'View branch on GitHub'); |
| 86 | + }); |
| 87 | +}); |
0 commit comments