Skip to content

Commit

Permalink
wip: fixed more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Chu committed Apr 3, 2024
1 parent e8116eb commit 12a5b78
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
12 changes: 7 additions & 5 deletions src/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,14 @@ describe('ReactSDKClient', () => {
it('fulfills the returned promise with success: true when a user is set', async () => {
jest.spyOn(mockInnerClient, 'onReady').mockResolvedValue({ success: true });
const instance = createInstance(config);
jest.spyOn(instance, 'fetchQualifiedSegments').mockResolvedValue(true);

await instance.setUser({
id: 'user12345',
});

const result = await instance.onReady();

expect(result.success).toBe(true);
});

Expand Down Expand Up @@ -194,18 +196,18 @@ describe('ReactSDKClient', () => {

it('waits for the inner client onReady to fulfill with success = false before fulfilling the returned promise', async () => {
const mockInnerClientOnReady = jest.spyOn(mockInnerClient, 'onReady');
let resolveInnerClientOnReady: (result: OnReadyResult) => void;
let resolveInnerClientOnReady: (result: OnReadyResult) => void = () => {};
const mockReadyPromise: Promise<OnReadyResult> = new Promise(res => {
resolveInnerClientOnReady = res;
});
mockInnerClientOnReady.mockReturnValueOnce(mockReadyPromise);
const userId = 'user999';
jest.spyOn(mockOptimizelyUserContext, 'getUserId').mockReturnValue(userId);
resolveInnerClientOnReady({ success: true });

await instance.setUser({
id: userId,
});
resolveInnerClientOnReady!({ success: true });
const result = await instance.onReady();

expect(result.success).toBe(false);
Expand Down Expand Up @@ -285,15 +287,15 @@ describe('ReactSDKClient', () => {
expect(onUserUpdateListener).toBeCalledTimes(1);
});

it('does not call fetchqualifiedsegements on setUser if onready is not called initially', async () => {
it('implicitly calls fetchqualifiedsegements', async () => {
const instance = createInstance(config);
jest.spyOn(instance, 'fetchQualifiedSegments').mockResolvedValue(true);

await instance.setUser({
id: 'xxfueaojfe8&86',
});

expect(instance.fetchQualifiedSegments).toBeCalledTimes(0);
expect(instance.fetchQualifiedSegments).toBeCalledTimes(1);
});

it('calls fetchqualifiedsegements internally on each setuser call after onready', async () => {
Expand Down
19 changes: 6 additions & 13 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,34 +372,27 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
attributes: userInfo.attributes || DefaultUser.attributes,
};

// if user is anonymous...
if (userInfo.id === DefaultUser.id) {
let fetchQualifedSegmentsSucceed = false;
if (userInfo.id === DefaultUser.id) { // if user is anonymous...
// wait for the SDK client to be ready before
await this._client?.onReady();
// setting the user context
this.setCurrentUserContext(userInfo);

this.user.id = this.userContext?.getUserId() || DefaultUser.id;

if (this.isClientReady) {
this.fetchQualifiedSegments();
}
fetchQualifedSegmentsSucceed = await this.fetchQualifiedSegments();
} else {
// otherwise if we have the user info, we can...
// create the user context
this.setCurrentUserContext(userInfo);

this.user.id = this.userContext?.getUserId() || DefaultUser.id;

// but we still have to wait for the client SDK to be ready
this._client?.onReady().then(() => {
// before we can fetch segments
this.fetchQualifiedSegments();
});
await this._client?.onReady();
fetchQualifedSegmentsSucceed = await this.fetchQualifiedSegments();
}

if (!this.isUserPromiseResolved) {
this.userPromiseResolver({ success: true });
this.userPromiseResolver({ success: fetchQualifedSegmentsSucceed });
this.isUserPromiseResolved = true;
}

Expand Down

0 comments on commit 12a5b78

Please sign in to comment.