diff --git a/__tests__/context/PathsContext.test.tsx b/__tests__/context/PathsContext.test.tsx new file mode 100644 index 00000000..269603a1 --- /dev/null +++ b/__tests__/context/PathsContext.test.tsx @@ -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 ( +
+

Paths Count: {paths.length}

+ + + +
+ ) +} + +describe('PathsContext', () => { + it('provides the correct default values', () => { + render( + + + + ) + + expect(screen.getByTestId('pathsCount')).toHaveTextContent(`Paths Count: 0`) + }) + + it('allows adding paths in the context', () => { + render( + + + + ) + + 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( + + + + ) + + 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`) + }) +})