Skip to content

Commit f2fc55d

Browse files
authored
fix(storage-browser): export UseView type (#6314)
1 parent 8558028 commit f2fc55d

File tree

10 files changed

+67
-36
lines changed

10 files changed

+67
-36
lines changed

.changeset/tame-rockets-perform.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@aws-amplify/ui-react-storage": patch
3+
---
4+
5+
fix(storage-browser): export UseView type

packages/react-storage/src/components/StorageBrowser/StorageBrowserDefault.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import { useViews } from './views/context';
3+
import { useViews } from './views';
44
import { useStore } from './providers/store';
55

66
/**

packages/react-storage/src/components/StorageBrowser/__tests__/StorageBrowserDefault.spec.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React from 'react';
22
import { render } from '@testing-library/react';
33

4+
import { LocationData } from '../actions';
45
import * as StoreModule from '../providers/store';
5-
import * as ViewsModule from '../views/context';
6+
import * as ViewsModule from '../views';
7+
68
import { StorageBrowserDefault } from '../StorageBrowserDefault';
7-
import { LocationData } from '../actions';
89

910
jest.spyOn(ViewsModule, 'useViews').mockReturnValue({
1011
primary: {

packages/react-storage/src/components/StorageBrowser/createStorageBrowser.tsx

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,14 @@ import {
1010
ActionConfigsProvider,
1111
ExtendedActionConfigs,
1212
} from './actions';
13-
1413
import { DEFAULT_COMPOSABLES } from './composables';
1514
import { elementsDefault } from './context/elements';
1615
import { ComponentsProvider } from './ComponentsProvider';
1716
import { componentsDefault } from './componentsDefault';
17+
import { DisplayTextProvider } from './displayText';
1818
import { ErrorBoundary } from './ErrorBoundary';
19-
2019
import { createConfigurationProvider, StoreProvider } from './providers';
2120
import { StorageBrowserDefault } from './StorageBrowserDefault';
22-
import { assertRegisterAuthListener } from './validators';
23-
import {
24-
CopyView,
25-
CreateFolderView,
26-
DeleteView,
27-
LocationActionView,
28-
LocationDetailView,
29-
LocationsView,
30-
UploadView,
31-
LocationActionViewType,
32-
} from './views';
33-
import { useView } from './views/useView';
34-
import { ViewsProvider } from './views/context';
35-
36-
import { DisplayTextProvider } from './displayText';
37-
3821
import {
3922
CreateStorageBrowserInput,
4023
CreateStorageBrowserOutput,
@@ -48,6 +31,19 @@ import {
4831
ActionHandlersProvider,
4932
useAction,
5033
} from './useAction';
34+
import { assertRegisterAuthListener } from './validators';
35+
import {
36+
CopyView,
37+
CreateFolderView,
38+
DeleteView,
39+
LocationActionView,
40+
LocationDetailView,
41+
LocationsView,
42+
UploadView,
43+
LocationActionViewType,
44+
useView,
45+
ViewsProvider,
46+
} from './views';
5147

5248
export function createStorageBrowser<
5349
Input extends CreateStorageBrowserInput,

packages/react-storage/src/components/StorageBrowser/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ export {
1212
CreateManagedAuthAdapterInput,
1313
StorageBrowserAuthAdapter,
1414
} from './adapters';
15+
export { DefaultStorageBrowserDisplayText } from './displayText';
1516
export {
1617
CreateStorageBrowserInput,
1718
StorageBrowserType,
1819
DerivedActionViewType,
1920
DerivedActionViews,
2021
} from './types';
21-
export { DefaultStorageBrowserDisplayText } from './displayText';
22+
export { UseView } from './views';

packages/react-storage/src/components/StorageBrowser/types.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ import {
77
ListLocations,
88
} from './actions';
99
import { GetLocationCredentials } from './credentials/types';
10-
11-
import { UseView } from './views/useView';
12-
1310
import { Components } from './ComponentsProvider';
14-
1511
import { RegisterAuthListener, StoreProviderProps } from './providers';
1612

1713
import {
@@ -23,6 +19,7 @@ import {
2319
LocationDetailViewType,
2420
LocationsViewType,
2521
Views,
22+
UseView,
2623
} from './views';
2724

2825
import { StorageBrowserDisplayText } from './displayText';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`useView throws when provided an unexepected key 1`] = `"Value of \`unexpected!\` cannot be used to index \`useView\`"`;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { renderHook } from '@testing-library/react';
2+
import { useView, DEFAULT_VIEW_HOOKS, UseViewType } from '../useView';
3+
4+
jest.mock('../LocationActionView');
5+
jest.mock('../LocationsView');
6+
jest.mock('../LocationDetailView');
7+
8+
describe('useView', () => {
9+
it.each(Object.entries(DEFAULT_VIEW_HOOKS))(
10+
'calls the expected hook when provided a %s type key',
11+
(type, hook) => {
12+
renderHook(() => useView(type as UseViewType));
13+
14+
expect(hook).toHaveBeenCalledTimes(1);
15+
}
16+
);
17+
18+
it('throws when provided an unexepected key', () => {
19+
// turn off console.error logging for unhappy path test case
20+
jest.spyOn(console, 'error').mockImplementation(() => {});
21+
22+
expect(() =>
23+
renderHook(() => useView('unexpected!' as UseViewType))
24+
).toThrowErrorMatchingSnapshot();
25+
});
26+
});

packages/react-storage/src/components/StorageBrowser/views/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { ViewsProvider, useViews } from './context';
12
export {
23
CopyView,
34
CopyViewType,
@@ -21,5 +22,6 @@ export {
2122
LocationsViewProps,
2223
LocationsViewType,
2324
} from './LocationsView';
25+
export { useView, UseView } from './useView';
2426

2527
export * from './types';

packages/react-storage/src/components/StorageBrowser/views/useView.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import { useLocationsView } from './LocationsView';
88
import { useLocationDetailView } from './LocationDetailView';
99

10-
const USE_VIEW_HOOKS = {
10+
export const DEFAULT_VIEW_HOOKS = {
1111
Copy: useCopyView,
1212
CreateFolder: useCreateFolderView,
1313
Delete: useDeleteView,
@@ -16,8 +16,8 @@ const USE_VIEW_HOOKS = {
1616
Upload: useUploadView,
1717
};
1818

19-
type DefaultUseViews = typeof USE_VIEW_HOOKS;
20-
export type UseViewType = keyof DefaultUseViews;
19+
type DefaultViewHooks = typeof DEFAULT_VIEW_HOOKS;
20+
export type UseViewType = keyof DefaultViewHooks;
2121

2222
export type ViewKey<T> = T extends Record<
2323
string,
@@ -28,21 +28,21 @@ export type ViewKey<T> = T extends Record<
2828
? K
2929
: never;
3030

31+
const isUseViewType = (value: unknown): value is UseViewType =>
32+
!!DEFAULT_VIEW_HOOKS?.[value as UseViewType];
33+
3134
export type UseView = <
32-
K extends keyof DefaultUseViews,
33-
S extends DefaultUseViews[K],
35+
K extends UseViewType,
36+
S extends ReturnType<DefaultViewHooks[K]>,
3437
>(
3538
type: K
36-
) => ReturnType<S>;
37-
38-
const isUseViewType = (value: unknown): value is UseViewType =>
39-
!!USE_VIEW_HOOKS?.[value as UseViewType];
39+
) => S;
4040

4141
// @ts-expect-error
4242
export const useView: UseView = (type) => {
4343
if (!isUseViewType(type)) {
4444
throw new Error(`Value of \`${type}\` cannot be used to index \`useView\``);
4545
}
4646

47-
return USE_VIEW_HOOKS[type]();
47+
return DEFAULT_VIEW_HOOKS[type]();
4848
};

0 commit comments

Comments
 (0)