Skip to content

Commit

Permalink
Merge pull request #1041 from dhis2/fix-remove-update-alert
Browse files Browse the repository at this point in the history
fix(offline): remove offlineInterface.init() functionality
  • Loading branch information
KaiVandivier authored Oct 7, 2021
2 parents 5e20dca + 01c4c49 commit e452100
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 84 deletions.
58 changes: 15 additions & 43 deletions services/offline/src/lib/__tests__/offline-provider.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AlertsProvider } from '@dhis2/app-service-alerts'
import { render, screen, waitFor } from '@testing-library/react'
import React from 'react'
import { mockOfflineInterface } from '../../utils/test-mocks'
Expand Down Expand Up @@ -27,31 +26,14 @@ afterEach(() => {
describe('Testing offline provider', () => {
it('Should render without failing', () => {
render(
<AlertsProvider>
<OfflineProvider offlineInterface={mockOfflineInterface}>
<div data-testid="test-div" />
</OfflineProvider>
</AlertsProvider>
<OfflineProvider offlineInterface={mockOfflineInterface}>
<div data-testid="test-div" />
</OfflineProvider>
)

expect(screen.getByTestId('test-div')).toBeInTheDocument()
})

it('Should initialize the offline interface with an update prompt', () => {
render(
<AlertsProvider>
<OfflineProvider offlineInterface={mockOfflineInterface} />
</AlertsProvider>
)

expect(mockOfflineInterface.init).toHaveBeenCalledTimes(1)

// Expect to have been called with a 'promptUpdate' function
const arg = mockOfflineInterface.init.mock.calls[0][0]
expect(arg).toHaveProperty('promptUpdate')
expect(typeof arg['promptUpdate']).toBe('function')
})

it('Should sync cached sections with indexedDB', async () => {
const testOfflineInterface = {
...mockOfflineInterface,
Expand All @@ -71,11 +53,9 @@ describe('Testing offline provider', () => {
}

render(
<AlertsProvider>
<OfflineProvider offlineInterface={testOfflineInterface}>
<CachedSections />
</OfflineProvider>
</AlertsProvider>
<OfflineProvider offlineInterface={testOfflineInterface}>
<CachedSections />
</OfflineProvider>
)

const { getByTestId } = screen
Expand All @@ -102,23 +82,19 @@ describe('Testing offline provider', () => {
}

render(
<AlertsProvider>
<OfflineProvider offlineInterface={mockOfflineInterface}>
<TestConsumer />
</OfflineProvider>
</AlertsProvider>
<OfflineProvider offlineInterface={mockOfflineInterface}>
<TestConsumer />
</OfflineProvider>
)

expect(screen.getByTestId('test-div')).toBeInTheDocument()
})

it('Should render without failing when no offlineInterface is provided', () => {
render(
<AlertsProvider>
<OfflineProvider>
<div data-testid="test-div" />
</OfflineProvider>
</AlertsProvider>
<OfflineProvider>
<div data-testid="test-div" />
</OfflineProvider>
)
expect(screen.getByTestId('test-div')).toBeInTheDocument()
})
Expand All @@ -129,15 +105,11 @@ describe('Testing offline provider', () => {
pwaEnabled: false,
}
render(
<AlertsProvider>
<OfflineProvider offlineInterface={testOfflineInterface}>
<div data-testid="test-div" />
</OfflineProvider>
</AlertsProvider>
<OfflineProvider offlineInterface={testOfflineInterface}>
<div data-testid="test-div" />
</OfflineProvider>
)

// Init should still be called - see comments in offline-provider.js
expect(testOfflineInterface.init).toHaveBeenCalled()
expect(screen.getByTestId('test-div')).toBeInTheDocument()
})
})
25 changes: 0 additions & 25 deletions services/offline/src/lib/offline-interface.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { useAlert } from '@dhis2/app-service-alerts'
import PropTypes from 'prop-types'
import React, { createContext, useContext } from 'react'
import { OfflineInterface } from '../types'

// This is to prevent 'offlineInterface could be null' type-checking errors
const noopOfflineInterface: OfflineInterface = {
pwaEnabled: false,
init: () => () => null,
startRecording: async () => undefined,
getCachedSections: async () => [],
removeSection: async () => false,
Expand All @@ -21,16 +19,6 @@ interface OfflineInterfaceProviderInput {
children: React.ReactNode
}

interface AlertAction {
label: string
onClick: () => void
}

interface PromptUpdateAlertOptions {
message: string
actions: AlertAction[]
}

/**
* Receives an OfflineInterface instance as a prop (presumably from the app
* adapter) and provides it as context for other offline tools.
Expand All @@ -43,19 +31,6 @@ export function OfflineInterfaceProvider({
offlineInterface,
children,
}: OfflineInterfaceProviderInput): JSX.Element {
const { show } = useAlert(
({ message }: PromptUpdateAlertOptions) => message,
({ actions }: PromptUpdateAlertOptions) => ({
actions,
permanent: true,
})
)

React.useEffect(() => {
// Init returns a tear-down function
return offlineInterface.init({ promptUpdate: show })
}, []) // eslint-disable-line react-hooks/exhaustive-deps

return (
<OfflineInterfaceContext.Provider value={offlineInterface}>
{children}
Expand Down
10 changes: 0 additions & 10 deletions services/offline/src/lib/offline-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ export function OfflineProvider({
return <>{children}</>
}

// If PWA is not enabled, just init interface to make sure new SW gets
// activated with code that unregisters SW. Not technically necessary if a
// killswitch SW takes over, but the killswitch may not always be in use.
// Then, skip adding any context
if (!offlineInterface.pwaEnabled) {
offlineInterface.init({ promptUpdate: ({ onConfirm }) => onConfirm() })
return <>{children}</>
}

return (
<OfflineInterfaceProvider offlineInterface={offlineInterface}>
<CacheableSectionProvider>{children}</CacheableSectionProvider>
Expand All @@ -39,7 +30,6 @@ export function OfflineProvider({
OfflineProvider.propTypes = {
children: PropTypes.node,
offlineInterface: PropTypes.shape({
init: PropTypes.func,
pwaEnabled: PropTypes.bool,
}),
}
5 changes: 0 additions & 5 deletions services/offline/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ export interface GlobalStateStore {

// Offline Interface types

interface PromptUpdate {
(params: { message: string; action: string; onConfirm: () => void }): void
}

interface StartRecording {
(params: {
sectionId: string
Expand All @@ -58,7 +54,6 @@ export interface IndexedDBCachedSection {

export interface OfflineInterface {
readonly pwaEnabled: boolean
init: (params: { promptUpdate: PromptUpdate }) => () => void
startRecording: StartRecording
getCachedSections: () => Promise<IndexedDBCachedSection[]>
removeSection: (id: string) => Promise<boolean>
Expand Down
1 change: 0 additions & 1 deletion services/offline/src/utils/test-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const failedMessageRecordingMock = jest

export const mockOfflineInterface = {
pwaEnabled: true,
init: jest.fn(),
startRecording: successfulRecordingMock,
getCachedSections: jest.fn().mockResolvedValue([]),
removeSection: jest.fn().mockResolvedValue(true),
Expand Down

0 comments on commit e452100

Please sign in to comment.