-
Notifications
You must be signed in to change notification settings - Fork 12
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
[WIP] add useTreatment hook #70
Draft
besh
wants to merge
1
commit into
splitio:development
Choose a base branch
from
besh:use-treatment-hook
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+149
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,125 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
|
||
/** Mocks */ | ||
import { mockSdk } from './testUtils/mockSplitSdk'; | ||
jest.mock('@splitsoftware/splitio', () => { | ||
return { SplitFactory: mockSdk() }; | ||
}); | ||
import { SplitFactory as SplitSdk } from '@splitsoftware/splitio'; | ||
import { sdkBrowser } from './testUtils/sdkConfigs'; | ||
jest.mock('../constants', () => { | ||
const actual = jest.requireActual('../constants'); | ||
return { | ||
...actual, | ||
getControlTreatmentsWithConfig: jest.fn(actual.getControlTreatmentsWithConfig), | ||
}; | ||
}); | ||
import { getControlTreatmentsWithConfig } from '../constants'; | ||
const logSpy = jest.spyOn(console, 'log'); | ||
|
||
/** Test target */ | ||
import SplitFactory from '../SplitFactory'; | ||
import SplitClient from '../SplitClient'; | ||
import useTreatment from '../useTreatment'; | ||
|
||
describe('useTreatment', () => { | ||
|
||
const splitName = 'split1'; | ||
const attributes = { att1: 'att1' }; | ||
|
||
test('returns the Treatment from the client at Split context updated by SplitFactory.', () => { | ||
const outerFactory = SplitSdk(sdkBrowser); | ||
let treatment; | ||
|
||
mount( | ||
<SplitFactory factory={outerFactory} >{ | ||
React.createElement(() => { | ||
treatment = useTreatment(splitName, attributes); | ||
return null; | ||
})}</SplitFactory>, | ||
); | ||
const getTreatmentWithConfig: jest.Mock = (outerFactory.client() as any).getTreatmentWithConfig; | ||
expect(getTreatmentWithConfig).toBeCalledWith(splitName, attributes); | ||
expect(getTreatmentWithConfig).toHaveReturnedWith(treatment); | ||
}); | ||
|
||
test('returns the Treatment from the client at Split context updated by SplitClient.', () => { | ||
const outerFactory = SplitSdk(sdkBrowser); | ||
let treatment; | ||
|
||
mount( | ||
<SplitFactory factory={outerFactory} > | ||
<SplitClient splitKey='user2' >{ | ||
React.createElement(() => { | ||
treatment = useTreatment(splitName, attributes); | ||
return null; | ||
})} | ||
</SplitClient> | ||
</SplitFactory>, | ||
); | ||
const getTreatmentWithConfig: jest.Mock = (outerFactory.client('user2') as any).getTreatmentWithConfig; | ||
expect(getTreatmentWithConfig).toBeCalledWith(splitName, attributes); | ||
expect(getTreatmentWithConfig).toHaveReturnedWith(treatment); | ||
}); | ||
|
||
test('returns the Treatment from a new client given a splitKey.', () => { | ||
const outerFactory = SplitSdk(sdkBrowser); | ||
let treatment; | ||
|
||
mount( | ||
<SplitFactory factory={outerFactory} >{ | ||
React.createElement(() => { | ||
treatment = useTreatment(splitName, attributes, 'user2'); | ||
return null; | ||
})} | ||
</SplitFactory>, | ||
); | ||
const getTreatmentWithConfig: jest.Mock = (outerFactory.client('user2') as any).getTreatmentWithConfig; | ||
expect(getTreatmentWithConfig).toBeCalledWith(splitName, attributes); | ||
expect(getTreatmentWithConfig).toHaveReturnedWith(treatment); | ||
}); | ||
|
||
test('returns Control Treatment if invoked outside Split context.', () => { | ||
let treatment; | ||
|
||
mount( | ||
React.createElement( | ||
() => { | ||
treatment = useTreatment(splitName, attributes); | ||
return null; | ||
}), | ||
); | ||
expect(getControlTreatmentsWithConfig).toBeCalledWith([splitName]); | ||
expect(getControlTreatmentsWithConfig).toHaveReturnedWith({ | ||
split1: { | ||
config: null, | ||
treatment: 'control' | ||
} | ||
}); | ||
}); | ||
|
||
/** | ||
* Input validation. Passing invalid split names or attributes while the Sdk | ||
* is not ready doesn't emit errors, and logs meaningful messages instead. | ||
*/ | ||
test('Input validation: invalid "name" and "attributes" params in useTreatment.', (done) => { | ||
mount( | ||
React.createElement( | ||
() => { | ||
// @ts-ignore | ||
let treatment = useTreatment('split1'); | ||
expect(treatment).toEqual({}); | ||
// @ts-ignore | ||
treatment = useTreatment([true]); | ||
expect(treatment).toEqual({}); | ||
return null; | ||
}), | ||
); | ||
expect(logSpy).toBeCalledWith('[ERROR] split names must be a non-empty array.'); | ||
expect(logSpy).toBeCalledWith('[ERROR] you passed an invalid split name, split name must be a non-empty string.'); | ||
|
||
done(); | ||
}); | ||
|
||
}); |
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,20 @@ | ||
import useClient from './useClient'; | ||
import { getControlTreatmentsWithConfig, ERROR_UT_NO_USECONTEXT } from './constants'; | ||
import { checkHooks } from './utils'; | ||
|
||
/** | ||
* 'useTreatment' is a custom hook that returns a treatment for a given split. | ||
* It uses the 'useContext' hook to access the client from the Split context, | ||
* and invokes the 'getTreatmentWithConfig' method. | ||
* | ||
* @return A TreatmentWithConfig instance, that might contain the control treatment if the client is not available or ready, or if split name does not exist. | ||
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#get-treatments-with-configurations} | ||
*/ | ||
const useTreatment = (splitName: string, attributes?: SplitIO.Attributes, key?: SplitIO.SplitKey): SplitIO.TreatmentWithConfig => { | ||
const client = checkHooks(ERROR_UT_NO_USECONTEXT) ? useClient(key) : null; | ||
return client ? | ||
client.getTreatmentWithConfig(splitName, attributes) : | ||
getControlTreatmentsWithConfig([splitName])[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to reuse the existing methods here, but I don't love passing in an array of a single item and returning the first entry. I could expose validateSplit and create a new Thoughts? |
||
}; | ||
|
||
export default useTreatment; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, I'd use
client.getTreatment
here instead, then I'd also have auseTreatmentWithConfig
hook that usesclient.getTreatmentWithConfig
. Are we okay with me making that addition?