Overview of @foscia/test unit testing library
@foscia/test is a unit testing library specifically designed to mock Foscia actions.
website·Indexed 32 days ago
https://foscia.dev/A TypeScript-based framework providing tools for serialization, deserialization, and API implementations including REST, JSON:API, and HTTP. It includes a suite of packages such as @foscia/serialization, @foscia/shared, and @foscia/test for mocking and testing.
Foscia is a framework-agnostic, type-safe, and modular API/data client for JS/TS that acts as an intermediary layer (similar to an ORM) between an application and its data source (API or database). It is built with functional programming principles and is fully tree-shakable. Key features include:
Model and Builder classes, Foscia uses simple functions to affect model instances and their context. This architectural choice ensures that unused model helpers, action enhancers, or runners are tree-shaken from the final production bundle, reducing bundle size.JsonApiDocument type alias defines the top-level structure of a JSON:API document. It is an object that can contain the primary data, error messages, included related resources, and metadata about the API version and the request.// Example of a JsonApiDocument structure
interface JsonApiDocument {
data?: JsonApiResource[] | JsonApiResource | JsonApiNewResource | null;
errors?: JsonApiError[];
included?: JsonApiResource[];
jsonapi?: {
meta?: JsonApiMeta;
version?: string;
};
links?: JsonApiLinks;
meta?: JsonApiMeta;
}HttpInterruptedError constructor takes three parameters to provide context about the interrupted request.new HttpInterruptedError(message: string, request: Request, source: unknown)Foscia integrates into an application as a layer between the UI/business logic and the data source. The integration workflow consists of:
onSaving(model, callback) to register a callback that triggers when a specific Model instance is about to be saved. The function returns a cleanup function that, when called, removes the registered hook.import { onSaving } from '@foscia/core';
// Example usage
const stopListening = onSaving(myModel, (model) => {
console.log('Model is saving...', model);
});
// To remove the hook:
stopListening();onSaving(composable, callback) to register a callback that triggers when a ModelComposable is about to be saved. The function returns a cleanup function to unsubscribe from the hook.import { onSaving } from '@foscia/core';
const stopListening = onSaving(myComposable, (composable) => {
console.log('Composable is saving...');
});onSaving(factory, callback) to register a callback that triggers when a ModelFactory is about to be saved. The function returns a cleanup function to unsubscribe from the hook.import { onSaving } from '@foscia/core';
const stopListening = onSaving(myFactory, (factory) => {
console.log('Factory is saving...');
});ModelSnapshot<M> type alias represents a snapshot of a model class or instance. It is a generic object where M (defaulting to any) represents the model type. It contains metadata about the model's state and its raw values.// Example of the ModelSnapshot structure
type ModelSnapshot<M = any> = {
$exists: boolean;
$loaded: Dictionary<true>;
$model: ModelClass;
$raw: any;
$values: Partial<ModelValues<M>>;
};