-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FSSDK-9984] fix: initialization and setUser errors (#255)
* fix: remove successful fetch requirement for onReady * Revert "fix: remove successful fetch requirement for onReady" This reverts commit 566daaf. * fix: error with OnReadyResult being undefined * fix: setUser should use VUID if possible * revert: timeout of undefined * docs: update copyright year * revert: Provider.tsx copyright since no code change * build: bump JS SDK version * refactor: `res` should never be `undefined` * docs: add clarifying comment * revert: retrieval & use of current user context * wip: partial solution; needs collab * refactor: setUser logic updated * revert: move setUser back to Provider constructor * style: remove commented code * fix: fetchQualifiedSegments under SSR/sync scenario * ci: VS Code jest settings to run via extension * test: use NotReadyReason enum * test: use NotReadyReason & add missing getUserId in mock user context * fix: add onInitStateChange for default ready result * fix: logic in Promise.all user & client readiness * refactor: isReady() to isReactClientReady() * test: fixes for uses of getUserId() in setUser() * wip: fixing tests * wip: fixed more tests * revert: refactor of isReactClientReady() * docs: Update copyrights * fix: later setUser not getting new usercontext (manual testing) * fix: PR review changes * test: add initial OptimizelyProvider tests * fix: PR review changes * docs: add clarification inline comments * build: bump underlying JS SDK version to 5.3.0 * fix: add missing getProjectConfig() from JS SDK * refactor: omit getProjectConfig instead of implementing it * style: remove unused import --------- Co-authored-by: Mike Chu <michael.chu@optmizely.com>
- Loading branch information
1 parent
a87fe08
commit fae169f
Showing
10 changed files
with
347 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"name": "vscode-jest-tests.v2.react-sdk", | ||
"request": "launch", | ||
"args": [ | ||
"--runInBand", | ||
"--watchAll=false", | ||
"--testNamePattern", | ||
"${jest.testNamePattern}", | ||
"--runTestsByPath", | ||
"${jest.testFile}" | ||
], | ||
"cwd": "${workspaceFolder}", | ||
"console": "integratedTerminal", | ||
"internalConsoleOptions": "neverOpen", | ||
"program": "${workspaceFolder}/node_modules/.bin/jest", | ||
"windows": { | ||
"program": "${workspaceFolder}/node_modules/jest/bin/jest" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
{ | ||
"jest.autoRun": { | ||
"onStartup": ["all-tests"] | ||
} | ||
"jest.runMode": "on-demand", | ||
"jest.jestCommandLine": "~/.nvm/nvm-exec yarn test" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* Copyright 2024 Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/// <reference types="jest" /> | ||
|
||
//jest.mock('./client'); | ||
|
||
import React from 'react'; | ||
import { render, act } from '@testing-library/react'; | ||
import { OptimizelyProvider } from './Provider'; | ||
import { DefaultUser, ReactSDKClient, createInstance } from './client'; | ||
|
||
describe('OptimizelyProvider', () => { | ||
let mockReactClient: ReactSDKClient; | ||
const config = { | ||
datafile: {}, | ||
}; | ||
|
||
beforeEach(() => { | ||
mockReactClient = ({ | ||
user: { | ||
id: 'test-id', | ||
attributes: {}, | ||
}, | ||
setUser: jest.fn().mockResolvedValue(undefined), | ||
} as unknown) as ReactSDKClient; | ||
}); | ||
|
||
it('should render successfully with user provided', () => { | ||
act(() => { | ||
render(<OptimizelyProvider optimizely={mockReactClient} user={{ id: 'user1' }} />); | ||
}); | ||
|
||
expect(mockReactClient.setUser).toHaveBeenCalledWith({ | ||
id: 'user1', | ||
attributes: {}, | ||
}); | ||
}); | ||
|
||
it('should render successfully with userId provided', () => { | ||
act(() => { | ||
render(<OptimizelyProvider optimizely={mockReactClient} userId="user1" />); | ||
}); | ||
|
||
expect(mockReactClient.setUser).toHaveBeenCalledWith({ | ||
id: 'user1', | ||
attributes: {}, | ||
}); | ||
}); | ||
|
||
it('should render successfully without user or userId provided', () => { | ||
act(() => { | ||
render(<OptimizelyProvider optimizely={mockReactClient} />); | ||
}); | ||
|
||
expect(mockReactClient.setUser).toHaveBeenCalledWith(DefaultUser); | ||
}); | ||
|
||
it('should render successfully with user id & attributes provided', () => { | ||
act(() => { | ||
render( | ||
<OptimizelyProvider optimizely={mockReactClient} user={{ id: 'user1', attributes: { attr1: 'value1' } }} /> | ||
); | ||
}); | ||
|
||
expect(mockReactClient.setUser).toHaveBeenCalledWith({ | ||
id: 'user1', | ||
attributes: { attr1: 'value1' }, | ||
}); | ||
}); | ||
|
||
it('should succeed just userAttributes provided', () => { | ||
act(() => { | ||
render(<OptimizelyProvider optimizely={mockReactClient} userAttributes={{ attr1: 'value1' }} />); | ||
}); | ||
|
||
expect(mockReactClient.setUser).toHaveBeenCalledWith({ | ||
id: DefaultUser.id, | ||
attributes: { attr1: 'value1' }, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.