From 6ebe2ce50d091d743e84d3de40eb48d11feea54d Mon Sep 17 00:00:00 2001 From: Muhammad Adeel Tajamul <77053848+muhammadadeeltajamul@users.noreply.github.com> Date: Mon, 18 Nov 2024 13:42:20 +0500 Subject: [PATCH] feat: added support to open notification tray from query params (#615) --- src/Notification/data/hook.js | 2 +- src/Notification/index.jsx | 14 ++++++++++++- src/Notification/index.test.jsx | 36 +++++++++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Notification/data/hook.js b/src/Notification/data/hook.js index feb7489b..f9f73130 100644 --- a/src/Notification/data/hook.js +++ b/src/Notification/data/hook.js @@ -98,7 +98,7 @@ export function useNotification() { newNotificationIds, notificationsKeyValuePair, pagination, } = normalizedData; - const existingNotificationIds = apps[appName]; + const existingNotificationIds = apps[appName] || []; const { count } = tabsCount; return { diff --git a/src/Notification/index.jsx b/src/Notification/index.jsx index 4714b90a..f1865f42 100644 --- a/src/Notification/index.jsx +++ b/src/Notification/index.jsx @@ -4,6 +4,7 @@ import React, { import classNames from 'classnames'; import PropTypes from 'prop-types'; +import { useSearchParams } from 'react-router-dom'; import { getConfig } from '@edx/frontend-platform'; import { useIntl } from '@edx/frontend-platform/i18n'; @@ -26,12 +27,14 @@ const Notifications = ({ notificationAppData, showLeftMargin }) => { const intl = useIntl(); const popoverRef = useRef(null); const headerRef = useRef(null); + const [searchParams] = useSearchParams(); const buttonRef = useRef(null); const [enableNotificationTray, setEnableNotificationTray] = useState(false); const [appName, setAppName] = useState('discussion'); const [isHeaderVisible, setIsHeaderVisible] = useState(true); const [notificationData, setNotificationData] = useState({}); const [tabsCount, setTabsCount] = useState(notificationAppData?.tabsCount); + const [openFlag, setOpenFlag] = useState(false); const isOnMediumScreen = useIsOnMediumScreen(); const isOnLargeScreen = useIsOnLargeScreen(); @@ -45,6 +48,15 @@ const Notifications = ({ notificationAppData, showLeftMargin }) => { } }, []); + useEffect(() => { + if (openFlag || Object.keys(tabsCount).length === 0) { + return; + } + setAppName(searchParams.get('app') || 'discussion'); + setEnableNotificationTray(searchParams.get('showNotifications') === 'true'); + setOpenFlag(true); + }, [tabsCount, openFlag, searchParams]); + useEffect(() => { setTabsCount(notificationAppData.tabsCount); setNotificationData(prevData => ({ @@ -97,10 +109,10 @@ const Notifications = ({ notificationAppData, showLeftMargin }) => { const notificationContextValue = useMemo(() => ({ enableNotificationTray, - appName, handleActiveTab, updateNotificationData, ...notificationData, + appName, }), [enableNotificationTray, appName, handleActiveTab, updateNotificationData, notificationData]); return ( diff --git a/src/Notification/index.test.jsx b/src/Notification/index.test.jsx index 57fe63d7..9e7df39a 100644 --- a/src/Notification/index.test.jsx +++ b/src/Notification/index.test.jsx @@ -34,9 +34,9 @@ const contextValue = { config: {}, }; -async function renderComponent() { +async function renderComponent(url = '/') { render( - + @@ -70,6 +70,38 @@ describe('Notification test cases.', () => { .reply(200, (Factory.build('notificationsCount', { count, showNotificationsTray }))); } + it.each(['true', 'false', null])( + 'Ensures correct rendering of the notification tray based on the showNotifications query parameter value %s', + async (showNotifications) => { + await setupMockNotificationCountResponse(); + + const url = showNotifications ? `/?showNotifications=${showNotifications}` : '/'; + await renderComponent(url); + await waitFor(() => { + expect(screen.queryByTestId('notification-bell-icon')).toBeInTheDocument(); + if (showNotifications === 'true') { + expect(screen.queryByTestId('notification-tray')).toBeInTheDocument(); + } else { + expect(screen.queryByTestId('notification-tray')).not.toBeInTheDocument(); + } + }); + }, + ); + + it.each(['discussion', 'grades'])( + 'Notification tray opens tab if app param is %s', + async (app) => { + await setupMockNotificationCountResponse(); + const url = `/?showNotifications=true&app=${app}`; + await renderComponent(url); + await waitFor(() => { + expect(screen.queryByTestId('notification-bell-icon')).toBeInTheDocument(); + expect(screen.queryByTestId('notification-tray')).toBeInTheDocument(); + expect(screen.queryByTestId(`notification-tab-${app}`)).toHaveClass('active'); + }); + }, + ); + it('Successfully showed bell icon and unseen count on it if unseen count is greater then 0.', async () => { await setupMockNotificationCountResponse(); await renderComponent();