Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mock-component

ike18t161MIT5.1.0TypeScript support: included

Function creating a mock based on a passed in component for test

Angular, Component, Mock, TestBed, Testing

readme

Build Status npm version

DEPRECATED: please use ng-mocks instead.

NgMocks consolidates mock-component, mock-directive, and mock-pipe into one package making it easier to maintain.

Also, if you like mock-component/ng-mocks you may also like shallow-render.

mock_component

Helper function for creating angular component mocks for test.

Why use this?

Sure, you could flip a flag on schema errors to make your component dependencies not matter. Or you could use this to mock them out and have the ability to assert on their inputs or emit on an output to assert on a side effect.

  • Mocked component with the same selector.
  • Inputs and Outputs with alias support
  • Each component instance has its own EventEmitter instances for outputs
  • Mocked component templates are ng-content tags to allow transclusion
  • Allows ng-model binding (You will have to add FormsModule to TestBed imports)
  • exportAs support

Usage Example

import { async, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { MockComponent } from 'mock-component';
import { DependencyComponent } from './dependency.component';
import { TestedComponent } from './tested.component';

describe('TestedComponent', () => {
  let fixture: ComponentFixture<TestedComponent>;

  beforeEach(async() => {
    TestBed.configureTestingModule({
      declarations: [
        MockComponent(DependencyComponent)
      ]
    })
    .compileComponents();
    .then(() => {
      fixture = TestBed.createComponent(ExampleComponentContainer);
      component = fixture.componentInstance;
    });
  }));

  it('should send the correct value to the dependency component input', () => {
    // let's pretend Dependency Component (unmocked) has 'someInput' as an  input
    // the input value will be passed into the mocked component so you can assert on it
    const mockedComponent = fixture.debugElement
                                  .query(By.css('dependency-component-selector'))
                                  .componentInstance as DependencyComponent; // casting to retain type safety
    expect(mockedComponent.someInput).toEqual('foo'); if you casted mockedComponent as the original component type then this is type safe
  });

  it('should do something when the dependency component emits on its output', () => {
    const mockedComponent = fixture.debugElement
                                   .query(By.directive(MockComponent(DependencyComponent)))
                                   .componentInstance as DependencyComponent; // casting to retain type safety
    // again, let's pretend DependencyComponent has an output called 'someOutput'
    // emit on the output that MockComponent setup when generating the mock of Dependency Component
    mockedComponent.someOutput.emit(new Foo()); if you casted mockedComponent as the original component type then this is type safe
    fixture.detectChanges();
    // assert on some side effect
  });
});

Find an issue or have a request?

Report it as an issue or submit at PR. I'm open to contributions.

You may also like:

https://www.npmjs.com/package/mock-directive

https://www.npmjs.com/package/mock-pipe

changelog

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

5.1.0 (2018-04-05)

5.0.0 (2018-02-03)

Features

  • memoize function by arg (cac00b3)

5.0.0-rc.3 (2018-01-28)

5.0.0-rc.2 (2018-01-26)

Features

  • add exportAs and alias support (14a1474)
  • Upgrade to angular 5 and pull in testbed for tests (7df64a8)

0.7.0 (2017-12-08)

Features

  • component mock implements control value accessor to support ngModel binding (67ea7c4)

0.6.0 (2017-12-07)

Bug Fixes

0.5.0 (2017-11-27)

Bug Fixes

  • instantiate event emitters in component constructor (fb4b97d)

0.4.0 (2017-11-21)

0.3.0 (2017-11-14)

Bug Fixes

  • add reflect-metadata back to devDeps (385c9c4)
  • works with component w/o inputs or outputs (b3d38e7)

0.2.0 (2017-10-26)

Features

  • Adding angular 2 compatibility and moving to peerDependency (#3) (4bd93db)

0.0.5 (2017-09-19)

0.0.4 (2017-09-19)

5.0.0 (2018-02-03)

Features

  • memoize function by arg (cac00b3)

5.0.0-rc.3 (2018-01-28)

5.0.0-rc.2 (2018-01-26)

Features

  • add exportAs and alias support (14a1474)
  • Upgrade to angular 5 and pull in testbed for tests (7df64a8)

0.7.0 (2017-12-08)

Features

  • component mock implements control value accessor to support ngModel binding (67ea7c4)

0.6.0 (2017-12-07)

Bug Fixes

0.5.0 (2017-11-27)

Bug Fixes

  • instantiate event emitters in component constructor (fb4b97d)

0.4.0 (2017-11-21)

0.3.0 (2017-11-14)

Bug Fixes

  • add reflect-metadata back to devDeps (385c9c4)
  • works with component w/o inputs or outputs (b3d38e7)

0.2.0 (2017-10-26)

Features

  • Adding angular 2 compatibility and moving to peerDependency (#3) (4bd93db)

0.0.5 (2017-09-19)

0.0.4 (2017-09-19)

5.0.0-rc.3 (2018-01-28)

5.0.0-rc.2 (2018-01-26)

Features

  • add exportAs and alias support (14a1474)
  • Upgrade to angular 5 and pull in testbed for tests (7df64a8)

0.7.0 (2017-12-08)

Features

  • component mock implements control value accessor to support ngModel binding (67ea7c4)

0.6.0 (2017-12-07)

Bug Fixes

0.5.0 (2017-11-27)

Bug Fixes

  • instantiate event emitters in component constructor (fb4b97d)

0.4.0 (2017-11-21)

0.3.0 (2017-11-14)

Bug Fixes

  • add reflect-metadata back to devDeps (385c9c4)
  • works with component w/o inputs or outputs (b3d38e7)

0.2.0 (2017-10-26)

Features

  • Adding angular 2 compatibility and moving to peerDependency (#3) (4bd93db)

0.0.5 (2017-09-19)

0.0.4 (2017-09-19)