Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: increase code coverage by adding more tests #259

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 0 additions & 175 deletions theme/src/components/__snapshots__/home-navigation.spec.js.snap

This file was deleted.

74 changes: 56 additions & 18 deletions theme/src/components/home-navigation.spec.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,70 @@
import React from 'react'
import renderer from 'react-test-renderer'

import { render, screen, fireEvent } from '@testing-library/react'
import '@testing-library/jest-dom'
import HomeNavigation from './home-navigation'
import useSiteMetadata from '../hooks/use-site-metadata'

jest.mock('../hooks/use-site-metadata')

const mockSiteMetadata = {
widgets: {
github: {
widgetDataSource: 'https://fake-api.chrisvogt.me/social/github'
},
goodreads: {
widgetDataSource: 'https://fake-api.chrisvogt.me/social/goodreads'
},
instagram: {
widgetDataSource: 'https://fake-api.chrisvogt.me/social/instagram'
},
spotify: {
widgetDataSource: 'https://fake-api.chrisvogt.me/social/spotify'
}
github: { widgetDataSource: 'https://fake-api.chrisvogt.me/social/github' },
goodreads: { widgetDataSource: 'https://fake-api.chrisvogt.me/social/goodreads' },
instagram: { widgetDataSource: 'https://fake-api.chrisvogt.me/social/instagram' },
spotify: { widgetDataSource: 'https://fake-api.chrisvogt.me/social/spotify' }
}
}

describe('HomeNavigation', () => {
useSiteMetadata.mockImplementation(() => mockSiteMetadata)
it('matches the snapshot', () => {
const tree = renderer.create(<HomeNavigation />).toJSON()
expect(tree).toMatchSnapshot()
beforeEach(() => {
useSiteMetadata.mockImplementation(() => mockSiteMetadata)
})

it('renders all links when all widgets are enabled', () => {
render(<HomeNavigation />)
expect(screen.getByText('Home')).toBeInTheDocument()
expect(screen.getByText('Latest Posts')).toBeInTheDocument()
expect(screen.getByText('Instagram')).toBeInTheDocument()
expect(screen.getByText('GitHub')).toBeInTheDocument()
expect(screen.getByText('Goodreads')).toBeInTheDocument()
expect(screen.getByText('Spotify')).toBeInTheDocument()
})

it('renders only mandatory links when no widgets are enabled', () => {
useSiteMetadata.mockImplementation(() => ({ widgets: {} }))
render(<HomeNavigation />)
expect(screen.getByText('Home')).toBeInTheDocument()
expect(screen.getByText('Latest Posts')).toBeInTheDocument()
expect(screen.queryByText('Instagram')).not.toBeInTheDocument()
expect(screen.queryByText('GitHub')).not.toBeInTheDocument()
expect(screen.queryByText('Goodreads')).not.toBeInTheDocument()
expect(screen.queryByText('Spotify')).not.toBeInTheDocument()
})

it('highlights the active section on scroll', () => {
render(<HomeNavigation />)

const instagramSection = document.createElement('div')
instagramSection.id = 'instagram'
document.body.appendChild(instagramSection)
instagramSection.getBoundingClientRect = jest.fn(() => ({
top: window.innerHeight / 2 - 1 // Simulate it being in view
}))

fireEvent.scroll(window)
expect(screen.getByText('Instagram').classList).toContain('active')
})

it('does not break when an invalid icon is provided', () => {
useSiteMetadata.mockImplementation(() => ({ widgets: {} }))
render(<HomeNavigation />)
expect(screen.queryByRole('img')).not.toBeInTheDocument() // No icons should render
})

it('renders the correct aria-label for navigation', () => {
render(<HomeNavigation />)
const nav = document.querySelector('nav[aria-label="On-page navigation"]')
expect(nav).not.toBeNull()
expect(nav.getAttribute('aria-label')).toBe('On-page navigation')
})
})
Loading
Loading