Skip to content

Commit

Permalink
fix: use mainHelper consistently (#5875)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhayab authored Oct 4, 2023
1 parent da70152 commit 121d13a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ describe('insights', () => {
}

const getUserToken = () =>
(instantSearchInstance.helper!.state as PlainSearchParameters).userToken;
(instantSearchInstance.mainHelper!.state as PlainSearchParameters)
.userToken;

return {
analytics,
Expand Down Expand Up @@ -94,10 +95,11 @@ describe('insights', () => {
});
instantSearchInstance.start();

const helper = instantSearchInstance.helper!;
const helper = instantSearchInstance.mainHelper!;

const getUserToken = () =>
(instantSearchInstance.helper!.state as PlainSearchParameters).userToken;
(instantSearchInstance.mainHelper!.state as PlainSearchParameters)
.userToken;

return {
analytics,
Expand Down Expand Up @@ -473,17 +475,17 @@ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-f
insightsClient,
})
);
expect(instantSearchInstance.helper!.state.clickAnalytics).toBe(true);
expect(instantSearchInstance.mainHelper!.state.clickAnalytics).toBe(true);
});

it("doesn't reset page", () => {
const { insightsClient, instantSearchInstance } = createTestEnvironment();
const middleware = createInsightsMiddleware({
insightsClient,
})({ instantSearchInstance });
instantSearchInstance.helper!.setPage(100);
instantSearchInstance.mainHelper!.setPage(100);
middleware.subscribe();
expect(instantSearchInstance.helper!.state.page).toBe(100);
expect(instantSearchInstance.mainHelper!.state.page).toBe(100);
});

it('adds user agent', () => {
Expand Down Expand Up @@ -670,15 +672,15 @@ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-f

insightsClient('setUserToken', 'abc');
instantSearchInstance.start();
instantSearchInstance.helper!.setPage(100);
instantSearchInstance.mainHelper!.setPage(100);

instantSearchInstance.use(
createInsightsMiddleware({
insightsClient,
})
);

expect(instantSearchInstance.helper!.state.page).toBe(100);
expect(instantSearchInstance.mainHelper!.state.page).toBe(100);
expect(getUserToken()).toEqual('abc');
});

Expand All @@ -702,7 +704,7 @@ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-f
createTestEnvironment({ started: false });

instantSearchInstance.start();
instantSearchInstance.helper!.setPage(100);
instantSearchInstance.mainHelper!.setPage(100);

instantSearchInstance.use(
createInsightsMiddleware({
Expand All @@ -714,7 +716,7 @@ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-f

await wait(0);

expect(instantSearchInstance.helper!.state.page).toEqual(100);
expect(instantSearchInstance.mainHelper!.state.page).toEqual(100);
expect(getUserToken()).toEqual('def');
});

Expand Down Expand Up @@ -774,7 +776,8 @@ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-f
await wait(0);

expect(
(instantSearchInstance.helper!.state as PlainSearchParameters).userToken
(instantSearchInstance.mainHelper!.state as PlainSearchParameters)
.userToken
).toEqual('def');
});

Expand Down Expand Up @@ -1183,7 +1186,7 @@ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-f

instantSearchInstance
.helper!.setState({
...instantSearchInstance.helper!.state,
...instantSearchInstance.mainHelper!.state,
query: 'test',
})
.search();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export function createInsightsMiddleware<
started() {
insightsClient('addAlgoliaAgent', 'insights-middleware');

helper = instantSearchInstance.helper!;
helper = instantSearchInstance.mainHelper!;

initialParameters = {
userToken: (helper.state as PlainSearchParameters).userToken,
Expand Down
5 changes: 4 additions & 1 deletion packages/instantsearch.js/src/widgets/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,10 @@ const index = (widgetParams: IndexWidgetParams): IndexWidget => {
};

derivedHelper = mainHelper.derive(() =>
mergeSearchParameters(...resolveSearchParameters(this))
mergeSearchParameters(
mainHelper.state,
...resolveSearchParameters(this)
)
);

const indexInitialResults =
Expand Down
4 changes: 2 additions & 2 deletions tests/common/widgets/current-refinements/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export function createOptionsTests(
<span
class="ais-CurrentRefinements-categoryLabel"
>
Apple
Samsung
</span>
<button
class="ais-CurrentRefinements-delete"
Expand All @@ -146,7 +146,7 @@ export function createOptionsTests(
<span
class="ais-CurrentRefinements-categoryLabel"
>
Samsung
Apple
</span>
<button
class="ais-CurrentRefinements-delete"
Expand Down

0 comments on commit 121d13a

Please sign in to comment.