@angular/core/testing#TestModuleMetadata TypeScript Examples

The following examples show how to use @angular/core/testing#TestModuleMetadata. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: angular-context.ts    From s-libs with MIT License 6 votes vote down vote up
/**
   * @param moduleMetadata passed along to [TestBed.configureTestingModule()]{@linkcode https://angular.io/api/core/testing/TestBed#configureTestingModule}. Automatically includes {@link HttpClientTestingModule} for you.
   */
  constructor(moduleMetadata: TestModuleMetadata = {}) {
    assert(
      !AngularContext.#current,
      'There is already another AngularContext in use (or it was not cleaned up)',
    );
    AngularContext.#current = this;
    TestBed.configureTestingModule(
      extendMetadata(moduleMetadata, {
        imports: [HttpClientTestingModule],
        providers: [MockErrorHandler.overrideProvider()],
      }),
    );
  }
Example #2
Source File: angular-context.ts    From s-libs with MIT License 6 votes vote down vote up
export function extendMetadata(
  metadata: TestModuleMetadata,
  toAdd: TestModuleMetadata,
): TestModuleMetadata {
  const result: any = clone(metadata);
  forOwn(toAdd, (val, key) => {
    const existing = result[key];
    if (isUndefined(existing)) {
      result[key] = val;
    } else if (key === 'imports') {
      // to allow ComponentContext to unconditionally disable animations, added imports override previous imports
      result[key] = [result[key], val];
    } else {
      // but for most things we want to let what comes in from subclasses and users win
      result[key] = [val, result[key]];
    }
  });
  return result;
}
Example #3
Source File: component-context.ts    From s-libs with MIT License 6 votes vote down vote up
/**
   * @param componentType `run()` will create a component of this type before running the rest of your test.
   * @param moduleMetadata passed along to [TestBed.configureTestingModule()]{@linkcode https://angular.io/api/core/testing/TestBed#configureTestingModule}. Automatically includes {@link NoopAnimationsModule}, in addition to those provided by {@link AngularContext}.
   * @param unboundInputs By default a synthetic parent component will be created that binds to all your component's inputs. Pass input names here that should NOT be bound. This is useful e.g. to test the default value of an input.
   */
  constructor(
    componentType: Type<T>,
    moduleMetadata: TestModuleMetadata = {},
    unboundInputs: Array<keyof T> = [],
  ) {
    // TODO: once cleanup of contexts is not so touchy, move this below super() and use shortcut `private` declarations on constructor params
    const inputProperties = WrapperComponent.wrap(componentType, unboundInputs);
    super(
      extendMetadata(moduleMetadata, {
        imports: [NoopAnimationsModule],
        declarations: [WrapperComponent, componentType],
      }),
    );

    this.componentType = componentType;
    this.inputProperties = new Set(inputProperties);
    this.inputs = {};
    this.wrapperStyles = {};
  }