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

Mocking Angular Material modules do not work #50

Closed
sinedied opened this issue Apr 26, 2019 · 11 comments · Fixed by #80
Closed

Mocking Angular Material modules do not work #50

sinedied opened this issue Apr 26, 2019 · 11 comments · Fixed by #80
Assignees

Comments

@sinedied
Copy link

I'm trying to mock Angular Material modules to avoid using NO_ERRORS_SCHEMA when doing shallow components testing, but it's not working for me.

At first, I tried mocking my material.module.ts which is just a module that re-exports all material individual modules, and it's not working at all as I get "unknown element <mat-card" and such.

Then I tried mocking all material modules individually with modules.map((module: any) => MockModule(module)); and use that in my shared.module.mock.ts, a module that I import in all my unit tests for convenience. This module imports & exports the mocked material modules.
This works better as I don't get "unknown element" errors, but this time I'm stuck on this error:

 TypeError: Cannot read property 'indexOf' of undefined
            at HammerGesturesPlugin.isCustomEvent (./node_modules/@angular/platform-browser/fesm5/platform-browser.js?:1639:102)
            at HammerGesturesPlugin.supports (./node_modules/@angular/platform-browser/fesm5/platform-browser.js?:1577:75)
            at EventManager._findPluginFor (./node_modules/@angular/platform-browser/fesm5/platform-browser.js?:911:26)
            at EventManager.addEventListener (./node_modules/@angular/platform-browser/fesm5/platform-browser.js?:882:27)
            at EmulatedEncapsulationDomRenderer2.DefaultDomRenderer2.listen (./node_modules/@angular/platform-browser/fesm5/platform-browser.js?:1190:34)

Obviously, this error is not there when I use the real material modules.

Do you have any idea or workaround for this?

Thanks!

@satanTime
Copy link
Member

Hi @sinedied,

thanks for for the report. Could you create simple project and share its code to demonstrate the error?

@sinedied
Copy link
Author

Sure! Is a stackblitz project ok for you?

@satanTime
Copy link
Member

it's perfect

@sinedied
Copy link
Author

It was hard to reproduce, but here it is: https://stackblitz.com/edit/ng-mock-material-issue?file=src%2Fapp%2Fshared.module.mock.ts

Seems it's somehow related to the mat-side-nav module, as if you comment this part from app.component.html it's working.

@satanTime
Copy link
Member

Thanks I'll check next days and prepare fix.

@sinedied
Copy link
Author

I have pinpointed the issue to the MatSliderModule: if I exclude only this one from the mocks, it's working -> https://stackblitz.com/edit/ng-mock-material-issue-7ecjrd?file=src%2Fapp%2Fshared.module.mock.ts

@getsaf
Copy link
Collaborator

getsaf commented Apr 26, 2019

This is an issue with auto-mocking the HAMMER_GESTURE_CONFIG. You will want to use the "real" version of that provider. MockModule auto-mocks the config which causes HammerJS to crap out when it tries to access a property on the config.

I would recommend enhancing MockModule in a way that allows you to specify which items in the module you would like to avoid mocking.

As a hack, you can re-provide a dummy config on top of your mocked modules like so:

@NgModule({
  imports: [
    // ...
    ...MaterialModulesMocks
  ],
  /* This is a hack.. You really want to avoid doing this at the MockModule level */
  providers: [
    {provide: HAMMER_GESTURE_CONFIG, useValue: {events: []}}
  ],
  // ...
})

Here's your first StackBlitz with this hack applied:
https://stackblitz.com/edit/ng-mock-material-issue-nnceql?file=src/app/shared.module.mock.ts

@getsaf
Copy link
Collaborator

getsaf commented Apr 26, 2019

Wanted to follow up that @satanTime has enhancements planned for MockModule that should address this in the future.

See his comment about it on another issue.

@satanTime
Copy link
Member

Hi guys, sorry for the long delay, work, you know. Checking the thing.

@satanTime
Copy link
Member

So, I was trying to reproduce the issue with angular 9, but no luck. The stackblitz.com sample is too old and I can't run it easily on node 12, it requires node 10.

@satanTime
Copy link
Member

Should be fixed by #80

@satanTime satanTime linked a pull request Feb 22, 2020 that will close this issue
@satanTime satanTime self-assigned this Feb 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants