Skip to content

Commit 825c376

Browse files
committed
MOBILE-4759 test: Fix rendering components on testing
1 parent f092248 commit 825c376

File tree

10 files changed

+69
-58
lines changed

10 files changed

+69
-58
lines changed

.github/workflows/testing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969
cat circular-dependencies
7070
lines=$(cat circular-dependencies | wc -l)
7171
echo "Total circular dependencies: $lines"
72-
test $lines -eq 93
72+
test $lines -eq 95
7373
- name: JavaScript code compatibility
7474
run: |
7575
npx check-es-compat www/*.js --polyfills="\{Array,String,TypedArray\}.prototype.at,Object.hasOwn"

src/core/components/tests/iframe.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('CoreIframeComponent', () => {
2626
const { nativeElement } = await renderTemplate(
2727
CoreIframeComponent,
2828
'<core-iframe src="https://moodle.org/"></core-iframe>',
29+
{ standalone: true },
2930
);
3031

3132
// Assert.

src/core/components/tests/user-avatar.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ describe('CoreUserAvatarComponent', () => {
2020

2121
it('should render', async () => {
2222
// Act.
23-
const { nativeElement } = await renderComponent(CoreUserAvatarComponent);
23+
const { nativeElement } = await renderComponent(
24+
CoreUserAvatarComponent,
25+
{ standalone: true },
26+
);
2427

2528
// Assert.
2629
expect(nativeElement.innerHTML.trim()).not.toHaveLength(0);

src/core/directives/tests/format-text.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe('CoreFormatTextDirective', () => {
5050
providers: [
5151
{ provide: IonContent, useValue: null },
5252
],
53+
standalone: true,
5354
};
5455
});
5556

@@ -79,6 +80,7 @@ describe('CoreFormatTextDirective', () => {
7980
const { nativeElement } = await renderTemplate(
8081
CoreFormatTextDirective,
8182
'<core-format-text text="Lorem ipsum dolor"></core-format-text>',
83+
{ standalone: true },
8284
);
8385

8486
// Assert
@@ -116,13 +118,15 @@ describe('CoreFormatTextDirective', () => {
116118
});
117119

118120
// Act
119-
const { nativeElement } = await renderTemplate(CoreFormatTextDirective, `
120-
<core-format-text
121+
const { nativeElement } = await renderTemplate(
122+
CoreFormatTextDirective,
123+
`<core-format-text
121124
text="Lorem ipsum dolor"
122125
contextLevel="course"
123126
[contextInstanceId]="42"
124-
></core-format-text>
125-
`);
127+
></core-format-text>`,
128+
{ standalone: true },
129+
);
126130

127131
// Assert
128132
const text = nativeElement.querySelector('core-format-text');
@@ -180,6 +184,7 @@ describe('CoreFormatTextDirective', () => {
180184
CoreFormatTextDirective,
181185
'core-format-text',
182186
{ text: '<a href="https://anchor-url/">Link</a>' },
187+
{ standalone: true },
183188
);
184189
const anchor = nativeElement.querySelector('a');
185190

src/core/directives/tests/link.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('CoreLinkDirective', () => {
2424
const fixture = await renderTemplate(
2525
CoreLinkDirective,
2626
'<a href="https://moodle.org/" core-link [capture]="true">Link</a>',
27+
{ standalone: true },
2728
);
2829

2930
// Assert
@@ -42,6 +43,7 @@ describe('CoreLinkDirective', () => {
4243
const { nativeElement } = await renderTemplate(
4344
CoreLinkDirective,
4445
'<a href="https://moodle.org/" core-link [capture]="true">Link</a>',
46+
{ standalone: true },
4547
);
4648

4749
const anchor = nativeElement.querySelector('a');

src/core/directives/update-non-reactive-attributes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { Directive, ElementRef, OnDestroy, OnInit } from '@angular/core';
2020
*
2121
* This is necessary in order to update some attributes that are not reactive, for example aria-label.
2222
*
23-
* @see https://github.com/ionic-team/ionic-framework/issues/21534
23+
* @see https://github.com/ionic-team/ionic-framework/issues/20127
2424
*/
2525
@Directive({
2626
selector: 'ion-button',

src/core/features/login/pages/credentials/credentials.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ <h1>{{ 'core.login.login' | translate }}</h1>
99
</ion-title>
1010

1111
<ion-buttons slot="end">
12-
<ion-button fill="clear" (click)="openSettings()" [ariaLabel]="'core.settings.appsettings' | translate">
12+
<ion-button fill="clear" (click)="openSettings()" [attr.aria-label]="'core.settings.appsettings' | translate">
1313
<ion-icon slot="icon-only" name="fas-gear" aria-hidden="true" />
1414
</ion-button>
15-
<ion-button fill="clear" (click)="showHelp()" [ariaLabel]="'core.help' | translate">
15+
<ion-button fill="clear" (click)="showHelp()" [attr.aria-label]="'core.help' | translate">
1616
<ion-icon slot="icon-only" name="far-circle-question" aria-hidden="true" />
1717
</ion-button>
1818
</ion-buttons>

src/core/features/login/tests/credentials.test.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { CoreSharedModule } from '@/core/shared.module';
1615
import { findElement, mock, mockSingleton, renderPageComponent, requireElement } from '@/testing/utils';
1716
import { CoreLoginError } from '@classes/errors/loginerror';
1817
import CoreLoginCredentialsPage from '@features/login/pages/credentials/credentials';
@@ -22,9 +21,6 @@ import { Http } from '@singletons';
2221
import { of } from 'rxjs';
2322
import { CoreLoginHelper } from '../services/login-helper';
2423
import { CoreConstants } from '@/core/constants';
25-
import { CoreSiteLogoComponent } from '@components/site-logo/site-logo';
26-
import { CoreLoginExceededAttemptsComponent } from '../components/exceeded-attempts/exceeded-attempts';
27-
import { CoreLoginMethodsComponent } from '../components/login-methods/login-methods';
2824

2925
describe('Credentials page', () => {
3026

@@ -86,12 +82,7 @@ describe('Credentials page', () => {
8682
// Act.
8783
const fixture = await renderPageComponent(CoreLoginCredentialsPage, {
8884
routeParams: { siteUrl },
89-
imports: [
90-
CoreSharedModule,
91-
CoreSiteLogoComponent,
92-
CoreLoginExceededAttemptsComponent,
93-
CoreLoginMethodsComponent,
94-
],
85+
standalone: true,
9586
});
9687

9788
// Assert.
@@ -141,12 +132,7 @@ describe('Credentials page', () => {
141132

142133
const fixture = await renderPageComponent(CoreLoginCredentialsPage, {
143134
routeParams: { siteUrl, siteCheck },
144-
imports: [
145-
CoreSharedModule,
146-
CoreSiteLogoComponent,
147-
CoreLoginExceededAttemptsComponent,
148-
CoreLoginMethodsComponent,
149-
],
135+
standalone: true,
150136
});
151137

152138
// Act.

src/testing/stubs/directives/core-external-content.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Directive, Input } from '@angular/core';
1616

1717
@Directive({
1818
selector: '[core-external-content]',
19+
standalone: true,
1920
})
2021
export class CoreExternalContentDirectiveStub { // eslint-disable-line @angular-eslint/directive-class-suffix
2122

src/testing/utils.ts

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,40 @@ const DEFAULT_SERVICE_SINGLETON_MOCKS: [CoreSingletonProxy, unknown][] = [
8686
* @returns A promise that resolves to the testing component fixture.
8787
*/
8888
async function renderAngularComponent<T>(component: Type<T>, config: RenderConfig): Promise<TestingComponentFixture<T>> {
89-
config.declarations.push(component);
90-
91-
TestBed.configureTestingModule({
92-
declarations: [
93-
...getDefaultDeclarations(),
94-
...config.declarations,
95-
],
96-
providers: [
97-
...getDefaultProviders(config),
98-
...config.providers,
99-
],
100-
schemas: [CUSTOM_ELEMENTS_SCHEMA],
101-
imports: [
102-
BrowserModule,
103-
NoopAnimationsModule,
104-
TranslateModule.forChild(),
105-
...config.imports,
106-
],
107-
});
89+
if (!config.standalone) {
90+
config.declarations.push(component);
91+
92+
TestBed.configureTestingModule({
93+
declarations: [
94+
...config.declarations,
95+
],
96+
providers: [
97+
...getDefaultProviders(config),
98+
...config.providers,
99+
],
100+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
101+
imports: [
102+
BrowserModule,
103+
NoopAnimationsModule,
104+
TranslateModule.forChild(),
105+
CoreExternalContentDirectiveStub,
106+
...config.imports,
107+
],
108+
});
109+
} else {
110+
TestBed.configureTestingModule({
111+
providers: [
112+
...getDefaultProviders(config),
113+
...config.providers,
114+
],
115+
imports: [
116+
component,
117+
NoopAnimationsModule,
118+
CoreExternalContentDirectiveStub,
119+
...config.imports,
120+
],
121+
});
122+
}
108123

109124
testBedInitialized = true;
110125

@@ -128,7 +143,13 @@ async function renderAngularComponent<T>(component: Type<T>, config: RenderConfi
128143
* @returns The wrapper component class.
129144
*/
130145
function createWrapperComponent<U>(template: string, componentClass: Type<U>): Type<WrapperComponent<U>> {
131-
@Component({ template })
146+
@Component({
147+
template,
148+
standalone: true,
149+
imports: [
150+
componentClass,
151+
],
152+
})
132153
class HostComponent extends WrapperComponent<U> {
133154

134155
@ViewChild(componentClass) child!: U;
@@ -138,17 +159,6 @@ function createWrapperComponent<U>(template: string, componentClass: Type<U>): T
138159
return HostComponent;
139160
}
140161

141-
/**
142-
* Gets the default declarations for testing.
143-
*
144-
* @returns An array of default declarations.
145-
*/
146-
function getDefaultDeclarations(): unknown[] {
147-
return [
148-
CoreExternalContentDirectiveStub,
149-
];
150-
}
151-
152162
/**
153163
* Gets the default providers for testing.
154164
*
@@ -217,6 +227,7 @@ export interface RenderConfig {
217227
providers: unknown[];
218228
imports: unknown[];
219229
translations?: Record<string, string>;
230+
standalone?: boolean;
220231
}
221232

222233
export interface RenderPageConfig extends RenderConfig {
@@ -447,8 +458,10 @@ export async function renderTemplate<T>(
447458
template: string,
448459
config: Partial<RenderConfig> = {},
449460
): Promise<WrapperComponentFixture<T>> {
450-
config.declarations = config.declarations ?? [];
451-
config.declarations.push(component);
461+
if (!config.standalone) {
462+
config.declarations = config.declarations ?? [];
463+
config.declarations.push(component);
464+
}
452465

453466
return renderAngularComponent(
454467
createWrapperComponent(template, component),

0 commit comments

Comments
 (0)