Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support selector-less components #266

Closed
GerkinDev opened this issue Dec 29, 2020 · 7 comments · Fixed by #267 or #268
Closed

Support selector-less components #266

GerkinDev opened this issue Dec 29, 2020 · 7 comments · Fixed by #267 or #268

Comments

@GerkinDev
Copy link

Hey there ! It's me again.

By fooling around with ng-mocks in my app's tests, I've found that ng-mocks does not correctly set the fixture's point property correctly if the tested component has no selector (like a page-only component).

Here is the failing test case:

import { Component } from '@angular/core';
import { MockBuilder, MockRender } from 'ng-mocks';

@Component({
  template: `<span>Hello world</span>`,
})
class TestedComponent {}

describe('MockRender', () => {
  beforeEach(() => MockBuilder(TestedComponent));

  it('should get the rendered component correctly if no selector', () => {
    const fixture = MockRender(TestedComponent);
    expect(fixture.point).toBeDefined();
  });
});

I've found a way to go around this issue like so (this test works)

import { Component, ɵReflectionCapabilities } from '@angular/core';
import { MockBuilder, MockRender } from 'ng-mocks';

@Component({
  template: `<span>Hello world</span>`,
})
class TestedComponent {}

describe('MockRender', () => {
  const TestedComponentWithSelector = Component({
    ...new ɵReflectionCapabilities().annotations(TestedComponent)[0],
    selector: 'this-can-be-anything'
  })(TestedComponent)
  // Do not forget to return the promise of MockBuilder.
  beforeEach(() => MockBuilder(TestedComponentWithSelector));

  it('should get the rendered component correctly', () => {
    const fixture = MockRender(TestedComponentWithSelector);
    expect(fixture.point).toBeDefined();
  });
});

Note: the weird bit using angular internal reflector is taken from here

🍻 cheers !

@satanTime
Copy link
Member

Hi again!

Thanks for the report! I'll fix it during the next days.

Happy New Year and happy coding!

@satanTime satanTime linked a pull request Dec 30, 2020 that will close this issue
6 tasks
@satanTime
Copy link
Member

Hi @GerkinDev,

might you confirm it works as you expect? ng-mocks.zip

satanTime added a commit that referenced this issue Jan 1, 2021
@satanTime
Copy link
Member

v11.3.0 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems.

@GerkinDev
Copy link
Author

GerkinDev commented Jan 2, 2021

Hi, sorry for not answering earlier. I see you like this : https://media1.tenor.com/images/7190a66af3e07bf4fc54e1f66b7e439b/tenor.gif.
I'm working on something unrelated for a couple of days, and will be able to test this in situ soon, but not now. Sorry. But I totally trust you. Moreover, you have at least one test case, and the bug is pretty simple to check if resolved

Thanks again, and 🍻 cheers!

@satanTime
Copy link
Member

Ahaha, thanks :)

Sure, no rush here, I have the same feelings that the issue has been fixed. Just need an extra proof once you have time.

Thanks in advance and happy coding!

@satanTime
Copy link
Member

v11.3.1 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems.

@GerkinDev
Copy link
Author

Hi again,

I can confirm this works in my real-life situation ! 🎉

Thank you so much, happy new year ! 🎉🎉🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants