Skip to content

Commit

Permalink
added context change tests, tsconfig options
Browse files Browse the repository at this point in the history
Signed-off-by: jarebudev <[email protected]>
  • Loading branch information
jarebudev committed Dec 8, 2024
1 parent 7c8a41b commit 10f70d1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
65 changes: 63 additions & 2 deletions libs/providers/unleash-web/src/lib/unleash-web-provider.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { UnleashWebProvider } from './unleash-web-provider';
import fetchMock, { enableFetchMocks } from 'jest-fetch-mock';
import { ProviderEvents } from '@openfeature/web-sdk';

import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
import testdata from './testdata.json';
import TestLogger from './test-logger';

Expand Down Expand Up @@ -102,6 +101,68 @@ describe('events', () => {
}, 10000);
});

describe('onContextChange', () => {
let provider: UnleashWebProvider;

beforeEach(async () => {
await jest.resetAllMocks();
provider = new UnleashWebProvider({ url: endpoint, clientKey: 'clientsecret', appName: 'test' }, logger);
jest.spyOn(provider.unleashClient as any, 'fetchToggles').mockImplementation();

Check warning on line 110 in libs/providers/unleash-web/src/lib/unleash-web-provider.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (18.x)

Unexpected any. Specify a different type

Check warning on line 110 in libs/providers/unleash-web/src/lib/unleash-web-provider.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (20.x)

Unexpected any. Specify a different type
});

afterEach(async () => {
await OpenFeature.close();
});

it('sets all unleash context options with no custom properties', async () => {
const unleashClientMock = jest.spyOn(provider.unleashClient as any, 'updateContext');

Check warning on line 118 in libs/providers/unleash-web/src/lib/unleash-web-provider.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (18.x)

Unexpected any. Specify a different type

Check warning on line 118 in libs/providers/unleash-web/src/lib/unleash-web-provider.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (20.x)

Unexpected any. Specify a different type
await OpenFeature.setProviderAndWait(provider);
await OpenFeature.setContext({
userId: 'theUserId',
appName: 'anAppName',
remoteAddress: 'the.remoteAddress',
currentTime: '8/12/24 10:10:23',
sessionId: '1234-3245-56567',
environment: 'dev',
});
expect(unleashClientMock).toHaveBeenCalledWith({
userId: 'theUserId',
appName: 'anAppName',
remoteAddress: 'the.remoteAddress',
currentTime: '8/12/24 10:10:23',
sessionId: '1234-3245-56567',
environment: 'dev',
});
});

it('sets all unleash context options with some custom properties', async () => {
const unleashClientMock = jest.spyOn(provider.unleashClient as any, 'updateContext');

Check warning on line 139 in libs/providers/unleash-web/src/lib/unleash-web-provider.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (18.x)

Unexpected any. Specify a different type

Check warning on line 139 in libs/providers/unleash-web/src/lib/unleash-web-provider.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (20.x)

Unexpected any. Specify a different type
await OpenFeature.setProviderAndWait(provider);
await OpenFeature.setContext({
userId: 'theUserId',
appName: 'anAppName',
remoteAddress: 'the.remoteAddress',
currentTime: '8/12/24 10:10:23',
sessionId: '1234-3245-56567',
environment: 'dev',
foo: 'bar',
hello: 'world',
});
expect(unleashClientMock).toHaveBeenCalledWith({
userId: 'theUserId',
appName: 'anAppName',
remoteAddress: 'the.remoteAddress',
currentTime: '8/12/24 10:10:23',
sessionId: '1234-3245-56567',
environment: 'dev',
properties: {
foo: 'bar',
hello: 'world',
},
});
});
});

describe('UnleashWebProvider evaluations', () => {
let provider: UnleashWebProvider;

Expand Down
8 changes: 7 additions & 1 deletion libs/providers/unleash-web/src/lib/unleash-web-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class UnleashWebProvider implements Provider {
this._client = new UnleashClient(config);
}

public get unleashClient() {
return this._client;
}

async initialize(): Promise<void> {
await this.initializeClient();
this._logger?.info('UnleashWebProvider initialized');
Expand Down Expand Up @@ -95,7 +99,9 @@ export class UnleashWebProvider implements Provider {
break;
}
});
unleashContext.set('properties', properties);
if (properties.size > 0) {
unleashContext.set('properties', Object.fromEntries(properties));
}
await this._client?.updateContext(Object.fromEntries(unleashContext));
this._logger?.info('Unleash context updated');
}
Expand Down
4 changes: 3 additions & 1 deletion libs/providers/unleash-web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true
"resolveJsonModule": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
},
"files": [],
"include": [],
Expand Down

0 comments on commit 10f70d1

Please sign in to comment.