Skip to content

Commit

Permalink
test: add test case for client environment
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklasl committed Jan 10, 2025
1 parent ca900d7 commit ab45361
Showing 1 changed file with 53 additions and 29 deletions.
82 changes: 53 additions & 29 deletions packages/sdk/src/FlagResolverClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,52 @@ const fetchImplementation = async (request: Request): Promise<Response> => {
}
};

resolveHandlerMock.mockImplementation(createFlagResolutionResponse);
beforeEach(() => {
resolveHandlerMock.mockImplementation(createFlagResolutionResponse);
});

afterEach(() => {
resolveHandlerMock.mockClear();
applyHandlerMock.mockClear();
});

describe('Client environment Evaluation', () => {
// const flagResolutionResponseJson = JSON.stringify(createFlagResolutionResponse());
const instanceUnderTest = new FetchingFlagResolverClient({
fetchImplementation,
clientSecret: 'secret',
applyTimeout: 10,
sdk: {
id: SdkId.SDK_ID_JS_CONFIDENCE,
version: 'test',
},
environment: 'client',
resolveTimeout: 10,
telemetry: new Telemetry({ disabled: true, logger: { warn: jest.fn() }, environment: 'client' }),
let instanceUnderTest: FetchingFlagResolverClient;

beforeEach(() => {
instanceUnderTest = new FetchingFlagResolverClient({
fetchImplementation,
clientSecret: 'secret',
applyTimeout: 10,
sdk: {
id: SdkId.SDK_ID_JS_CONFIDENCE,
version: 'test',
},
environment: 'client',
resolveTimeout: 10,
telemetry: new Telemetry({ disabled: true, logger: { warn: jest.fn() }, environment: 'client' }),
});
});

it('should resolve a FailedFlagResolution on fetch errors', async () => {
resolveHandlerMock.mockRejectedValue(new Error('Test error'));
const flagResolution = await instanceUnderTest.resolve({}, []);
expect(flagResolution).toBeInstanceOf(FailedFlagResolution);
// Expect this error to log as a Resolve timeout in the client environment
// This is due the request logic that's only used in the client environment
expect(flagResolution.evaluate('testflag', {})).toEqual({
errorCode: 'TIMEOUT',
errorMessage: 'Resolve timeout',
reason: 'ERROR',
value: {},
});
});

describe('apply', () => {
it('should send an apply event', async () => {
const flagResolution = await instanceUnderTest.resolve({}, []);
flagResolution.evaluate('testflag.bool', false);

const [applyRequest] = await nextMockArgs(applyHandlerMock);
expect(applyRequest).toMatchObject({
clientSecret: 'secret',
Expand All @@ -81,23 +105,23 @@ describe('Client environment Evaluation', () => {
],
});
});
});

it('should apply when a flag has no segment match', async () => {
const flagResolution = await instanceUnderTest.resolve({}, []);
flagResolution.evaluate('no-seg-flag.enabled', false);
const [applyRequest] = await nextMockArgs(applyHandlerMock);
expect(applyRequest).toMatchObject({
clientSecret: 'secret',
resolveToken: dummyResolveToken,
sendTime: expect.any(Date),
sdk: { id: 13, version: 'test' },
flags: [
{
applyTime: expect.any(Date),
flag: 'flags/no-seg-flag',
},
],
it('should apply when a flag has no segment match', async () => {
const flagResolution = await instanceUnderTest.resolve({}, []);
flagResolution.evaluate('no-seg-flag.enabled', false);
const [applyRequest] = await nextMockArgs(applyHandlerMock);
expect(applyRequest).toMatchObject({
clientSecret: 'secret',
resolveToken: dummyResolveToken,
sendTime: expect.any(Date),
sdk: { id: 13, version: 'test' },
flags: [
{
applyTime: expect.any(Date),
flag: 'flags/no-seg-flag',
},
],
});
});
});
});
Expand Down

0 comments on commit ab45361

Please sign in to comment.