Skip to content

fix(integrations): Fix type/async errors in Offline tests #4755

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

Merged
merged 6 commits into from
Mar 23, 2022
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
3 changes: 3 additions & 0 deletions packages/integrations/src/offline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ type LocalForage = {
callback?: (err: any, result: U) => void,
): Promise<U>;
removeItem(key: string, callback?: (err: any) => void): Promise<void>;
length(): Promise<number>;
};

export type Item = { key: string; value: Event };

/**
* cache offline errors and send when connected
*/
Expand Down
58 changes: 27 additions & 31 deletions packages/integrations/test/offline.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { Event, EventProcessor, Hub, Integration } from '@sentry/types';
import { Event, EventProcessor, Hub, Integration, IntegrationClass } from '@sentry/types';
import * as utils from '@sentry/utils';

import { Offline } from '../src/offline';
import { Item, Offline } from '../src/offline';

// mock localforage methods
jest.mock('localforage', () => ({
createInstance(_options: { name: string }): any {
let items: { key: string; value: Event }[] = [];
let items: Item[] = [];

return {
async getItem(key: string): Event {
async getItem(key: string): Promise<Item | void> {
return items.find(item => item.key === key);
},
async iterate(callback: () => void): void {
async iterate(callback: (event: Event, key: string, index: number) => void): Promise<void> {
items.forEach((item, index) => {
callback(item.value, item.key, index);
});
},
async length(): number {
async length(): Promise<number> {
return items.length;
},
async removeItem(key: string): void {
async removeItem(key: string): Promise<void> {
items = items.filter(item => item.key !== key);
},
async setItem(key: string, value: Event): void {
async setItem(key: string, value: Event): Promise<void> {
items.push({
key,
value,
Expand All @@ -33,7 +33,7 @@ jest.mock('localforage', () => ({
},
}));

let integration: Integration;
let integration: Offline;
let online: boolean;

describe('Offline', () => {
Expand All @@ -52,15 +52,10 @@ describe('Offline', () => {
});

describe('when there are already events in the cache from a previous offline session', () => {
beforeEach(done => {
beforeEach(async () => {
const event = { message: 'previous event' };

integration.offlineEventStore
.setItem('previous', event)
.then(() => {
done();
})
.catch(error => error);
await integration.offlineEventStore.setItem('previous', event);
});

it('sends stored events', async () => {
Expand Down Expand Up @@ -130,16 +125,14 @@ describe('Offline', () => {
});

describe('when connectivity is restored', () => {
it('sends stored events', async done => {
it('sends stored events', async () => {
initIntegration();
setupOnce();
prepopulateEvents(1);
processEvents();
processEventListeners();

expect(await integration.offlineEventStore.length()).toEqual(0);

setImmediate(done);
});
});
});
Expand All @@ -150,7 +143,7 @@ let eventProcessors: EventProcessor[];
let events: Event[];

/** JSDoc */
function addGlobalEventProcessor(callback: () => void): void {
function addGlobalEventProcessor(callback: EventProcessor): void {
eventProcessors.push(callback);
}

Expand All @@ -160,11 +153,11 @@ function getCurrentHub(): Hub {
captureEvent(_event: Event): string {
return 'an-event-id';
},
getIntegration(_integration: Integration): any {
getIntegration<T extends Integration>(_integration: IntegrationClass<T>): T | null {
// pretend integration is enabled
return true;
return {} as T;
},
};
} as Hub;
}

/** JSDoc */
Expand All @@ -173,14 +166,17 @@ function initIntegration(options: { maxStoredEvents?: number } = {}): void {
eventProcessors = [];
events = [];

utils.getGlobalObject = jest.fn(() => ({
addEventListener: (_windowEvent, callback) => {
eventListeners.push(callback);
},
navigator: {
onLine: online,
},
}));
jest.spyOn(utils, 'getGlobalObject').mockImplementation(
() =>
({
addEventListener: (_windowEvent, callback) => {
eventListeners.push(callback);
},
navigator: {
onLine: online,
},
} as any),
);

integration = new Offline(options);
}
Expand Down