|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
| 4 | +import { MemoryRouter } from 'react-router-dom'; |
| 5 | +import { createFixtureSite } from '../../../test/frontend/support/data/sites'; |
| 6 | +import { createFixtureOrg } from '../../../test/frontend/support/data/organizations'; |
| 7 | +import * as datetimeUtils from '@util/datetime'; |
| 8 | + |
| 9 | +import SiteListItem from './SiteListItem'; |
| 10 | +import GitHubLink from '@shared/GitHubLink'; |
| 11 | + |
| 12 | +const mockSite = createFixtureSite({ name: 'test-site' }); |
| 13 | +const mockOrganization = createFixtureOrg({ name: 'test-org' }); |
| 14 | + |
| 15 | +jest.mock('@shared/GitHubLink', () => { |
| 16 | + return jest.fn(() => null); |
| 17 | +}); |
| 18 | + |
| 19 | +describe('<SiteListItem />', () => { |
| 20 | + it('renders the organization name in a heading element', () => { |
| 21 | + render( |
| 22 | + /* MemoryRouter allows us to render components that use react Link */ |
| 23 | + <MemoryRouter> |
| 24 | + <SiteListItem organization={mockOrganization} site={mockSite} /> |
| 25 | + </MemoryRouter>, |
| 26 | + ); |
| 27 | + const orgName = screen.getByRole('heading', { |
| 28 | + name: `organization - ${mockOrganization.name}`, |
| 29 | + }); |
| 30 | + expect(orgName).toBeInTheDocument(); |
| 31 | + }); |
| 32 | + |
| 33 | + // eslint-disable-next-line max-len |
| 34 | + it('renders the site name by concatenating the site owner and repo name in a heading element', () => { |
| 35 | + render( |
| 36 | + /* MemoryRouter allows us to render components that use react Link */ |
| 37 | + <MemoryRouter> |
| 38 | + <SiteListItem organization={mockOrganization} site={mockSite} /> |
| 39 | + </MemoryRouter>, |
| 40 | + ); |
| 41 | + const combinedName = `${mockSite.owner}/${mockSite.repository}`; |
| 42 | + const text = screen.getByText(combinedName); |
| 43 | + expect(text).toBeInTheDocument(); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('renders the last published state', () => { |
| 47 | + let dateAndTimeSpy; |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + dateAndTimeSpy = jest.spyOn(datetimeUtils, 'dateAndTime'); |
| 51 | + }); |
| 52 | + afterEach(() => { |
| 53 | + dateAndTimeSpy.mockRestore(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('renders the "Last published on" message when publishedAt exists', () => { |
| 57 | + const formattedDate = datetimeUtils.dateAndTime(mockSite.publishedAt); |
| 58 | + |
| 59 | + render( |
| 60 | + <MemoryRouter> |
| 61 | + <SiteListItem organization={mockOrganization} site={mockSite} /> |
| 62 | + </MemoryRouter>, |
| 63 | + ); |
| 64 | + |
| 65 | + const message = screen.getByText(`Last published on ${formattedDate}`); |
| 66 | + expect(message).toBeInTheDocument(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('displays "Please wait" if publishedAt is null', () => { |
| 70 | + const unPublishedSite = { |
| 71 | + ...mockSite, |
| 72 | + publishedAt: null, |
| 73 | + }; |
| 74 | + render( |
| 75 | + <MemoryRouter> |
| 76 | + <SiteListItem organization={mockOrganization} site={unPublishedSite} /> |
| 77 | + </MemoryRouter>, |
| 78 | + ); |
| 79 | + const message = screen.getByText( |
| 80 | + 'Please wait for build to complete or check logs for error message.', |
| 81 | + ); |
| 82 | + expect(message).toBeInTheDocument(); |
| 83 | + expect(dateAndTimeSpy).not.toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('renders a button link to the GitHub repo for this site', () => { |
| 88 | + render( |
| 89 | + <MemoryRouter> |
| 90 | + <SiteListItem organization={mockOrganization} site={mockSite} /> |
| 91 | + </MemoryRouter>, |
| 92 | + ); |
| 93 | + expect(GitHubLink).toHaveBeenCalledWith( |
| 94 | + { |
| 95 | + owner: mockSite.owner, |
| 96 | + repository: mockSite.repository, |
| 97 | + text: 'View repo', |
| 98 | + isButton: true, |
| 99 | + }, |
| 100 | + expect.anything(), // ref |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it('renders sandbox message when organization is a sandbox', () => { |
| 105 | + const sandboxOrg = { |
| 106 | + ...mockOrganization, |
| 107 | + isSandbox: true, |
| 108 | + daysUntilSandboxCleaning: 7, |
| 109 | + }; |
| 110 | + render( |
| 111 | + <MemoryRouter> |
| 112 | + <SiteListItem organization={sandboxOrg} site={mockSite} /> |
| 113 | + </MemoryRouter>, |
| 114 | + ); |
| 115 | + const sandboxMsg = screen.getByText((content) => |
| 116 | + content.includes( |
| 117 | + // eslint-disable-next-line max-len |
| 118 | + `All site data for this sandbox organization will be removed in ${sandboxOrg.daysUntilSandboxCleaning} days`, |
| 119 | + ), |
| 120 | + ); |
| 121 | + expect(sandboxMsg).toBeInTheDocument(); |
| 122 | + }); |
| 123 | + |
| 124 | + // eslint-disable-next-line max-len |
| 125 | + it('renders the site name as a link to the site’s build history if site and org are active', () => { |
| 126 | + render( |
| 127 | + <MemoryRouter> |
| 128 | + <SiteListItem organization={mockOrganization} site={mockSite} /> |
| 129 | + </MemoryRouter>, |
| 130 | + ); |
| 131 | + const link = screen.getByRole('link', { |
| 132 | + name: `${mockSite.owner}/${mockSite.repository}`, |
| 133 | + }); |
| 134 | + expect(link).toHaveAttribute('href', `/sites/${mockSite.id}/builds`); |
| 135 | + expect(link).toHaveAttribute('title', `View site builds`); |
| 136 | + }); |
| 137 | + |
| 138 | + // eslint-disable-next-line max-len |
| 139 | + it('renders the site name as text with "(Inactive)" instead of a link if org is inactive', () => { |
| 140 | + const inactiveOrg = { ...mockOrganization, isActive: false }; |
| 141 | + render(<SiteListItem organization={inactiveOrg} site={mockSite} />); |
| 142 | + const text = screen.getByText(`${mockSite.owner}/${mockSite.repository} (Inactive)`); |
| 143 | + const link = screen.queryByRole('link', { |
| 144 | + name: `${mockSite.owner}/${mockSite.repository}`, |
| 145 | + }); |
| 146 | + expect(link).not.toBeInTheDocument(); |
| 147 | + expect(text).toBeInTheDocument(); |
| 148 | + }); |
| 149 | + |
| 150 | + // eslint-disable-next-line max-len |
| 151 | + it('renders the site name as text with "(Inactive)" instead of a link if site is inactive', () => { |
| 152 | + const inactiveSite = { ...mockSite, isActive: false }; |
| 153 | + render(<SiteListItem organization={mockOrganization} site={inactiveSite} />); |
| 154 | + const link = screen.queryByRole('link', { |
| 155 | + name: `${mockSite.owner}/${mockSite.repository}`, |
| 156 | + }); |
| 157 | + const text = screen.getByText( |
| 158 | + `${inactiveSite.owner}/${inactiveSite.repository} (Inactive)`, |
| 159 | + ); |
| 160 | + expect(link).not.toBeInTheDocument(); |
| 161 | + expect(text).toBeInTheDocument(); |
| 162 | + }); |
| 163 | +}); |
0 commit comments