Skip to content

Commit 01c4c49

Browse files
committed
fix(offline): remove offlineInterface.init() functionality
1 parent 5e20dca commit 01c4c49

5 files changed

Lines changed: 15 additions & 84 deletions

File tree

services/offline/src/lib/__tests__/offline-provider.test.tsx

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { AlertsProvider } from '@dhis2/app-service-alerts'
21
import { render, screen, waitFor } from '@testing-library/react'
32
import React from 'react'
43
import { mockOfflineInterface } from '../../utils/test-mocks'
@@ -27,31 +26,14 @@ afterEach(() => {
2726
describe('Testing offline provider', () => {
2827
it('Should render without failing', () => {
2928
render(
30-
<AlertsProvider>
31-
<OfflineProvider offlineInterface={mockOfflineInterface}>
32-
<div data-testid="test-div" />
33-
</OfflineProvider>
34-
</AlertsProvider>
29+
<OfflineProvider offlineInterface={mockOfflineInterface}>
30+
<div data-testid="test-div" />
31+
</OfflineProvider>
3532
)
3633

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

40-
it('Should initialize the offline interface with an update prompt', () => {
41-
render(
42-
<AlertsProvider>
43-
<OfflineProvider offlineInterface={mockOfflineInterface} />
44-
</AlertsProvider>
45-
)
46-
47-
expect(mockOfflineInterface.init).toHaveBeenCalledTimes(1)
48-
49-
// Expect to have been called with a 'promptUpdate' function
50-
const arg = mockOfflineInterface.init.mock.calls[0][0]
51-
expect(arg).toHaveProperty('promptUpdate')
52-
expect(typeof arg['promptUpdate']).toBe('function')
53-
})
54-
5537
it('Should sync cached sections with indexedDB', async () => {
5638
const testOfflineInterface = {
5739
...mockOfflineInterface,
@@ -71,11 +53,9 @@ describe('Testing offline provider', () => {
7153
}
7254

7355
render(
74-
<AlertsProvider>
75-
<OfflineProvider offlineInterface={testOfflineInterface}>
76-
<CachedSections />
77-
</OfflineProvider>
78-
</AlertsProvider>
56+
<OfflineProvider offlineInterface={testOfflineInterface}>
57+
<CachedSections />
58+
</OfflineProvider>
7959
)
8060

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

10484
render(
105-
<AlertsProvider>
106-
<OfflineProvider offlineInterface={mockOfflineInterface}>
107-
<TestConsumer />
108-
</OfflineProvider>
109-
</AlertsProvider>
85+
<OfflineProvider offlineInterface={mockOfflineInterface}>
86+
<TestConsumer />
87+
</OfflineProvider>
11088
)
11189

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

11593
it('Should render without failing when no offlineInterface is provided', () => {
11694
render(
117-
<AlertsProvider>
118-
<OfflineProvider>
119-
<div data-testid="test-div" />
120-
</OfflineProvider>
121-
</AlertsProvider>
95+
<OfflineProvider>
96+
<div data-testid="test-div" />
97+
</OfflineProvider>
12298
)
12399
expect(screen.getByTestId('test-div')).toBeInTheDocument()
124100
})
@@ -129,15 +105,11 @@ describe('Testing offline provider', () => {
129105
pwaEnabled: false,
130106
}
131107
render(
132-
<AlertsProvider>
133-
<OfflineProvider offlineInterface={testOfflineInterface}>
134-
<div data-testid="test-div" />
135-
</OfflineProvider>
136-
</AlertsProvider>
108+
<OfflineProvider offlineInterface={testOfflineInterface}>
109+
<div data-testid="test-div" />
110+
</OfflineProvider>
137111
)
138112

139-
// Init should still be called - see comments in offline-provider.js
140-
expect(testOfflineInterface.init).toHaveBeenCalled()
141113
expect(screen.getByTestId('test-div')).toBeInTheDocument()
142114
})
143115
})

services/offline/src/lib/offline-interface.tsx

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { useAlert } from '@dhis2/app-service-alerts'
21
import PropTypes from 'prop-types'
32
import React, { createContext, useContext } from 'react'
43
import { OfflineInterface } from '../types'
54

65
// This is to prevent 'offlineInterface could be null' type-checking errors
76
const noopOfflineInterface: OfflineInterface = {
87
pwaEnabled: false,
9-
init: () => () => null,
108
startRecording: async () => undefined,
119
getCachedSections: async () => [],
1210
removeSection: async () => false,
@@ -21,16 +19,6 @@ interface OfflineInterfaceProviderInput {
2119
children: React.ReactNode
2220
}
2321

24-
interface AlertAction {
25-
label: string
26-
onClick: () => void
27-
}
28-
29-
interface PromptUpdateAlertOptions {
30-
message: string
31-
actions: AlertAction[]
32-
}
33-
3422
/**
3523
* Receives an OfflineInterface instance as a prop (presumably from the app
3624
* adapter) and provides it as context for other offline tools.
@@ -43,19 +31,6 @@ export function OfflineInterfaceProvider({
4331
offlineInterface,
4432
children,
4533
}: OfflineInterfaceProviderInput): JSX.Element {
46-
const { show } = useAlert(
47-
({ message }: PromptUpdateAlertOptions) => message,
48-
({ actions }: PromptUpdateAlertOptions) => ({
49-
actions,
50-
permanent: true,
51-
})
52-
)
53-
54-
React.useEffect(() => {
55-
// Init returns a tear-down function
56-
return offlineInterface.init({ promptUpdate: show })
57-
}, []) // eslint-disable-line react-hooks/exhaustive-deps
58-
5934
return (
6035
<OfflineInterfaceContext.Provider value={offlineInterface}>
6136
{children}

services/offline/src/lib/offline-provider.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ export function OfflineProvider({
2020
return <>{children}</>
2121
}
2222

23-
// If PWA is not enabled, just init interface to make sure new SW gets
24-
// activated with code that unregisters SW. Not technically necessary if a
25-
// killswitch SW takes over, but the killswitch may not always be in use.
26-
// Then, skip adding any context
27-
if (!offlineInterface.pwaEnabled) {
28-
offlineInterface.init({ promptUpdate: ({ onConfirm }) => onConfirm() })
29-
return <>{children}</>
30-
}
31-
3223
return (
3324
<OfflineInterfaceProvider offlineInterface={offlineInterface}>
3425
<CacheableSectionProvider>{children}</CacheableSectionProvider>
@@ -39,7 +30,6 @@ export function OfflineProvider({
3930
OfflineProvider.propTypes = {
4031
children: PropTypes.node,
4132
offlineInterface: PropTypes.shape({
42-
init: PropTypes.func,
4333
pwaEnabled: PropTypes.bool,
4434
}),
4535
}

services/offline/src/types.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ export interface GlobalStateStore {
3535

3636
// Offline Interface types
3737

38-
interface PromptUpdate {
39-
(params: { message: string; action: string; onConfirm: () => void }): void
40-
}
41-
4238
interface StartRecording {
4339
(params: {
4440
sectionId: string
@@ -58,7 +54,6 @@ export interface IndexedDBCachedSection {
5854

5955
export interface OfflineInterface {
6056
readonly pwaEnabled: boolean
61-
init: (params: { promptUpdate: PromptUpdate }) => () => void
6257
startRecording: StartRecording
6358
getCachedSections: () => Promise<IndexedDBCachedSection[]>
6459
removeSection: (id: string) => Promise<boolean>

services/offline/src/utils/test-mocks.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export const failedMessageRecordingMock = jest
3030

3131
export const mockOfflineInterface = {
3232
pwaEnabled: true,
33-
init: jest.fn(),
3433
startRecording: successfulRecordingMock,
3534
getCachedSections: jest.fn().mockResolvedValue([]),
3635
removeSection: jest.fn().mockResolvedValue(true),

0 commit comments

Comments
 (0)