Mock Components

Last updated 3 months ago

Sometimes it's helpful to pass a mock implementation of other components. You can mock components using the ng-mocks library:

import { createHostComponentFactory } from '@netbasal/spectator';
import { MockComponent } from 'ng-mocks';
import { FooComponent } from './path/to/foo.component';
const createHost = createHostComponentFactory({
component: SettingsPanelComponent,
declarations: [
MockComponent(FooComponent)
]
});

Another example:

import { createTestComponentFactory } from '@netbasal/spectator';
import { MockComponent } from 'ng-mocks';
describe('spectator', () => {
const create = createTestComponentFactory({
component: MyComponent,
detectChanges: false,
declarations: [MockComponent(SubComponent)]
});
it('should emit event', () => {
const spectator = create();
spyOn(spectator.component, 'boundMethod');
const instances = spectator.queryAll<SubComponent>(SubComponent);
instances[0].emitter.emit();
expect(spectator.component.boundMethod).toHaveBeenCalledTimes(1);
});
});