UNPKG

signalstory

Version:

Signal-based state management for Angular that grows with your project. Explore a versatile toolbox with enriching plugins for developers at all levels.

100 lines (95 loc) 3.63 kB
import { Injector } from '@angular/core'; /* eslint-disable @typescript-eslint/no-explicit-any */ /** * Following methods are intendet to simplify testing with stores. You can achieve everything the regular way. * The philosophy is, that setup logic (arrange) can be completely done by each test case independently and that * the setup is as readable and easy to follow as possible. Note, that you could still share setup logic using those methods. * * As every store can have its own injector (store.config.injector) you can create a separate injection context * for every test case. This is especially handy if you are testing effect or query objects as you don't necessarily * need to use Testbed or jest.mock (or any other mocking lib) to setup a mocked DI Context + mocked dependencies. * But you could also use this for independent services, register them in the stores injector and using the method getFromStoreInjector to get an instance */ /** * Configures a mocked Dependency Injection (DI) context for a specific store, allowing * the injection of mocked dependencies during testing. This has no sideefffects for other injection contexts (e.g. root) * Only the injection context for the given store is created and configured using this method * * @param store - The SignalStory store for which the injection context is being configured. * @param configure - A callback function that receives an {@link InjectionContextConfiguration} object * for specifying providers in the injection context. * * @example * ```typescript * configureInjectionContext(myStore, (config) => { * config.addRegular(MyService); * config.addMocked(OtherService, (instance) => { * instance.methodToMock = jest.fn(() => 'mocked result'); * }); * }); * ``` */ function configureInjectionContext(store, configure) { const options = new DefaultInjectionContextConfiguration(); configure(options); store.config.injector = Injector.create({ providers: options.collectProviders(), }); } /** * Retrieves an instance of a specified type from the store's injector. * * @param store - The SignalStory store from which to retrieve the instance. * @param classType - The class type for the instance to be retrieved. * @returns The instance of the specified type if found; otherwise, `undefined`. */ function getFromStoreInjector(store, classType) { return store.config.injector?.get(classType); } /** * Default implementation of the {@link InjectionContextConfiguration} interface. * * @public */ class DefaultInjectionContextConfiguration { constructor() { this.providers = []; } collectProviders() { return this.providers; } add(provider) { this.providers.push(provider); return this; } addRegular(classType) { this.providers.push({ provide: classType }); return this; } addExisting(classType, instance) { this.providers.push({ provide: classType, useFactory: () => instance, }); return this; } addMocked(classType, configureInstance) { this.providers.push({ provide: classType, useFactory: () => { const instance = Object.create(classType); configureInstance(instance); return instance; }, }); return this; } } /* * Public API Surface of signalstory/testing */ /** * Generated bundle index. Do not edit. */ export { configureInjectionContext, getFromStoreInjector }; //# sourceMappingURL=signalstory-testing.mjs.map