Skip to content

Commit 6ebe2ce

Browse files
feat: added support to open notification tray from query params (#615)
1 parent 85a0935 commit 6ebe2ce

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/Notification/data/hook.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function useNotification() {
9898
newNotificationIds, notificationsKeyValuePair, pagination,
9999
} = normalizedData;
100100

101-
const existingNotificationIds = apps[appName];
101+
const existingNotificationIds = apps[appName] || [];
102102
const { count } = tabsCount;
103103

104104
return {

src/Notification/index.jsx

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, {
44

55
import classNames from 'classnames';
66
import PropTypes from 'prop-types';
7+
import { useSearchParams } from 'react-router-dom';
78

89
import { getConfig } from '@edx/frontend-platform';
910
import { useIntl } from '@edx/frontend-platform/i18n';
@@ -26,12 +27,14 @@ const Notifications = ({ notificationAppData, showLeftMargin }) => {
2627
const intl = useIntl();
2728
const popoverRef = useRef(null);
2829
const headerRef = useRef(null);
30+
const [searchParams] = useSearchParams();
2931
const buttonRef = useRef(null);
3032
const [enableNotificationTray, setEnableNotificationTray] = useState(false);
3133
const [appName, setAppName] = useState('discussion');
3234
const [isHeaderVisible, setIsHeaderVisible] = useState(true);
3335
const [notificationData, setNotificationData] = useState({});
3436
const [tabsCount, setTabsCount] = useState(notificationAppData?.tabsCount);
37+
const [openFlag, setOpenFlag] = useState(false);
3538
const isOnMediumScreen = useIsOnMediumScreen();
3639
const isOnLargeScreen = useIsOnLargeScreen();
3740

@@ -45,6 +48,15 @@ const Notifications = ({ notificationAppData, showLeftMargin }) => {
4548
}
4649
}, []);
4750

51+
useEffect(() => {
52+
if (openFlag || Object.keys(tabsCount).length === 0) {
53+
return;
54+
}
55+
setAppName(searchParams.get('app') || 'discussion');
56+
setEnableNotificationTray(searchParams.get('showNotifications') === 'true');
57+
setOpenFlag(true);
58+
}, [tabsCount, openFlag, searchParams]);
59+
4860
useEffect(() => {
4961
setTabsCount(notificationAppData.tabsCount);
5062
setNotificationData(prevData => ({
@@ -97,10 +109,10 @@ const Notifications = ({ notificationAppData, showLeftMargin }) => {
97109

98110
const notificationContextValue = useMemo(() => ({
99111
enableNotificationTray,
100-
appName,
101112
handleActiveTab,
102113
updateNotificationData,
103114
...notificationData,
115+
appName,
104116
}), [enableNotificationTray, appName, handleActiveTab, updateNotificationData, notificationData]);
105117

106118
return (

src/Notification/index.test.jsx

+34-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ const contextValue = {
3434
config: {},
3535
};
3636

37-
async function renderComponent() {
37+
async function renderComponent(url = '/') {
3838
render(
39-
<MemoryRouter>
39+
<MemoryRouter initialEntries={[url]}>
4040
<AppContext.Provider value={contextValue}>
4141
<IntlProvider locale="en" messages={{}}>
4242
<LearningHeader />
@@ -70,6 +70,38 @@ describe('Notification test cases.', () => {
7070
.reply(200, (Factory.build('notificationsCount', { count, showNotificationsTray })));
7171
}
7272

73+
it.each(['true', 'false', null])(
74+
'Ensures correct rendering of the notification tray based on the showNotifications query parameter value %s',
75+
async (showNotifications) => {
76+
await setupMockNotificationCountResponse();
77+
78+
const url = showNotifications ? `/?showNotifications=${showNotifications}` : '/';
79+
await renderComponent(url);
80+
await waitFor(() => {
81+
expect(screen.queryByTestId('notification-bell-icon')).toBeInTheDocument();
82+
if (showNotifications === 'true') {
83+
expect(screen.queryByTestId('notification-tray')).toBeInTheDocument();
84+
} else {
85+
expect(screen.queryByTestId('notification-tray')).not.toBeInTheDocument();
86+
}
87+
});
88+
},
89+
);
90+
91+
it.each(['discussion', 'grades'])(
92+
'Notification tray opens tab if app param is %s',
93+
async (app) => {
94+
await setupMockNotificationCountResponse();
95+
const url = `/?showNotifications=true&app=${app}`;
96+
await renderComponent(url);
97+
await waitFor(() => {
98+
expect(screen.queryByTestId('notification-bell-icon')).toBeInTheDocument();
99+
expect(screen.queryByTestId('notification-tray')).toBeInTheDocument();
100+
expect(screen.queryByTestId(`notification-tab-${app}`)).toHaveClass('active');
101+
});
102+
},
103+
);
104+
73105
it('Successfully showed bell icon and unseen count on it if unseen count is greater then 0.', async () => {
74106
await setupMockNotificationCountResponse();
75107
await renderComponent();

0 commit comments

Comments
 (0)