Skip to content

Commit 88442bb

Browse files
Merge branch 'update_version_overwriting' into add_hooks
2 parents 1a215d6 + 60afbda commit 88442bb

8 files changed

Lines changed: 22 additions & 14 deletions

File tree

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ module.exports = {
3535
'@typescript-eslint/no-explicit-any': 'off',
3636
'@typescript-eslint/no-non-null-assertion': 'off',
3737
'@typescript-eslint/no-unused-vars': ['warn', { 'argsIgnorePattern': '^_' }],
38+
'prefer-const': ['error', {
39+
'destructuring': 'all'
40+
}]
3841
},
3942
'overrides': [{
4043
'files': ['src/**/*.ts', 'src/**/*.tsx'],

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @splitio/sdk

src/SplitFactory.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22

33
import { SplitComponent } from './SplitClient';
44
import { ISplitFactoryProps } from './types';
5-
import { VERSION, WARN_SF_CONFIG_AND_FACTORY, ERROR_SF_NO_CONFIG_AND_FACTORY } from './constants';
5+
import { WARN_SF_CONFIG_AND_FACTORY, ERROR_SF_NO_CONFIG_AND_FACTORY } from './constants';
66
import { getSplitFactory, destroySplitFactory, IFactoryWithClients } from './utils';
77

88
/**
@@ -46,8 +46,6 @@ class SplitFactory extends React.Component<ISplitFactoryProps, { factory: SplitI
4646
factory = propFactory;
4747
} else {
4848
if (config) {
49-
// Don't try this at home. Used to overwrite the settings version when we create our own factory.
50-
(config as any).version = VERSION;
5149
// We use an idempotent variant of the Split factory builder (i.e., given the same config, it returns the same already
5250
// created instance), since React component constructors is part of render-phase and can be invoked multiple times.
5351
factory = getSplitFactory(config);

src/__tests__/SplitClient.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { render, RenderResult, act } from '@testing-library/react';
2+
import { render, act } from '@testing-library/react';
33

44
/** Mocks and test utils */
55
import { mockSdk, Event, assertNoListeners, clientListenerCount } from './testUtils/mockSplitSdk';
@@ -263,8 +263,7 @@ describe('SplitClient', () => {
263263
test(`passes a new client if re-rendered with a different splitKey.
264264
Only updates the state if the new client triggers an event, but not the previous one.`, (done) => {
265265
const outerFactory = SplitSdk(sdkBrowser);
266-
let renderTimes = 0; // eslint-disable-next-line prefer-const
267-
let wrapper: RenderResult;
266+
let renderTimes = 0;
268267

269268
class InnerComponent extends React.Component<any, { splitKey: string }> {
270269

@@ -294,6 +293,7 @@ describe('SplitClient', () => {
294293
expect(renderTimes).toBe(6);
295294

296295
// check that outerFactory's clients have no event listeners
296+
// eslint-disable-next-line no-use-before-define
297297
wrapper.unmount();
298298
assertNoListeners(outerFactory);
299299
done();
@@ -352,7 +352,7 @@ describe('SplitClient', () => {
352352
}
353353
}
354354

355-
wrapper = render(
355+
const wrapper = render(
356356
<SplitFactory factory={outerFactory} >
357357
<InnerComponent />
358358
</SplitFactory>);

src/__tests__/testUtils/mockSplitSdk.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function mockClient(_key: SplitIO.SplitKey, _trafficType?: string) {
108108

109109
export function mockSdk() {
110110

111-
return jest.fn((config: SplitIO.IBrowserSettings) => {
111+
return jest.fn((config: SplitIO.IBrowserSettings, __updateModules) => {
112112

113113
// Manager
114114
const names: jest.Mock = jest.fn().mockReturnValue([]);
@@ -134,6 +134,8 @@ export function mockSdk() {
134134
}, config),
135135
};
136136

137+
if (__updateModules) __updateModules(factory);
138+
137139
return factory;
138140
});
139141

src/useClientAndContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { getSplitSharedClient, checkHooks, initAttributes, IClientWithContext }
1313
export function useClientAndContext(key?: SplitIO.SplitKey, trafficType?: string, attributes?: SplitIO.Attributes): ISplitContextValues {
1414
if (!checkHooks(ERROR_UC_NO_USECONTEXT)) return INITIAL_CONTEXT;
1515

16-
const context = React.useContext(SplitContext); // eslint-disable-next-line prefer-const
16+
const context = React.useContext(SplitContext);
1717
let { factory, client } = context;
1818
if (key && factory) {
1919
client = getSplitSharedClient(factory, key, trafficType, attributes);

src/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { SplitFactory as SplitSdk } from '@splitsoftware/splitio/client';
3+
import { VERSION } from './constants';
34

45
// Utils used to access singleton instances of Split factories and clients, and to gracefully shutdown all clients together.
56

@@ -31,7 +32,10 @@ export const __factories: Map<SplitIO.IBrowserSettings, IFactoryWithClients> = n
3132
export function getSplitFactory(config: SplitIO.IBrowserSettings): IFactoryWithClients {
3233
if (!__factories.has(config)) {
3334
// SplitSDK is not an idempotent operation
34-
const newFactory = SplitSdk(config) as IFactoryWithClients;
35+
// @ts-expect-error. 2nd param is not part of type definitions. Used to overwrite the SDK version
36+
const newFactory = SplitSdk(config, (modules) => {
37+
modules.settings.version = VERSION;
38+
}) as IFactoryWithClients;
3539
newFactory.sharedClientInstances = new Set();
3640
newFactory.config = config;
3741
__factories.set(config, newFactory);

types/SplitTreatments.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { ISplitTreatmentsProps } from './types';
77
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#get-treatments-with-configurations}
88
*/
99
declare class SplitTreatments extends React.Component<ISplitTreatmentsProps> {
10-
private logWarning?;
11-
private evaluateFeatureFlags;
12-
render(): JSX.Element;
13-
componentDidMount(): void;
10+
private logWarning?;
11+
private evaluateFeatureFlags;
12+
render(): JSX.Element;
13+
componentDidMount(): void;
1414
}
1515
export default SplitTreatments;

0 commit comments

Comments
 (0)