UNPKG

@aws-amplify/core

Version:

Core category of aws-amplify

88 lines (85 loc) • 3.92 kB
import { AMPLIFY_CONTEXT_BRAND } from '../context/contextBrand.mjs'; import { createAmplifyContextToken } from '../context/contextToken.mjs'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 /** * 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: 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, 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)); } export { createMockAmplifyContext, withTokens }; //# sourceMappingURL=createMockAmplifyContext.mjs.map