Skip to content

Commit

Permalink
fix(integrations): Fix type/async errors in Offline tests (#4755)
Browse files Browse the repository at this point in the history
This fixes a number of longstanding types errors in the tests for the `Offline` integration. It also simplifies some of the async code (to avoid mixing async functions and `done()`) and changes a mock so it is no longer assigning to a read-only property.

Getting these fixed will help unmuddy the waters with respect to the `Offline` test errors which are cropping up in our TS upgrade.
  • Loading branch information
lobsterkatie committed Mar 23, 2022
1 parent 2209dab commit 20b3e38
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
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

0 comments on commit 20b3e38

Please sign in to comment.