How do I add svg files via MatIconRegistry in unit tests?

前端 未结 6 2038
不思量自难忘°
不思量自难忘° 2021-02-05 10:16

I\'m currently implementing \'unit testing\' in my angular application. However, if I run them, I receive multiple warnings/errors similar to this one: \'Error retrieving

6条回答
  •  既然无缘
    2021-02-05 10:51

    Mock the mat-icon selector with the following component at the top of the unit test

    @Component({
        selector: 'mat-icon',
        template: ''
    })
    class MockMatIconComponent {
        @Input() svgIcon: any;
        @Input() fontSet: any;
        @Input() fontIcon: any;
    }
    

    Then override the MatIconModule in the unit test as follows

    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [ ...],
            providers: [ ...  ],
            imports: [ MatIconModule, NoopAnimationsModule ]
        })
        .overrideModule(MatIconModule, {
        remove: {
           declarations: [MatIcon],
           exports: [MatIcon]
        },
        add: {
            declarations: [MockMatIconComponent],
            exports: [MockMatIconComponent]
       }
       })
      .compileComponents();
    

    You will no longer have the 'Error retrieving icon: Unable to find icon with the name ":myIcon"' issue when running the unit tests

提交回复
热议问题