Files
mostlymatter/webapp/platform/client/src/client4.test.ts
Guillermo Vayá 4cd73fc2d5 [MM-61193] Create trackFeatureEvent telemetry handler for paid features in web (#28834)
* frontend paid feature usage tracking

* fixes on the tooling

* fix lint errors

* fix types

* fix ci

* move from paid to SKU

* fix linter

* move categories and skus to depend on event instead of feature

* move to constants
2024-10-21 15:04:27 +02:00

103 строки
3.2 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import nock from 'nock';
import Client4, {ClientError, HEADER_X_VERSION_ID} from './client4';
import type {TelemetryHandler} from './telemetry';
describe('Client4', () => {
beforeAll(() => {
if (!nock.isActive()) {
nock.activate();
}
});
afterAll(() => {
nock.restore();
});
describe('doFetchWithResponse', () => {
test('serverVersion should be set from response header', async () => {
const client = new Client4();
client.setUrl('http://mattermost.example.com');
expect(client.serverVersion).toEqual('');
nock(client.getBaseRoute()).
get('/users/me').
reply(200, '{}', {[HEADER_X_VERSION_ID]: '5.0.0.5.0.0.abc123'});
await client.getMe();
expect(client.serverVersion).toEqual('5.0.0.5.0.0.abc123');
nock(client.getBaseRoute()).
get('/users/me').
reply(200, '{}', {[HEADER_X_VERSION_ID]: '5.3.0.5.3.0.abc123'});
await client.getMe();
expect(client.serverVersion).toEqual('5.3.0.5.3.0.abc123');
});
});
});
describe('ClientError', () => {
test('standard fields should be enumerable', () => {
const error = new ClientError('https://example.com', {
message: 'This is a message',
server_error_id: 'test.app_error',
status_code: 418,
url: 'https://example.com/api/v4/error',
});
const copy = {...error};
expect(copy.message).toEqual(error.message);
expect(copy.server_error_id).toEqual(error.server_error_id);
expect(copy.status_code).toEqual(error.status_code);
expect(copy.url).toEqual(error.url);
});
test('cause should be preserved when provided', () => {
const cause = new Error('the original error');
const error = new ClientError('https://example.com', {
message: 'This is a message',
server_error_id: 'test.app_error',
status_code: 418,
url: 'https://example.com/api/v4/error',
}, cause);
const copy = {...error};
expect(copy.message).toEqual(error.message);
expect(copy.server_error_id).toEqual(error.server_error_id);
expect(copy.status_code).toEqual(error.status_code);
expect(copy.url).toEqual(error.url);
expect(error.cause).toEqual(cause);
});
});
describe('trackEvent', () => {
class TestTelemetryHandler implements TelemetryHandler {
trackEvent = jest.fn();
trackFeatureEvent = jest.fn();
pageVisited = jest.fn();
}
test('should call the attached RudderTelemetryHandler, if one is attached to Client4', () => {
const client = new Client4();
client.setUrl('http://mattermost.example.com');
expect(() => client.trackEvent('test', 'onClick')).not.toThrowError();
const handler = new TestTelemetryHandler();
client.setTelemetryHandler(handler);
client.trackEvent('test', 'onClick');
expect(handler.trackEvent).toHaveBeenCalledTimes(1);
});
});