Skip to content
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

SSR with flag treatments in the server-side [WIP] #202

Draft
wants to merge 1 commit into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@splitsoftware/splitio-react",
"version": "1.13.1-rc.0",
"version": "1.13.1-rc.1",
"description": "A React library to easily integrate and use Split JS SDK",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down Expand Up @@ -63,7 +63,7 @@
},
"homepage": "https://github.com/splitio/react-client#readme",
"dependencies": {
"@splitsoftware/splitio": "10.28.1-rc.2",
"@splitsoftware/splitio": "10.28.1-rc.4",
"memoize-one": "^5.1.1",
"shallowequal": "^1.1.0",
"tslib": "^2.3.1"
Expand Down
1 change: 1 addition & 0 deletions src/SplitFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class SplitFactory extends React.Component<ISplitFactoryProps, { factory:
// We use an idempotent variant of the Split factory builder (i.e., given the same config, it returns the same already
// created instance), since React component constructors is part of render-phase and can be invoked multiple times.
factory = getSplitFactory(config);
factory.init();
}
}
this.isFactoryExternal = propFactory ? true : false;
Expand Down
9 changes: 8 additions & 1 deletion src/SplitFactoryProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ export function SplitFactoryProvider(props: ISplitFactoryProps) {

const [configFactory, setConfigFactory] = React.useState<IFactoryWithClients | null>(null);
const factory = React.useMemo(() => {
return propFactory || (configFactory && config === configFactory.config ? configFactory : null);
return propFactory ?
propFactory :
configFactory && config === configFactory.config ?
configFactory :
config && config.preloadedData ?
getSplitFactory(config) :
null;
}, [config, propFactory, configFactory]);
const client = factory ? getSplitClient(factory) : null;

// Effect to initialize and destroy the factory
React.useEffect(() => {
if (config) {
const factory = getSplitFactory(config);
factory.init();

return () => {
destroySplitFactory(factory);
Expand Down
25 changes: 25 additions & 0 deletions src/__tests__/SplitFactoryProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ describe('SplitFactoryProvider', () => {
);
});

test('passes ready from cache props to the child if initialized with a config with preloaded data.', () => {
const configWithPreloadedData = { ...sdkBrowser, preloadedData: {} };
let internalFactory;

render(
<SplitFactoryProvider config={configWithPreloadedData} >
{({ factory, client, isReady, isReadyFromCache, hasTimedout, isTimedout, isDestroyed, lastUpdate }: ISplitFactoryChildProps) => {
internalFactory = factory;
expect(factory).toBeDefined();
expect(client).toBeDefined();
expect(isReady).toBe(false);
expect(isReadyFromCache).toBe(true);
expect(hasTimedout).toBe(false);
expect(isTimedout).toBe(false);
expect(isDestroyed).toBe(false);
expect(lastUpdate).toBe(0);
return null;
}}
</SplitFactoryProvider>
);

expect(internalFactory.init).toBeCalledTimes(1);
});

test('passes ready props to the child if initialized with a ready factory.', async () => {
const outerFactory = SplitSdk(sdkBrowser);
(outerFactory as any).client().__emitter__.emit(Event.SDK_READY_FROM_CACHE);
Expand Down Expand Up @@ -99,6 +123,7 @@ describe('SplitFactoryProvider', () => {
);

const innerFactory = (SplitSdk as jest.Mock).mock.results.slice(-1)[0].value;
expect(innerFactory.init).toBeCalledTimes(1);
act(() => (innerFactory as any).client().__emitter__.emit(Event.SDK_READY_TIMED_OUT));
act(() => (innerFactory as any).client().__emitter__.emit(Event.SDK_READY_FROM_CACHE));
act(() => (innerFactory as any).client().__emitter__.emit(Event.SDK_READY));
Expand Down
18 changes: 12 additions & 6 deletions src/__tests__/testUtils/mockSplitSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function mockSdk() {
function mockClient(_key: SplitIO.SplitKey, _trafficType?: string) {
// Readiness
let isReady = false;
let isReadyFromCache = false;
let isReadyFromCache = config.preloadedData ? true : false;
let hasTimedout = false;
let isDestroyed = false;
let lastUpdate = 0;
Expand Down Expand Up @@ -147,20 +147,26 @@ export function mockSdk() {
return Promise.all(Object.keys(__clients__).map(instanceId => __clients__[instanceId].destroy()));
});

// SDK internal modules
const modules = {
settings: Object.assign({
version: jsSdkVersion,
}, config),
isPure: undefined,
}
if (__updateModules) __updateModules(modules);

// SDK factory
const factory = {
client,
manager,
destroy,
__names__: names,
__clients__,
settings: Object.assign({
version: jsSdkVersion,
}, config),
settings: modules.settings,
init: modules.isPure ? jest.fn() : undefined
};

if (__updateModules) __updateModules(factory);

return factory;
});

Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface IClientWithContext extends SplitIO.IBrowserClient {
*/
export interface IFactoryWithClients extends SplitIO.IBrowserSDK {
config: SplitIO.IBrowserSettings;
init(): void;
}

// exported for testing purposes
Expand All @@ -38,6 +39,7 @@ export function getSplitFactory(config: SplitIO.IBrowserSettings) {
// @ts-expect-error. 2nd param is not part of type definitions. Used to overwrite the SDK version
const newFactory = SplitSdk(config, (modules) => {
modules.settings.version = VERSION;
modules.isPure = true;
}) as IFactoryWithClients;
newFactory.config = config;
__factories.set(config, newFactory);
Expand Down
Loading