@aws-amplify/core
Version:
Core category of aws-amplify
90 lines (88 loc) • 4.05 kB
JavaScript
;
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMockAmplifyContext = createMockAmplifyContext;
exports.withTokens = withTokens;
const contextBrand_1 = require("../context/contextBrand");
const contextToken_1 = require("../context/contextToken");
/**
* Creates a mock {@link AmplifyContext} for unit tests.
*
* The returned context is branded via {@link AMPLIFY_CONTEXT_BRAND} (so it
* passes `isAmplifyContext()` / `resolveCtxArgs()` checks) but — unlike
* `createAmplifyContext()` — it is deliberately **not** frozen, because tests
* mutate the mocks between cases.
*
* Note: `resourcesConfig` is exposed through a getter only (read-only) —
* unlike the per-package mocks this helper replaces, it cannot be reassigned.
* Tests that need to change config per test should pass the
* {@link CreateMockAmplifyContextOptions.getConfig} option (or create a new
* context) instead of assigning `ctx.resourcesConfig`.
*
* @param resourcesConfig - A (deep-partial) resource config, or the options
* object (mirrors the per-package copies this helper consolidates).
* @param options - Optional overrides; see {@link CreateMockAmplifyContextOptions}.
*/
function createMockAmplifyContext(resourcesConfig, options) {
// Determine whether the first argument is the options form.
const configIsOptions = isCreateMockAmplifyContextOptions(resourcesConfig);
const resolvedOptions = (configIsOptions
? resourcesConfig
: options) ?? {};
const staticConfig = configIsOptions
? undefined
: resourcesConfig;
const ctx = {
// Live getter so `getConfig`-driven per-test config changes propagate.
get resourcesConfig() {
return resolvedOptions.getConfig
? resolvedOptions.getConfig()
: (staticConfig ?? {});
},
libraryOptions: resolvedOptions.libraryOptions ?? {},
// Unique, frozen per-context identity handle, matching the real
// producers (see AmplifyContextToken) — also satisfies
// @aws-amplify/data-schema's structural ContextSpec duck-check in tests.
token: (0, contextToken_1.createAmplifyContextToken)(),
fetchAuthSession: resolvedOptions.fetchAuthSession ?? jest.fn().mockResolvedValue({}),
clearCredentials: resolvedOptions.clearCredentials ??
jest.fn().mockResolvedValue(undefined),
getTokens: resolvedOptions.getTokens ?? jest.fn().mockResolvedValue(undefined),
};
// Brand the context (non-enumerable) for runtime identification by
// isAmplifyContext(). Deliberately NOT frozen — see the function docs.
Object.defineProperty(ctx, contextBrand_1.AMPLIFY_CONTEXT_BRAND, {
value: true,
enumerable: false,
});
return ctx;
}
/**
* Convenience: primes the given mock context's session/token mocks with the
* provided access token, so signed-request tests need a single call instead
* of two `mockResolvedValue` invocations.
*
* @param ctx - A context created by {@link createMockAmplifyContext}.
* @param accessToken - Raw token string (wrapped in a minimal {@link JWT}
* with an empty payload) or an already-constructed {@link JWT}.
* @returns The same context, for chaining.
*/
function withTokens(ctx, accessToken) {
const jwt = typeof accessToken === 'string'
? { payload: {}, toString: () => accessToken }
: accessToken;
const tokens = { accessToken: jwt };
ctx.getTokens.mockResolvedValue(tokens);
ctx.fetchAuthSession.mockResolvedValue({ tokens });
return ctx;
}
function isCreateMockAmplifyContextOptions(value) {
return (value != null &&
('getConfig' in value ||
'libraryOptions' in value ||
'fetchAuthSession' in value ||
'clearCredentials' in value ||
'getTokens' in value));
}
//# sourceMappingURL=createMockAmplifyContext.js.map