This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(network-status): implement useNetworkStatus hook
- Loading branch information
1 parent
f7ab985
commit 452015a
Showing
6 changed files
with
43 additions
and
24 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
27 changes: 23 additions & 4 deletions
27
src/__tests__/components/network-status/NetworkStatusMessage.test.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,23 +1,42 @@ | ||
import { render, shallow } from 'enzyme' | ||
import React from 'react' | ||
|
||
import { NetworkStatusMessage } from '../../../components/network-status' | ||
import { | ||
NetworkStatusMessage, | ||
OFFLINE_MESSAGE, | ||
ONLINE_MESSAGE, | ||
} from '../../../components/network-status/NetworkStatusMessage' | ||
import { useNetworkStatus } from '../../../components/network-status/useNetworkStatus' | ||
|
||
jest.mock('../../../components/network-status/useNetworkStatus') | ||
const useNetworkStatusCast = (useNetworkStatus as unknown) as jest.MockInstance< | ||
ReturnType<typeof useNetworkStatus>, | ||
any | ||
> | ||
|
||
describe('NetworkStatusMessage', () => { | ||
it('returns null if the app has always been online', () => { | ||
const wrapper = shallow(<NetworkStatusMessage online wasOffline={false} />) | ||
useNetworkStatusCast.mockReturnValue({ | ||
isOnline: true, | ||
wasOffline: false, | ||
}) | ||
const wrapper = shallow(<NetworkStatusMessage />) | ||
expect(wrapper.equals(null as any)).toBe(true) | ||
}) | ||
it(`shows the message "${OFFLINE_MESSAGE}" if the app goes offline`, () => { | ||
const wrapper = render(<NetworkStatusMessage online={false} wasOffline={false} />) | ||
useNetworkStatusCast.mockReturnValue({ | ||
isOnline: false, | ||
wasOffline: false, | ||
}) | ||
const wrapper = render(<NetworkStatusMessage />) | ||
expect(wrapper.text()).toContain(OFFLINE_MESSAGE) | ||
}) | ||
it(`shows the message "${ONLINE_MESSAGE}" if the app goes back online after it was offline`, () => { | ||
const wrapper = render(<NetworkStatusMessage online wasOffline />) | ||
useNetworkStatusCast.mockReturnValue({ | ||
isOnline: true, | ||
wasOffline: true, | ||
}) | ||
const wrapper = render(<NetworkStatusMessage />) | ||
expect(wrapper.text()).toContain(ONLINE_MESSAGE) | ||
}) | ||
}) |
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
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,2 @@ | ||
export { NetworkStatusMessage } from './NetworkStatusMessage' | ||
export { useNetworkStatus } from './useNetworkStatus' |
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,4 @@ | ||
export interface NetworkStatus { | ||
isOnline: boolean | ||
wasOffline: boolean | ||
} |
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