Skip to content

Commit

Permalink
test: add test for withTelemetryData
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklasl committed Nov 27, 2024
1 parent a63db81 commit ec06a1d
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/sdk/src/FlagResolverClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import {
FlagResolverClient,
PendingResolution,
withRequestLogic,
withTelemetryData,
} from './FlagResolverClient';
import { setMaxListeners } from 'node:events';
import { SdkId } from './generated/confidence/flags/resolver/v1/types';
import { abortableSleep } from './fetch-util';
import { ApplyFlagsRequest, ResolveFlagsRequest } from './generated/confidence/flags/resolver/v1/api';
import { FlagResolution } from './FlagResolution';
import { Telemetry } from './Telemetry';
import { LibraryTraces_Library, LibraryTraces_TraceId } from './generated/confidence/telemetry/v1/telemetry';
const RESOLVE_ENDPOINT = 'https://resolver.confidence.dev/v1/flags:resolve';
const APPLY_ENDPOINT = 'https://resolver.confidence.dev/v1/flags:apply';

Expand Down Expand Up @@ -293,6 +295,41 @@ describe('withRequestLogic', () => {
);
});

describe('withTelemetryData', () => {
const fetchMock = jest.fn<Promise<Response>, [Request]>();

Check failure on line 299 in packages/sdk/src/FlagResolverClient.test.ts

View workflow job for this annotation

GitHub Actions / Build-And-Test (18.x)

'fetchMock' is already declared in the upper scope on line 276 column 9
const telemetryMock = jest.mocked(new Telemetry({ disabled: false }));

let underTest: typeof fetch;

Check failure on line 302 in packages/sdk/src/FlagResolverClient.test.ts

View workflow job for this annotation

GitHub Actions / Build-And-Test (18.x)

'underTest' is already declared in the upper scope on line 278 column 7

beforeEach(() => {
jest.useFakeTimers();
jest.setSystemTime(0);
underTest = withTelemetryData(fetchMock, telemetryMock);
telemetryMock.getSnapshot = jest.fn().mockReturnValue({
libraryTraces: [
{
library: LibraryTraces_Library.LIBRARY_REACT,
libraryVersion: 'test',
traces: [{ id: LibraryTraces_TraceId.TRACE_ID_FLAG_TYPE_MISMATCH }],
},
],
});
});
afterEach(() => {
if (jest.getTimerCount() !== 0) throw new Error('test finished with remaining timers');
jest.useRealTimers();
});

it('should add telemetry header', async () => {
fetchMock.mockResolvedValue(new Response());
await underTest('https://resolver.confidence.dev/v1/flags:resolve', { method: 'POST', body: '{}' });
expect(fetchMock).toBeCalledTimes(1);
const request = fetchMock.mock.calls[0][0];
expect(request.headers.has('X-Confidence-Telemetry')).toBeTruthy();
expect(request.headers.get('X-Confidence-Telemetry')).toEqual('CgwIAxIEdGVzdBoCCAM=');
});
});

describe('resolve', () => {
function makeResolveRequest(timeout: number): Promise<Response> {
const abortController = new AbortController();
Expand Down

0 comments on commit ec06a1d

Please sign in to comment.