1
+ import { createAlgoliaInsightsPlugin } from '@algolia/autocomplete-plugin-algolia-insights' ;
2
+ import { createRedirectUrlPlugin } from '@algolia/autocomplete-plugin-redirect-url' ;
1
3
import { fireEvent , waitFor } from '@testing-library/dom' ;
2
4
import userEvent from '@testing-library/user-event' ;
3
5
@@ -9,6 +11,20 @@ import {
9
11
runAllMicroTasks ,
10
12
} from '../../../../test/utils' ;
11
13
import { createAutocomplete } from '../createAutocomplete' ;
14
+ import { createCancelablePromiseList , getPluginSubmitPromise } from '../utils' ;
15
+
16
+ jest . mock ( '../utils/createCancelablePromiseList' , ( ) => ( {
17
+ createCancelablePromiseList : jest . fn (
18
+ jest . requireActual ( '../utils/createCancelablePromiseList' )
19
+ . createCancelablePromiseList
20
+ ) ,
21
+ } ) ) ;
22
+
23
+ jest . mock ( '../utils/getPluginSubmitPromise' , ( ) => ( {
24
+ getPluginSubmitPromise : jest . fn (
25
+ jest . requireActual ( '../utils/getPluginSubmitPromise' ) . getPluginSubmitPromise
26
+ ) ,
27
+ } ) ) ;
12
28
13
29
describe ( 'getInputProps' , ( ) => {
14
30
beforeEach ( ( ) => {
@@ -1287,6 +1303,55 @@ describe('getInputProps', () => {
1287
1303
) ;
1288
1304
} ) ;
1289
1305
1306
+ describe ( 'a plugin is configured with the option "awaitSubmit"' , ( ) => {
1307
+ const cancelAll = jest . fn ( ) ;
1308
+ const event = { ...new KeyboardEvent ( 'keydown' ) , key : 'Enter' } ;
1309
+
1310
+ beforeEach ( ( ) => {
1311
+ cancelAll . mockClear ( ) ;
1312
+ ( createCancelablePromiseList as jest . Mock ) . mockReturnValueOnce ( {
1313
+ add : jest . fn ,
1314
+ cancelAll,
1315
+ isEmpty : jest . fn ,
1316
+ wait : jest . fn ,
1317
+ } ) ;
1318
+ } ) ;
1319
+
1320
+ test . each ( [ true , 1000 ] ) (
1321
+ 'when returning %s it should not cancel pending requests' ,
1322
+ ( timeout ) => {
1323
+ ( getPluginSubmitPromise as jest . Mock ) . mockResolvedValueOnce ( { } ) ;
1324
+
1325
+ const plugins = [
1326
+ createRedirectUrlPlugin ( { awaitSubmit : ( ) => timeout } ) ,
1327
+ createAlgoliaInsightsPlugin ( { } ) , // "awaitSubmit" is neither configurable nor defined
1328
+ ] ;
1329
+
1330
+ const { inputProps } = createPlayground ( createAutocomplete , {
1331
+ plugins,
1332
+ } ) ;
1333
+
1334
+ inputProps . onKeyDown ( event ) ;
1335
+
1336
+ expect ( cancelAll ) . toHaveBeenCalledTimes ( 0 ) ;
1337
+ }
1338
+ ) ;
1339
+
1340
+ test ( 'when returning false it should cancel pending requests' , ( ) => {
1341
+ const plugins = [
1342
+ createRedirectUrlPlugin ( { awaitSubmit : ( ) => false } ) ,
1343
+ ] ;
1344
+
1345
+ const { inputProps } = createPlayground ( createAutocomplete , {
1346
+ plugins,
1347
+ } ) ;
1348
+
1349
+ inputProps . onKeyDown ( event ) ;
1350
+
1351
+ expect ( cancelAll ) . toHaveBeenCalledTimes ( 1 ) ;
1352
+ } ) ;
1353
+ } ) ;
1354
+
1290
1355
describe ( 'Plain Enter' , ( ) => {
1291
1356
test ( 'calls onSelect with item URL' , ( ) => {
1292
1357
const onSelect = jest . fn ( ) ;
0 commit comments