angular-google-maps icon indicating copy to clipboard operation
angular-google-maps copied to clipboard

MapsApiLoader.load not recognised in karma test

Open abyrne85 opened this issue 8 years ago • 9 comments

I'm trying to mock the call to the maps api loader but karma keeps failing saying that load() is not recognised

Test:

describe('CommuteDetailsModalComponent', () => {
  let component: CommuteDetailsModalComponent;
  let fixture: ComponentFixture<CommuteDetailsModalComponent>;
  let mapsApiLoader;
  
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [CommuteDetailsModalComponent],
      providers: [
        MapsAPILoader
      ]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(CommuteDetailsModalComponent);
    component = fixture.componentInstance;
    mapsApiLoader = fixture.debugElement.injector.get(MapsAPILoader);
    spyOn(mapsApiLoader, 'load').and.callFake(() => {
      return true;
    });
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

Result :

Error: <spyOn> : load() method does not exist
Usage: spyOn(<object>, <methodName>)

Any ideas what I'm missing?

abyrne85 avatar Jan 26 '18 11:01 abyrne85

@abyrne85 MapsApiLoader is not the implementation class that is used internally. Generally I don't think it's a good idea to test this in an integration-style test. You could use NO_ERRORS_SCHEMA to disable the maps logic. I'm closing this as this is no bug or feature request. Feel free to comment below.

sebholstein avatar Feb 01 '18 19:02 sebholstein

@abyrne85 did you fix the problem?

@SebastianM, how can I use NO_ERRORS_SCHEMA?

rochapablo avatar Mar 29 '18 14:03 rochapablo

Got it!

beforeEach(() => {
  TestBed.configureTestingModule({
    ...
    schemas: [
      NO_ERRORS_SCHEMA
    ]
  });
});

rochapablo avatar Mar 29 '18 14:03 rochapablo

To add to this, you should be able to mock the MapsAPILoader with a mock such as:

export class MockMapsAPILoader {
  public load(): Promise<boolean> {
    return new Promise(() => {
      return true;
    });
  }
}

You shouldn't have to use NO_ERRORS_SCHEMA as a result.

liamleppard avatar Apr 09 '18 17:04 liamleppard

" I don't think it's a good idea to test this in an integration-style test"

I respectfully disagree. First, automated tests are actually a good idea to have, you should use them and recommend them to all your users. Second, the ticket specifically mentions karma for unit testing, and mocks are required for unit tests, this has nothing to do with integration tests. Third, NO_ERRORS_SCHEMA is a terrible idea and can cause many problems.

Seems odd to see a library that does not want its end users running automated tests. You could literally copy the 'MockMapsAPILoader ' mentioned above to solve this.

Anyway, good luck on the plugin!

samqhr avatar Jun 12 '18 23:06 samqhr

I was able to resolve it like this. Import from core.umd:

import { AgmCoreModule, MapsAPILoader } from '@agm/core/core.umd';

Then provide MapsAPILoader like so:

beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ AgmCoreModule ],
      providers: [
        {
          provide: MapsAPILoader,
          useValue: {
            load: jest.fn().mockReturnValue(new Promise(resolve => resolve(true)))
          }
        }
      ]
    })
    .compileComponents();
  }));

this is with jest but you can just pass a mock function for load if you aren't using jest

inorganik avatar Aug 01 '18 18:08 inorganik

Quick update, importing from '@agm/core/core.umd' caused an issue with the prod build (because you have to import it that way in the actual module) - I got the error

Function calls are not supported in decorators but AgmCoreModule was called

So it appears I have to choose between tests passing or prod build succeeding. Luckily I can just omit the module spec, and this solves my issue for now.

inorganik avatar Aug 01 '18 19:08 inorganik

Hi guys,

Try:

beforeEach(async(() => { TestBed.configureTestingModule({ imports: [..., AgmCoreModule.forRoot()], declarations: [..., AgmMarkerCluster] }) .compileComponents(); }));

Works for me.

jonh-copin avatar Apr 03 '19 14:04 jonh-copin

@jonhnanthan thank you, this solved this issue for me too.

m-usai-almaviva avatar Sep 06 '19 09:09 m-usai-almaviva