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: add test cases for PathsContext #238

Merged
merged 1 commit into from
Oct 21, 2024
Merged
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
84 changes: 84 additions & 0 deletions __tests__/context/PathsContext.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React from 'react'
import { expect } from '@jest/globals'
import { act, render, screen } from '@testing-library/react'
import '@testing-library/jest-dom/jest-globals'

import { usePathsContext, PathsProvider } from '../../src/context/PathsContext'

const TestComponent = () => {
const { paths, setPaths } = usePathsContext()

const handleAddPath = () => {
setPaths([...paths, '/path/to/1'])
}

const handleClearPaths = () => {
setPaths([])
}

return (
<div>
<p data-testid="pathsCount">Paths Count: {paths.length}</p>

<button onClick={handleAddPath}>Add Path</button>
<button onClick={handleClearPaths}>Clear Paths</button>
</div>
)
}

describe('PathsContext', () => {
it('provides the correct default values', () => {
render(
<PathsProvider>
<TestComponent />
</PathsProvider>
)

expect(screen.getByTestId('pathsCount')).toHaveTextContent(`Paths Count: 0`)
})

it('allows adding paths in the context', () => {
render(
<PathsProvider>
<TestComponent />
</PathsProvider>
)

const addPathBtn = screen.getByText('Add Path')

act(() => {
addPathBtn.click()
})

expect(screen.getByTestId('pathsCount')).toHaveTextContent(`Paths Count: 1`)

act(() => {
addPathBtn.click()
})

expect(screen.getByTestId('pathsCount')).toHaveTextContent(`Paths Count: 2`)
})

it('allows updating paths in the context', () => {
render(
<PathsProvider>
<TestComponent />
</PathsProvider>
)

const clearPathsBtn = screen.getByText('Clear Paths')
const addPathBtn = screen.getByText('Add Path')

act(() => {
clearPathsBtn.click()
})

expect(screen.getByTestId('pathsCount')).toHaveTextContent(`Paths Count: 0`)

act(() => {
addPathBtn.click()
})

expect(screen.getByTestId('pathsCount')).toHaveTextContent(`Paths Count: 1`)
})
})