Angular2 Unit ViewportRuler Material Unit Error

I have an Angular2 component that contains a tab control from @angular/material .

I try to test my component (see simplified code below - I understand that it makes no sense to test the component, that it is simple), and I get the following error:

 Error: Error in ./MdTabHeader class MdTabHeader - inline template:0:0 caused by: No provider for ViewportRuler! Error: No provider for ViewportRuler! 

My guess was to try to include ViewportRuler ( https://github.com/angular/material2/blob/master/src/lib/core/overlay/position/viewport-ruler.ts ) as a provider. When I do this (see Numbered lines below), karma returns:

 Uncaught SyntaxError: Unexpected token import at http://localhost:9876/context.html:10 

Which of the bits does Googling suggest that it serves the .ts file in the browser, rather than compiling .js. Perhaps I refer to him from the wrong place.

My question is: how do I compile my tests?

My code is:

my.component.ts:

 @Component({ selector: 'test', template: require('./test.component.html') }) export class TestComponent { items: any[]; constructor() { this.items = [{ title: 'test 1', description: 'description 1' }, { title: 'test 2', description: 'description 2' }]; } } 

my.component.html:

 <md-tab-group> <md-tab *ngFor="let link of items"> <template md-tab-label> <h4>{{link.title}}</h4> </template> {{link.description}} </md-tab> </md-tab-group> 

my.component.spec.ts:

 import { TestBed } from '@angular/core/testing'; import { Component} from '@angular/core'; import { MaterialModule } from '@angular/material'; import { ViewportRuler} from '@angular/material/core/overlay/position/viewport-ruler' import { TestComponent } from './test.component'; describe("TestComponent", () => { let fixture, component; beforeEach(() => { TestBed.configureTestingModule({ imports: [MaterialModule], declarations: [TestComponent], providers: [ //{ provide: ViewportRuler } ] }); fixture = TestBed.createComponent(TestComponent); component = fixture.componentInstance; }); it('true = true', () => { expect(true).toBe(true); }); }); 

I tried to include as much information as possible, but I'm new to Angular all over the world, so let me know if there is anything else I can provide.

Many thanks.

+6
source share
1 answer

2.0.0-beta.3

MaterialModule deprecated. Developers can either consume individual components and their dependencies as necessary (for example, MdButtonModule ), or create their own custom module.

Materialmodule

  • MaterialModule (and MaterialRootModule) are marked as deprecated.

We found that in the current state of tree tremors in the world, using an aggregate NgModule, such as MaterialModule, means that tools cannot eliminate code for components that are not in use.

To provide users with the minimum code size possible, we condemn the MaterialModule, which should be removed in a future release.

To replace the MaterialModule, users can create their own β€œMaterial” modul as part of their application (for example, the GmailMaterialModule), which only imports the set of components actually used in the application.

https://github.com/angular/material2/releases/tag/2.0.0-beta.3


2.0.0-beta.2

The material command has been removed .forRoot() , which makes it no problem.

Using the forRoot module is deprecated and will be removed in a future version. Instead, simply import the MaterialModule directly:

 @NgModule({ imports: [ ... MaterialModule, ... ] ... }); 

https://github.com/angular/material2/releases/tag/2.0.0-beta.2


MaterialModule.forRoot() sets up the providers you need in the testing module. This should fix bugs like yours and those similar, like No provider for MdIconRegistry! .

+5
source

Source: https://habr.com/ru/post/1013101/


All Articles