Skip to content

Commit 3fca986

Browse files
authoredMay 3, 2021
feat(widget): add access to "parent" in dispose (#4745)
If the widget is conditionally adding widgets to the parent in init & render, it needs to be able to remove those on dispose.
1 parent b9c884d commit 3fca986

File tree

27 files changed

+166
-92
lines changed

27 files changed

+166
-92
lines changed
 

‎src/connectors/breadcrumb/__tests__/connectBreadcrumb-test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import connectBreadcrumb from '../connectBreadcrumb';
77
import { createSearchClient } from '../../../../test/mock/createSearchClient';
88
import { createSingleSearchResponse } from '../../../../test/mock/createAPIResponse';
99
import {
10+
createDisposeOptions,
1011
createInitOptions,
1112
createRenderOptions,
1213
} from '../../../../test/mock/createWidget';
@@ -1021,7 +1022,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/breadcrumb/
10211022
const widget = makeWidget({ attributes: ['category'] });
10221023

10231024
expect(() =>
1024-
widget.dispose!({ helper, state: helper.state })
1025+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
10251026
).not.toThrow();
10261027
});
10271028

@@ -1044,7 +1045,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/breadcrumb/
10441045
})
10451046
);
10461047

1047-
const newState = widget.dispose!({ helper, state: helper.state });
1048+
const newState = widget.dispose!(
1049+
createDisposeOptions({ helper, state: helper.state })
1050+
);
10481051

10491052
expect(newState).toBeUndefined();
10501053
});

‎src/connectors/clear-refinements/__tests__/connectClearRefinements-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import algoliasearchHelper, { SearchResults } from 'algoliasearch-helper';
22
import {
3+
createDisposeOptions,
34
createInitOptions,
45
createRenderOptions,
56
} from '../../../../test/mock/createWidget';
@@ -122,7 +123,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/clear-refin
122123
const widget = makeWidget({});
123124

124125
expect(() =>
125-
widget.dispose!({ helper, state: helper.state })
126+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
126127
).not.toThrow();
127128
});
128129

‎src/connectors/current-refinements/__tests__/connectCurrentRefinements-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import connectCurrentRefinements, {
88
} from '../connectCurrentRefinements';
99
import { createSearchClient } from '../../../../test/mock/createSearchClient';
1010
import {
11+
createDisposeOptions,
1112
createInitOptions,
1213
createRenderOptions,
1314
} from '../../../../test/mock/createWidget';
@@ -119,7 +120,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/current-ref
119120
const widget = customCurrentRefinements({});
120121

121122
expect(() =>
122-
widget.dispose!({ helper, state: helper.state })
123+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
123124
).not.toThrow();
124125
});
125126

‎src/connectors/hierarchical-menu/__tests__/connectHierarchicalMenu-test.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import algoliasearchHelper, {
55
import { warning } from '../../../lib/utils';
66
import connectHierarchicalMenu from '../connectHierarchicalMenu';
77
import {
8+
createDisposeOptions,
89
createInitOptions,
910
createRenderOptions,
1011
} from '../../../../test/mock/createWidget';
@@ -393,7 +394,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hierarchica
393394
})
394395
);
395396
expect(() =>
396-
widget.dispose!({ helper, state: helper.state })
397+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
397398
).not.toThrow();
398399
});
399400

@@ -412,9 +413,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hierarchica
412413
})
413414
);
414415

415-
expect(widget.dispose!({ helper, state: helper.state })).toEqual(
416-
new SearchParameters({ index: indexName })
417-
);
416+
expect(
417+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
418+
).toEqual(new SearchParameters({ index: indexName }));
418419
});
419420

420421
it('unsets refinement', () => {
@@ -448,9 +449,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hierarchica
448449
category: ['zombo.com'],
449450
});
450451

451-
expect(widget.dispose!({ helper, state: helper.state })).toEqual(
452-
new SearchParameters({ index: indexName })
453-
);
452+
expect(
453+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
454+
).toEqual(new SearchParameters({ index: indexName }));
454455
});
455456
});
456457

‎src/connectors/hits-per-page/__tests__/connectHitsPerPage-test.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import algoliasearchHelper, {
55
import { connectHitsPerPage } from '../..';
66
import { HitsPerPageConnectorParams } from '../connectHitsPerPage';
77
import {
8+
createDisposeOptions,
89
createInitOptions,
910
createRenderOptions,
1011
} from '../../../../test/mock/createWidget';
@@ -607,7 +608,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits-per-pa
607608

608609
expect(unmountFn).toHaveBeenCalledTimes(0);
609610

610-
widget.dispose!({ helper, state: helper.state });
611+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }));
611612

612613
expect(unmountFn).toHaveBeenCalledTimes(1);
613614
});
@@ -626,7 +627,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits-per-pa
626627
});
627628

628629
expect(() =>
629-
widget.dispose!({ helper, state: helper.state })
630+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
630631
).not.toThrow();
631632
});
632633

@@ -648,10 +649,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits-per-pa
648649

649650
expect(helper.state.hitsPerPage).toBe(5);
650651

651-
const nextState = widget.dispose!({
652-
helper,
653-
state: helper.state,
654-
}) as SearchParameters;
652+
const nextState = widget.dispose!(
653+
createDisposeOptions({
654+
helper,
655+
state: helper.state,
656+
})
657+
) as SearchParameters;
655658

656659
expect(nextState.hitsPerPage).toBeUndefined();
657660
});

‎src/connectors/hits/__tests__/connectHits-test.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import algoliasearchHelper, {
55
import { TAG_PLACEHOLDER, deserializePayload } from '../../../lib/utils';
66
import connectHits from '../connectHits';
77
import {
8+
createDisposeOptions,
89
createInitOptions,
910
createRenderOptions,
1011
} from '../../../../test/mock/createWidget';
@@ -605,7 +606,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
605606

606607
expect(unmountFn).toHaveBeenCalledTimes(0);
607608

608-
widget.dispose!({ helper, state: helper.state });
609+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }));
609610

610611
expect(unmountFn).toHaveBeenCalledTimes(1);
611612
});
@@ -618,7 +619,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
618619
const widget = makeWidget({});
619620

620621
expect(() =>
621-
widget.dispose!({ helper, state: helper.state })
622+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
622623
).not.toThrow();
623624
});
624625

@@ -639,10 +640,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
639640
TAG_PLACEHOLDER.highlightPostTag
640641
);
641642

642-
const nextState = widget.dispose!({
643-
helper,
644-
state: helper.state,
645-
}) as SearchParameters;
643+
const nextState = widget.dispose!(
644+
createDisposeOptions({
645+
helper,
646+
state: helper.state,
647+
})
648+
) as SearchParameters;
646649

647650
expect(nextState.highlightPreTag).toBeUndefined();
648651
expect(nextState.highlightPostTag).toBeUndefined();
@@ -663,10 +666,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
663666
expect(helper.state.highlightPreTag).toBe('<mark>');
664667
expect(helper.state.highlightPostTag).toBe('</mark>');
665668

666-
const nextState = widget.dispose!({
667-
helper,
668-
state: helper.state,
669-
}) as SearchParameters;
669+
const nextState = widget.dispose!(
670+
createDisposeOptions({
671+
helper,
672+
state: helper.state,
673+
})
674+
) as SearchParameters;
670675

671676
expect(nextState.highlightPreTag).toBe('<mark>');
672677
expect(nextState.highlightPostTag).toBe('</mark>');

‎src/connectors/hits/__tests__/connectHitsWithInsights-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import algoliasearchHelper, { SearchResults } from 'algoliasearch-helper';
22
import { createSearchClient } from '../../../../test/mock/createSearchClient';
33
import { createInstantSearch } from '../../../../test/mock/createInstantSearch';
44
import {
5+
createDisposeOptions,
56
createInitOptions,
67
createRenderOptions,
78
} from '../../../../test/mock/createWidget';
@@ -100,7 +101,7 @@ describe('connectHitsWithInsights', () => {
100101
const widget = makeWidget({});
101102
const helper = algoliasearchHelper(createSearchClient(), '', {});
102103
expect(() => {
103-
widget.dispose!({ helper, state: helper.state });
104+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }));
104105
}).not.toThrow();
105106
});
106107
});

‎src/connectors/infinite-hits/__tests__/connectInfiniteHits-test.ts

+21-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import algoliasearchHelper, {
55
import { SearchClient, HitAttributeHighlightResult } from '../../../types';
66
import { createInstantSearch } from '../../../../test/mock/createInstantSearch';
77
import {
8+
createDisposeOptions,
89
createInitOptions,
910
createRenderOptions,
1011
} from '../../../../test/mock/createWidget';
@@ -837,7 +838,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
837838

838839
expect(unmountFn).toHaveBeenCalledTimes(0);
839840

840-
widget.dispose!({ helper, state: helper.state });
841+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }));
841842

842843
expect(unmountFn).toHaveBeenCalledTimes(1);
843844
});
@@ -850,7 +851,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
850851
const widget = makeWidget({});
851852

852853
expect(() =>
853-
widget.dispose!({ helper, state: helper.state })
854+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
854855
).not.toThrow();
855856
});
856857

@@ -871,10 +872,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
871872
TAG_PLACEHOLDER.highlightPostTag
872873
);
873874

874-
const nextState = widget.dispose!({
875-
helper,
876-
state: helper.state,
877-
}) as SearchParameters;
875+
const nextState = widget.dispose!(
876+
createDisposeOptions({
877+
helper,
878+
state: helper.state,
879+
})
880+
) as SearchParameters;
878881

879882
expect(nextState.highlightPreTag).toBeUndefined();
880883
expect(nextState.highlightPostTag).toBeUndefined();
@@ -895,10 +898,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
895898
expect(helper.state.highlightPreTag).toBe('<mark>');
896899
expect(helper.state.highlightPostTag).toBe('</mark>');
897900

898-
const nextState = widget.dispose!({
899-
helper,
900-
state: helper.state,
901-
}) as SearchParameters;
901+
const nextState = widget.dispose!(
902+
createDisposeOptions({
903+
helper,
904+
state: helper.state,
905+
})
906+
) as SearchParameters;
902907

903908
expect(nextState.highlightPreTag).toBe('<mark>');
904909
expect(nextState.highlightPostTag).toBe('</mark>');
@@ -915,10 +920,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
915920

916921
expect(helper.state.page).toBe(5);
917922

918-
const nextState = widget.dispose!({
919-
helper,
920-
state: helper.state,
921-
}) as SearchParameters;
923+
const nextState = widget.dispose!(
924+
createDisposeOptions({
925+
helper,
926+
state: helper.state,
927+
})
928+
) as SearchParameters;
922929

923930
expect(nextState.page).toBeUndefined();
924931
});

‎src/connectors/infinite-hits/__tests__/connectInfiniteHitsWithInsights-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import algoliasearchHelper, { SearchResults } from 'algoliasearch-helper';
22
import {
3+
createDisposeOptions,
34
createInitOptions,
45
createRenderOptions,
56
} from '../../../../test/mock/createWidget';
@@ -116,7 +117,7 @@ describe('connectInfiniteHitsWithInsights', () => {
116117
const helper = algoliasearchHelper(createSearchClient(), '', {});
117118
const widget = makeWidget({});
118119
expect(() =>
119-
widget.dispose!({ helper, state: helper.state })
120+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
120121
).not.toThrow();
121122
});
122123
});

‎src/connectors/menu/__tests__/connectMenu-test.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { createSearchClient } from '../../../../test/mock/createSearchClient';
66
import { createSingleSearchResponse } from '../../../../test/mock/createAPIResponse';
77
import { createInstantSearch } from '../../../../test/mock/createInstantSearch';
88
import {
9+
createDisposeOptions,
910
createInitOptions,
1011
createRenderOptions,
1112
} from '../../../../test/mock/createWidget';
@@ -475,7 +476,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
475476
})
476477
);
477478
expect(() =>
478-
widget.dispose!!({ helper, state: helper.state })
479+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
479480
).not.toThrow();
480481
});
481482

@@ -1194,7 +1195,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
11941195
})
11951196
);
11961197

1197-
const newState = widget.dispose!({ state: helper.state, helper });
1198+
const newState = widget.dispose!(
1199+
createDisposeOptions({ state: helper.state, helper })
1200+
);
11981201

11991202
expect(newState).toEqual(
12001203
new SearchParameters({
@@ -1236,7 +1239,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
12361239
})
12371240
);
12381241

1239-
const newState = widget.dispose!({ state: helper.state, helper });
1242+
const newState = widget.dispose!(
1243+
createDisposeOptions({ state: helper.state, helper })
1244+
);
12401245

12411246
expect(newState).toEqual(
12421247
new SearchParameters({
@@ -1260,7 +1265,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
12601265
})
12611266
);
12621267
helper.search = jest.fn();
1263-
const newState = widget.dispose!({ state, helper });
1268+
const newState = widget.dispose!(createDisposeOptions({ state, helper }));
12641269

12651270
expect(newState).toEqual(new SearchParameters());
12661271
});

‎src/connectors/numeric-menu/__tests__/connectNumericMenu-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import connectNumericMenu, {
99
} from '../connectNumericMenu';
1010
import { createSearchClient } from '../../../../test/mock/createSearchClient';
1111
import {
12+
createDisposeOptions,
1213
createInitOptions,
1314
createRenderOptions,
1415
} from '../../../../test/mock/createWidget';
@@ -734,7 +735,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/numeric-men
734735
const helper = jsHelper(createSearchClient(), '');
735736

736737
expect(() =>
737-
widget.dispose!({ helper, state: helper.state })
738+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
738739
).not.toThrow();
739740
});
740741

‎src/connectors/pagination/__tests__/connectPagination-test.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import connectPagination, {
88
PaginationRenderState,
99
} from '../connectPagination';
1010
import {
11+
createDisposeOptions,
1112
createInitOptions,
1213
createRenderOptions,
1314
} from '../../../../test/mock/createWidget';
@@ -308,7 +309,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/pagination/
308309

309310
expect(unmountFn).toHaveBeenCalledTimes(0);
310311

311-
widget.dispose!({ helper, state: helper.state });
312+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }));
312313

313314
expect(unmountFn).toHaveBeenCalledTimes(1);
314315
});
@@ -321,7 +322,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/pagination/
321322
const widget = makeWidget({});
322323

323324
expect(() =>
324-
widget.dispose!({ helper, state: helper.state })
325+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
325326
).not.toThrow();
326327
});
327328

@@ -336,7 +337,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/pagination/
336337

337338
expect(helper.state.page).toBe(5);
338339

339-
const nextState = widget.dispose!({ helper, state: helper.state });
340+
const nextState = widget.dispose!(
341+
createDisposeOptions({ helper, state: helper.state })
342+
);
340343

341344
// error used for typescript
342345
if (!nextState) {

‎src/connectors/powered-by/__tests__/connectPoweredBy-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import jsHelper from 'algoliasearch-helper';
22
import { createSearchClient } from '../../../../test/mock/createSearchClient';
33
import {
4+
createDisposeOptions,
45
createInitOptions,
56
createRenderOptions,
67
} from '../../../../test/mock/createWidget';
@@ -119,7 +120,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/powered-by/
119120
const widget = makeWidget({});
120121
const helper = jsHelper(createSearchClient(), '');
121122
expect(() =>
122-
widget.dispose!({ helper, state: helper.state })
123+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
123124
).not.toThrow();
124125
});
125126

‎src/connectors/query-rules/__tests__/connectQueryRules-test.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import algoliasearchHelper, {
66
import { createInstantSearch } from '../../../../test/mock/createInstantSearch';
77
import { createSearchClient } from '../../../../test/mock/createSearchClient';
88
import {
9+
createDisposeOptions,
910
createInitOptions,
1011
createRenderOptions,
1112
} from '../../../../test/mock/createWidget';
@@ -190,7 +191,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/query-rules
190191
})
191192
);
192193

193-
widget.dispose!({ helper, state: helper.state });
194+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }));
194195

195196
expect(unmountFn).toHaveBeenCalledTimes(1);
196197
});
@@ -209,7 +210,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/query-rules
209210
);
210211

211212
expect(() =>
212-
widget.dispose!({ helper, state: helper.state })
213+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
213214
).not.toThrow();
214215
});
215216
});
@@ -963,7 +964,9 @@ Consider using \`transformRuleContexts\` to minimize the number of rules sent to
963964
'ais-brand-Apple',
964965
]);
965966

966-
const nextState = widget.dispose!({ helper, state: helper.state });
967+
const nextState = widget.dispose!(
968+
createDisposeOptions({ helper, state: helper.state })
969+
);
967970

968971
expect((nextState as SearchParameters).ruleContexts).toEqual([
969972
'initial-rule',
@@ -1002,7 +1005,7 @@ Consider using \`transformRuleContexts\` to minimize the number of rules sent to
10021005
expect(brandFilterSpy).toHaveBeenCalledTimes(1);
10031006
expect(brandFilterSpy).toHaveBeenCalledWith(['Samsung']);
10041007

1005-
widget.dispose!({ helper, state: helper.state });
1008+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }));
10061009

10071010
helper.setState({
10081011
disjunctiveFacetsRefinements: {

‎src/connectors/range/__tests__/connectRange-test.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import jsHelper, {
55
} from 'algoliasearch-helper';
66
import connectRange from '../connectRange';
77
import {
8+
createDisposeOptions,
89
createInitOptions,
910
createRenderOptions,
1011
} from '../../../../test/mock/createWidget';
@@ -1261,7 +1262,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/range-input
12611262
})
12621263
);
12631264
expect(() =>
1264-
widget.dispose!({ helper, state: helper.state })
1265+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
12651266
).not.toThrow();
12661267
});
12671268

@@ -1279,7 +1280,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/range-input
12791280
})
12801281
);
12811282

1282-
const newState = widget.dispose!({ helper, state: helper.state });
1283+
const newState = widget.dispose!(
1284+
createDisposeOptions({ helper, state: helper.state })
1285+
);
12831286

12841287
expect(newState).toEqual(new SearchParameters({ index: indexName }));
12851288
});
@@ -1323,7 +1326,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/range-input
13231326
})
13241327
);
13251328

1326-
const newState = widget.dispose!({ helper, state: helper.state });
1329+
const newState = widget.dispose!(
1330+
createDisposeOptions({ helper, state: helper.state })
1331+
);
13271332

13281333
expect(newState).toEqual(new SearchParameters({ index: indexName }));
13291334
});

‎src/connectors/rating-menu/__tests__/connectRatingMenu-test.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import jsHelper, {
44
} from 'algoliasearch-helper';
55
import connectRatingMenu from '../connectRatingMenu';
66
import {
7+
createDisposeOptions,
78
createInitOptions,
89
createRenderOptions,
910
} from '../../../../test/mock/createWidget';
@@ -313,15 +314,15 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/rating-menu
313314
const { widget, helper } = getInitializedWidget();
314315

315316
expect(() =>
316-
widget.dispose!({ helper, state: helper.state })
317+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
317318
).not.toThrow();
318319
});
319320

320321
test('calls the unmount function', () => {
321322
const unmount = jest.fn();
322323
const { widget, helper } = getInitializedWidget({}, unmount);
323324

324-
widget.dispose!({ helper, state: helper.state });
325+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }));
325326

326327
expect(unmount).toHaveBeenCalledTimes(1);
327328
});
@@ -357,7 +358,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/rating-menu
357358
})
358359
);
359360

360-
const nextState = widget.dispose!({ helper, state: helper.state });
361+
const nextState = widget.dispose!(
362+
createDisposeOptions({ helper, state: helper.state })
363+
);
361364

362365
expect(nextState).toEqual(
363366
new SearchParameters({

‎src/connectors/refinement-list/__tests__/connectRefinementList-test.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { TAG_PLACEHOLDER } from '../../../lib/utils';
66
import connectRefinementList from '../connectRefinementList';
77
import { createInstantSearch } from '../../../../test/mock/createInstantSearch';
88
import {
9+
createDisposeOptions,
910
createInitOptions,
1011
createRenderOptions,
1112
} from '../../../../test/mock/createWidget';
@@ -2070,7 +2071,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/refinement-
20702071
);
20712072

20722073
expect(() =>
2073-
widget.dispose!({ helper, state: helper.state })
2074+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
20742075
).not.toThrow();
20752076
});
20762077

@@ -2156,10 +2157,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/refinement-
21562157
})
21572158
);
21582159

2159-
const newState = widget.dispose!({
2160-
state: helper.state,
2161-
helper,
2162-
});
2160+
const newState = widget.dispose!(
2161+
createDisposeOptions({
2162+
state: helper.state,
2163+
helper,
2164+
})
2165+
);
21632166

21642167
expect(newState).toEqual(
21652168
new SearchParameters({
@@ -2249,10 +2252,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/refinement-
22492252
})
22502253
);
22512254

2252-
const newState = widget.dispose!({
2253-
state: helper.state,
2254-
helper,
2255-
});
2255+
const newState = widget.dispose!(
2256+
createDisposeOptions({
2257+
state: helper.state,
2258+
helper,
2259+
})
2260+
);
22562261

22572262
expect(newState).toEqual(
22582263
new SearchParameters({

‎src/connectors/search-box/__tests__/connectSearchBox-test.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import algoliasearchHelper, {
44
} from 'algoliasearch-helper';
55
import connectSearchBox from '../connectSearchBox';
66
import {
7+
createDisposeOptions,
78
createInitOptions,
89
createRenderOptions,
910
} from '../../../../test/mock/createWidget';
@@ -500,7 +501,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/search-box/
500501

501502
expect(unmountFn).toHaveBeenCalledTimes(0);
502503

503-
widget.dispose!({ helper, state: helper.state });
504+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }));
504505

505506
expect(unmountFn).toHaveBeenCalledTimes(1);
506507
});
@@ -513,7 +514,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/search-box/
513514
const widget = makeWidget({});
514515

515516
expect(() =>
516-
widget.dispose!({ helper, state: helper.state })
517+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
517518
).not.toThrow();
518519
});
519520

@@ -528,10 +529,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/search-box/
528529

529530
expect(helper.state.query).toBe('Apple');
530531

531-
const nextState = widget.dispose!({
532-
helper,
533-
state: helper.state,
534-
}) as SearchParameters;
532+
const nextState = widget.dispose!(
533+
createDisposeOptions({
534+
helper,
535+
state: helper.state,
536+
})
537+
) as SearchParameters;
535538

536539
expect(nextState.query).toBeUndefined();
537540
});
@@ -561,7 +564,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/search-box/
561564
});
562565
});
563566

564-
test('should give back the same instance if the value is alreay in the uiState', () => {
567+
test('should give back the same instance if the value is already in the uiState', () => {
565568
const [widget, helper, refine] = getInitializedWidget();
566569
refine('query');
567570
const uiStateBefore = widget.getWidgetUiState(

‎src/connectors/sort-by/__tests__/connectSortBy-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import index from '../../../widgets/index/index';
88
import { createSearchClient } from '../../../../test/mock/createSearchClient';
99
import { createInstantSearch } from '../../../../test/mock/createInstantSearch';
1010
import {
11+
createDisposeOptions,
1112
createInitOptions,
1213
createRenderOptions,
1314
} from '../../../../test/mock/createWidget';
@@ -144,7 +145,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/sort-by/js/
144145
const helper = algoliasearchHelper(createSearchClient(), items[0].value);
145146

146147
expect(() =>
147-
widget.dispose!({ helper, state: helper.state })
148+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
148149
).not.toThrow();
149150
});
150151

‎src/connectors/stats/__tests__/connectStats-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const SearchResults = jsHelper.SearchResults;
33
import { createSearchClient } from '../../../../test/mock/createSearchClient';
44
import { createSingleSearchResponse } from '../../../../test/mock/createAPIResponse';
55
import {
6+
createDisposeOptions,
67
createInitOptions,
78
createRenderOptions,
89
} from '../../../../test/mock/createWidget';
@@ -427,7 +428,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/stats/js/#c
427428
const widget = makeWidget({});
428429
const helper = jsHelper(createSearchClient(), '');
429430
expect(() =>
430-
widget.dispose!({ helper, state: helper.state })
431+
widget.dispose!(createDisposeOptions({ helper, state: helper.state }))
431432
).not.toThrow();
432433
});
433434
});

‎src/types/widget.ts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type RenderOptions = SharedRenderOptions & {
4141
export type DisposeOptions = {
4242
helper: Helper;
4343
state: SearchParameters;
44+
parent: IndexWidget;
4445
};
4546

4647
export type BuiltinTypes =

‎src/widgets/index/__tests__/index-test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge
574574
expect(widget.dispose).toHaveBeenCalledWith({
575575
helper: instance.getHelper(),
576576
state: instance.getHelper()!.state,
577+
parent: instance,
577578
});
578579
});
579580
});
@@ -2676,6 +2677,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge
26762677
expect(widget.dispose).toHaveBeenCalledWith({
26772678
state: helper!.state,
26782679
helper,
2680+
parent: instance,
26792681
});
26802682
});
26812683
});

‎src/widgets/index/index.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,11 @@ const index = (widgetParams: IndexWidgetParams): IndexWidget => {
365365
if (localInstantSearchInstance && Boolean(widgets.length)) {
366366
const nextState = widgets.reduce((state, widget) => {
367367
// the `dispose` method exists at this point we already assert it
368-
const next = widget.dispose!({ helper: helper!, state });
368+
const next = widget.dispose!({
369+
helper: helper!,
370+
state,
371+
parent: this,
372+
});
369373

370374
return next || state;
371375
}, helper!.state);
@@ -655,7 +659,11 @@ const index = (widgetParams: IndexWidgetParams): IndexWidget => {
655659
// `dispose` because the index is removed. We can't call `removeWidgets`
656660
// because we want to keep the widgets on the instance, to allow idempotent
657661
// operations on `add` & `remove`.
658-
widget.dispose({ helper: helper!, state: helper!.state });
662+
widget.dispose({
663+
helper: helper!,
664+
state: helper!.state,
665+
parent: this,
666+
});
659667
}
660668
});
661669

‎src/widgets/menu-select/__tests__/menu-select-test.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import algoliasearchHelper, { SearchParameters } from 'algoliasearch-helper';
33
import menuSelect from '../menu-select';
44
import { createSearchClient } from '../../../../test/mock/createSearchClient';
55
import {
6+
createDisposeOptions,
67
createInitOptions,
78
createRenderOptions,
89
} from '../../../../test/mock/createWidget';
@@ -121,10 +122,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu-select
121122

122123
expect(render).toHaveBeenCalledTimes(0);
123124

124-
const newState = widget.dispose!({
125-
state: helper.state,
126-
helper,
127-
});
125+
const newState = widget.dispose!(
126+
createDisposeOptions({
127+
state: helper.state,
128+
helper,
129+
})
130+
);
128131

129132
expect(render).toHaveBeenCalledTimes(1);
130133
expect(render).toHaveBeenLastCalledWith(null, container);

‎src/widgets/panel/__tests__/panel-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('Lifecycle', () => {
199199

200200
widgetWithPanel.init!(createInitOptions());
201201
widgetWithPanel.render!(createRenderOptions());
202-
widgetWithPanel.dispose!(createRenderOptions());
202+
widgetWithPanel.dispose!(createDisposeOptions());
203203

204204
expect(widget.init).toHaveBeenCalledTimes(1);
205205
expect(widget.render).toHaveBeenCalledTimes(1);

‎src/widgets/query-rule-custom-data/__tests__/query-rule-custom-data-test.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import algoliasearchHelper, {
33
AlgoliaSearchHelper as Helper,
44
} from 'algoliasearch-helper';
55
import { createSearchClient } from '../../../../test/mock/createSearchClient';
6-
import { createInitOptions } from '../../../../test/mock/createWidget';
6+
import {
7+
createDisposeOptions,
8+
createInitOptions,
9+
} from '../../../../test/mock/createWidget';
710
import { castToJestMock } from '../../../../test/utils/castToJestMock';
811
import queryRuleCustomData from '../query-rule-custom-data';
912
import { QueryRuleCustomDataProps } from '../../../components/QueryRuleCustomData/QueryRuleCustomData';
@@ -203,10 +206,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/query-rule-
203206

204207
expect(render).toHaveBeenCalledTimes(1);
205208

206-
widget.dispose!({
207-
helper,
208-
state: helper.state,
209-
});
209+
widget.dispose!(
210+
createDisposeOptions({
211+
helper,
212+
state: helper.state,
213+
})
214+
);
210215

211216
expect(render).toHaveBeenCalledTimes(2);
212217
expect(render).toHaveBeenCalledWith(null, container);

‎test/mock/createWidget.ts

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const createDisposeOptions = (
7272
return {
7373
helper: instantSearchInstance.helper!,
7474
state: instantSearchInstance.helper!.state,
75+
parent: instantSearchInstance.mainIndex,
7576
...args,
7677
};
7778
};

0 commit comments

Comments
 (0)
Please sign in to comment.