From 1506228c74ea301c172a40019ccbdc8ebfd7e2ea Mon Sep 17 00:00:00 2001 From: Alfonso Salces Date: Mon, 13 Mar 2023 10:19:59 +0100 Subject: [PATCH] MOBILE-4245 Credentials: Fix NullInjectorError when provide HttpClient --- .../components/show-password/show-password.ts | 11 +++++-- .../features/login/tests/credentials.test.ts | 33 ++++++++++++------- .../features/sitehome/tests/links.test.ts | 3 ++ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/core/components/show-password/show-password.ts b/src/core/components/show-password/show-password.ts index 3b61044bf..735a4c4c5 100644 --- a/src/core/components/show-password/show-password.ts +++ b/src/core/components/show-password/show-password.ts @@ -65,9 +65,14 @@ export class CoreShowPasswordComponent implements OnInit, AfterViewInit { */ async ngAfterViewInit(): Promise { if (this.ionInput) { - // It's an ion-input, use it to get the native element. - this.input = await this.ionInput.getInputElement(); - this.setData(this.input); + try { + // It's an ion-input, use it to get the native element. + this.input = await this.ionInput.getInputElement(); + this.setData(this.input); + } catch (error) { + // This should never fail, but it does in some testing environment because Ionic elements are not + // rendered properly. So in case this fails, we'll just ignore the error. + } return; } diff --git a/src/core/features/login/tests/credentials.test.ts b/src/core/features/login/tests/credentials.test.ts index 2d0b98341..5a80112af 100644 --- a/src/core/features/login/tests/credentials.test.ts +++ b/src/core/features/login/tests/credentials.test.ts @@ -18,17 +18,26 @@ import { CoreLoginError } from '@classes/errors/loginerror'; import { CoreLoginComponentsModule } from '@features/login/components/components.module'; import { CoreLoginCredentialsPage } from '@features/login/pages/credentials/credentials'; import { CoreSites } from '@services/sites'; +import { Http } from '@singletons'; +import { of } from 'rxjs'; +import { CoreLoginHelper } from '../services/login-helper'; describe('Credentials page', () => { + const siteUrl = 'https://campus.example.edu'; + + beforeEach(() => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + mockSingleton(Http, { get: () => of(null as any) }); + }); + it('renders', async () => { // Arrange. - const siteUrl = 'https://campus.example.edu'; mockSingleton(CoreSites, { getPublicSiteConfigByUrl: async () => ({ - wwwroot: 'https://campus.example.edu', - httpswwwroot: 'https://campus.example.edu', + wwwroot: siteUrl, + httpswwwroot: siteUrl, sitename: 'Example Campus', guestlogin: 0, rememberusername: 0, @@ -45,6 +54,8 @@ describe('Credentials page', () => { }), }); + mockSingleton(CoreLoginHelper, { getAvailableSites: async () => [{ url: siteUrl, name: 'Example Campus' }] }); + // Act. const fixture = await renderPageComponent(CoreLoginCredentialsPage, { routeParams: { siteUrl }, @@ -58,7 +69,7 @@ describe('Credentials page', () => { expect(findElement(fixture, '.core-siteurl', siteUrl)).not.toBeNull(); }); - it('suggests contacting support after multiple failed attempts', async () => { + it('suggests contacting support after multiple failed attempts', async (done) => { // Arrange. mockSingleton(CoreSites, { getUserToken: () => { @@ -69,17 +80,15 @@ describe('Credentials page', () => { }, }); + mockSingleton(CoreLoginHelper, { getAvailableSites: async () => [] }); + const fixture = await renderPageComponent(CoreLoginCredentialsPage, { - routeParams: { - siteUrl: 'https://campus.example.edu', - siteConfig: { supportpage: '' }, - }, - imports: [ - CoreSharedModule, - CoreLoginComponentsModule, - ], + routeParams: { siteUrl, siteConfig: { supportpage: '' } }, + imports: [CoreSharedModule, CoreLoginComponentsModule], }); + done(); + // Act. const form = requireElement(fixture, 'form'); const formControls = fixture.componentInstance.credForm.controls; diff --git a/src/core/features/sitehome/tests/links.test.ts b/src/core/features/sitehome/tests/links.test.ts index 5c515fb65..528e11b6e 100644 --- a/src/core/features/sitehome/tests/links.test.ts +++ b/src/core/features/sitehome/tests/links.test.ts @@ -15,6 +15,7 @@ import { mock, mockSingleton } from '@/testing/utils'; import { CoreSite } from '@classes/site'; import { CoreContentLinksDelegate } from '@features/contentlinks/services/contentlinks-delegate'; +import { CoreLoginHelper } from '@features/login/services/login-helper'; import { CoreSiteHomeIndexLinkHandlerService } from '@features/sitehome/services/handlers/index-link'; import { CoreNavigator } from '@services/navigator'; import { CoreSites } from '@services/sites'; @@ -33,6 +34,8 @@ describe('Site Home link handlers', () => { getSiteIdsFromUrl: () => Promise.resolve([siteId]), })); + mockSingleton(CoreLoginHelper, { getAvailableSites: async () => [{ url: siteUrl, name: 'Example Campus' }] }); + CoreContentLinksDelegate.registerHandler(new CoreSiteHomeIndexLinkHandlerService()); // Act.