-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: SB-765 Migrate notifications list query to Apollo
- Loading branch information
Showing
13 changed files
with
225 additions
and
194 deletions.
There are no files selected for viewing
This file contains 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
39 changes: 19 additions & 20 deletions
39
packages/webapp/src/shared/components/notifications/notifications.component.tsx
This file contains 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 |
---|---|---|
@@ -1,36 +1,35 @@ | ||
import { Suspense, useEffect } from 'react'; | ||
import ClickAwayListener from 'react-click-away-listener'; | ||
import { useQueryLoader } from 'react-relay'; | ||
import graphql from 'babel-plugin-relay/macro'; | ||
import { NetworkStatus, useQuery } from '@apollo/client'; | ||
import { useOpenState } from '../../hooks/useOpenState'; | ||
import { notificationsListQuery } from './__generated__/notificationsListQuery.graphql'; | ||
import { NotificationsButton } from './notificationsButton'; | ||
import { NotificationsList } from './notificationsList'; | ||
import { NOTIFICATIONS_LIST_QUERY } from './notifications.graphql'; | ||
import { NOTIFICATIONS_PER_PAGE } from './notificationsList/notificationsList.constants'; | ||
|
||
export const Notifications = () => { | ||
const notifications = useOpenState(false); | ||
|
||
const [listQueryRef, loadListQuery] = useQueryLoader<notificationsListQuery>( | ||
graphql` | ||
query notificationsListQuery($count: Int = 20, $cursor: String) { | ||
...notificationsListContent | ||
...notificationsButtonContent | ||
} | ||
` | ||
); | ||
const { loading, data, fetchMore, networkStatus } = useQuery(NOTIFICATIONS_LIST_QUERY); | ||
|
||
useEffect(() => { | ||
loadListQuery({}); | ||
}, [loadListQuery]); | ||
if (loading && networkStatus === NetworkStatus.loading) { | ||
return <NotificationsButton.Fallback />; | ||
} | ||
|
||
if (!listQueryRef) return null; | ||
const onLoadMore = (cursor, count = NOTIFICATIONS_PER_PAGE) => { | ||
fetchMore({ | ||
variables: { | ||
cursor, | ||
count, | ||
}, | ||
}); | ||
}; | ||
|
||
return ( | ||
<Suspense fallback={<NotificationsButton.Fallback />}> | ||
<NotificationsButton listQueryRef={listQueryRef} onClick={notifications.toggle} /> | ||
<> | ||
<NotificationsButton queryResult={data} onClick={notifications.toggle} /> | ||
<ClickAwayListener onClickAway={notifications.clickAway}> | ||
<NotificationsList isOpen={notifications.isOpen} listQueryRef={listQueryRef} /> | ||
<NotificationsList isOpen={notifications.isOpen} queryResult={data} loading={loading} onLoadMore={onLoadMore} /> | ||
</ClickAwayListener> | ||
</Suspense> | ||
</> | ||
); | ||
}; |
8 changes: 8 additions & 0 deletions
8
packages/webapp/src/shared/components/notifications/notifications.graphql.ts
This file contains 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,8 @@ | ||
import { gql } from '../../services/graphqlApi/__generated/gql'; | ||
|
||
export const NOTIFICATIONS_LIST_QUERY = gql(/* GraphQL */ ` | ||
query notificationsListQuery($count: Int = 20, $cursor: String) { | ||
...notificationsListContentFragment | ||
...notificationsButtonContent | ||
} | ||
`); |
41 changes: 12 additions & 29 deletions
41
...src/shared/components/notifications/notificationsButton/notificationsButton.component.tsx
This file contains 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
49 changes: 26 additions & 23 deletions
49
...components/notifications/notificationsList/__tests__/notificationsList.component.spec.tsx
This file contains 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 |
---|---|---|
@@ -1,66 +1,69 @@ | ||
import { screen } from '@testing-library/react'; | ||
import { times } from 'ramda'; | ||
import { useQuery } from '@apollo/client'; | ||
import { render } from '../../../../../tests/utils/rendering'; | ||
import { NotificationsList, NotificationsListProps } from '../notificationsList.component'; | ||
import { fillNotificationsListQuery, notificationFactory } from '../../../../../mocks/factories'; | ||
import { ExtractNodeType } from '../../../../utils/graphql'; | ||
import { notificationsListContent$data } from '../__generated__/notificationsListContent.graphql'; | ||
import { getRelayEnv } from '../../../../../tests/utils/relay'; | ||
import { NOTIFICATIONS_LIST_QUERY } from '../../notifications.graphql'; | ||
|
||
describe('NotificationsList: Component', () => { | ||
const Component = (props: Partial<NotificationsListProps>) => ( | ||
<NotificationsList isOpen listQueryRef={{} as any} {...props} /> | ||
); | ||
const Component = (props: Partial<NotificationsListProps>) => { | ||
const { loading, data } = useQuery(NOTIFICATIONS_LIST_QUERY); | ||
return <NotificationsList isOpen onLoadMore={() => null} loading={loading} queryResult={data} {...props} />; | ||
}; | ||
|
||
const renderWithNotifications = ( | ||
notifications: Array<Partial<ExtractNodeType<notificationsListContent$data['allNotifications']>>> | ||
) => { | ||
const env = getRelayEnv(); | ||
fillNotificationsListQuery(env, notifications); | ||
const mockRequest = fillNotificationsListQuery(env, notifications); | ||
|
||
if (mockRequest.result?.data) { | ||
mockRequest.result.data.hasUnreadNotifications = false; | ||
} | ||
const apolloMocks = [mockRequest]; | ||
|
||
render( | ||
<Component | ||
listQueryRef={ | ||
{ | ||
environment: env, | ||
isDisposed: false, | ||
} as any | ||
} | ||
/>, | ||
{ relayEnvironment: env } | ||
); | ||
render(<Component />, { relayEnvironment: env, apolloMocks }); | ||
}; | ||
|
||
it('should render no items correctly', () => { | ||
it('should render no items correctly', async () => { | ||
renderWithNotifications([]); | ||
|
||
expect(screen.getAllByLabelText('Loading notification')).toHaveLength(2); | ||
expect(await screen.findByText('Mark all as read')).toBeInTheDocument(); | ||
expect(await screen.findByText('No notifications')).toBeInTheDocument(); | ||
expect(screen.queryByRole('link')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should not render non registered notifications', () => { | ||
it('should not render non registered notifications', async () => { | ||
renderWithNotifications([ | ||
{ | ||
notificationFactory({ | ||
type: 'some_random_type_that_doesnt_exist', | ||
}, | ||
}), | ||
]); | ||
|
||
expect(screen.queryByRole('link')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should render correct notifications', () => { | ||
it('should render correct notifications', async () => { | ||
const notifications = times(() => notificationFactory(), 3); | ||
renderWithNotifications(notifications); | ||
|
||
expect(screen.getAllByRole('link')).toHaveLength(notifications.length); | ||
expect(screen.getAllByLabelText('Loading notification')).toHaveLength(2); | ||
expect(await screen.findByText('Mark all as read')).toBeInTheDocument(); | ||
expect(await screen.findAllByRole('link')).toHaveLength(notifications.length); | ||
}); | ||
|
||
it('should not render wrong notifications', () => { | ||
it('should not render wrong notifications', async () => { | ||
const correctNotifications = times(() => notificationFactory(), 3); | ||
const malformedNotification = notificationFactory({ | ||
data: null, | ||
}); | ||
renderWithNotifications([...correctNotifications, malformedNotification]); | ||
|
||
expect(screen.getAllByRole('link')).toHaveLength(correctNotifications.length); | ||
expect(await screen.findAllByRole('link')).toHaveLength(correctNotifications.length); | ||
}); | ||
}); |
This file contains 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
22 changes: 22 additions & 0 deletions
22
...webapp/src/shared/components/notifications/notificationsList/notificationsList.graphql.ts
This file contains 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,22 @@ | ||
import { gql } from '../../../services/graphqlApi/__generated/gql'; | ||
|
||
export const notificationsListContentFragment = gql(/* GraphQL */ ` | ||
fragment notificationsListContentFragment on Query { | ||
hasUnreadNotifications | ||
allNotifications(first: $count, after: $cursor) { | ||
edges { | ||
node { | ||
id | ||
data | ||
createdAt | ||
readAt | ||
type | ||
} | ||
} | ||
pageInfo { | ||
endCursor | ||
hasNextPage | ||
} | ||
} | ||
} | ||
`); |
Oops, something went wrong.