-
Notifications
You must be signed in to change notification settings - Fork 2
Slide panel tests #174
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
Merged
Merged
Slide panel tests #174
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* eslint-disable @typescript-eslint/non-nullable-type-assertion-style */ | ||
| import React from 'react' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { act, fireEvent, render } from '@testing-library/react' | ||
| import SlidePanel from '../../../src/components/viewers/SlidePanel.js' | ||
|
|
||
| describe('SlidePanel', () => { | ||
| // Minimal localStorage mock | ||
| const localStorageMock = (() => { | ||
| let store: Record<string, string> = {} | ||
| return { | ||
| getItem: (key: string) => store[key] ?? null, | ||
| setItem: (key: string, value: string) => { store[key] = value }, | ||
| clear: () => { store = {} }, | ||
| // eslint-disable-next-line @typescript-eslint/no-dynamic-delete | ||
| removeItem: (key: string) => { delete store[key] }, | ||
| } | ||
| })() | ||
|
|
||
| beforeEach(() => { | ||
| vi.stubGlobal('localStorage', localStorageMock) | ||
| localStorage.clear() | ||
| }) | ||
|
|
||
| it('renders main and panel content', () => { | ||
| const { getByText } = render( | ||
| <SlidePanel | ||
| mainContent={<div data-testid="main-content">Main</div>} | ||
| panelContent={<div data-testid="panel-content">Panel</div>} | ||
| isPanelOpen | ||
| /> | ||
| ) | ||
| expect(getByText('Main')).toBeDefined() | ||
| expect(getByText('Panel')).toBeDefined() | ||
| }) | ||
|
|
||
| it('does not render the resizer if panel is closed', () => { | ||
| const { container } = render( | ||
| <SlidePanel | ||
| mainContent={<div>Main</div>} | ||
| panelContent={<div>Panel</div>} | ||
| isPanelOpen={false} | ||
| /> | ||
| ) | ||
| const resizer = container.querySelector('.resizer') | ||
| expect(resizer).toBeNull() | ||
| }) | ||
|
|
||
| it('uses default width of 400 when localStorage is empty', () => { | ||
| const { container } = render( | ||
| <SlidePanel | ||
| mainContent={<div>Main</div>} | ||
| panelContent={<div>Panel</div>} | ||
| isPanelOpen | ||
| /> | ||
| ) | ||
| const panel = container.querySelector('.slidePanel') as HTMLElement | ||
| expect(panel.style.width).toBe('400px') | ||
| }) | ||
|
|
||
| it('loads width from localStorage if present', () => { | ||
| localStorage.setItem('panelWidth', '250') | ||
| const { container } = render( | ||
| <SlidePanel | ||
| mainContent={<div>Main</div>} | ||
| panelContent={<div>Panel</div>} | ||
| isPanelOpen | ||
| /> | ||
| ) | ||
| const panel = container.querySelector('.slidePanel') as HTMLElement | ||
| expect(panel.style.width).toBe('250px') | ||
| }) | ||
|
|
||
| it('falls back to default width if localStorage width is invalid', () => { | ||
| localStorage.setItem('panelWidth', 'not-a-number') | ||
| const { container } = render( | ||
| <SlidePanel | ||
| mainContent={<div>Main</div>} | ||
| panelContent={<div>Panel</div>} | ||
| isPanelOpen | ||
| /> | ||
| ) | ||
| const panel = container.querySelector('.slidePanel') as HTMLElement | ||
| // parseInt of 'not-a-number' yields NaN so default width of 400 is expected | ||
| expect(panel.style.width).toBe('400px') | ||
| }) | ||
|
|
||
| it('respects minWidth from config', () => { | ||
| const { container } = render( | ||
| <SlidePanel | ||
| mainContent={<div>Main</div>} | ||
| panelContent={<div>Panel</div>} | ||
| isPanelOpen | ||
| config={{ slidePanel: { minWidth: 300 } }} | ||
| /> | ||
| ) | ||
| const resizer = container.querySelector('.resizer') as HTMLElement | ||
| const panel = container.querySelector('.slidePanel') as HTMLElement | ||
| expect(panel.style.width).toBe('400px') | ||
|
|
||
| // Simulate mousedown on resizer with clientX 800 | ||
| act(() => { | ||
| fireEvent.mouseDown(resizer, { clientX: 800 }) | ||
| }) | ||
|
|
||
| // Simulate mousemove on document with clientX such that new width is less than minWidth | ||
| act(() => { | ||
| fireEvent.mouseMove(document, { clientX: 950 }) | ||
| fireEvent.mouseUp(document) | ||
| }) | ||
|
|
||
| // resizingClientX was set to 800 + 400 = 1200 so new width = max(300, 1200 - 950) = 300 | ||
| expect(panel.style.width).toBe('300px') | ||
| }) | ||
|
|
||
| it('handles dragging to resize', () => { | ||
| const { container } = render( | ||
| <SlidePanel | ||
| mainContent={<div>Main</div>} | ||
| panelContent={<div>Panel</div>} | ||
| isPanelOpen | ||
| /> | ||
| ) | ||
| const resizer = container.querySelector('.resizer') as HTMLElement | ||
| const panel = container.querySelector('.slidePanel') as HTMLElement | ||
| expect(panel.style.width).toBe('400px') | ||
|
|
||
| // Mock panel's offsetWidth to be 400px | ||
| Object.defineProperty(panel, 'offsetWidth', { value: 400, configurable: true }) | ||
|
|
||
| // Simulate mousedown | ||
| act(() => { | ||
| fireEvent.mouseDown(resizer, { clientX: 800 }) | ||
| }) | ||
|
|
||
| // Simulate dragging | ||
| act(() => { | ||
| fireEvent.mouseMove(document, { clientX: 750 }) | ||
| }) | ||
|
|
||
| // End dragging | ||
| act(() => { | ||
| fireEvent.mouseUp(document) | ||
| }) | ||
|
|
||
| // Expected new width = 1200 - 750 = 450 | ||
| expect(panel.style.width).toBe('450px') | ||
| expect(localStorage.getItem('panelWidth')).toBe('450') | ||
| }) | ||
|
|
||
| it('uses config defaultWidth if valid', () => { | ||
| const { container } = render( | ||
| <SlidePanel | ||
| mainContent={<div>Main</div>} | ||
| panelContent={<div>Panel</div>} | ||
| isPanelOpen | ||
| config={{ slidePanel: { defaultWidth: 500 } }} | ||
| /> | ||
| ) | ||
| const panel = container.querySelector('.slidePanel') as HTMLElement | ||
| expect(panel.style.width).toBe('500px') | ||
| }) | ||
|
|
||
| it('ignores negative config.defaultWidth and uses 400 instead', () => { | ||
| const { container } = render( | ||
| <SlidePanel | ||
| mainContent={<div>Main</div>} | ||
| panelContent={<div>Panel</div>} | ||
| isPanelOpen | ||
| config={{ slidePanel: { defaultWidth: -10 } }} | ||
| /> | ||
| ) | ||
| const panel = container.querySelector('.slidePanel') as HTMLElement | ||
| expect(panel.style.width).toBe('400px') | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.