Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,27 @@ describe('Analytics KinesisFirehose Provider Util: resolveConfig', () => {
};

const getConfigSpy = jest.spyOn(Amplify, 'getConfig');
const assertConfiguredSpy = jest.spyOn(Amplify, 'assertConfigured');

beforeEach(() => {
getConfigSpy.mockReset();
assertConfiguredSpy.mockReset();
});

it('throws if Amplify is not configured', () => {
assertConfiguredSpy.mockImplementation(() => {
throw new Error(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

expect(resolveConfig).toThrow(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

it('returns required config', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
getConfigSpy.mockReturnValue({
Analytics: { KinesisFirehose: providedConfig },
});
Expand All @@ -30,6 +45,7 @@ describe('Analytics KinesisFirehose Provider Util: resolveConfig', () => {
});

it('use default config for optional fields', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
const requiredFields = {
region: 'us-east-1',
bufferSize: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,27 @@ describe('Analytics Kinesis Provider Util: resolveConfig', () => {
};

const getConfigSpy = jest.spyOn(Amplify, 'getConfig');
const assertConfiguredSpy = jest.spyOn(Amplify, 'assertConfigured');

beforeEach(() => {
getConfigSpy.mockReset();
assertConfiguredSpy.mockReset();
});

it('throws if Amplify is not configured', () => {
assertConfiguredSpy.mockImplementation(() => {
throw new Error(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

expect(resolveConfig).toThrow(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

it('returns required config', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
getConfigSpy.mockReturnValue({
Analytics: { Kinesis: kinesisConfig },
});
Expand All @@ -30,6 +45,7 @@ describe('Analytics Kinesis Provider Util: resolveConfig', () => {
});

it('use default config for optional fields', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
const requiredFields = {
region: 'us-east-1',
bufferSize: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,27 @@ describe('Analytics Personalize Provider Util: resolveConfig', () => {
};

const getConfigSpy = jest.spyOn(Amplify, 'getConfig');
const assertConfiguredSpy = jest.spyOn(Amplify, 'assertConfigured');

beforeEach(() => {
getConfigSpy.mockReset();
assertConfiguredSpy.mockReset();
});

it('throws if Amplify is not configured', () => {
assertConfiguredSpy.mockImplementation(() => {
throw new Error(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

expect(resolveConfig).toThrow(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

it('returns required config', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
getConfigSpy.mockReturnValue({
Analytics: { Personalize: providedConfig },
});
Expand All @@ -35,6 +50,7 @@ describe('Analytics Personalize Provider Util: resolveConfig', () => {
});

it('use default config for optional fields', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
const requiredFields = {
region: 'us-east-1',
trackingId: 'trackingId1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,27 @@ describe('Analytics Pinpoint Provider Util: resolveConfig', () => {
};
// create spies
const getConfigSpy = jest.spyOn(Amplify, 'getConfig');
const assertConfiguredSpy = jest.spyOn(Amplify, 'assertConfigured');

beforeEach(() => {
getConfigSpy.mockReset();
assertConfiguredSpy.mockReset();
});

it('throws if Amplify is not configured', () => {
assertConfiguredSpy.mockImplementation(() => {
throw new Error(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

expect(resolveConfig).toThrow(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

it('returns required config', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
getConfigSpy.mockReturnValue({
Analytics: { Pinpoint: pinpointConfig },
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { DEFAULT_KINESIS_FIREHOSE_CONFIG } from './constants';

export const resolveConfig = () => {
Amplify.assertConfigured();
Copy link
Contributor

Choose a reason for hiding this comment

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

Quite a few of these occur right before we call Amplify.getConfig(). Do we always want to assert configured with getConfig? If so, could we include this in the getConfig behavior to remove some of the amount of change happening here?

Also, there are a number of places where this isn't couple with getConfig. This seems counterintuitive, right? Are these other callsites still calling getConfig, but a few layers of abstraction removed? Is there a pattern for accessing the config different from using getConfig?

const config = Amplify.getConfig().Analytics?.KinesisFirehose;
const {
region,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { DEFAULT_KINESIS_CONFIG } from './constants';

export const resolveConfig = () => {
Amplify.assertConfigured();
const config = Amplify.getConfig().Analytics?.Kinesis;
const {
region,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from './constants';

export const resolveConfig = () => {
Amplify.assertConfigured();
const config = Amplify.getConfig().Analytics?.Personalize;
const {
region,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
* @internal
*/
export const resolveConfig = () => {
Amplify.assertConfigured();
const { appId, region, bufferSize, flushSize, flushInterval, resendLimit } =
Amplify.getConfig().Analytics?.Pinpoint ?? {};
assertValidationError(!!appId, AnalyticsValidationErrorCode.NoAppId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ const mockUnauthenticatedHandler = jest.mocked(unauthenticatedHandler);
const mockParseJsonError = jest.mocked(parseJsonError);
const mockGetRetryDecider = jest.mocked(getRetryDecider);
const mockFetchAuthSession = jest.fn();
const mockAssertConfigured = jest.fn();
const mockAmplifyInstance = {
Auth: {
fetchAuthSession: mockFetchAuthSession,
},
isConfigured: true,
assertConfigured: mockAssertConfigured,
} as any as AmplifyClassV6;

const successResponse = {
Expand Down
2 changes: 2 additions & 0 deletions packages/api-rest/__tests__/apis/common/publicApis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ const mockConfig = {
const mockParseJsonError = parseJsonError as jest.Mock;
const mockRestHeaders = jest.fn();
const mockGetConfig = jest.fn();
const mockAssertConfigured = jest.fn();
const mockAmplifyInstance = {
Auth: {
fetchAuthSession: mockFetchAuthSession,
},
getConfig: mockGetConfig,
assertConfigured: mockAssertConfigured,
libraryOptions: {
API: {
REST: {
Expand Down
1 change: 1 addition & 0 deletions packages/api-rest/src/apis/common/internalPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* @param postInput.abortController The abort controller used to cancel the POST request
* @returns a {@link RestApiResponse}
*
* @throws an {@link AmplifyError} with `Network Error` as the `message` when the external resource is unreachable due to one

Check warning on line 50 in packages/api-rest/src/apis/common/internalPost.ts

View workflow job for this annotation

GitHub Actions / unit-tests / Unit Test - @aws-amplify/api-rest

The type 'AmplifyError' is undefined
* of the following reasons:
* 1. no network connection
* 2. CORS error
Expand All @@ -57,6 +57,7 @@
amplify: AmplifyClassV6,
{ url, options, abortController }: InternalPostInput,
): Promise<RestApiResponse> => {
amplify.assertConfigured();
const controller = abortController ?? new AbortController();
const responsePromise = createCancellableOperation(async () => {
const response = transferHandler(
Expand Down
1 change: 1 addition & 0 deletions packages/api-rest/src/apis/common/publicApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const publicHandler = (
method: string,
) =>
createCancellableOperation(async abortSignal => {
amplify.assertConfigured();
const { apiName, options: apiOptions = {}, path: apiPath } = options;
const url = resolveApiUrl(
amplify,
Expand Down
1 change: 1 addition & 0 deletions packages/api-rest/src/apis/common/transferHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const getRetryDeciderFromStrategy = (
const resolveCredentials = async (
amplify: AmplifyClassV6,
): Promise<AWSCredentials | null> => {
amplify.assertConfigured();
try {
const { credentials } = await amplify.Auth.fetchAuthSession();
if (credentials) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { authAPITestParams } from './testUtils/authApiTestParams';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('../../../src/client/utils/store');
jest.mock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jest.mock('../../../src/providers/cognito/factories');

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock(
'../../../src/foundation/factories/serviceClients/cognitoIdentityProvider',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand All @@ -34,6 +34,7 @@ describe('fetchUserAttributes', () => {
const mockCreateCognitoUserPoolEndpointResolver = jest.mocked(
createCognitoUserPoolEndpointResolver,
);
const mockAssertConfigured = Amplify.assertConfigured as jest.Mock;

beforeAll(() => {
setUpGetConfig(Amplify);
Expand All @@ -43,6 +44,7 @@ describe('fetchUserAttributes', () => {
});

beforeEach(() => {
mockAssertConfigured.mockReturnValue(undefined);
mockGetUser.mockResolvedValue({
UserAttributes: [
{ Name: 'email', Value: 'XXXXXXXXXXXXX' },
Expand All @@ -60,6 +62,7 @@ describe('fetchUserAttributes', () => {
mockGetUser.mockReset();
mockFetchAuthSession.mockClear();
mockCreateGetUserClient.mockClear();
mockAssertConfigured.mockReset();
});

it('should return the current user attributes into a map format', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { Auth: { getTokens: jest.fn() }, getConfig: jest.fn(() => ({})) },
Amplify: {
Auth: { getTokens: jest.fn() },
getConfig: jest.fn(() => ({})),
assertConfigured: jest.fn(),
},
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand All @@ -25,12 +29,14 @@ describe('getCurrentUser', () => {
const mockedUsername = 'XXXXXXXXXXXXXX';
// assert mocks
const mockGetTokensFunction = Amplify.Auth.getTokens as jest.Mock;
const mockAssertConfigured = Amplify.assertConfigured as jest.Mock;

beforeAll(() => {
setUpGetConfig(Amplify);
});

beforeEach(() => {
mockAssertConfigured.mockReturnValue(undefined);
mockGetTokensFunction.mockResolvedValue({
accessToken: decodeJWT(mockAccessToken),
idToken: {
Expand All @@ -48,6 +54,7 @@ describe('getCurrentUser', () => {

afterEach(() => {
mockGetTokensFunction.mockReset();
mockAssertConfigured.mockReset();
});

it('should get current user', async () => {
Expand All @@ -71,4 +78,16 @@ describe('getCurrentUser', () => {
expect(error.name).toBe(USER_UNAUTHENTICATED_EXCEPTION);
}
});

it('throws if Amplify is not configured', async () => {
mockAssertConfigured.mockImplementationOnce(() => {
throw new Error(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

expect(getCurrentUser()).rejects.toThrow(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Expand Down
Loading
Loading