Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage-browser): integrate browser navigation events #5941

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/react-storage/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const config: Config = {
// functions: 90,
// lines: 95,
// statements: 95,
branches: 81,
functions: 83,
lines: 91,
statements: 90,
branches: 80,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor drop in branch coverage but will be addressed in other migration PRs

functions: 86,
lines: 92,
statements: 91,
},
},
moduleNameMapper: { '^uuid$': '<rootDir>/../../node_modules/uuid' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,122 +1,66 @@
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import { render } from '@testing-library/react';

import * as ActionsModule from '../do-not-import-from-here/actions';
import * as ControlsModule from '../context/control';
import { ViewsProvider } from '../views/context';
import * as StoreModule from '../providers/store';
import * as ViewsModule from '../views/context';
import { StorageBrowserDefault } from '../StorageBrowserDefault';

jest.spyOn(ActionsModule, 'useLocationsData').mockReturnValue([
{
isLoading: false,
data: { result: [], nextToken: undefined },
hasError: false,
message: undefined,
},
jest.fn(),
]);

jest.spyOn(ActionsModule, 'useAction').mockReturnValue([
{
data: { result: [], nextToken: undefined },
hasError: false,
isLoading: false,
message: undefined,
},
jest.fn(),
]);

const INITIAL_NAVIGATE_STATE = [
{ location: undefined, history: [], path: undefined },
jest.fn(),
];
const INITIAL_LOCATION_ACTIONS_STATE = [
{ selected: { type: undefined, items: undefined }, actions: {} },
jest.fn(),
];
jest.spyOn(ViewsModule, 'useViews').mockReturnValue({
LocationsView: () => <div data-testid="LOCATIONS_VIEW" />,
LocationDetailView: () => <div data-testid="LOCATION_DETAIL_VIEW" />,
LocationActionView: () => <div data-testid="LOCATION_ACTION_VIEW" />,
});

const useControlSpy = jest.spyOn(ControlsModule, 'useControl');
const useStoreSpy = jest.spyOn(StoreModule, 'useStore');

const WrappedStorageBrowser = () => (
<ViewsProvider>
<StorageBrowserDefault />
</ViewsProvider>
);
const location = {
id: 'an-id-👍🏼',
bucket: 'test-bucket',
permission: 'READWRITE',
prefix: 'test-prefix/',
type: 'PREFIX',
};

describe('StorageBrowserDefault', () => {
beforeEach(() => {
useControlSpy.mockClear();
afterEach(jest.clearAllMocks);

it('renders the `LocationsView` by default', () => {
useStoreSpy.mockReturnValueOnce([
{
actionType: undefined,
history: { current: undefined, previous: undefined },
} as StoreModule.UseStoreState,
jest.fn(),
]);
const { getByTestId } = render(<StorageBrowserDefault />);

expect(getByTestId('LOCATIONS_VIEW')).toBeInTheDocument();
});

it('renders the `LocationsView` by default', async () => {
useControlSpy.mockImplementation(
(type) =>
({
LOCATION_ACTIONS: INITIAL_LOCATION_ACTIONS_STATE,
NAVIGATE: INITIAL_NAVIGATE_STATE,
})[type]
);
it('renders the `LocationDetailView` when a location is selected', () => {
useStoreSpy.mockReturnValueOnce([
{
actionType: undefined,
history: { current: location, previous: [location] },
} as StoreModule.UseStoreState,
jest.fn(),
]);

await waitFor(() => {
render(<WrappedStorageBrowser />);
});
const { getByTestId } = render(<StorageBrowserDefault />);

expect(screen.getByTestId('LOCATIONS_VIEW')).toBeInTheDocument();
expect(getByTestId('LOCATION_DETAIL_VIEW')).toBeInTheDocument();
});

it('renders the `LocationDetailView` when a location is selected', async () => {
useControlSpy.mockImplementation(
(type) =>
({
LOCATION_ACTIONS: INITIAL_LOCATION_ACTIONS_STATE,
NAVIGATE: [
{
location: {
scope: 's3://test-bucket/*',
permission: 'READWRITE',
type: 'BUCKET',
},
history: [{ prefix: '', position: 0 }],
},
],
})[type]
);

await waitFor(() => {
render(<WrappedStorageBrowser />);
});

expect(screen.getByTestId('LOCATION_DETAIL_VIEW')).toBeInTheDocument();
});

it('renders the `LocationActionView` when an action is selected', async () => {
useControlSpy.mockImplementation(
(type) =>
({
LOCATION_ACTIONS: [
{
actions: { CREATE_FOLDER: {} },
selected: { type: 'CREATE_FOLDER', items: undefined },
},
jest.fn(),
],
NAVIGATE: [
{
location: {
scope: 's3://test-bucket/*',
permission: 'READWRITE',
type: 'BUCKET',
},
history: [{ prefix: '', position: 0 }],
},
],
})[type]
);

render(<WrappedStorageBrowser />);

await waitFor(() => {
expect(screen.getByTestId('LOCATION_ACTION_VIEW')).toBeInTheDocument();
});
it('renders the `LocationActionView` when an action is selected', () => {
useStoreSpy.mockReturnValueOnce([
{
actionType: 'super-coll-action-type',
history: { current: location, previous: [location] },
} as StoreModule.UseStoreState,
jest.fn(),
]);
const { getByTestId } = render(<StorageBrowserDefault />);

expect(getByTestId('LOCATION_ACTION_VIEW')).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';

import * as ActionsModule from '../do-not-import-from-here/actions';
import * as ControlsModule from '../context/control';

import { createStorageBrowser } from '../createStorageBrowser';

Expand Down Expand Up @@ -35,8 +34,6 @@ const INITIAL_ACTION_STATE = [
jest.fn(),
];

const useControlSpy = jest.spyOn(ControlsModule, 'useControl');

const accountId = '012345678901';
const getLocationCredentials = jest.fn();
const listLocations = jest.fn();
Expand All @@ -53,10 +50,6 @@ const config = {
const input = { config };

describe('createStorageBrowser', () => {
beforeEach(() => {
useControlSpy.mockClear();
});

it('throws when registerAuthListener is not a function', () => {
const input = {
config: { getLocationCredentials, listLocations, region },
Expand All @@ -69,13 +62,6 @@ describe('createStorageBrowser', () => {
});

it('renders the `LocationsView` by default', async () => {
useControlSpy.mockImplementation(
(type) =>
({
LOCATION_ACTIONS: INITIAL_ACTION_STATE,
NAVIGATE: INITIAL_NAVIGATE_STATE,
})[type]
);
const { StorageBrowser } = createStorageBrowser(input);

await waitFor(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {

const file = {
key: 'key',
id: 'id',
lastModified: new Date(),
size: 100,
type: 'FILE' as const,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const baseInput: CopyHandlerInput = {
credentials: jest.fn(),
region: 'region',
},
data: { key: 'key', payload: { destinationPrefix: 'destination/' } },
key: 'key',
data: { id: 'identity', payload: { destinationPrefix: 'destination/' } },
};

describe('copyHandler', () => {
Expand All @@ -33,14 +34,14 @@ describe('copyHandler', () => {
source: {
expectedBucketOwner: `${baseInput.config.accountId}`,
bucket,
path: `${baseInput.prefix}${baseInput.data.key}`,
path: `${baseInput.prefix}${baseInput.key}`,
},
options: {
locationCredentialsProvider: baseInput.config.credentials,
},
};

expect(copySpy).toHaveBeenCalledWith(expected);
expect(key).toBe(baseInput.data.key);
expect(key).toBe(baseInput.key);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const removeSpy = jest.spyOn(StorageModule, 'remove');

const baseInput: DeleteHandlerInput = {
prefix: 'prefix/',
key: 'key',
config: {
accountId: '012345678901',
bucket: 'bucket',
Expand All @@ -20,7 +21,7 @@ describe('deleteHandler', () => {
const { key } = deleteHandler(baseInput);

const expected: StorageModule.RemoveInput = {
path: `${baseInput.prefix}${baseInput.data.key}`,
path: `${baseInput.prefix}${baseInput.key}`,
options: {
expectedBucketOwner: baseInput.config.accountId,
bucket: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const onProgress = jest.fn();

const baseInput: UploadHandlerInput = {
config,
data: { key: payload.name, payload },
key: payload.name,
data: { id: 'an-id', payload },
prefix,
};

Expand Down Expand Up @@ -61,9 +62,9 @@ describe('uploadHandler', () => {

expect(await result).toBe('COMPLETE');

expect(key).toBe(baseInput.data.key);
expect(key).toBe(baseInput.key);
expect(onComplete).toHaveBeenCalledTimes(1);
expect(onComplete).toHaveBeenCalledWith(baseInput.data.key);
expect(onComplete).toHaveBeenCalledWith(baseInput.key);
});

it('calls upload with the expected values', () => {
Expand Down Expand Up @@ -108,9 +109,9 @@ describe('uploadHandler', () => {

expect(await result).toBe('COMPLETE');

expect(key).toBe(baseInput.data.key);
expect(key).toBe(baseInput.key);
expect(onProgress).toHaveBeenCalledTimes(1);
expect(onProgress).toHaveBeenCalledWith(baseInput.data.key, 1);
expect(onProgress).toHaveBeenCalledWith(baseInput.key, 1);
});

it('calls provided onProgress callback as expected when `totalBytes` is `undefined`', async () => {
Expand All @@ -134,9 +135,9 @@ describe('uploadHandler', () => {

expect(await result).toBe('COMPLETE');

expect(key).toBe(baseInput.data.key);
expect(key).toBe(baseInput.key);
expect(onProgress).toHaveBeenCalledTimes(1);
expect(onProgress).toHaveBeenCalledWith(baseInput.data.key, undefined);
expect(onProgress).toHaveBeenCalledWith(baseInput.key, undefined);
});

it('returns the expected callback values for a file size greater than 5 mb', async () => {
Expand All @@ -155,7 +156,8 @@ describe('uploadHandler', () => {

const { key, result, ...callbacks } = uploadHandler({
...baseInput,
data: { key: bigFile.name, payload: bigFile },
key: bigFile.name,
data: { id: 'hi!', payload: bigFile },
});

expect(await result).toBe('COMPLETE');
Expand All @@ -177,7 +179,8 @@ describe('uploadHandler', () => {

const { key, result, ...callbacks } = uploadHandler({
...baseInput,
data: { key: smallFile.name, payload: smallFile },
key: smallFile.name,
data: { id: 'ohh', payload: smallFile },
});

expect(await result).toBe('COMPLETE');
Expand All @@ -200,7 +203,7 @@ describe('uploadHandler', () => {
expect(await result).toBe('FAILED');

expect(onError).toHaveBeenCalledTimes(1);
expect(onError).toHaveBeenCalledWith(baseInput.data.key, error.message);
expect(onError).toHaveBeenCalledWith(baseInput.key, error.message);
});

it('handles a cancel failure as expected', async () => {
Expand All @@ -220,6 +223,6 @@ describe('uploadHandler', () => {
expect(await result).toBe('CANCELED');

expect(onCancel).toHaveBeenCalledTimes(1);
expect(onCancel).toHaveBeenCalledWith(baseInput.data.key);
expect(onCancel).toHaveBeenCalledWith(baseInput.key);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import {
parseResult,
} from '../listLocationItemsAction';

let uuid = 0;
Object.defineProperty(globalThis, 'crypto', {
value: {
randomUUID: () => {
uuid++;
return uuid.toString();
},
},
});

const listSpy = jest.spyOn(StorageModule, 'list');
const config = {
bucket: 'bucket',
Expand Down Expand Up @@ -151,13 +161,13 @@ describe('parseResult', () => {
const result = parseResult(output, prefix);
expect(result).toHaveLength(3); // excludes prefix
const subFolderWithObject = result[0];
expect(subFolderWithObject.key).toBe('Cloudberry/');
expect(subFolderWithObject.key).toBe(`${prefix}Cloudberry/`);
expect(subFolderWithObject.type).toBe('FOLDER');
const zeroByteSubFolder = result[1];
expect(zeroByteSubFolder.key).toBe('Banana/');
expect(zeroByteSubFolder.key).toBe(`${prefix}Banana/`);
expect(zeroByteSubFolder.type).toBe('FOLDER');
const file = result[2];
expect(file.key).toBe('Orange.jpg');
expect(file.key).toBe(`${prefix}Orange.jpg`);
expect(file.type).toBe('FILE');
});

Expand Down
Loading
Loading