Skip to content

Commit 0a67f03

Browse files
authored
Feat/add test footer (Klimatbyran#289)
* cleanup sharebutton * add tests for footer components
1 parent f6d432a commit 0a67f03

10 files changed

+202
-93
lines changed

__tests__/components/Button.test.tsx

-28
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { render, screen } from '@testing-library/react'
2+
import Footer from '../../../components/Footer/Footer'
3+
4+
describe('Footer component', () => {
5+
it('should render without crashing', () => {
6+
render(
7+
<Footer />,
8+
)
9+
expect(screen.getByText('Samarbetspartners')).toBeInTheDocument()
10+
})
11+
12+
it('should display the newsletter subscription', () => {
13+
render(
14+
<Footer />,
15+
)
16+
// Assuming you add data-testid="newsletter-subscribe" to your NewsletterSubscribe component
17+
const newsletterElement = screen.getByText('Vill du få nyheter om Klimatkollen?')
18+
expect(newsletterElement).toBeInTheDocument()
19+
})
20+
21+
it('should display partners', () => {
22+
render(
23+
<Footer />,
24+
)
25+
expect(screen.getByAltText('Postkodstiftelsen logo')).toBeInTheDocument()
26+
})
27+
28+
it('should display tagline', () => {
29+
render(
30+
<Footer />,
31+
)
32+
expect(screen.getByText('Klimatkollen är en medborgarplattform som tillgängliggör klimatdata')).toBeInTheDocument()
33+
})
34+
35+
it('should display the copyright text', () => {
36+
render(
37+
<Footer />,
38+
)
39+
expect(screen.getByText(/CC BY-SA/)).toBeInTheDocument()
40+
})
41+
42+
it('should display logo', () => {
43+
render(
44+
<Footer />,
45+
)
46+
expect(screen.getByAltText('Klimatkollen logo')).toBeInTheDocument()
47+
})
48+
49+
it('should display SoMe links', () => {
50+
render(
51+
<Footer />,
52+
)
53+
expect(screen.getByText('Maila oss')).toBeInTheDocument()
54+
expect(screen.getByAltText('Linkedin logo')).toBeInTheDocument()
55+
})
56+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { render, screen, fireEvent } from '@testing-library/react'
2+
import NewsletterForm from '../../../components/Footer/FooterNewsletterForm'
3+
4+
describe('NewsletterForm component', () => {
5+
const mockOnValidated = vi.fn()
6+
7+
beforeEach(() => {
8+
render(
9+
<NewsletterForm status={null} onValidated={mockOnValidated} />,
10+
)
11+
})
12+
13+
it('should render without crashing', () => {
14+
const header = screen.getByText('Vill du få nyheter om Klimatkollen?')
15+
expect(header).toBeInTheDocument()
16+
})
17+
18+
it('should display a placeholder text in email input', () => {
19+
const emailInput = screen.getByPlaceholderText('Ange mailadress')
20+
expect(emailInput).toBeInTheDocument()
21+
})
22+
23+
it('should update email value on input change', () => {
24+
const emailInput = screen.getByPlaceholderText('Ange mailadress') as HTMLInputElement
25+
fireEvent.change(emailInput, { target: { value: '[email protected]' } })
26+
expect(emailInput.value).toBe('[email protected]')
27+
})
28+
29+
it('should call onValidated when form is submitted', () => {
30+
const emailInput = screen.getByPlaceholderText('Ange mailadress') as HTMLInputElement
31+
const form = screen.getByPlaceholderText('Ange mailadress')
32+
33+
fireEvent.change(emailInput, { target: { value: '[email protected]' } })
34+
fireEvent.submit(form)
35+
36+
expect(mockOnValidated).toHaveBeenCalledWith({ EMAIL: '[email protected]' })
37+
})
38+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react'
2+
import { render, screen } from '@testing-library/react'
3+
import '@testing-library/jest-dom/extend-expect'
4+
import NewsletterSubscribe from '../../../components/Footer/FooterNewsletterSubscribe'
5+
6+
// Mock NewsletterForm
7+
vi.mock('../FooterNewsletterForm', () => ({ default: () => <div placeholder="mockNewsletterForm" /> }))
8+
9+
describe('NewsletterSubscribe component', () => {
10+
// FIXME this test needs to be updated, not working atm
11+
// it('should throw an error if the Mailchimp URL is undefined', () => {
12+
// // Setup environment variable to undefined
13+
// process.env.NEXT_PUBLIC_MAILCHIMP_URL = undefined
14+
// expect(() => render(<NewsletterSubscribe />)).toThrow('Must have a mailchimp URL')
15+
// })
16+
17+
it('should render without error if Mailchimp URL is set', () => {
18+
render(<NewsletterSubscribe />)
19+
20+
const newsletterForm = screen.getByText('Vill du få nyheter om Klimatkollen?')
21+
expect(newsletterForm).toBeInTheDocument()
22+
})
23+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from 'react'
2+
import { render, screen } from '@testing-library/react'
3+
import Partners from '../../../components/Footer/FooterPartners'
4+
5+
describe('Partners component', () => {
6+
beforeEach(() => {
7+
render(<Partners />)
8+
})
9+
10+
it('should render all partner links', () => {
11+
const links = screen.getAllByRole('link')
12+
expect(links.length).toBe(5)
13+
14+
// Checking partner URL
15+
expect(links[0]).toHaveAttribute('href', 'https://postkodstiftelsen.se/')
16+
expect(links[1]).toHaveAttribute('href', 'https://www.climateview.global/')
17+
expect(links[2]).toHaveAttribute('href', 'https://www.wwf.se/')
18+
expect(links[3]).toHaveAttribute('href', 'https://researchersdesk.se/')
19+
expect(links[4]).toHaveAttribute('href', 'https://www.klimatklubben.se/')
20+
})
21+
22+
it('should render all partner images with correct alt text', () => {
23+
const images = screen.getAllByRole('img')
24+
expect(images.length).toBe(5)
25+
26+
// Checking image alt text
27+
expect(images[0]).toHaveAttribute('alt', 'Postkodstiftelsen logo')
28+
expect(images[1]).toHaveAttribute('alt', 'ClimateView logo')
29+
expect(images[2]).toHaveAttribute('alt', 'WWF logo')
30+
expect(images[3]).toHaveAttribute('alt', 'Researchers desk logo')
31+
expect(images[4]).toHaveAttribute('alt', 'Klimatklubben logo')
32+
})
33+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react'
2+
import { render, screen } from '@testing-library/react'
3+
import '@testing-library/jest-dom/extend-expect'
4+
import SocialList from '../../../components/Footer/FooterSocialLinks'
5+
6+
describe('SocialList component', () => {
7+
beforeEach(() => {
8+
render(<SocialList />)
9+
})
10+
11+
it('should render the email social list item', () => {
12+
const emailIcon = screen.getByAltText('Email icon')
13+
const emailLink = screen.getByText('Maila oss')
14+
expect(emailIcon).toBeInTheDocument()
15+
expect(emailLink).toHaveAttribute('href', 'mailto:[email protected]')
16+
})
17+
18+
it('should render the Twitter social list item', () => {
19+
const twitterIcon = screen.getByAltText('X (Twitter) logo')
20+
const twitterLink = screen.getByText('X (Twitter)')
21+
expect(twitterIcon).toBeInTheDocument()
22+
expect(twitterLink).toHaveAttribute('href', 'https://twitter.com/klimatkollen')
23+
})
24+
25+
it('should render the LinkedIn social list item', () => {
26+
const linkedInIcon = screen.getByAltText('Linkedin logo')
27+
const linkedInLink = screen.getByText('LinkedIn')
28+
expect(linkedInIcon).toBeInTheDocument()
29+
expect(linkedInLink).toHaveAttribute('href', 'https://www.linkedin.com/company/klimatkollen/')
30+
})
31+
32+
it('should render the GitHub social list item', () => {
33+
const gitHubIcon = screen.getByAltText('GitHub logo')
34+
const gitHubLink = screen.getByText('GitHub')
35+
expect(gitHubIcon).toBeInTheDocument()
36+
expect(gitHubLink).toHaveAttribute('href', 'https://github.com/Klimatbyran/klimatkollen')
37+
})
38+
})

components/Button.tsx

-53
This file was deleted.

components/Footer/FooterNewsletterSubscribe.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import NewsletterForm from './FooterNewsletterForm'
33

44
function NewsletterSubscribe() {
55
const MAILCHIMP_URL = process.env.NEXT_PUBLIC_MAILCHIMP_URL
6-
if (typeof MAILCHIMP_URL === 'undefined') throw new Error('Must have a mailchimp URL')
6+
if (MAILCHIMP_URL === undefined) throw new Error('Must have a mailchimp URL')
77

88
return (
99
<MailchimpSubscribe

pages/404.tsx

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ const Wrapper = styled.div`
1111
`
1212

1313
const Button = styled.button`
14-
height: 56px;
15-
background: ${({ theme }) => theme.midGreen};
16-
border: 0;
17-
border-radius: 4px;
18-
font-weight: bold;
19-
font-size: 16px;
20-
cursor: pointer;
21-
width: 100%;
22-
&:hover {
23-
background: ${({ theme }) => theme.lightGreen};
24-
}
14+
height: 56px;
15+
background: ${({ theme }) => theme.midGreen};
16+
border: 0;
17+
border-radius: 4px;
18+
font-weight: bold;
19+
font-size: 16px;
20+
cursor: pointer;
21+
width: 100%;
22+
&:hover {
23+
background: ${({ theme }) => theme.lightGreen};
24+
}
2525
`
2626

2727
function FourOhFour() {

test-setup.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
// Used for __tests__/testing-library.js
55
// Learn more: https://github.com/testing-library/jest-dom
66
import '@testing-library/jest-dom'
7+
8+
process.env.NEXT_PUBLIC_MAILCHIMP_URL = 'test-mailchimp-url'

0 commit comments

Comments
 (0)