Repository navigation
feat(theme): Implementing theme switch functionality #121
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
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
18cfd5b
Implementing theme switch functionality
manu-059 c7f596c
Addressing Review Comments
manu-059 746e952
Merge remote-tracking branch 'upstream/main' into theme-switch
manu-059 e0a5647
Merge remote-tracking branch 'upstream/main' into theme-switch
manu-059 6bf173c
Tests, Documentation & Color Updates
manu-059 52bb450
docs(CSS): restructure to be modular and clear
Ryan-Millard a1a3bc7
feat(ThemeSwitch): align styles & UX with the rest of app
Ryan-Millard 8514c7b
docs(ThemeSwitch): fix broken links
Ryan-Millard a4b7b79
docs(useTheme): fix broken links
Ryan-Millard b505e84
test(ThemeSwitch, useTheme): split test file into distinct files for …
Ryan-Millard b715eb9
docs(useTheme tests): add initial page for useTheme's tests
Ryan-Millard 0e752c8
docs(NavBar tests): remove test file since it isn't tested
Ryan-Millard e1b0263
docs(ThemeSwitch tests): fix inconsistency in statement
Ryan-Millard 91a0649
docs(ThemeSwitch tests): update to match new test file precisely
Ryan-Millard d4ba5ab
docs(fix broken links): fix all broken links that prevented compilati…
Ryan-Millard cc41bc9
docs(ThemeSwitch tests): fix file location reference
Ryan-Millard 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "label": "NavBar", | ||
| "position": 4 | ||
| } |
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,19 @@ | ||
| --- | ||
| title: NavBar | ||
| --- | ||
|
|
||
| This page documents the **NavBar** component used across Img2Num. It handles: | ||
|
|
||
| - Navigation between internal pages | ||
| - External links (e.g., GitHub) | ||
| - Theme switching | ||
| - Responsive hamburger menu for small screens | ||
| - Tooltips for links and buttons | ||
|
|
||
| :::caution Note | ||
| Full documentation and tests are still in progress. | ||
| ::: | ||
|
|
||
| ## Want to help? | ||
| We’d love your contributions! You can help by writing documentation or adding tests. | ||
| Check out [issue #166](https://github.com/Ryan-Millard/Img2Num/issues/166) to get started. |
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,202 @@ | ||
| --- | ||
| title: ThemeSwitch Tests | ||
| --- | ||
|
|
||
| The ThemeSwitch component has comprehensive test coverage with 18 tests organized into 6 test suites. | ||
|
|
||
| ## Test file location | ||
|
|
||
| ``` | ||
| src/test/ThemeSwitch.test.jsx | ||
| ``` | ||
|
|
||
| ## Running the tests | ||
|
|
||
| ```bash | ||
| # Run all tests | ||
| npm test | ||
|
|
||
| # Run only ThemeSwitch tests | ||
| npm test -- ThemeSwitch.test.jsx | ||
|
|
||
| # Run tests in watch mode | ||
| npm test -- --watch ThemeSwitch.test.jsx | ||
| ``` | ||
|
|
||
| ## Test organization | ||
|
|
||
| The test suite is organized into the following groups: | ||
|
|
||
| ### 1. Rendering (4 tests) | ||
|
|
||
| Verifies basic component rendering and structure: | ||
|
|
||
| - ✓ Should render a button | ||
| - ✓ Should have type="button" attribute | ||
| - ✓ Should have proper aria-label for accessibility | ||
| - ✓ Should apply the themeButton CSS class | ||
|
|
||
| ### 2. Icon display based on theme (4 tests) | ||
|
|
||
| Tests conditional rendering of icons: | ||
|
|
||
| - ✓ Should display Moon icon when theme is light | ||
| - ✓ Should display Sun icon when theme is dark | ||
| - ✓ Should apply icon CSS class to Moon icon | ||
| - ✓ Should apply icon CSS class to Sun icon | ||
|
|
||
| ### 3. Theme toggling functionality (4 tests) | ||
|
|
||
| Verifies click and keyboard interactions: | ||
|
|
||
| - ✓ Should call toggleTheme when button is clicked | ||
| - ✓ Should call toggleTheme multiple times on multiple clicks | ||
| - ✓ Should be accessible via keyboard (Enter key) | ||
| - ✓ Should be accessible via keyboard (Space key) | ||
|
|
||
| ### 4. Integration with useTheme hook (3 tests) | ||
|
|
||
| Tests integration with the underlying hook: | ||
|
|
||
| - ✓ Should use theme value from useTheme hook | ||
| - ✓ Should use toggleTheme function from useTheme hook | ||
| - ✓ Should call useTheme hook on component mount | ||
|
|
||
| ### 5. Edge cases (3 tests) | ||
|
|
||
| Handles unexpected or edge case scenarios: | ||
|
|
||
| - ✓ Should handle undefined theme gracefully | ||
| - ✓ Should handle null theme gracefully | ||
| - ✓ Should handle unexpected theme value gracefully | ||
|
|
||
| ## Mocking strategy | ||
|
|
||
| The tests use comprehensive mocking to isolate the component: | ||
|
|
||
| ### Mock useTheme hook | ||
|
|
||
| ```javascript | ||
| import * as useThemeModule from '@hooks/useTheme'; | ||
|
|
||
| vi.spyOn(useThemeModule, 'useTheme').mockReturnValue({ | ||
| theme: 'light', | ||
| toggleTheme: mockToggleTheme, | ||
| }); | ||
| ``` | ||
|
|
||
| ### Mock CSS modules | ||
|
|
||
| ```javascript | ||
| vi.mock('@components/ThemeSwitch.module.css', () => ({ | ||
| default: { | ||
| themeButton: 'mocked-theme-button-class', | ||
| icon: 'mocked-icon-class', | ||
| }, | ||
| })); | ||
| ``` | ||
|
|
||
| ### Mock Lucide React icons | ||
|
|
||
| ```javascript | ||
| vi.mock('lucide-react', () => ({ | ||
| Moon: ({ className }) => ( | ||
| <span data-testid="moon-icon" className={className}> | ||
| Moon Icon | ||
| </span> | ||
| ), | ||
| Sun: ({ className }) => ( | ||
| <span data-testid="sun-icon" className={className}> | ||
| Sun Icon | ||
| </span> | ||
| ), | ||
| })); | ||
| ``` | ||
|
|
||
| ## Example tests | ||
|
|
||
| ### Testing icon display | ||
|
|
||
| ```javascript | ||
| it('should display Moon icon when theme is light', () => { | ||
| vi.spyOn(useThemeModule, 'useTheme').mockReturnValue({ | ||
| theme: 'light', | ||
| toggleTheme: mockToggleTheme, | ||
| }); | ||
|
|
||
| render(<ThemeSwitch />); | ||
|
|
||
| const moonIcon = screen.getByTestId('moon-icon'); | ||
| expect(moonIcon).toBeInTheDocument(); | ||
| expect(screen.queryByTestId('sun-icon')).not.toBeInTheDocument(); | ||
| }); | ||
| ``` | ||
|
|
||
| ### Testing click handler | ||
|
|
||
| ```javascript | ||
| it('should call toggleTheme when button is clicked', () => { | ||
| vi.spyOn(useThemeModule, 'useTheme').mockReturnValue({ | ||
| theme: 'light', | ||
| toggleTheme: mockToggleTheme, | ||
| }); | ||
|
|
||
| render(<ThemeSwitch />); | ||
|
|
||
| const button = screen.getByRole('button'); | ||
| fireEvent.click(button); | ||
|
|
||
| expect(mockToggleTheme).toHaveBeenCalledTimes(1); | ||
| }); | ||
| ``` | ||
|
|
||
| ### Testing edge cases | ||
|
|
||
| ```javascript | ||
| it('should handle undefined theme gracefully', () => { | ||
| vi.spyOn(useThemeModule, 'useTheme').mockReturnValue({ | ||
| theme: undefined, | ||
| toggleTheme: mockToggleTheme, | ||
| }); | ||
|
|
||
| render(<ThemeSwitch />); | ||
|
|
||
| // Should default to showing Moon icon (theme !== 'dark') | ||
| expect(screen.getByTestId('moon-icon')).toBeInTheDocument(); | ||
| }); | ||
| ``` | ||
|
|
||
| ## Test utilities | ||
|
|
||
| The tests use: | ||
|
|
||
| - **Vitest** - Test framework | ||
| - **React Testing Library** - Component rendering and queries | ||
| - **vi.fn()** - Mock functions | ||
| - **vi.spyOn()** - Spy on module exports | ||
| - **fireEvent** - Simulate user interactions | ||
|
|
||
| ## Coverage metrics | ||
|
|
||
| The tests achieve comprehensive coverage of: | ||
|
|
||
| - All component renders (light/dark themes) | ||
| - All user interactions (click, keyboard) | ||
| - All edge cases (undefined, null, unexpected values) | ||
| - Integration with external dependencies | ||
| - Accessibility features | ||
| - CSS class application | ||
|
|
||
| ## Best practices demonstrated | ||
|
|
||
| 1. **Isolation** - Mocking external dependencies to test component in isolation | ||
| 2. **Accessibility testing** - Verifying aria-labels and keyboard support | ||
| 3. **Edge case handling** - Testing undefined, null, and unexpected values | ||
| 4. **User-centric testing** - Using `getByRole`, `getByTestId` for queries | ||
| 5. **Clear organization** - Grouping related tests with `describe` blocks | ||
| 6. **Setup/teardown** - Using `beforeEach` and `afterEach` for clean test state | ||
|
|
||
| ## Related | ||
|
|
||
| - [ThemeSwitch Component](/reference/react/components/ThemeSwitch) - Component documentation | ||
| - [useTheme Hook](/reference/react/hooks/useTheme) - Hook used by the component | ||
4 changes: 4 additions & 0 deletions
4
docs/docs/reference/react/components/ThemeSwitch/_category_.json
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,4 @@ | ||
| { | ||
| "label": "ThemeSwitch", | ||
| "position": 3 | ||
| } |
Oops, something went wrong.
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.