-
Notifications
You must be signed in to change notification settings - Fork 2
[CSL-3248] Refactor useCioClient to match other OS Implementations #49
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,10 @@ describe('CioPlp React Client-Side Rendering', () => { | |
| expect(() => render(<CioPlp apiKey={DEMO_API_KEY} />)).not.toThrow(); | ||
| }); | ||
|
|
||
| it('renders CioPlp provider without children on the client without error, if client is provided', () => { | ||
| expect(() => render(<CioPlp cioClient={{}} />)).not.toThrow(); | ||
|
||
| }); | ||
|
|
||
| it('renders CioPlp provider with children correctly on the client', () => { | ||
| // Render the component to a string | ||
| const { getByText } = render( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,29 +6,35 @@ import { | |
| import { useMemo } from 'react'; | ||
| import version from '../version'; | ||
|
|
||
| export type CioClientConfig = { apiKey?: string }; | ||
| type UseCioClient = ( | ||
| apiKey: string, | ||
| options?: Omit<ConstructorClientOptions, 'apiKey' | 'sendTrackingEvents' | 'version'>, | ||
| ) => Nullable<ConstructorIOClient> | never; | ||
| type UseCioClientProps = { | ||
| apiKey?: string; | ||
| cioClient?: any; | ||
|
||
| options?: Omit<ConstructorClientOptions, 'apiKey' | 'sendTrackingEvents' | 'version'>; | ||
| }; | ||
|
|
||
| type UseCioClient = (props: UseCioClientProps) => Nullable<ConstructorIOClient> | never; | ||
|
|
||
| const useCioClient: UseCioClient = (apiKey, options?) => { | ||
| if (!apiKey) { | ||
| throw new Error('Api Key required'); | ||
| const useCioClient: UseCioClient = ({ apiKey, cioClient, options } = {}) => { | ||
| if (!apiKey && !cioClient) { | ||
| throw new Error('Api Key or Constructor Client required'); | ||
| } | ||
|
|
||
| const memoizedCioClient = useMemo(() => { | ||
| if (cioClient) return cioClient; | ||
| if (apiKey && typeof window !== 'undefined') { | ||
| return new ConstructorIOClient({ | ||
| apiKey, | ||
| sendTrackingEvents: true, | ||
| version: `cio-ui-plp-${version}`, | ||
| ...options, | ||
| }); | ||
| return ( | ||
| cioClient || | ||
|
||
| new ConstructorIOClient({ | ||
| apiKey, | ||
| sendTrackingEvents: true, | ||
| version: `cio-ui-plp-${version}`, | ||
| ...options, | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| return null; | ||
| }, [apiKey, options]); | ||
| }, [apiKey, cioClient, options]); | ||
| return memoizedCioClient!; | ||
| }; | ||
|
|
||
|
|
||
This file was deleted.
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.
Why we're not passing the mocked JS client here?
Uh oh!
There was an error while loading. Please reload this page.
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.
I was going for an arbitrary client, but we can do that too.Scratch that, did you mean the mock client intest-utils? Wouldn't that resolve tonullin this server context?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.
You're correct it's not necessary