@jsteffen wrote:
I’ve been trying to get my unit tests in Karma to work but keep getting this error:
Uncaught Error: Uncaught (in promise): NotSupportedError: screen.orientation.lock() is not available on this device. thrown
I have tried creating a mock class for screen orientation but it doesn’t seem to work. Just interested in if anyone has experienced this problem before and found a way to fix this.
Here is my full spec file:
import { async, ComponentFixture, TestBed } from '@angular/core/testing' import { ScreenOrientation } from "@ionic-native/screen-orientation" import { HttpClientTestingModule } from '@angular/common/http/testing' import { IonicStorageModule } from '@ionic/storage' import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from '@angular/core' import { Idle, IdleExpiry } from '@ng-idle/core' import { Keepalive } from '@ng-idle/keepalive' import { RouterTestingModule } from '@angular/router/testing' import { BarcodeProvider } from './../../scripts/barcode' import { oAuthService } from './../../scripts/oauth' import { AlphaScanInputComponent } from './alpha-scan-input.component' class ScreenOrientationMock extends ScreenOrientation { lock(type) { return new Promise((resolve, reject) => { resolve("locked"); }) } } describe('AlphaScanInputComponent', () => { let component: AlphaScanInputComponent let fixture: ComponentFixture<AlphaScanInputComponent> beforeEach( async( () => { TestBed.configureTestingModule({ schemas: [ CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA ], declarations: [AlphaScanInputComponent], providers: [ { provide: Idle, useClass: Idle }, { provide: IdleExpiry, useClass: IdleExpiry }, { provide: Keepalive, useClass: Keepalive }, { provide: BarcodeProvider }, { provide: oAuthService }, { provide: ScreenOrientation, useClass: ScreenOrientationMock } ], imports: [ RouterTestingModule.withRoutes([]), HttpClientTestingModule, IonicStorageModule.forRoot() ] }).compileComponents() }) ) beforeEach(() => { fixture = TestBed.createComponent(AlphaScanInputComponent) component = fixture.componentInstance fixture.detectChanges() }) it('should create', () => { expect(component).toBeTruthy() }) })
Posts: 1
Participants: 1