Skip to content

Commit

Permalink
tests now passing
Browse files Browse the repository at this point in the history
  • Loading branch information
tanderson-ld committed Sep 6, 2024
1 parent 115bd82 commit e180de6
Show file tree
Hide file tree
Showing 17 changed files with 969 additions and 282 deletions.
13 changes: 11 additions & 2 deletions packages/sdk/react-native/src/ReactNativeLDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {
base64UrlEncode,
BasicLogger,
ConnectionMode,
Encoding,
internal,
LDClientImpl,
type LDContext,
StreamingPaths,
} from '@launchdarkly/js-client-sdk-common';

import validateOptions, { filterToBaseOptions } from './options';
Expand Down Expand Up @@ -103,8 +105,15 @@ export default class ReactNativeLDClient extends LDClientImpl {
return base64UrlEncode(JSON.stringify(context), this.platform.encoding!);
}

override createStreamUriPath(context: LDContext) {
return `/meval/${this.encodeContext(context)}`;
override getStreamingPaths(): StreamingPaths {
return {
pathGet(encoding: Encoding, _credential: string, _plainContextString: string): string {
return `/meval/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathReport(_encoding: Encoding, _credential: string, _plainContextString: string): string {
return `/meval`;
},
};
}

override createPollUriPath(context: LDContext): string {
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/react-native/src/platform/PlatformRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export default class PlatformRequests implements Requests {

createEventSource(url: string, eventSourceInitDict: EventSourceInitDict): EventSource {
return new RNEventSource<EventName>(url, {
method: eventSourceInitDict.method,
headers: eventSourceInitDict.headers,
body: eventSourceInitDict.body,
retryAndHandleError: eventSourceInitDict.errorFilter,
logger: this.logger,
});
Expand Down
4 changes: 3 additions & 1 deletion packages/shared/common/src/api/platform/EventSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ export interface EventSource {
}

export interface EventSourceInitDict {
errorFilter: (err: HttpErrorResponse) => boolean;
method?: string;
headers: { [key: string]: string | string[] };
body?: string;
errorFilter: (err: HttpErrorResponse) => boolean;
initialRetryDelayMillis: number;
readTimeoutMillis: number;
retryResetIntervalMillis: number;
Expand Down
29 changes: 23 additions & 6 deletions packages/shared/sdk-client/src/LDClientImpl.events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
AutoEnvAttributes,
ClientContext,
clone,
Encoding,
internal,
LDContext,
subsystem,
Expand All @@ -10,11 +11,11 @@ import {
createBasicPlatform,
createLogger,
MockEventProcessor,
setupMockStreamingProcessor,
} from '@launchdarkly/private-js-mocks';

import * as mockResponseJson from './evaluation/mockResponse.json';
import LDClientImpl from './LDClientImpl';
import { MockEventSource } from './LDClientImpl.mocks';
import { Flags } from './types';

type InputCustomEvent = internal.InputCustomEvent;
Expand All @@ -36,7 +37,6 @@ jest.mock('@launchdarkly/js-sdk-common', () => {
...{
internal: {
...actual.internal,
StreamingProcessor: m.MockStreamingProcessor,
EventProcessor: m.MockEventProcessor,
},
},
Expand All @@ -45,6 +45,7 @@ jest.mock('@launchdarkly/js-sdk-common', () => {

const testSdkKey = 'test-sdk-key';
let ldc: LDClientImpl;
let mockEventSource: MockEventSource;
let defaultPutResponse: Flags;
const carContext: LDContext = { kind: 'car', key: 'test-car' };

Expand All @@ -66,15 +67,31 @@ describe('sdk-client object', () => {
sendEvent: mockedSendEvent,
}),
);
setupMockStreamingProcessor(false, defaultPutResponse);

const simulatedEvents = [{ data: JSON.stringify(defaultPutResponse) }];
mockPlatform.storage.get.mockImplementation(() => undefined);
mockPlatform.requests.createEventSource.mockImplementation(
(streamUri: string = '', options: any = {}) => {
mockEventSource = new MockEventSource(streamUri, options);
mockEventSource.simulateEvents('put', simulatedEvents);
return mockEventSource;
},
);

mockPlatform.crypto.randomUUID.mockReturnValue('random1');

ldc = new LDClientImpl(testSdkKey, AutoEnvAttributes.Enabled, mockPlatform, {
logger,
});
jest
.spyOn(LDClientImpl.prototype as any, 'createStreamUriPath')
.mockReturnValue('/stream/path');

jest.spyOn(LDClientImpl.prototype as any, 'getStreamingPaths').mockReturnValue({
pathGet(_encoding: Encoding, _credential: string, _plainContextString: string): string {
return '/stream/path';
},
pathReport(_encoding: Encoding, _credential: string, _plainContextString: string): string {
return '/stream/path';
},
});
});

afterEach(() => {
Expand Down
50 changes: 50 additions & 0 deletions packages/shared/sdk-client/src/LDClientImpl.mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { EventSource, EventSourceInitDict } from '@launchdarkly/js-sdk-common';

export class MockEventSource implements EventSource {
eventsByType: Map<string, { data?: any }[]> = new Map<string, { data?: any }[]>();

handlers: Record<string, (event?: { data?: any }) => void> = {};

closed = false;

url: string;

options: EventSourceInitDict;

constructor(url: string, options: EventSourceInitDict) {
this.url = url;
this.options = options;
}

onclose: (() => void) | undefined;

onerror: (() => void) | undefined;

onopen: (() => void) | undefined;

onretrying: ((e: { delayMillis: number }) => void) | undefined;

addEventListener(type: string, listener: (event?: { data?: any }) => void): void {
this.handlers[type] = listener;

// replay events to listener
(this.eventsByType.get(type) ?? []).forEach((event) => {
listener(event);
});
}

close(): void {
this.closed = true;
}

simulateEvents(type: string, events: { data?: any }[]) {
this.eventsByType.set(type, events);
}

simulateError(error: { status: number; message: string }) {
const shouldRetry = this.options.errorFilter(error);
if (!shouldRetry) {
this.closed = true;
}
}
}
Loading

0 comments on commit e180de6

Please sign in to comment.