Skip to content

Commit

Permalink
[Test] Enable unit test suite: telemetry_sender.test.ts (#500)
Browse files Browse the repository at this point in the history
* [Test] Enable unit test suite: telemetry_sender.test.ts (#498)

All the unit tests related to unused telemetry are temporarily
skipped at forking. Unit tests of the disabled telemetry
functions should also be modified correspondingly. To build
a clean unit test, we decide to modify and enable all the
working unit tests. This PR modifies and enables
telemetry_sender.test.ts.

Signed-off-by: Anan Zhuang <[email protected]>

* [Test] Enable unit test suite: telemetry_sender.test.ts

Remove all the original comment out lines. If needs
restore the unit test, can revert this commit.

Signed-off-by: Anan Zhuang <[email protected]>
  • Loading branch information
ananzh authored Jun 26, 2021
1 parent 061feb9 commit 1e7b8f1
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions src/plugins/telemetry/public/services/telemetry_sender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Object.defineProperty(window, 'localStorage', {
value: mockLocalStorage,
});

describe.skip('TelemetrySender', () => {
describe('TelemetrySender', () => {
beforeEach(() => {
mockLocalStorage.getItem.mockClear();
mockLocalStorage.setItem.mockClear();
Expand All @@ -56,7 +56,7 @@ describe.skip('TelemetrySender', () => {
})
);

describe.skip('constructor', () => {
describe('constructor', () => {
it('defaults lastReport if unset', () => {
const telemetryService = mockTelemetryService();
const telemetrySender = new TelemetrySender(telemetryService);
Expand All @@ -74,7 +74,7 @@ describe.skip('TelemetrySender', () => {
});
});

describe.skip('saveToBrowser', () => {
describe('saveToBrowser', () => {
it('uses lastReport', () => {
const lastReport = `${Date.now()}`;
const telemetryService = mockTelemetryService();
Expand All @@ -90,7 +90,7 @@ describe.skip('TelemetrySender', () => {
});
});

describe.skip('shouldSendReport', () => {
describe('shouldSendReport', () => {
it('returns false whenever optIn is false', () => {
const telemetryService = mockTelemetryService();
telemetryService.getIsOptedIn = jest.fn().mockReturnValue(false);
Expand All @@ -108,7 +108,7 @@ describe.skip('TelemetrySender', () => {
const shouldSendRerpot = telemetrySender['shouldSendReport']();

expect(telemetrySender['lastReported']).toBeUndefined();
expect(shouldSendRerpot).toBe(true);
expect(shouldSendRerpot).toBe(false);
});

it('returns true if lastReported passed REPORT_INTERVAL_MS', () => {
Expand All @@ -119,7 +119,7 @@ describe.skip('TelemetrySender', () => {
const telemetrySender = new TelemetrySender(telemetryService);
telemetrySender['lastReported'] = `${lastReported}`;
const shouldSendRerpot = telemetrySender['shouldSendReport']();
expect(shouldSendRerpot).toBe(true);
expect(shouldSendRerpot).toBe(false);
});

it('returns false if lastReported is within REPORT_INTERVAL_MS', () => {
Expand All @@ -139,10 +139,10 @@ describe.skip('TelemetrySender', () => {
const telemetrySender = new TelemetrySender(telemetryService);
telemetrySender['lastReported'] = `random_malformed_string`;
const shouldSendRerpot = telemetrySender['shouldSendReport']();
expect(shouldSendRerpot).toBe(true);
expect(shouldSendRerpot).toBe(false);
});

describe.skip('sendIfDue', () => {
describe('sendIfDue', () => {
let originalFetch: typeof window['fetch'];
let mockFetch: jest.Mock<typeof window['fetch']>;

Expand Down Expand Up @@ -189,15 +189,8 @@ describe.skip('TelemetrySender', () => {
telemetrySender['isSending'] = false;
await telemetrySender['sendIfDue']();

expect(telemetryService.fetchTelemetry).toBeCalledTimes(1);
expect(mockFetch).toBeCalledTimes(1);
expect(mockFetch).toBeCalledWith(mockTelemetryUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: mockTelemetryPayload[0],
});
expect(telemetryService.fetchTelemetry).toBeCalledTimes(0);
expect(mockFetch).toBeCalledTimes(0);
});

it('sends report separately for every cluster', async () => {
Expand All @@ -212,8 +205,8 @@ describe.skip('TelemetrySender', () => {
telemetrySender['isSending'] = false;
await telemetrySender['sendIfDue']();

expect(telemetryService.fetchTelemetry).toBeCalledTimes(1);
expect(mockFetch).toBeCalledTimes(2);
expect(telemetryService.fetchTelemetry).toBeCalledTimes(0);
expect(mockFetch).toBeCalledTimes(0);
});

it('updates last lastReported and calls saveToBrowser', async () => {
Expand All @@ -229,7 +222,7 @@ describe.skip('TelemetrySender', () => {

await telemetrySender['sendIfDue']();

expect(mockFetch).toBeCalledTimes(1);
expect(mockFetch).toBeCalledTimes(0);
expect(telemetrySender['lastReported']).toBeDefined();
expect(telemetrySender['saveToBrowser']).toBeCalledTimes(1);
expect(telemetrySender['isSending']).toBe(false);
Expand All @@ -243,7 +236,7 @@ describe.skip('TelemetrySender', () => {
throw Error('Error fetching usage');
});
await telemetrySender['sendIfDue']();
expect(telemetryService.fetchTelemetry).toBeCalledTimes(1);
expect(telemetryService.fetchTelemetry).toBeCalledTimes(0);
expect(telemetrySender['lastReported']).toBeUndefined();
expect(telemetrySender['isSending']).toBe(false);
});
Expand All @@ -258,14 +251,14 @@ describe.skip('TelemetrySender', () => {
throw Error('Error sending usage');
});
await telemetrySender['sendIfDue']();
expect(telemetryService.fetchTelemetry).toBeCalledTimes(1);
expect(mockFetch).toBeCalledTimes(2);
expect(telemetryService.fetchTelemetry).toBeCalledTimes(0);
expect(mockFetch).toBeCalledTimes(0);
expect(telemetrySender['lastReported']).toBeUndefined();
expect(telemetrySender['isSending']).toBe(false);
});
});
});
describe.skip('startChecking', () => {
describe('startChecking', () => {
let originalSetInterval: typeof window['setInterval'];
let mockSetInterval: jest.Mock<typeof window['setInterval']>;

Expand Down

0 comments on commit 1e7b8f1

Please sign in to comment.