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 2 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
55 changes: 43 additions & 12 deletions src/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import * as optimizely from '@optimizely/optimizely-sdk';

import { createInstance, OnReadyResult, ReactSDKClient } from './client';
import { logger } from './logger';
import * as utils from './utils';
import { OptimizelyUserContext } from '@optimizely/optimizely-sdk';

interface MockedReactSDKClient extends ReactSDKClient {
client: optimizely.Client;
Expand Down Expand Up @@ -1671,35 +1673,64 @@ describe('ReactSDKClient', () => {
});
});

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

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

it('returns null if userContext is not initialized', () => {
it('should log a warning if client is not defined', () => {
// @ts-ignore
instance._client = null;

instance.getUserContextInstance({ id: 'user1' });

expect(logger.warn).toHaveBeenCalledTimes(1);
expect(logger.warn).toBeCalledWith("Unable to get user context for user id \"%s\" because Optimizely client failed to initialize.", "user1");
});

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

expect(instance.getQualifedSegments()).toBeNull();
const context = instance.getUserContextInstance({ id: 'user1' });

expect(context).toBeDefined();
});

it('returns qualified segments if userContext is initialized', () => {
it('should test that this.userContext are equal', () => {
const expectedUserInfo = { id: 'user1', attributes: { custom: 'attribute' } } as utils.UserInfo;
jest.spyOn(utils, 'areUsersEqual');
// @ts-ignore
instance.userContext = mockOptimizelyUserContext;
// @ts-ignore
instance.userContext = {
qualifiedSegments: ['segment1', 'segment2'],
};
instance.user = expectedUserInfo;

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

expect(utils.areUsersEqual).toBeCalledTimes(1);
expect(utils.areUsersEqual).toHaveBeenCalledWith(expectedUserInfo, expectedUserInfo);
});

it('should return userContext if userContext is not defined', () => {
const expectedUserInfo = { id: 'user1', attributes: { custom: 'attribute' } } as utils.UserInfo;
// @ts-ignore
instance.userContext = null;

const actualContext = instance.getUserContextInstance(expectedUserInfo);

expect(actualContext).toBeDefined();
});

it('logs a warning if userContext is not initialized', () => {
const loggerWarnSpy = jest.spyOn(logger, 'warn');
it('should set and return userContext if userContext is defined', () => {
const expectedUserInfo = { id: 'user1', attributes: { custom: 'attribute' } } as utils.UserInfo;
// @ts-ignore
instance.userContext = {};

instance.getQualifedSegments();
const actualContext = instance.getUserContextInstance(expectedUserInfo);

expect(loggerWarnSpy).toHaveBeenCalledWith('Unable to get qualified segments for user because Optimizely user context failed to initialize.');
expect(actualContext).toBeDefined();
});
});
});
15 changes: 3 additions & 12 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export interface ReactSDKClient extends Omit<optimizely.Client, 'createUserConte

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

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

getVuid(): string | undefined;
}
Expand Down Expand Up @@ -347,8 +347,8 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
}

if (userInfo.id) {
this.userContext = this._client.createUserContext(userInfo.id, userInfo.attributes);
return this.userContext;
userContext = this._client.createUserContext(userInfo.id, userInfo.attributes);
return userContext;
}

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

public getQualifedSegments(): string[] | null {
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
Loading