Skip to content

Commit

Permalink
test(storage): add server api unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP committed Oct 25, 2024
1 parent 9ebaadd commit fb30a25
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 3 deletions.
3 changes: 0 additions & 3 deletions packages/storage/__tests__/providers/s3/apis/copy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ describe('client-side copy', () => {
const input: CopyInput = {
source: {
key: 'source-key',
bucket: { bucketName: 'source-bucket', region: 'source-region' },
expectedBucketOwner: '123',
},
destination: {
key: 'destination-key',
expectedBucketOwner: '123',
},
};
expect(copy(input)).toEqual(mockInternalResult);
Expand Down
54 changes: 54 additions & 0 deletions packages/storage/__tests__/providers/s3/apis/server/copy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { getAmplifyServerContext } from '@aws-amplify/core/internals/adapter-core';

import { CopyInput, CopyWithPathInput } from '../../../../../src';
import { copy } from '../../../../../src/providers/s3/apis/server';
import { copy as internalCopyImpl } from '../../../../../src/providers/s3/apis/internal/copy';

jest.mock('../../../../../src/providers/s3/apis/internal/copy');
jest.mock('@aws-amplify/core/internals/adapter-core');

const mockInternalCopyImpl = jest.mocked(internalCopyImpl);
const mockGetAmplifyServerContext = jest.mocked(getAmplifyServerContext);
const mockInternalResult = 'RESULT' as any;
const mockAmplifyClass = 'AMPLIFY_CLASS' as any;
const mockAmplifyContextSpec = {
token: { value: Symbol('123') },
};

describe('server-side copy', () => {
beforeEach(() => {
mockGetAmplifyServerContext.mockReturnValue({
amplify: mockAmplifyClass,
});
mockInternalCopyImpl.mockReturnValue(mockInternalResult);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should pass through input with key and output to internal implementation', async () => {
const input: CopyInput = {
source: {
key: 'source-key',
},
destination: {
key: 'destination-key',
},
};
expect(copy(mockAmplifyContextSpec, input)).toEqual(mockInternalResult);
expect(mockInternalCopyImpl).toBeCalledWith(mockAmplifyClass, input);
});

it('should pass through input with path and output to internal implementation', async () => {
const input: CopyWithPathInput = {
source: { path: 'abc' },
destination: { path: 'abc' },
};
expect(copy(mockAmplifyContextSpec, input)).toEqual(mockInternalResult);
expect(mockInternalCopyImpl).toBeCalledWith(mockAmplifyClass, input);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { getAmplifyServerContext } from '@aws-amplify/core/internals/adapter-core';

import {
GetPropertiesInput,
GetPropertiesWithPathInput,
} from '../../../../../src';
import { getProperties } from '../../../../../src/providers/s3/apis/server';
import { getProperties as internalGetPropertiesImpl } from '../../../../../src/providers/s3/apis/internal/getProperties';

jest.mock('../../../../../src/providers/s3/apis/internal/getProperties');
jest.mock('@aws-amplify/core/internals/adapter-core');

const mockInternalGetPropertiesImpl = jest.mocked(internalGetPropertiesImpl);
const mockGetAmplifyServerContext = jest.mocked(getAmplifyServerContext);
const mockInternalResult = 'RESULT' as any;
const mockAmplifyClass = 'AMPLIFY_CLASS' as any;
const mockAmplifyContextSpec = {
token: { value: Symbol('123') },
};

describe('server-side getProperties', () => {
beforeEach(() => {
mockGetAmplifyServerContext.mockReturnValue({
amplify: mockAmplifyClass,
});
mockInternalGetPropertiesImpl.mockReturnValue(mockInternalResult);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should pass through input with key and output to internal implementation', async () => {
const input: GetPropertiesInput = {
key: 'source-key',
};
expect(getProperties(mockAmplifyContextSpec, input)).toEqual(
mockInternalResult,
);
expect(mockInternalGetPropertiesImpl).toBeCalledWith(
mockAmplifyClass,
input,
);
});

it('should pass through input with path and output to internal implementation', async () => {
const input: GetPropertiesWithPathInput = {
path: 'abc',
};
expect(getProperties(mockAmplifyContextSpec, input)).toEqual(
mockInternalResult,
);
expect(mockInternalGetPropertiesImpl).toBeCalledWith(
mockAmplifyClass,
input,
);
});
});
59 changes: 59 additions & 0 deletions packages/storage/__tests__/providers/s3/apis/server/getUrl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { getAmplifyServerContext } from '@aws-amplify/core/internals/adapter-core';

import { GetUrlInput, GetUrlWithPathInput } from '../../../../../src';
import { getUrl } from '../../../../../src/providers/s3/apis/server';
import { getUrl as internalGetUrlImpl } from '../../../../../src/providers/s3/apis/internal/getUrl';

jest.mock('../../../../../src/providers/s3/apis/internal/getUrl');
jest.mock('@aws-amplify/core/internals/adapter-core');

const mockInternalGetUrlImpl = jest.mocked(internalGetUrlImpl);
const mockGetAmplifyServerContext = jest.mocked(getAmplifyServerContext);
const mockInternalResult = 'RESULT' as any;
const mockAmplifyClass = 'AMPLIFY_CLASS' as any;

describe('server-side getUrl', () => {
beforeEach(() => {
mockGetAmplifyServerContext.mockReturnValue({
amplify: mockAmplifyClass,
});
mockInternalGetUrlImpl.mockReturnValue(mockInternalResult);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should pass through input with key and output to internal implementation', async () => {
const input: GetUrlInput = {
key: 'source-key',
};
expect(
getUrl(
{
token: { value: Symbol('123') },
},
input,
),
).toEqual(mockInternalResult);
expect(mockInternalGetUrlImpl).toBeCalledWith(mockAmplifyClass, input);
});

it('should pass through input with path and output to internal implementation', async () => {
const input: GetUrlWithPathInput = {
path: 'abc',
};
expect(
getUrl(
{
token: { value: Symbol('123') },
},
input,
),
).toEqual(mockInternalResult);
expect(mockInternalGetUrlImpl).toBeCalledWith(mockAmplifyClass, input);
});
});
77 changes: 77 additions & 0 deletions packages/storage/__tests__/providers/s3/apis/server/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { getAmplifyServerContext } from '@aws-amplify/core/internals/adapter-core';

import {
ListAllInput,
ListAllWithPathInput,
ListPaginateInput,
ListPaginateWithPathInput,
} from '../../../../../src';
import { list } from '../../../../../src/providers/s3/apis/server';
import { list as internalListImpl } from '../../../../../src/providers/s3/apis/internal/list';

jest.mock('../../../../../src/providers/s3/apis/internal/list');
jest.mock('@aws-amplify/core/internals/adapter-core');

const mockInternalListImpl = jest.mocked(internalListImpl);
const mockGetAmplifyServerContext = jest.mocked(getAmplifyServerContext);
const mockInternalResult = 'RESULT' as any;
const mockAmplifyClass = 'AMPLIFY_CLASS' as any;
const mockAmplifyContextSpec = {
token: { value: Symbol('123') },
};

describe('server-side list', () => {
beforeEach(() => {
mockGetAmplifyServerContext.mockReturnValue({
amplify: mockAmplifyClass,
});
mockInternalListImpl.mockReturnValue(mockInternalResult);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should pass through list all input with key and output to internal implementation', async () => {
const input: ListAllInput = {
prefix: 'source-key',
};
expect(list(mockAmplifyContextSpec, input)).toEqual(mockInternalResult);
expect(mockInternalListImpl).toBeCalledWith(mockAmplifyClass, input);
});

it('should pass through list paginate input with key and output to internal implementation', async () => {
const input: ListPaginateInput = {
prefix: 'source-key',
options: {
nextToken: '123',
pageSize: 10,
},
};
expect(list(mockAmplifyContextSpec, input)).toEqual(mockInternalResult);
expect(mockInternalListImpl).toBeCalledWith(mockAmplifyClass, input);
});

it('should pass through list all input with path and output to internal implementation', async () => {
const input: ListAllWithPathInput = {
path: 'abc',
};
expect(list(mockAmplifyContextSpec, input)).toEqual(mockInternalResult);
expect(mockInternalListImpl).toBeCalledWith(mockAmplifyClass, input);
});

it('should pass through list paginate input with path and output to internal implementation', async () => {
const input: ListPaginateWithPathInput = {
path: 'abc',
options: {
nextToken: '123',
pageSize: 10,
},
};
expect(list(mockAmplifyContextSpec, input)).toEqual(mockInternalResult);
expect(mockInternalListImpl).toBeCalledWith(mockAmplifyClass, input);
});
});
48 changes: 48 additions & 0 deletions packages/storage/__tests__/providers/s3/apis/server/remove.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { getAmplifyServerContext } from '@aws-amplify/core/internals/adapter-core';

import { RemoveInput, RemoveWithPathInput } from '../../../../../src';
import { remove } from '../../../../../src/providers/s3/apis/server';
import { remove as internalRemoveImpl } from '../../../../../src/providers/s3/apis/internal/remove';

jest.mock('../../../../../src/providers/s3/apis/internal/remove');
jest.mock('@aws-amplify/core/internals/adapter-core');

const mockInternalRemoveImpl = jest.mocked(internalRemoveImpl);
const mockGetAmplifyServerContext = jest.mocked(getAmplifyServerContext);
const mockInternalResult = 'RESULT' as any;
const mockAmplifyClass = 'AMPLIFY_CLASS' as any;
const mockAmplifyContextSpec = {
token: { value: Symbol('123') },
};

describe('client-side remove', () => {
beforeEach(() => {
mockGetAmplifyServerContext.mockReturnValue({
amplify: mockAmplifyClass,
});
mockInternalRemoveImpl.mockReturnValue(mockInternalResult);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should pass through input with key and output to internal implementation', async () => {
const input: RemoveInput = {
key: 'source-key',
};
expect(remove(mockAmplifyContextSpec, input)).toEqual(mockInternalResult);
expect(mockInternalRemoveImpl).toBeCalledWith(mockAmplifyClass, input);
});

it('should pass through input with path and output to internal implementation', async () => {
const input: RemoveWithPathInput = {
path: 'abc',
};
expect(remove(mockAmplifyContextSpec, input)).toEqual(mockInternalResult);
expect(mockInternalRemoveImpl).toBeCalledWith(mockAmplifyClass, input);
});
});

0 comments on commit fb30a25

Please sign in to comment.