Skip to content

Commit

Permalink
fix: disable network checker if navigator is unavailable (#876)
Browse files Browse the repository at this point in the history
  • Loading branch information
izaaz authored Sep 18, 2024
2 parents 5c00e55 + ac29dec commit c64df83
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export const networkConnectivityCheckerPlugin = (): BeforePlugin => {
};

const setup = async (config: BrowserConfig, amplitude: BrowserClient) => {
if (typeof navigator === 'undefined') {
config.loggerProvider.debug(
'Network connectivity checker plugin is disabled because navigator is not available.',
);
config.offline = false;
return;
}

config.offline = !navigator.onLine;

addNetworkListener('online', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,17 @@ describe('networkConnectivityCheckerPlugin', () => {
expect(removeEventListenerSpy).toHaveBeenCalledWith('online', expect.any(Function));
expect(removeEventListenerSpy).toHaveBeenCalledWith('offline', expect.any(Function));
});

test('should do nothing when not on a browser', async () => {
const plugin = networkConnectivityCheckerPlugin();
// @ts-expect-error we are mocking a node.js environment
jest.spyOn(window, 'navigator', 'get').mockReturnValue(undefined);
const addEventListenerSpy = jest.spyOn(window, 'addEventListener');

await plugin.setup?.(config, amplitude);

expect(config.offline).toEqual(false);
expect(addEventListenerSpy).not.toHaveBeenCalled();
addEventListenerSpy.mockRestore();
});
});

0 comments on commit c64df83

Please sign in to comment.