-
Notifications
You must be signed in to change notification settings - Fork 298
feat: add notification tray status to session storage #1823
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
Open
filippovskii09
wants to merge
4
commits into
openedx:master
Choose a base branch
from
raccoongang:filippovskii/feat/sesion-storage-for-notifications-tray
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+368
−63
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0606bca
feat: add notification tray status to session storage
filippovskii09 aa55953
fix: fix test and upgrade coverage
filippovskii09 281a5bc
fix: fix bug and remove extra code
filippovskii09 97e27a3
fix: remove extra file and add him to gitignore
filippovskii09 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
fix: fix test and upgrade coverage
- Loading branch information
commit aa55953cd7f060ab44f36eae157df25b6b739bcd
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
199 changes: 199 additions & 0 deletions
199
src/courseware/course/sidebar/SidebarContextProvider.test.jsx
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,199 @@ | ||
| import { useContext } from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { useWindowSize } from '@openedx/paragon'; | ||
|
|
||
| import { useModel } from '@src/generic/model-store'; | ||
| import { getLocalStorage, setLocalStorage } from '@src/data/localStorage'; | ||
| import { getSessionStorage } from '@src/data/sessionStorage'; | ||
|
|
||
| import SidebarProvider from './SidebarContextProvider'; | ||
| import SidebarContext from './SidebarContext'; | ||
| import * as discussionsSidebar from './sidebars/discussions'; | ||
| import * as notificationsSidebar from './sidebars/notifications'; | ||
|
|
||
| jest.mock('@openedx/paragon', () => ({ | ||
| ...jest.requireActual('@openedx/paragon'), | ||
| useWindowSize: jest.fn(), | ||
| breakpoints: { | ||
| extraLarge: { minWidth: 1200 }, | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('@src/generic/model-store', () => ({ | ||
| useModel: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('@src/data/localStorage', () => ({ | ||
| getLocalStorage: jest.fn(), | ||
| setLocalStorage: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('@src/data/sessionStorage', () => ({ | ||
| getSessionStorage: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('./sidebars/discussions', () => ({ ID: 'discussions' })); | ||
| jest.mock('./sidebars/notifications', () => ({ ID: 'notifications' })); | ||
| jest.mock('./sidebars', () => ({ | ||
| SIDEBARS: { | ||
| discussions: { ID: 'discussions' }, | ||
| notifications: { ID: 'notifications' }, | ||
| }, | ||
| })); | ||
|
|
||
| const TestConsumer = () => { | ||
| const { | ||
| currentSidebar, | ||
| toggleSidebar, | ||
| onNotificationSeen, | ||
| notificationStatus, | ||
| } = useContext(SidebarContext); | ||
|
|
||
| return ( | ||
| <div> | ||
| <div data-testid="current-sidebar">{currentSidebar || 'none'}</div> | ||
| <div data-testid="notification-status">{notificationStatus || 'none'}</div> | ||
| <button type="button" onClick={() => toggleSidebar(discussionsSidebar.ID)}>Toggle Discussions</button> | ||
| <button type="button" onClick={onNotificationSeen}>See Notifications</button> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| describe('SidebarContextProvider', () => { | ||
| const defaultProps = { | ||
| courseId: 'course-v1:test', | ||
| unitId: 'unit-1', | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| useWindowSize.mockReturnValue({ width: 1400 }); | ||
| useModel.mockReturnValue({}); | ||
| getLocalStorage.mockReturnValue(null); | ||
| getSessionStorage.mockReturnValue(null); | ||
| }); | ||
|
|
||
| it('renders without crashing and provides default context', () => { | ||
| render( | ||
| <SidebarProvider {...defaultProps}> | ||
| <TestConsumer /> | ||
| </SidebarProvider>, | ||
| ); | ||
| expect(screen.getByTestId('current-sidebar')).toHaveTextContent('none'); | ||
| }); | ||
|
|
||
| it('initializes with notifications sidebar if notification tray is open in session storage', () => { | ||
| getSessionStorage.mockReturnValue('open'); | ||
|
|
||
| render( | ||
| <SidebarProvider {...defaultProps}> | ||
| <TestConsumer /> | ||
| </SidebarProvider>, | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('current-sidebar')).toHaveTextContent(notificationsSidebar.ID); | ||
| }); | ||
|
|
||
| it('loads initial sidebar from local storage on small screens (mobile behavior)', () => { | ||
| useWindowSize.mockReturnValue({ width: 800 }); | ||
| getLocalStorage.mockImplementation((key) => { | ||
| if (key === `sidebar.${defaultProps.courseId}`) { return discussionsSidebar.ID; } | ||
| return null; | ||
| }); | ||
|
|
||
| render( | ||
| <SidebarProvider {...defaultProps}> | ||
| <TestConsumer /> | ||
| </SidebarProvider>, | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('current-sidebar')).toHaveTextContent(discussionsSidebar.ID); | ||
| }); | ||
|
|
||
| it('does not load from local storage on large screens (desktop behavior)', () => { | ||
| useWindowSize.mockReturnValue({ width: 1400 }); | ||
| getLocalStorage.mockReturnValue(discussionsSidebar.ID); | ||
|
|
||
| render( | ||
| <SidebarProvider {...defaultProps}> | ||
| <TestConsumer /> | ||
| </SidebarProvider>, | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('current-sidebar')).toHaveTextContent('none'); | ||
| }); | ||
|
|
||
| it('toggles sidebar open and updates local storage', async () => { | ||
| const user = userEvent.setup(); | ||
| render( | ||
| <SidebarProvider {...defaultProps}> | ||
| <TestConsumer /> | ||
| </SidebarProvider>, | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('current-sidebar')).toHaveTextContent('none'); | ||
|
|
||
| await user.click(screen.getByText('Toggle Discussions')); | ||
|
|
||
| expect(screen.getByTestId('current-sidebar')).toHaveTextContent(discussionsSidebar.ID); | ||
| expect(setLocalStorage).toHaveBeenCalledWith(`sidebar.${defaultProps.courseId}`, discussionsSidebar.ID); | ||
| }); | ||
|
|
||
| it('toggles sidebar closed (null) if clicking the same sidebar', async () => { | ||
| useWindowSize.mockReturnValue({ width: 800 }); | ||
| getLocalStorage.mockReturnValue(discussionsSidebar.ID); | ||
| const user = userEvent.setup(); | ||
|
|
||
| render( | ||
| <SidebarProvider {...defaultProps}> | ||
| <TestConsumer /> | ||
| </SidebarProvider>, | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('current-sidebar')).toHaveTextContent(discussionsSidebar.ID); | ||
|
|
||
| await user.click(screen.getByText('Toggle Discussions')); | ||
|
|
||
| expect(screen.getByTestId('current-sidebar')).toHaveTextContent('none'); | ||
| expect(setLocalStorage).toHaveBeenCalledWith(`sidebar.${defaultProps.courseId}`, null); | ||
| }); | ||
|
|
||
| it('updates notification status when seen', async () => { | ||
| const user = userEvent.setup(); | ||
| render( | ||
| <SidebarProvider {...defaultProps}> | ||
| <TestConsumer /> | ||
| </SidebarProvider>, | ||
| ); | ||
|
|
||
| await user.click(screen.getByText('See Notifications')); | ||
|
|
||
| expect(setLocalStorage).toHaveBeenCalledWith(`notificationStatus.${defaultProps.courseId}`, 'inactive'); | ||
| expect(screen.getByTestId('notification-status')).toHaveTextContent('inactive'); | ||
| }); | ||
|
|
||
| it('updates current sidebar when unitId changes (Effect trigger)', () => { | ||
| useWindowSize.mockReturnValue({ width: 800 }); | ||
| getLocalStorage.mockReturnValue(notificationsSidebar.ID); | ||
|
|
||
| const { rerender } = render( | ||
| <SidebarProvider {...defaultProps}> | ||
| <TestConsumer /> | ||
| </SidebarProvider>, | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('current-sidebar')).toHaveTextContent(notificationsSidebar.ID); | ||
|
|
||
| useModel.mockImplementation((model) => { | ||
| if (model === 'discussionTopics') { return { id: 'topic-1', enabledInContext: true }; } | ||
| return {}; | ||
| }); | ||
|
|
||
| rerender( | ||
| <SidebarProvider {...defaultProps} unitId="unit-2"> | ||
| <TestConsumer /> | ||
| </SidebarProvider>, | ||
| ); | ||
| }); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we import this from
setupTest?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, fixed