Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: disable network checker if navigator is unavailable #876

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
izaaz marked this conversation as resolved.
Show resolved Hide resolved
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();
});
});
Loading