Skip to content

Commit

Permalink
additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmst committed Mar 25, 2024
1 parent 228ba6d commit abf4bf9
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 101 deletions.
9 changes: 9 additions & 0 deletions src/data/queries/tests/__snapshots__/banners.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ exports[`Banners return formatted data outputs formatted data 1`] = `
},
{
"alertType": "warning",
"bannerAlertVamcs": [
{
"id": 2363,
"office": {
"path": null,
},
"path": "/butler-health-care/operating-status",
},
],
"body": "<p>this is a full width banner alert</p>",
"dismiss": true,
"emailUpdatesButton": true,
Expand Down
84 changes: 0 additions & 84 deletions src/data/queries/tests/__snapshots__/eventTeaser.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,89 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`alert non reusable formatData outputs formatted data 1`] = `
{
"changed": "2023-08-16T15:20:30+00:00",
"entityBundle": "node--event",
"entityId": "16349f16-be65-46e3-9660-1d3d598a4a0b",
"entityPublished": true,
"entityUrl": {
"path": "/central-iowa-health-care/events/52265",
},
"fieldAdditionalInformationAbo": null,
"fieldAdditionalListings": null,
"fieldAddress": {
"addressLine1": "9000 Douglas Ave",
"addressLine2": null,
"administrativeArea": "IA",
"countryCode": "US",
"locality": "Urbandale",
"postalCode": null,
},
"fieldAdministration": {
"entity": {
"entityId": "3a530a55-e922-48de-b18c-9c9f92a9ae98",
},
},
"fieldBody": {
"format": "rich_text",
"processed": "<p>Pickleball Club</p>
<p>Meets Thursdays from 9 to 11 a.m. </p>
<p>Contact Kay Queck (515) 214-4578</p>",
"value": "<p>Pickleball Club</p>
<p>Meets Thursdays from 9 to 11 a.m.&nbsp;</p>
<p>Contact Kay Queck (515) 214-4578</p>
",
},
"fieldCtaEmail": "[email protected]",
"fieldDatetimeRangeTimezone": [
{
"duration": 120,
"endTime": "2023-09-07T16:00:00+00:00",
"endValue": 1694102400,
"end_value": "2023-09-07T16:00:00+00:00",
"rrule": 180,
"rrule_index": 1,
"startTime": "2023-09-07T14:00:00+00:00",
"timezone": "America/Chicago",
"value": 1694095200,
},
{
"duration": 120,
"endTime": "2023-09-14T16:00:00+00:00",
"endValue": 1694707200,
"end_value": "2023-09-14T16:00:00+00:00",
"rrule": 180,
"rrule_index": 2,
"startTime": "2023-09-14T14:00:00+00:00",
"timezone": "America/Chicago",
"value": 1694700000,
},
],
"fieldDescription": "Pickleball ",
"fieldEventCost": "Free",
"fieldEventCta": null,
"fieldEventRegistrationrequired": false,
"fieldFacilityLocation": null,
"fieldFeatured": false,
"fieldHowToSignUp": "email",
"fieldLink": null,
"fieldListing": {
"entity": {
"entityId": "55cb823c-df6d-4f77-814d-6024e08cd4f6",
},
},
"fieldLocationHumanreadable": "Walker Johnston Park",
"fieldLocationType": "non_facility",
"fieldOrder": null,
"fieldUrlOfAnOnlineEvent": null,
"title": "Pickleball Club",
}
`;

exports[`event teaser formatData outputs formatted data 1`] = `
{
"changed": "2023-08-16T15:20:30+00:00",
Expand Down
17 changes: 0 additions & 17 deletions src/data/queries/tests/__snapshots__/healthServices.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`alert single formatData outputs formatted data 1`] = `
[
{
"alsoKnownAs": null,
"body": "<p>Military sexual trauma can happen to both men and women. If you experienced sexual assault or harassment during military service - no matter when you served – we provide counseling and treatment.</p>
",
"commonlyTreatedCondition": null,
"description": "Military sexual trauma can happen to both genders. If you experienced sexual assault or harassment during military service—no matter when you served—we provide counseling and treatment.",
"name": "Military sexual trauma care",
"vetCenterComConditions": null,
"vetCenterFriendlyName": null,
"vetCenterServiceDescription": "If you experienced sexual assault or harassment during military service, we can help you get the counseling you need. Any Veteran or service member, including members of the National Guard and Reserve forces, who experienced military sexual trauma is eligible to receive counseling. This applies to people of all genders from any service era.",
"vetCenterTypeOfCare": "counseling",
},
]
`;

exports[`healthServices formatData outputs formatted data 1`] = `
[
{
Expand Down
26 changes: 26 additions & 0 deletions src/templates/common/commonAndPopular/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { render, screen } from '@testing-library/react'
import { CommonAndPopular } from './'

describe('CommonAndPopular Component', () => {
test('renders common and popular links correctly', () => {
render(<CommonAndPopular />)

const commonQuestionsLinks = screen.getAllByRole('link', {
name: /how do i /i,
})
expect(commonQuestionsLinks).toHaveLength(3)
expect(commonQuestionsLinks[0]).toHaveAttribute(
'href',
expect.stringMatching(/\/health-care\/how-to-apply\/?/)
)

const popularLinks = screen.getAllByRole('link', {
name: /view|find nearby va locations|contact the veterans crisis line/i,
})
expect(popularLinks).toHaveLength(3)
expect(popularLinks[0]).toHaveAttribute(
'href',
expect.stringMatching(/\/find-locations\/?/)
)
})
})
49 changes: 49 additions & 0 deletions src/templates/common/contentFooter/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { render, screen, fireEvent } from '@testing-library/react'
import { ContentFooter } from './'

// Mocked dependencies
jest.mock('@/templates/common/medallia', () => ({
MedalliaAssets: jest.fn().mockReturnValue(null),
}))
jest.mock('@/lib/utils/medallia', () => ({
getSurveyNumber: jest.fn((pathname, isProduction) => {
return isProduction ? 'mockedSurveyNumber' : null
}),
showForm: jest.fn(),
}))
jest.mock('@/lib/utils/date', () => ({
parseDate: jest.fn().mockReturnValue(new Date('2022-03-28')),
getDateParts: jest.fn().mockReturnValue({
month: { name: 'March', numeric: 3, twoDigit: '03' },
day: { numeric: 28, twoDigit: '28' },
year: { numeric: 2022 },
}),
}))

describe('ContentFooter Component', () => {
test('renders last updated date correctly', () => {
const lastUpdated = '2022-03-28'
render(<ContentFooter lastUpdated={lastUpdated} />)

expect(screen.getByText('Last updated:')).toBeInTheDocument()
expect(screen.getByText('March 28, 2022')).toBeInTheDocument()
expect(screen.getByText('March 28, 2022')).toHaveAttribute(
'dateTime',
'2022-03-28'
)
})

test('renders feedback button correctly', () => {
render(<ContentFooter />)

expect(
screen.getByRole('button', { name: /feedback/i })
).toBeInTheDocument()

fireEvent.click(screen.getByRole('button', { name: /feedback/i }))
expect(
require('@/lib/utils/medallia').getSurveyNumber
).toHaveBeenCalledTimes(1)
expect(require('@/lib/utils/medallia').showForm).toHaveBeenCalledTimes(1)
})
})
9 changes: 9 additions & 0 deletions src/templates/common/footer/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { render, screen } from '@testing-library/react'
import { Footer } from './'

describe('Footer Component', () => {
test('renders without errors', () => {
render(<Footer />)
expect(screen.getByTestId('footer')).toBeInTheDocument()
})
})
20 changes: 20 additions & 0 deletions src/templates/common/googleMapsDirections/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { render, screen } from '@testing-library/react'
import { GoogleMapsDirections } from './'

describe('GoogleMapsDirections Component', () => {
test('renders anchor element with correct href attribute', () => {
const title = 'Target Location'
const address = '123 Main St, City, Country'
const expectedUrl = `https://maps.google.com?saddr=Current+Location&daddr=${encodeURIComponent(
address
)}`

render(<GoogleMapsDirections title={title} address={address} />)

const anchorElement = screen.getByRole('link', {
name: /get directions on google maps/i,
})
expect(anchorElement).toBeInTheDocument()
expect(anchorElement).toHaveAttribute('href', expectedUrl)
})
})
12 changes: 12 additions & 0 deletions src/templates/common/header/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { render } from '@testing-library/react'
import { Header } from './'

describe('Header Component', () => {
test('renders TopNav component within the Header', () => {
render(<Header />)
const headerComponent = document.querySelector('#header-v2')
const topnavComponent = document.querySelector('#legacy-header')
expect(headerComponent).toBeInTheDocument()
expect(topnavComponent).toBeInTheDocument()
})
})
16 changes: 16 additions & 0 deletions src/templates/common/heading/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { render } from '@testing-library/react'
import { HeadingElement } from './'

describe('HeadingElement Component', () => {
test('renders heading element with correct level', () => {
const { container } = render(
<HeadingElement headingLevel="h2" slot="my-slot">
My Heading
</HeadingElement>
)
const headingElement = container.querySelector('h2')
expect(headingElement).toBeInTheDocument()
expect(headingElement).toHaveAttribute('slot', 'my-slot')
expect(headingElement.innerHTML).toBe('My Heading')
})
})
38 changes: 38 additions & 0 deletions src/templates/common/link/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { render } from '@testing-library/react'
import Link from './'

// Mock useRouter hook for link component
jest.mock('next/router', () => ({
useRouter: () => ({
basePath: '',
pathname: '/',
query: {},
asPath: '',
push: jest.fn(),
replace: jest.fn(),
reload: jest.fn(),
back: jest.fn(),
prefetch: async () => undefined,
beforePopState: () => undefined,
events: {
on: () => undefined,
off: () => undefined,
emit: () => undefined,
},
isFallback: false,
}),
}))

describe('Link Component', () => {
test('renders link with text content correctly', () => {
const { getByText } = render(
<Link href="/some-link">
<span>Link Text</span>
</Link>
)

const linkElement = getByText('Link Text')
expect(linkElement).toBeInTheDocument()
expect(linkElement.tagName).toBe('SPAN')
})
})
36 changes: 36 additions & 0 deletions src/templates/common/medallia/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { render, screen } from '@testing-library/react'
import { MedalliaAssets } from './'

// Mocking useEffect
jest.mock('react', () => ({
...jest.requireActual('react'),
useEffect: jest.fn(),
}))

// Mock Script
jest.mock('next/script', () => ({ id, src }) => (
<div data-testid={id} data-src={src} />
))

// Mock medallia
jest.mock('@/lib/utils/medallia', () => ({
getSurveyNumber: jest.fn(),
loadForm: jest.fn(),
onMedalliaLoaded: jest.fn((callback) => callback()),
setWindowVaSurvey: jest.fn(),
}))

describe('MedalliaAssets Component', () => {
test('renders Script component with correct props', () => {
process.env.NEXT_PUBLIC_BUILD_TYPE = 'local'

render(<MedalliaAssets />)

const scriptElement = screen.getByTestId('medallia') as HTMLDivElement
expect(scriptElement).toBeInTheDocument()

const expectedSrc =
'https://resource.digital.voice.va.gov/wdcvoice/5/onsite/embed.js'
expect(scriptElement.getAttribute('data-src')).toEqual(expectedSrc)
})
})
24 changes: 24 additions & 0 deletions src/templates/common/util/HTMLComment.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { render } from '@testing-library/react'
import HTMLComment from './HTMLComment'

describe('HTMLComment Component', () => {
test('renders comment in head position', () => {
const content = 'Test comment'
render(<HTMLComment position="head" content={content} />)

const commentElement = document.head.firstChild as Comment

expect(commentElement.nodeType).toBe(Node.COMMENT_NODE)
expect(commentElement.nodeValue).toBe(content)
})

test('renders comment in footer position', () => {
const content = 'Test comment'
render(<HTMLComment position="footer" content={content} />)

const commentElement = document.body.lastChild as Comment

expect(commentElement.nodeType).toBe(Node.COMMENT_NODE)
expect(commentElement.nodeValue).toBe(content)
})
})

0 comments on commit abf4bf9

Please sign in to comment.