MOBILE-4245 Credentials: Fix NullInjectorError when provide HttpClient

main
Alfonso Salces 2023-03-13 10:19:59 +01:00
parent 7a78bb9375
commit 1506228c74
3 changed files with 32 additions and 15 deletions

View File

@ -65,9 +65,14 @@ export class CoreShowPasswordComponent implements OnInit, AfterViewInit {
*/
async ngAfterViewInit(): Promise<void> {
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;
}

View File

@ -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<HTMLFormElement>(fixture, 'form');
const formControls = fixture.componentInstance.credForm.controls;

View File

@ -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.