Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FSSDK-9726] AAT gap fill #221

Merged
merged 16 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"name": "React SDK",

"image": "mcr.microsoft.com/devcontainers/javascript-node:1-18-bullseye",

"postCreateCommand": "npm install -g npm && yarn install",

"postCreateCommand": "yarn install",
"customizations": {
"vscode": {
"extensions": [
Expand All @@ -14,7 +11,8 @@
"Gruntfuggly.todo-tree",
"github.vscode-github-actions",
"Orta.vscode-jest",
"ms-vscode.test-adapter-converter"
"ms-vscode.test-adapter-converter",
"GitHub.copilot-chat"
],
"settings": {
"files.eol": "\n"
Expand Down
80 changes: 79 additions & 1 deletion src/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ describe('ReactSDKClient', () => {
await instance.setUser({
id: 'xxfueaojfe8&86',
});
await instance.onReady()
await instance.onReady();

await instance.setUser({
id: 'xxfueaojfe8&87',
Expand Down Expand Up @@ -1624,4 +1624,82 @@ describe('ReactSDKClient', () => {
expect(mockInnerClient.sendOdpEvent).toHaveBeenCalledTimes(1);
});
});

describe('getVuid', () => {
const vuidFormat = /^vuid_[a-f0-9]{27}$/;
let instance: ReactSDKClient;

beforeEach(async () => {
instance = createInstance(config);
});

it('should return undefined if client is null', () => {
// @ts-ignore
instance._client = null;

const vuid = instance.getVuid();

expect(vuid).toBeUndefined();
});

it('should call the JS getVuid if a user set', async () => {
await instance.setUser({
id: 'user1',
});

instance.getVuid();

expect(mockInnerClient.getVuid).toHaveBeenCalledTimes(1);
expect(logger.warn).toHaveBeenCalledTimes(0);
});

it('should call the JS getVuid if even if a user is not set', async () => {
instance.getVuid();

expect(mockInnerClient.getVuid).toHaveBeenCalledTimes(1);
expect(logger.warn).toHaveBeenCalledTimes(0);
});

it('should return a valid vuid', async () => {
const validVuid = 'vuid_8de3bb278fce47f6b000cadc1ac';
const mockGetVuid = mockInnerClient.getVuid as jest.Mock;
mockGetVuid.mockReturnValue(validVuid);

const vuid = instance.getVuid();

expect(vuid).toMatch(vuidFormat);
mikechu-optimizely marked this conversation as resolved.
Show resolved Hide resolved
});
});

describe('getQualifedSegments', () => {
let instance: ReactSDKClient;

beforeEach(async () => {
instance = createInstance(config);
});

it('returns null if userContext is not initialized', () => {
// @ts-ignore
instance.userContext = null;

expect(instance.getQualifedSegments()).toBeNull();
});

it('returns qualified segments if userContext is initialized', () => {
// @ts-ignore
instance.userContext = {
qualifiedSegments: ['segment1', 'segment2'],
};

expect(instance.getQualifedSegments()).toEqual(['segment1', 'segment2']);
});

it('logs a warning if userContext is not initialized', () => {
const loggerWarnSpy = jest.spyOn(logger, 'warn');

instance.getQualifedSegments();

expect(loggerWarnSpy).toHaveBeenCalledWith('Unable to get qualified segments for user because Optimizely user context failed to initialize.');
});
});
});
31 changes: 26 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const default_user: UserInfo = {
attributes: {},
};

export interface ReactSDKClient extends Omit<optimizely.Client, 'createUserContext' | 'getVuid'> {
export interface ReactSDKClient extends Omit<optimizely.Client, 'createUserContext'> {
user: UserInfo;

onReady(opts?: { timeout?: number }): Promise<any>;
Expand Down Expand Up @@ -180,9 +180,13 @@ export interface ReactSDKClient extends Omit<optimizely.Client, 'createUserConte
getForcedDecision(decisionContext: optimizely.OptimizelyDecisionContext): optimizely.OptimizelyForcedDecision | null;

fetchQualifiedSegments(options?: optimizely.OptimizelySegmentOption[]): Promise<boolean>;

getQualifedSegments(): string[] | null;
mikechu-optimizely marked this conversation as resolved.
Show resolved Hide resolved

getVuid(): string | undefined;
}

export const DEFAULT_ON_READY_TIMEOUT = 5000;
export const DEFAULT_ON_READY_TIMEOUT = 5_000;

class OptimizelyReactSDKClient implements ReactSDKClient {
private userContext: optimizely.OptimizelyUserContext | null = null;
Expand Down Expand Up @@ -219,7 +223,7 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
*/
constructor(config: optimizely.Config) {
this.initialConfig = config;
this.userPromiseResolver = () => { };
this.userPromiseResolver = () => {};

const configWithClientInfo = {
...config,
Expand Down Expand Up @@ -343,8 +347,8 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
}

if (userInfo.id) {
userContext = this._client.createUserContext(userInfo.id, userInfo.attributes);
return userContext;
this.userContext = this._client.createUserContext(userInfo.id, userInfo.attributes);
mikechu-optimizely marked this conversation as resolved.
Show resolved Hide resolved
return this.userContext;
}

return null;
Expand All @@ -367,6 +371,15 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
return await this.userContext.fetchQualifiedSegments(options);
}

public getQualifedSegments(): string[] | null {
mikechu-optimizely marked this conversation as resolved.
Show resolved Hide resolved
if (!this.userContext) {
logger.warn('Unable to get qualified segments for user because Optimizely user context failed to initialize.');
return null;
}

return this.userContext.qualifiedSegments;
}

public async setUser(userInfo: UserInfo): Promise<void> {
this.isUserReady = true;

Expand Down Expand Up @@ -1227,6 +1240,14 @@ class OptimizelyReactSDKClient implements ReactSDKClient {

this.client?.sendOdpEvent(action, type, identifiers, data);
}

public getVuid(): string | undefined {
if (!this._client) {
logger.warn('Unable to get VUID because Optimizely client failed to initialize.');
return undefined;
}
return this._client.getVuid();
}
}

export function createInstance(config: optimizely.Config): ReactSDKClient {
Expand Down
Loading