Giter Club home page Giter Club logo

Comments (17)

dirkluijk avatar dirkluijk commented on June 13, 2024 2

All right, maybe it's good to update the docs accordingly (and deprecate the Spectator one, and update the existing unit tests to properly demonstrate this compatibility).

from spectator.

dirkluijk avatar dirkluijk commented on June 13, 2024 1

New doc proposal for: https://netbasal.gitbook.io/spectator/components/untitled-1 (Mock Components).

Mock Components

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)
   ]
 });

For more info about mocking components, please take a look at the ng-mocks docs.

from spectator.

olivlomp avatar olivlomp commented on June 13, 2024 1

Hey all, thanks for the quick responses and the swift resolution and clarification! 🥇

from spectator.

ike18t avatar ike18t commented on June 13, 2024 1

I came across this lib after reading @NetanelBasal 's medium article. I'm stoked that you guys are going to pull in ng-mocks. Let me know if there is anything you guys need from me.

from spectator.

NetanelBasal avatar NetanelBasal commented on June 13, 2024 1

@ike18t Yes, we are using ng-mocks. Spectator and ng-mocks together are a win-win :)

from spectator.

NetanelBasal avatar NetanelBasal commented on June 13, 2024

I see what the problem. It's because it's on the prototype. I think we should move to the complete implementation of Mocks in Angular.

https://github.com/ike18t/ng-mocks

We're already using it actually in our code. It auto mocks the inputs, outputs, etc.

from spectator.

NetanelBasal avatar NetanelBasal commented on June 13, 2024

Yes, this is working as expected:

import { createTestComponentFactory } from '@netbasal/spectator';
import { MockComponentEmittersComponent } from './mock-component-emitters.component';
import { AppSubcomponentDemoComponent } from '../app-subcomponent-demo/app-subcomponent-demo.component';
import { MockComponent } from 'ng-mocks';

fdescribe('spectator', () => {
  const create = createTestComponentFactory({
    component: MockComponentEmittersComponent,
    shallow: true,
    detectChanges: false,
    declarations: [MockComponent(AppSubcomponentDemoComponent)]
  });

  it('should differentiate between instances of event emitters', () => {
    const spectator = create();
    spyOn(spectator.component, 'boundMethod');

    const instances = spectator.queryAll<AppSubcomponentDemoComponent>(MockComponent(AppSubcomponentDemoComponent));

    instances[0].emitter.emit();
    expect(spectator.component.boundMethod).toHaveBeenCalledTimes(1);
  });
});

from spectator.

NetanelBasal avatar NetanelBasal commented on June 13, 2024

@dirkluijk for your knowledge.

from spectator.

dirkluijk avatar dirkluijk commented on June 13, 2024

Nice library @NetanelBasal. Maybe I have some time left the next days to submit a PR to integrate it. 😄

from spectator.

NetanelBasal avatar NetanelBasal commented on June 13, 2024

We need the Jest docs first :)

from spectator.

dirkluijk avatar dirkluijk commented on June 13, 2024

Of course. By the way, if ng-mocks is preferred over the Spectator MockComponent, does that mean that no integration is needed at all and that we just have to deprecate the current implementation in favour of the ng-mocks version of MockComponent? Or what do you suggest?

from spectator.

NetanelBasal avatar NetanelBasal commented on June 13, 2024

Yes, that's what we did in our application. We're using ng-mocks directly; it's much more comfortable.

from spectator.

ike18t avatar ike18t commented on June 13, 2024

I noticed in the spectator docs for mocking a component there is the following line
const instances = spectator.queryAll<SubComponent>(MockComponent(SubComponent));

That call to MockComponent isn't necessary since this commit:
help-me-mom/ng-mocks@7ccd3f5#diff-e618e42c87439e062f6fc60c40d0e5abR37

My bad for not representing that in the README, I just updated it.

It can be:
const instances = spectator.queryAll<SubComponent>(SubComponent);

That being said you can possibly update your method sig for the query methods from:
query<R>(directiveOrSelector: Type<any> to query<R>(directiveOrSelector: R
and get the usage down to:
const instances = spectator.queryAll(SubComponent);

from spectator.

NetanelBasal avatar NetanelBasal commented on June 13, 2024

Awesome!

from spectator.

maxime1992 avatar maxime1992 commented on June 13, 2024

Yes, we are using ng-mocks. Spectator and ng-mocks together are a win-win :)

This has been really useful but it's nowhere in the doc. It'd be a nice addition to the readme I think

from spectator.

NetanelBasal avatar NetanelBasal commented on June 13, 2024

@maxime1992 would love to see a PR.

from spectator.

maxime1992 avatar maxime1992 commented on June 13, 2024

👆 #206

from spectator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.