Skip to content

Commit 0a2d0dc

Browse files
refactor(core): extract Navigator to allow testing
1 parent 586f0f1 commit 0a2d0dc

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

packages/autocomplete-core/src/getDefaultProps.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getNavigator } from './getNavigator';
12
import { InternalAutocompleteOptions, AutocompleteOptions } from './types';
23
import {
34
generateAutocompleteId,
@@ -9,7 +10,9 @@ import {
910
export function getDefaultProps<TItem>(
1011
props: AutocompleteOptions<TItem>
1112
): InternalAutocompleteOptions<TItem> {
12-
const environment: typeof window = (typeof window !== 'undefined'
13+
const environment: InternalAutocompleteOptions<
14+
unknown
15+
>['environment'] = (typeof window !== 'undefined'
1316
? window
1417
: {}) as typeof window;
1518
const plugins = props.plugins || [];
@@ -38,6 +41,7 @@ export function getDefaultProps<TItem>(
3841
context: {},
3942
...props.initialState,
4043
},
44+
plugins,
4145
onStateChange(params) {
4246
props.onStateChange?.(params);
4347
plugins.forEach((plugin) => {
@@ -70,21 +74,8 @@ export function getDefaultProps<TItem>(
7074
);
7175
},
7276
navigator: {
73-
navigate({ itemUrl }) {
74-
environment.location.assign(itemUrl);
75-
},
76-
navigateNewTab({ itemUrl }) {
77-
const windowReference = environment.open(itemUrl, '_blank', 'noopener');
78-
79-
if (windowReference) {
80-
windowReference.focus();
81-
}
82-
},
83-
navigateNewWindow({ itemUrl }) {
84-
environment.open(itemUrl, '_blank', 'noopener');
85-
},
77+
...getNavigator({ environment }),
8678
...props.navigator,
8779
},
88-
plugins,
8980
};
9081
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { InternalAutocompleteOptions } from './types';
2+
3+
export function getNavigator({
4+
environment,
5+
}: Pick<
6+
InternalAutocompleteOptions<unknown>,
7+
'environment'
8+
>): InternalAutocompleteOptions<unknown>['navigator'] {
9+
return {
10+
navigate({ itemUrl }) {
11+
environment.location.assign(itemUrl);
12+
},
13+
navigateNewTab({ itemUrl }) {
14+
const windowReference = environment.open(itemUrl, '_blank', 'noopener');
15+
16+
if (windowReference) {
17+
windowReference.focus();
18+
}
19+
},
20+
navigateNewWindow({ itemUrl }) {
21+
environment.open(itemUrl, '_blank', 'noopener');
22+
},
23+
};
24+
}

0 commit comments

Comments
 (0)