Skip to content

Commit 4a98951

Browse files
Copilotrenemadsen
andcommitted
Add TranslateModule to all component specs to fix translate pipe errors
Co-authored-by: renemadsen <[email protected]>
1 parent 9d50cf2 commit 4a98951

File tree

6 files changed

+44
-14
lines changed

6 files changed

+44
-14
lines changed

TEST_FIXES.md

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,79 @@ The Angular unit tests were failing with errors like:
1010
These errors occurred because Angular's test compiler was trying to compile the component templates and couldn't find the declarations for various directives and pipes used in the templates (like Material components, translate pipe, mtx-grid, etc.).
1111

1212
## Solution
13-
Added `NO_ERRORS_SCHEMA` to all component test specs. This schema tells Angular to ignore unknown elements and attributes during template compilation, which is appropriate for unit tests that focus on component logic rather than template rendering.
13+
Added `NO_ERRORS_SCHEMA` to all component test specs to ignore unknown elements and attributes, AND imported `TranslateModule` to provide the `translate` pipe.
1414

15-
`NO_ERRORS_SCHEMA` is a testing best practice for unit tests because:
16-
1. **Minimal changes** - Only requires adding one line to each test configuration
17-
2. **Isolation** - Unit tests should focus on component logic, not template rendering
18-
3. **Simplicity** - Avoids importing dozens of Angular Material and other modules
19-
4. **Performance** - Tests run faster without compiling full module trees
15+
**Important Note:** `NO_ERRORS_SCHEMA` only ignores unknown elements and attributes - it does NOT ignore missing pipes. Since all component templates use the `translate` pipe, we must import `TranslateModule.forRoot()` in the test configuration.
16+
17+
This combined approach is appropriate for unit tests because:
18+
1. **Focus on Logic** - Unit tests should test component logic, not template rendering
19+
2. **Minimal Changes** - Only requires adding imports and one line to config
20+
3. **Performance** - Tests run faster without compiling full module trees for Material components
21+
4. **Handles Pipes** - TranslateModule provides the translate pipe that NO_ERRORS_SCHEMA cannot ignore
2022
5. **Maintenance** - Less brittle when templates change
2123

2224
## Files Modified
2325

2426
1. **time-plannings-table.component.spec.ts**
2527
- Added `NO_ERRORS_SCHEMA` import
28+
- Added `TranslateModule` import
2629
- Added `schemas: [NO_ERRORS_SCHEMA]` to TestBed configuration
30+
- Added `TranslateModule.forRoot()` to imports array
2731

2832
2. **time-plannings-container.component.spec.ts**
2933
- Added `NO_ERRORS_SCHEMA` import
34+
- Added `TranslateModule` import
3035
- Added `schemas: [NO_ERRORS_SCHEMA]` to TestBed configuration
36+
- Added `TranslateModule.forRoot()` to imports array
3137

3238
3. **assigned-site-dialog.component.spec.ts**
3339
- Added `NO_ERRORS_SCHEMA` import
40+
- Added `TranslateModule` import
3441
- Added `schemas: [NO_ERRORS_SCHEMA]` to TestBed configuration
42+
- Added `TranslateModule.forRoot()` to imports array
3543

3644
4. **workday-entity-dialog.component.spec.ts**
3745
- Added `NO_ERRORS_SCHEMA` import
46+
- Added `TranslateModule` import
3847
- Added `schemas: [NO_ERRORS_SCHEMA]` to TestBed configuration
48+
- Added `TranslateModule.forRoot()` to imports array
3949

4050
5. **download-excel-dialog.component.spec.ts**
4151
- Added `NO_ERRORS_SCHEMA` import
52+
- Added `TranslateModule` import
4253
- Added `schemas: [NO_ERRORS_SCHEMA]` to TestBed configuration
54+
- Added `TranslateModule.forRoot()` to imports array
4355

4456
## Change Summary
4557
- **5 files changed**
46-
- **10 insertions(+)**, **1 deletion(-)**
47-
- All changes are additive and minimal
58+
- **15 insertions(+)**, **5 deletions(-)**
59+
- All changes are minimal and focused
4860

4961
## Testing
5062
To run the tests, use the command specified in the issue:
5163
```bash
5264
npm run test:unit -- --testPathPatterns=time-planning-pn --coverage --collectCoverageFrom='src/app/plugins/modules/time-planning-pn/**/*.ts' --coveragePathIgnorePatterns='\.spec\.ts'
5365
```
5466

55-
## Alternative Approach (Not Used)
56-
The alternative would have been to import all the required modules in each test:
57-
- `TranslateModule.forRoot()`
67+
## Why This Approach Works
68+
69+
### NO_ERRORS_SCHEMA handles:
70+
- Unknown elements (mat-tab, mat-form-field, mat-button, etc.)
71+
- Unknown attributes (matTooltip, matStartDate, etc.)
72+
- Unknown components (mtx-grid, mtx-select, etc.)
73+
74+
### TranslateModule handles:
75+
- The `translate` pipe used throughout templates
76+
- Provides actual pipe implementation for template compilation
77+
78+
### Alternative Approach (Not Used)
79+
The alternative would have been to import all the required modules:
5880
- All Material modules (MatTabModule, MatFormFieldModule, MatInputModule, etc.)
5981
- MtxGridModule
6082
- Other dependencies
6183

6284
This approach was rejected because:
63-
- Much more invasive changes
85+
- Much more invasive changes (20+ module imports per test)
6486
- Harder to maintain
6587
- Slower test execution
6688
- Not appropriate for unit tests (better suited for integration tests)

eform-client/src/app/plugins/modules/time-planning-pn/components/plannings/time-planning-actions/assigned-site/assigned-site-dialog.component.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { TimePlanningPnSettingsService } from '../../../../services';
66
import { Store } from '@ngrx/store';
77
import { of } from 'rxjs';
88
import { NO_ERRORS_SCHEMA } from '@angular/core';
9+
import { TranslateModule } from '@ngx-translate/core';
910

1011
describe('AssignedSiteDialogComponent', () => {
1112
let component: AssignedSiteDialogComponent;
@@ -67,7 +68,7 @@ describe('AssignedSiteDialogComponent', () => {
6768

6869
await TestBed.configureTestingModule({
6970
declarations: [AssignedSiteDialogComponent],
70-
imports: [ReactiveFormsModule],
71+
imports: [ReactiveFormsModule, TranslateModule.forRoot()],
7172
schemas: [NO_ERRORS_SCHEMA],
7273
providers: [
7374
FormBuilder,

eform-client/src/app/plugins/modules/time-planning-pn/components/plannings/time-planning-actions/download-excel/download-excel-dialog.component.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ToastrService } from 'ngx-toastr';
66
import { of, throwError } from 'rxjs';
77
import { format } from 'date-fns';
88
import { NO_ERRORS_SCHEMA } from '@angular/core';
9+
import { TranslateModule } from '@ngx-translate/core';
910

1011
describe('DownloadExcelDialogComponent', () => {
1112
let component: DownloadExcelDialogComponent;
@@ -25,6 +26,7 @@ describe('DownloadExcelDialogComponent', () => {
2526

2627
await TestBed.configureTestingModule({
2728
declarations: [DownloadExcelDialogComponent],
29+
imports: [TranslateModule.forRoot()],
2830
schemas: [NO_ERRORS_SCHEMA],
2931
providers: [
3032
{ provide: MAT_DIALOG_DATA, useValue: [] },

eform-client/src/app/plugins/modules/time-planning-pn/components/plannings/time-planning-actions/workday-entity/workday-entity-dialog.component.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { TranslateService } from '@ngx-translate/core';
77
import { DatePipe } from '@angular/common';
88
import { of } from 'rxjs';
99
import { NO_ERRORS_SCHEMA } from '@angular/core';
10+
import { TranslateModule } from '@ngx-translate/core';
1011

1112
describe('WorkdayEntityDialogComponent', () => {
1213
let component: WorkdayEntityDialogComponent;
@@ -97,7 +98,7 @@ describe('WorkdayEntityDialogComponent', () => {
9798

9899
await TestBed.configureTestingModule({
99100
declarations: [WorkdayEntityDialogComponent],
100-
imports: [ReactiveFormsModule],
101+
imports: [ReactiveFormsModule, TranslateModule.forRoot()],
101102
schemas: [NO_ERRORS_SCHEMA],
102103
providers: [
103104
FormBuilder,

eform-client/src/app/plugins/modules/time-planning-pn/components/plannings/time-plannings-container/time-plannings-container.component.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Store } from '@ngrx/store';
77
import { of } from 'rxjs';
88
import { format } from 'date-fns';
99
import { NO_ERRORS_SCHEMA } from '@angular/core';
10+
import { TranslateModule } from '@ngx-translate/core';
1011

1112
describe('TimePlanningsContainerComponent', () => {
1213
let component: TimePlanningsContainerComponent;
@@ -40,6 +41,7 @@ describe('TimePlanningsContainerComponent', () => {
4041

4142
await TestBed.configureTestingModule({
4243
declarations: [TimePlanningsContainerComponent],
44+
imports: [TranslateModule.forRoot()],
4345
schemas: [NO_ERRORS_SCHEMA],
4446
providers: [
4547
{ provide: TimePlanningPnPlanningsService, useValue: mockPlanningsService },

eform-client/src/app/plugins/modules/time-planning-pn/components/plannings/time-plannings-table/time-plannings-table.component.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DatePipe } from '@angular/common';
88
import { ChangeDetectorRef, NO_ERRORS_SCHEMA } from '@angular/core';
99
import { Store } from '@ngrx/store';
1010
import { of } from 'rxjs';
11+
import { TranslateModule } from '@ngx-translate/core';
1112

1213
describe('TimePlanningsTableComponent', () => {
1314
let component: TimePlanningsTableComponent;
@@ -45,6 +46,7 @@ describe('TimePlanningsTableComponent', () => {
4546

4647
await TestBed.configureTestingModule({
4748
declarations: [TimePlanningsTableComponent],
49+
imports: [TranslateModule.forRoot()],
4850
schemas: [NO_ERRORS_SCHEMA],
4951
providers: [
5052
{ provide: TimePlanningPnPlanningsService, useValue: mockPlanningsService },

0 commit comments

Comments
 (0)