studiocms
Version:
Astro Native CMS for AstroDB. Built from the ground up by the Astro community.
106 lines (105 loc) • 4.77 kB
TypeScript
import type { AstroIntegration, AstroIntegrationLogger } from 'astro';
import type { SCMSAuthServiceFnOpts, SCMSDashboardFnOpts, SCMSFrontendFnOpts, SCMSImageServiceFnOpts, SCMSRenderingFnOpts, SCMSSiteMapFnOpts, StudioCMSPlugin } from './schemas/index.js';
type HookRun<T> = {
hasHook: boolean;
hookResults: T;
};
export interface PluginHookResults {
astroConfig: HookRun<{
integrations: AstroIntegration[];
}>;
studiocmsConfig: HookRun<{
authService: Partial<SCMSAuthServiceFnOpts>;
dashboard: Partial<SCMSDashboardFnOpts>;
frontend: Partial<SCMSFrontendFnOpts>;
imageService: Partial<SCMSImageServiceFnOpts>;
rendering: Partial<SCMSRenderingFnOpts>;
sitemap: Partial<SCMSSiteMapFnOpts>;
}>;
}
/**
* Utility class for testing StudioCMS plugins by simulating hook execution and collecting results.
*
* The `StudioCMSPluginTester` provides methods to:
* - Create mock loggers for use in plugin hooks.
* - Execute the `studiocms:astro:config` and `studiocms:config:setup` hooks, if present, on a given plugin.
* - Collect and return integration and configuration data set by these hooks.
* - Retrieve basic plugin metadata.
*
* @remarks
* This class is intended for use in test environments to facilitate inspection and validation of plugin behavior.
*
* @example
* ```typescript
* const tester = new StudioCMSPluginTester(myPlugin);
* const info = tester.getPluginInfo();
* const hookResults = await tester.getHookResults();
* ```
*/
export declare class StudioCMSPluginTester {
private readonly plugin;
private readonly injectedLogger?;
constructor(plugin: StudioCMSPlugin, logger?: AstroIntegrationLogger);
/**
* Creates a mock logger object for testing purposes.
*
* The returned logger implements the `AstroIntegrationLogger` interface and provides
* stubbed logging methods (`info`, `warn`, `error`, `debug`) that delegate to the corresponding
* `console` methods. It also includes a `fork` method, which returns a new mock logger
* instance with an attached label for testing logger forking behavior.
*
* @returns {AstroIntegrationLogger} A mock logger instance suitable for use in tests.
*/
private createMockLogger;
/**
* Executes the 'studiocms:astro:config' hook if it exists on the plugin,
* providing a mock logger and a method to collect Astro integrations.
*
* @returns A promise that resolves to an object containing the collected integrations.
*
* @remarks
* This method checks if the plugin defines a 'studiocms:astro:config' hook as a function.
* If so, it invokes the hook with a mock logger and an `addIntegrations` callback,
* which accumulates integrations into an array. The collected integrations are then
* returned in an object.
*/
private runAstroConfigHook;
/**
* Executes the `studiocms:config:setup` hook on the current plugin, if available,
* and collects configuration options set by the hook into partial option objects.
*
* This method mocks the hook's context by providing setter functions for various
* StudioCMS configuration aspects, such as authentication, dashboard, frontend,
* image service, rendering, and sitemap. Each setter captures the values provided
* by the hook and stores them in corresponding partial option objects.
*
* @returns A promise that resolves to an object containing the collected configuration
* options for authentication service, dashboard, frontend, image service,
* rendering, and sitemap.
*
* @remarks
* This utility is primarily intended for testing purposes, allowing inspection of
* configuration values set by a plugin's `studiocms:config:setup` hook.
*/
private runStudioCMSConfigHook;
/**
* Retrieves information about the current plugin.
*
* @returns An object containing the plugin's identifier, name, minimum required StudioCMS version, and dependencies.
*/
getPluginInfo(): {
identifier: string;
name: string;
studiocmsMinimumVersion: string;
requires: string[] | undefined;
};
/**
* Asynchronously retrieves the results of configured plugin hooks.
*
* @returns An object containing the presence and results of the 'studiocms:astro:config' and 'studiocms:config:setup' hooks.
* - `astroConfig`: Indicates if the 'studiocms:astro:config' hook exists and provides its execution results.
* - `studiocmsConfig`: Indicates if the 'studiocms:config:setup' hook exists and provides its execution results.
*/
getHookResults(): Promise<PluginHookResults>;
}
export {};