@dhis2/app-service-offline
Advanced tools
Comparing version 2.9.0 to 2.9.1
@@ -37,3 +37,7 @@ "use strict"; | ||
waitForNextUpdate | ||
} = (0, _reactHooks.renderHook)(() => (0, _onlineStatus.useOnlineStatus)()); | ||
} = (0, _reactHooks.renderHook)((...args) => (0, _onlineStatus.useOnlineStatus)(...args), { | ||
initialProps: { | ||
debounceDelay: 50 | ||
} | ||
}); | ||
(0, _reactHooks.act)(() => { | ||
@@ -45,3 +49,3 @@ // Trigger callback captured by addEventListener mock | ||
await waitForNextUpdate({ | ||
timeout: 1010 | ||
timeout: 60 | ||
}); | ||
@@ -51,3 +55,3 @@ expect(result.current.online).toBe(false); | ||
}); | ||
it('switches from offline to online when the "offline" event triggers', async () => { | ||
it('switches from offline to online when the "online" event triggers', async () => { | ||
jest.spyOn(navigator, 'onLine', 'get').mockReturnValueOnce(false); | ||
@@ -59,3 +63,7 @@ const events = {}; | ||
waitForNextUpdate | ||
} = (0, _reactHooks.renderHook)(() => (0, _onlineStatus.useOnlineStatus)()); | ||
} = (0, _reactHooks.renderHook)((...args) => (0, _onlineStatus.useOnlineStatus)(...args), { | ||
initialProps: { | ||
debounceDelay: 50 | ||
} | ||
}); | ||
(0, _reactHooks.act)(() => { | ||
@@ -66,3 +74,3 @@ events.online(new Event('online')); | ||
await waitForNextUpdate({ | ||
timeout: 1010 | ||
timeout: 60 | ||
}); | ||
@@ -124,2 +132,47 @@ expect(result.current.online).toBe(true); | ||
}); | ||
it('can have the debounce delay changed during its lifecycle', async () => { | ||
// Start with 150 ms debounce | ||
jest.spyOn(navigator, 'onLine', 'get').mockReturnValueOnce(true); | ||
const events = {}; | ||
window.addEventListener = jest.fn((event, cb) => events[event] = cb); | ||
const { | ||
result, | ||
waitForNextUpdate, | ||
rerender | ||
} = (0, _reactHooks.renderHook)((...args) => (0, _onlineStatus.useOnlineStatus)(...args), { | ||
initialProps: { | ||
debounceDelay: 150 | ||
} | ||
}); | ||
await (0, _reactHooks.act)(async () => { | ||
// Multiple events in succession | ||
events.offline(new Event('offline')); | ||
events.online(new Event('online')); | ||
events.offline(new Event('offline')); | ||
}); // Immediately, nothing should happen | ||
expect(result.current.online).toBe(true); // 150ms later, final "offline" event should finally resolve | ||
await waitForNextUpdate({ | ||
timeout: 160 | ||
}); | ||
expect(result.current.online).toBe(false); // Change to 50 ms debounce | ||
rerender({ | ||
debounceDelay: 50 | ||
}); | ||
await (0, _reactHooks.act)(async () => { | ||
// Multiple events in succession | ||
events.online(new Event('online')); | ||
events.offline(new Event('offline')); | ||
events.online(new Event('online')); | ||
}); // Immediately, nothing should happen | ||
expect(result.current.online).toBe(false); // 50ms later, final "online" event should finally resolve | ||
await waitForNextUpdate({ | ||
timeout: 60 | ||
}); | ||
expect(result.current.online).toBe(true); | ||
}); | ||
}); |
@@ -14,2 +14,5 @@ "use strict"; | ||
// TODO: Add option to periodically ping server to check online status. | ||
// TODO: Add logic to return a variable indicating unstable connection. | ||
/** | ||
@@ -21,5 +24,2 @@ * Returns the browser's online status. Updates in response to 'online' and | ||
* | ||
* TODO: Add option to periodically ping server to check online status. | ||
* TODO: Add logic to return a variable indicating unstable connection. | ||
* | ||
* @param {Object} [options] | ||
@@ -35,3 +35,3 @@ * @param {Number} [options.debounceDelay] - Timeout delay to debounce updates, in ms | ||
type | ||
}) => setOnline(type === 'online'), (options === null || options === void 0 ? void 0 : options.debounceDelay) || 1000), []); // on 'online' or 'offline' events, set state | ||
}) => setOnline(type === 'online'), (options === null || options === void 0 ? void 0 : options.debounceDelay) || 1000), [options]); // on 'online' or 'offline' events, set state | ||
@@ -38,0 +38,0 @@ (0, _react.useEffect)(() => { |
@@ -33,3 +33,7 @@ import { act, renderHook } from '@testing-library/react-hooks'; | ||
waitForNextUpdate | ||
} = renderHook(() => useOnlineStatus()); | ||
} = renderHook((...args) => useOnlineStatus(...args), { | ||
initialProps: { | ||
debounceDelay: 50 | ||
} | ||
}); | ||
act(() => { | ||
@@ -41,3 +45,3 @@ // Trigger callback captured by addEventListener mock | ||
await waitForNextUpdate({ | ||
timeout: 1010 | ||
timeout: 60 | ||
}); | ||
@@ -47,3 +51,3 @@ expect(result.current.online).toBe(false); | ||
}); | ||
it('switches from offline to online when the "offline" event triggers', async () => { | ||
it('switches from offline to online when the "online" event triggers', async () => { | ||
jest.spyOn(navigator, 'onLine', 'get').mockReturnValueOnce(false); | ||
@@ -55,3 +59,7 @@ const events = {}; | ||
waitForNextUpdate | ||
} = renderHook(() => useOnlineStatus()); | ||
} = renderHook((...args) => useOnlineStatus(...args), { | ||
initialProps: { | ||
debounceDelay: 50 | ||
} | ||
}); | ||
act(() => { | ||
@@ -62,3 +70,3 @@ events.online(new Event('online')); | ||
await waitForNextUpdate({ | ||
timeout: 1010 | ||
timeout: 60 | ||
}); | ||
@@ -120,2 +128,47 @@ expect(result.current.online).toBe(true); | ||
}); | ||
it('can have the debounce delay changed during its lifecycle', async () => { | ||
// Start with 150 ms debounce | ||
jest.spyOn(navigator, 'onLine', 'get').mockReturnValueOnce(true); | ||
const events = {}; | ||
window.addEventListener = jest.fn((event, cb) => events[event] = cb); | ||
const { | ||
result, | ||
waitForNextUpdate, | ||
rerender | ||
} = renderHook((...args) => useOnlineStatus(...args), { | ||
initialProps: { | ||
debounceDelay: 150 | ||
} | ||
}); | ||
await act(async () => { | ||
// Multiple events in succession | ||
events.offline(new Event('offline')); | ||
events.online(new Event('online')); | ||
events.offline(new Event('offline')); | ||
}); // Immediately, nothing should happen | ||
expect(result.current.online).toBe(true); // 150ms later, final "offline" event should finally resolve | ||
await waitForNextUpdate({ | ||
timeout: 160 | ||
}); | ||
expect(result.current.online).toBe(false); // Change to 50 ms debounce | ||
rerender({ | ||
debounceDelay: 50 | ||
}); | ||
await act(async () => { | ||
// Multiple events in succession | ||
events.online(new Event('online')); | ||
events.offline(new Event('offline')); | ||
events.online(new Event('online')); | ||
}); // Immediately, nothing should happen | ||
expect(result.current.online).toBe(false); // 50ms later, final "online" event should finally resolve | ||
await waitForNextUpdate({ | ||
timeout: 60 | ||
}); | ||
expect(result.current.online).toBe(true); | ||
}); | ||
}); |
import debounce from 'lodash/debounce'; | ||
import { useState, useEffect, useCallback } from 'react'; | ||
// TODO: Add option to periodically ping server to check online status. | ||
// TODO: Add logic to return a variable indicating unstable connection. | ||
@@ -10,5 +12,2 @@ /** | ||
* | ||
* TODO: Add option to periodically ping server to check online status. | ||
* TODO: Add logic to return a variable indicating unstable connection. | ||
* | ||
* @param {Object} [options] | ||
@@ -24,3 +23,3 @@ * @param {Number} [options.debounceDelay] - Timeout delay to debounce updates, in ms | ||
type | ||
}) => setOnline(type === 'online'), (options === null || options === void 0 ? void 0 : options.debounceDelay) || 1000), []); // on 'online' or 'offline' events, set state | ||
}) => setOnline(type === 'online'), (options === null || options === void 0 ? void 0 : options.debounceDelay) || 1000), [options]); // on 'online' or 'offline' events, set state | ||
@@ -27,0 +26,0 @@ useEffect(() => { |
@@ -15,5 +15,2 @@ declare type milliseconds = number; | ||
* | ||
* TODO: Add option to periodically ping server to check online status. | ||
* TODO: Add logic to return a variable indicating unstable connection. | ||
* | ||
* @param {Object} [options] | ||
@@ -20,0 +17,0 @@ * @param {Number} [options.debounceDelay] - Timeout delay to debounce updates, in ms |
{ | ||
"name": "@dhis2/app-service-offline", | ||
"description": "A runtime service for online/offline detection and offline caching", | ||
"version": "2.9.0", | ||
"version": "2.9.1", | ||
"main": "./build/cjs/index.js", | ||
@@ -36,3 +36,3 @@ "module": "./build/es/index.js", | ||
"peerDependencies": { | ||
"@dhis2/app-service-alerts": "2.9.0", | ||
"@dhis2/app-service-alerts": "2.9.1", | ||
"prop-types": "^15.7.2", | ||
@@ -39,0 +39,0 @@ "react": "^16.8.6", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
17786
416
0