Skip to content

Commit

Permalink
Merge branch 'hooks_baseline' into hooks_export
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed Nov 10, 2023
2 parents 3af2878 + 9d2ba00 commit 3629879
Show file tree
Hide file tree
Showing 4 changed files with 462 additions and 445 deletions.
99 changes: 11 additions & 88 deletions src/__tests__/useClient.test.tsx
Original file line number Diff line number Diff line change
@@ -1,100 +1,23 @@
import React from 'react';
import { render } from '@testing-library/react';

/** Mocks */
import { mockSdk } from './testUtils/mockSplitSdk';
jest.mock('@splitsoftware/splitio/client', () => {
return { SplitFactory: mockSdk() };
});
import { SplitFactory as SplitSdk } from '@splitsoftware/splitio/client';
import { sdkBrowser } from './testUtils/sdkConfigs';
const useSplitClientMock = jest.fn();
jest.mock('../useSplitClient', () => ({
useSplitClient: useSplitClientMock
}));

/** Test target */
import { SplitFactory } from '../SplitFactory';
import { SplitClient } from '../SplitClient';
import { useClient } from '../useClient';
import { testAttributesBinding, TestComponentProps } from './testUtils/utils';

describe('useClient', () => {

test('returns the main client from the context updated by SplitFactory.', () => {
const outerFactory = SplitSdk(sdkBrowser);
let client;
render(
<SplitFactory factory={outerFactory} >
{React.createElement(() => {
client = useClient();
return null;
})}
</SplitFactory>
);
expect(client).toBe(outerFactory.client());
});

test('returns the client from the context updated by SplitClient.', () => {
const outerFactory = SplitSdk(sdkBrowser);
let client;
render(
<SplitFactory factory={outerFactory} >
<SplitClient splitKey='user2' >
{React.createElement(() => {
client = useClient();
return null;
})}
</SplitClient>
</SplitFactory>
);
expect(client).toBe(outerFactory.client('user2'));
});

test('returns a new client from the factory at Split context given a splitKey.', () => {
const outerFactory = SplitSdk(sdkBrowser);
let client;
render(
<SplitFactory factory={outerFactory} >
{React.createElement(() => {
(outerFactory.client as jest.Mock).mockClear();
client = useClient('user2', 'user');
return null;
})}
</SplitFactory>
);
expect(outerFactory.client as jest.Mock).toBeCalledWith('user2', 'user');
expect(outerFactory.client as jest.Mock).toHaveReturnedWith(client);
});

test('returns null if invoked outside Split context.', () => {
let client;
let sharedClient;
render(
React.createElement(() => {
client = useClient();
sharedClient = useClient('user2', 'user');
return null;
})
);
expect(client).toBe(null);
expect(sharedClient).toBe(null);
});

test('attributes binding test with utility', (done) => {

// eslint-disable-next-line react/prop-types
const InnerComponent = ({ splitKey, attributesClient, testSwitch}) => {
useClient(splitKey, 'user', attributesClient);
testSwitch(done, splitKey);
return null;
};
test('calls useSplitClient with the correct arguments and returns the client.', () => {
const attributes = { someAttribute: 'someValue' };
const client = 'client';
useSplitClientMock.mockReturnValue({ client, isReady: false });

function Component({ attributesFactory, attributesClient, splitKey, testSwitch, factory }: TestComponentProps) {
return (
<SplitFactory factory={factory} attributes={attributesFactory} >
<InnerComponent splitKey={splitKey} attributesClient={attributesClient} testSwitch={testSwitch} />
</SplitFactory>
);
}
expect(useClient('someKey', 'someTrafficType', attributes)).toBe(client);

testAttributesBinding(Component);
expect(useSplitClientMock).toHaveBeenCalledTimes(1);
expect(useSplitClientMock).toHaveBeenCalledWith({ splitKey: 'someKey', trafficType: 'someTrafficType', attributes });
});

});
Loading

0 comments on commit 3629879

Please sign in to comment.