Skip to content

Commit

Permalink
feat(index): remove getWidgetState (#6476)
Browse files Browse the repository at this point in the history
The old method has long been deprecated and has no usage inside instantsearch outside of running a warning.

The warning on adding a widget with getWidgetState is still present, but slightly reworded.

[FX-3197]

BREAKING CHANGE: rename getWidgetState to getWidgetUiState in your custom widgets or connectors.
  • Loading branch information
Haroenv committed Dec 18, 2024
1 parent 4ef5af9 commit 68ce8dd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 131 deletions.
17 changes: 1 addition & 16 deletions packages/instantsearch-core/src/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,6 @@ type RequiredUiStateLifeCycle<TWidgetDescription extends WidgetDescription> = {
}
) => Partial<IndexUiState & TWidgetDescription['indexUiState']>;

/**
* This function is required for a widget to be taken in account for routing.
* It will derive a uiState for this widget based on the existing uiState and
* the search parameters applied.
*
* @deprecated Use `getWidgetUiState` instead.
* @param uiState - Current state.
* @param widgetStateOptions - Extra information to calculate uiState.
*/
getWidgetState?: RequiredUiStateLifeCycle<TWidgetDescription>['getWidgetUiState'];

/**
* This function is required for a widget to behave correctly when a URL is
* loaded via e.g. Routing. It receives the current UiState and applied search
Expand Down Expand Up @@ -330,7 +319,7 @@ export type Widget<

export type IndexWidget<TUiState extends UiState = UiState> = Omit<
Widget<IndexWidgetDescription & { widgetParams: IndexWidgetParams }>,
'getWidgetUiState' | 'getWidgetState'
'getWidgetUiState'
> & {
// public API
addWidgets: (widgets: Array<Widget | IndexWidget>) => IndexWidget;
Expand Down Expand Up @@ -371,10 +360,6 @@ export type IndexWidget<TUiState extends UiState = UiState> = Omit<
init: (options: IndexInitOptions) => void;
render: (options: IndexRenderOptions) => void;
dispose: (options?: DisposeOptions) => void;
/**
* @deprecated
*/
getWidgetState: (uiState: UiState) => UiState;
getWidgetUiState: <TSpecificUiState extends UiState = TUiState>(
uiState: TSpecificUiState
) => TSpecificUiState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
createIndexInitOptions,
createDisposeOptions,
} from '../../../test/createWidget';
import { warnCache } from '../../lib/public';

import type { Widget } from '../../types';
import type { PlainSearchParameters } from 'algoliasearch-helper';
Expand Down Expand Up @@ -3231,94 +3230,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge
});
});

describe('getWidgetState', () => {
test('warns when index has this method', () => {
warnCache.current = {};

const instance = index({ indexName: 'indexName' });

expect(() => {
instance.getWidgetState({});
}).toWarnDev(
'[InstantSearch]: The `getWidgetState` method is renamed `getWidgetUiState` and will no longer exist under that name in InstantSearch.js 5.x. Please use `getWidgetUiState` instead.'
);
});

test('warns when widget has this method', () => {
warnCache.current = {};

const createDeprecatedSearchBox = (args: Partial<Widget> = {}): Widget =>
createWidget({
dispose: jest.fn(({ state }) => {
return state.setQueryParameter('query', undefined);
}),
getWidgetState: jest.fn((uiState, { searchParameters }) => {
if (!searchParameters.query) {
return uiState;
}

return {
...uiState,
query: searchParameters.query,
};
}),
getWidgetSearchParameters: jest.fn(
(searchParameters, { uiState }) => {
return searchParameters.setQueryParameter(
'query',
uiState.query || ''
);
}
),
...args,
});

const instance = index({ indexName: 'indexName' });
const searchClient = createSearchClient();
const mainHelper = algoliasearchHelper(searchClient, '', {});
const instantSearchInstance = createInstantSearch({
mainHelper,
});

instance.addWidgets([createDeprecatedSearchBox()]);

expect(() => {
instance.init(
createIndexInitOptions({
instantSearchInstance,
parent: null,
})
);
}).toWarnDev(
'[InstantSearch]: The `getWidgetState` method is renamed `getWidgetUiState` and will no longer exist under that name in InstantSearch.js 5.x. Please use `getWidgetUiState` instead.'
);
});

test('does not warn for index itself', () => {
warnCache.current = {};

const instance = index({ indexName: 'indexName' });
const searchClient = createSearchClient();
const mainHelper = algoliasearchHelper(searchClient, '', {});
const instantSearchInstance = createInstantSearch({
mainHelper,
});

instance.addWidgets([index({ indexName: 'other' })]);

expect(() => {
instance.init(
createIndexInitOptions({
instantSearchInstance,
parent: null,
})
);
}).not.toWarnDev(
'[InstantSearch]: The `getWidgetState` method is renamed `getWidgetUiState` and will no longer exist under that name in InstantSearch.js 5.x. Please use `getWidgetUiState` instead.'
);
});
});

describe('setIndexUiState', () => {
it('updates main UI state with an object', () => {
const instance = index({ indexName: 'indexName' });
Expand Down
23 changes: 1 addition & 22 deletions packages/instantsearch-core/src/widgets/index-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import algoliasearchHelper from 'algoliasearch-helper';
import {
addWidgetId,
createDocumentationMessageGenerator,
warning,
isIndexWidget,
createInitArgs,
createRenderArgs,
Expand Down Expand Up @@ -120,15 +119,11 @@ function getLocalWidgetsUiState(
return uiState;
}

if (!widget.getWidgetUiState && !widget.getWidgetState) {
return uiState;
}

if (widget.getWidgetUiState) {
return widget.getWidgetUiState(uiState, widgetStateOptions);
}

return widget.getWidgetState!(uiState, widgetStateOptions);
return uiState;
}, initialUiState);
}

Expand Down Expand Up @@ -685,13 +680,6 @@ export const index = (widgetParams: IndexWidgetParams): IndexWidget => {
});

localWidgets.forEach((widget) => {
warning(
// if it has NO getWidgetState or if it has getWidgetUiState, we don't warn
// aka we warn if there's _only_ getWidgetState
!widget.getWidgetState || Boolean(widget.getWidgetUiState),
'The `getWidgetState` method is renamed `getWidgetUiState` and will no longer exist under that name in InstantSearch.js 5.x. Please use `getWidgetUiState` instead.'
);

if (widget.init) {
widget.init(createInitArgs(instantSearchInstance, this, uiState));
}
Expand Down Expand Up @@ -847,15 +835,6 @@ export const index = (widgetParams: IndexWidgetParams): IndexWidget => {
);
},

getWidgetState(uiState: UiState) {
warning(
false,
'The `getWidgetState` method is renamed `getWidgetUiState` and will no longer exist under that name in InstantSearch.js 5.x. Please use `getWidgetUiState` instead.'
);

return this.getWidgetUiState(uiState);
},

getWidgetSearchParameters(searchParameters, { uiState }) {
return getLocalWidgetsSearchParameters(localWidgets, {
uiState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsear
expect(renderArgs.createURL()).toBe('#');
});

it('allows for widgets without getWidgetState', () => {
it('allows for widgets without getWidgetUiState', () => {
let instantSearchInstance;
mount({
mixins: [
Expand All @@ -1255,12 +1255,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsear
const widget = {
init: jest.fn(),
render: jest.fn(),
getWidgetState(uiState) {
getWidgetUiState(uiState) {
return uiState;
},
};

const widgetWithoutGetWidgetState = {
const widgetWithoutGetWidgetUiState = {
init: jest.fn(),
render: jest.fn(),
};
Expand All @@ -1269,7 +1269,10 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsear
lol: createSerializedState(),
});

instantSearchInstance.addWidgets([widget, widgetWithoutGetWidgetState]);
instantSearchInstance.addWidgets([
widget,
widgetWithoutGetWidgetUiState,
]);

instantSearchInstance.__forceRender(
widget,
Expand Down

0 comments on commit 68ce8dd

Please sign in to comment.