Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: fix bug and remove extra code
  • Loading branch information
filippovskii09 committed Nov 26, 2025
commit 281a5bcaed65d622ad93dcf19200cc409eab6b78
1 change: 1 addition & 0 deletions src/courseware/course/sidebar/SidebarContextProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const SidebarProvider = ({
}, [courseId]);

const toggleSidebar = useCallback((sidebarId) => {
// Switch to new sidebar or hide the current sidebar
const newSidebar = sidebarId === currentSidebar ? null : sidebarId;
setCurrentSidebar(newSidebar);
setLocalStorage(`sidebar.${courseId}`, newSidebar);
Expand Down
16 changes: 12 additions & 4 deletions src/courseware/course/sidebar/SidebarContextProvider.test.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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 { initializeTestStore, render, screen } from '@src/setupTest';
import SidebarProvider from './SidebarContextProvider';
import SidebarContext from './SidebarContext';
import * as discussionsSidebar from './sidebars/discussions';
Expand All @@ -20,9 +20,13 @@ jest.mock('@openedx/paragon', () => ({
},
}));

jest.mock('@src/generic/model-store', () => ({
useModel: jest.fn(),
}));
jest.mock('@src/generic/model-store', () => {
const actual = jest.requireActual('@src/generic/model-store');
return {
...actual,
useModel: jest.fn(),
};
});

jest.mock('@src/data/localStorage', () => ({
getLocalStorage: jest.fn(),
Expand Down Expand Up @@ -66,6 +70,10 @@ describe('SidebarContextProvider', () => {
unitId: 'unit-1',
};

beforeAll(async () => {
await initializeTestStore();
});

beforeEach(() => {
jest.clearAllMocks();
useWindowSize.mockReturnValue({ width: 1400 });
Expand Down
4 changes: 2 additions & 2 deletions src/courseware/course/sidebar/common/SidebarBase.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ const SidebarBase = ({
{shouldDisplayFullScreen ? (
<div
className="pt-2 pb-2.5 border-bottom border-light-400 d-flex align-items-center ml-2"
onClick={() => toggleSidebar(null)}
onKeyDown={() => toggleSidebar(null)}
onClick={handleCloseNotificationTray}
onKeyDown={handleCloseNotificationTray}
role="button"
tabIndex="0"
>
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the comments in this file. Are all of them really necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import {
fireEvent, initializeTestStore, render, screen,
} from '../../../../../setupTest';
import SidebarContext from '../../SidebarContext';
// FIX 1: Імпортуємо ID
import NotificationTrigger, { ID } from './NotificationTrigger';

// FIX 2: Використовуємо модульні моки замість window.localStorage

jest.mock('@src/data/localStorage', () => ({
getLocalStorage: jest.fn(),
setLocalStorage: jest.fn(),
Expand Down Expand Up @@ -44,7 +41,6 @@ describe('Notification Trigger', () => {
upgradeNotificationCurrentState: 'FPDdaysLeft',
};

// Default mocks
getLocalStorage.mockReturnValue(null);
getSessionStorage.mockReturnValue('closed');
});
Expand All @@ -69,9 +65,8 @@ describe('Notification Trigger', () => {
const toggleNotificationTray = jest.fn();
const testData = {
...mockData,
toggleSidebar: toggleNotificationTray, // Fix naming for consistnecy if needed, usually passed via context
toggleSidebar: toggleNotificationTray,
};
// We are testing the onClick passed to component, not context toggle here specifically for the trigger click
const onClickProp = jest.fn();

renderWithProvider(testData, onClickProp);
Expand All @@ -82,7 +77,6 @@ describe('Notification Trigger', () => {
fireEvent.click(notificationTrigger);

expect(onClickProp).toHaveBeenCalledTimes(1);
// Check SessionStorage update via module mock
expect(setSessionStorage).toHaveBeenCalledWith(`notificationTrayStatus.${mockData.courseId}`, 'open');
});

Expand All @@ -95,7 +89,6 @@ describe('Notification Trigger', () => {
});

it('renders notification trigger icon WITHOUT red dot within the same phase', async () => {
// FIX: Mock return value directly
getLocalStorage.mockReturnValue('sameState');

const container = renderWithProvider({
Expand All @@ -105,7 +98,6 @@ describe('Notification Trigger', () => {

expect(container).toBeInTheDocument();

// Check module mock call
expect(getLocalStorage).toHaveBeenCalledWith(`upgradeNotificationLastSeen.${mockData.courseId}`);

const buttonIcon = container.querySelectorAll('svg');
Expand All @@ -114,7 +106,6 @@ describe('Notification Trigger', () => {
});

it('makes the right updates when rendering a new phase from an UpgradeNotification change (before -> after)', async () => {
// FIX: Mock implementation to return 'before' for lastSeen
getLocalStorage.mockImplementation((key) => {
if (key.includes('upgradeNotificationLastSeen')) { return 'before'; }
return null;
Expand All @@ -126,19 +117,15 @@ describe('Notification Trigger', () => {

expect(container).toBeInTheDocument();

// Verify calls to module mocks
expect(getLocalStorage).toHaveBeenCalledWith(`upgradeNotificationLastSeen.${mockData.courseId}`);
expect(setLocalStorage).toHaveBeenCalledWith(`notificationStatus.${mockData.courseId}`, 'active');
expect(setLocalStorage).toHaveBeenCalledWith(`upgradeNotificationLastSeen.${mockData.courseId}`, 'after');
});

it('handles localStorage from a different course', async () => {
// This test logic was checking if localStorage affects other courses.
// Since we mock the module, we verify that we call it with the CORRECT course ID.

getLocalStorage.mockImplementation((key) => {
if (key === `upgradeNotificationLastSeen.${mockData.courseId}`) { return 'before'; }
return 'accessDateView'; // Simulate other data existing
return 'accessDateView';
});

const container = renderWithProvider({
Expand All @@ -147,16 +134,12 @@ describe('Notification Trigger', () => {

expect(container).toBeInTheDocument();

// Verify we updated OUR course
expect(setLocalStorage).toHaveBeenCalledWith(`upgradeNotificationLastSeen.${mockData.courseId}`, 'after');
expect(setLocalStorage).toHaveBeenCalledWith(`notificationStatus.${mockData.courseId}`, 'active');

// Verify we did NOT update the other course (mock check)
expect(setLocalStorage).not.toHaveBeenCalledWith(expect.stringContaining('second_id'), expect.anything());
});

// --- Coverage Tests ---

it('initializes default localStorage values if they are missing', () => {
getLocalStorage.mockReturnValue(null);

Expand All @@ -172,7 +155,6 @@ describe('Notification Trigger', () => {

renderWithProvider(contextData);

// FIX 1: ID is now imported and defined
expect(contextData.toggleSidebar).toHaveBeenCalledWith(ID);
});

Expand Down
Empty file added webpack.dev-tutor.config.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s remove this file from the PR and add it to .gitignore instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Empty file.