Skip to content

Commit

Permalink
Migrate away from modules in tests
Browse files Browse the repository at this point in the history
Instead, use providers, and avoid `imports` in
`configureTestingModule()` even for the component being tested, because
our code is exclusively standalone.
  • Loading branch information
PowerKiKi committed Jun 12, 2024
1 parent 79420e8 commit b550025
Show file tree
Hide file tree
Showing 31 changed files with 74 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {Component} from '@angular/core';
import {NaturalCustomCssDirective, prefixCss} from './custom-css.directive';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {DOCUMENT} from '@angular/common';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {By} from '@angular/platform-browser';

@Component({
Expand All @@ -22,9 +21,7 @@ describe('NaturalLinkableTabDirective', () => {
let document: Document;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [NoopAnimationsModule],
}).compileComponents();
await TestBed.configureTestingModule({}).compileComponents();

fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {TestBed} from '@angular/core/testing';
import {MockApolloProvider} from '../testing/mock-apollo.provider';
import {ItemService, Item} from '../testing/item.service';
import {NaturalAbstractEditableList} from '@ecodev/natural';
import {RouterTestingModule} from '@angular/router/testing';
import {Directive} from '@angular/core';

@Directive()
Expand All @@ -15,7 +14,6 @@ describe('NaturalAbstractEditableList', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [MockApolloProvider],
imports: [RouterTestingModule],
});

service = TestBed.inject(ItemService);
Expand Down
2 changes: 0 additions & 2 deletions projects/natural/src/lib/classes/abstract-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from '@ecodev/natural';
import {ItemService} from '../testing/item.service';
import {Component} from '@angular/core';
import {RouterTestingModule} from '@angular/router/testing';
import {ActivatedRoute, Data} from '@angular/router';
import {MockApolloProvider} from '../testing/mock-apollo.provider';

Expand Down Expand Up @@ -74,7 +73,6 @@ describe('NaturalAbstractList', () => {
} as any;

TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [
naturalProviders,
MockApolloProvider,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {MAT_DATE_LOCALE, MatNativeDateModule} from '@angular/material/core';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {MAT_DATE_LOCALE, provideNativeDateAdapter} from '@angular/material/core';
import {
FilterGroupConditionField,
NATURAL_DROPDOWN_DATA,
NaturalDropdownData,
TypeDateRangeComponent,
TypeDateRangeConfiguration,
} from '@ecodev/natural';
import {provideNoopAnimations} from '@angular/platform-browser/animations';

describe('TypeDateRangeComponent', () => {
let component: TypeDateRangeComponent;
Expand Down Expand Up @@ -38,8 +38,9 @@ describe('TypeDateRangeComponent', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [NoopAnimationsModule, MatNativeDateModule],
providers: [
provideNativeDateAdapter(),
provideNoopAnimations(),
{
provide: NATURAL_DROPDOWN_DATA,
useValue: data,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {DateAdapter, MAT_DATE_LOCALE, MatNativeDateModule, NativeDateAdapter} from '@angular/material/core';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {DateAdapter, MAT_DATE_LOCALE, NativeDateAdapter, provideNativeDateAdapter} from '@angular/material/core';
import {
FilterGroupConditionField,
NATURAL_DROPDOWN_DATA,
Expand All @@ -9,6 +8,7 @@ import {
TypeDateConfiguration,
} from '@ecodev/natural';
import {Injectable} from '@angular/core';
import {provideNoopAnimations} from '@angular/platform-browser/animations';

@Injectable({providedIn: 'root'})
class ImpossibleParsingDateAdapter extends NativeDateAdapter {
Expand Down Expand Up @@ -86,8 +86,9 @@ describe('TypeDateComponent', () => {

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [NoopAnimationsModule, MatNativeDateModule],
providers: [
provideNativeDateAdapter(),
provideNoopAnimations(),
{
provide: NATURAL_DROPDOWN_DATA,
useValue: data,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {
FilterGroupConditionField,
NATURAL_DROPDOWN_DATA,
Expand All @@ -8,6 +7,7 @@ import {
TypeNumberComponent,
TypeNumberConfiguration,
} from '@ecodev/natural';
import {provideNoopAnimations} from '@angular/platform-browser/animations';

describe('TypeNumberComponent', () => {
let component: TypeNumberComponent;
Expand Down Expand Up @@ -42,8 +42,8 @@ describe('TypeNumberComponent', () => {
dialogCloseSpy = spyOn(dialogRef, 'close');

await TestBed.configureTestingModule({
imports: [NoopAnimationsModule],
providers: [
provideNoopAnimations(),
{
provide: NATURAL_DROPDOWN_DATA,
useValue: data,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {
FilterGroupConditionField,
NATURAL_DROPDOWN_DATA,
NaturalDropdownData,
NaturalDropdownRef,
} from '@ecodev/natural';
import {TypeTextComponent} from './type-text.component';
import {provideNoopAnimations} from '@angular/platform-browser/animations';

describe('TypeTextComponent', () => {
let component: TypeTextComponent;
Expand All @@ -26,8 +26,8 @@ describe('TypeTextComponent', () => {
dialogCloseSpy = spyOn(dialogRef, 'close');

await TestBed.configureTestingModule({
imports: [NoopAnimationsModule],
providers: [
provideNoopAnimations(),
{
provide: NATURAL_DROPDOWN_DATA,
useValue: data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {NaturalFixedButtonDetailComponent} from './fixed-button-detail.component
import {ActivatedRoute} from '@angular/router';
import {Subject} from 'rxjs';
import {FormGroup} from '@angular/forms';
import {RouterTestingModule} from '@angular/router/testing';

describe('NaturalFixedButtonDetailComponent', () => {
let component: NaturalFixedButtonDetailComponent;
Expand All @@ -16,7 +15,6 @@ describe('NaturalFixedButtonDetailComponent', () => {
};

await TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [
{
provide: ActivatedRoute,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {RouterTestingModule} from '@angular/router/testing';
import {NaturalFixedButtonComponent} from './fixed-button.component';
import {provideRouter} from '@angular/router';

describe('FixedButtonComponent', () => {
let component: NaturalFixedButtonComponent;
let fixture: ComponentFixture<NaturalFixedButtonComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [provideRouter([])],
}).compileComponents();
fixture = TestBed.createComponent(NaturalFixedButtonComponent);
component = fixture.componentInstance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {OverlayContainer} from '@angular/cdk/overlay';
import {fakeAsync, flush, TestBed} from '@angular/core/testing';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {
HierarchicDialogConfig,
HierarchicDialogResult,
Expand All @@ -15,7 +14,6 @@ describe('NaturalHierarchicSelectorDialogService', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [NoopAnimationsModule],
providers: [naturalProviders],
});
dialog = TestBed.inject(NaturalHierarchicSelectorDialogService);
Expand Down
6 changes: 4 additions & 2 deletions projects/natural/src/lib/modules/icon/icon.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
import {Component, DebugElement} from '@angular/core';
import {By} from '@angular/platform-browser';
import {MatIconModule} from '@angular/material/icon';
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
import {HttpTestingController, provideHttpClientTesting} from '@angular/common/http/testing';
import {NaturalIconDirective, provideIcons} from '@ecodev/natural';
import {provideHttpClient} from '@angular/common/http';

@Component({
template: `
Expand Down Expand Up @@ -37,8 +38,9 @@ describe('NaturalIconComponent', () => {

beforeEach(() => {
fixture = TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
provideHttpClient(),
provideHttpClientTesting(),
provideIcons({
customFontName: {font: 'download'},
customSvgName: {svg: 'foo.svg'},
Expand Down
27 changes: 15 additions & 12 deletions projects/natural/src/lib/modules/logger/error-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
NaturalLoggerType,
provideErrorHandler,
} from '@ecodev/natural';
import {HttpClientTestingModule, HttpTestingController, RequestMatch} from '@angular/common/http/testing';
import {HttpTestingController, provideHttpClientTesting, RequestMatch} from '@angular/common/http/testing';
import {Observable, of, throwError} from 'rxjs';
import {HttpClient, HttpClientModule} from '@angular/common/http';
import {HttpClient, provideHttpClient} from '@angular/common/http';

const expectedRequest: RequestMatch = {
url: 'http://example.com',
Expand Down Expand Up @@ -38,8 +38,7 @@ describe('NaturalErrorHandler', () => {
describe('with empty configuration', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [provideErrorHandler(null)],
providers: [provideHttpClient(), provideHttpClientTesting(), provideErrorHandler(null)],
});

httpTestingController = TestBed.inject(HttpTestingController);
Expand Down Expand Up @@ -70,8 +69,7 @@ describe('NaturalErrorHandler', () => {
describe('with minimal configuration', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [provideErrorHandler('http://example.com')],
providers: [provideHttpClient(), provideHttpClientTesting(), provideErrorHandler('http://example.com')],
});

httpTestingController = TestBed.inject(HttpTestingController);
Expand Down Expand Up @@ -112,8 +110,11 @@ describe('NaturalErrorHandler', () => {
describe('with maximal configuration', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [provideErrorHandler('http://example.com', Extra)],
providers: [
provideHttpClient(),
provideHttpClientTesting(),
provideErrorHandler('http://example.com', Extra),
],
});

httpTestingController = TestBed.inject(HttpTestingController);
Expand Down Expand Up @@ -167,8 +168,11 @@ describe('NaturalErrorHandler', () => {
describe('with extra producer error configuration', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [provideErrorHandler('http://example.com', ExtraError)],
providers: [
provideHttpClient(),
provideHttpClientTesting(),
provideErrorHandler('http://example.com', ExtraError),
],
});

httpTestingController = TestBed.inject(HttpTestingController);
Expand Down Expand Up @@ -205,8 +209,7 @@ describe('NaturalErrorHandler bis', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule],
providers: [provideErrorHandler('http://example.com', ExtraError)],
providers: [provideHttpClient(), provideErrorHandler('http://example.com', ExtraError)],
});

const httpClient = TestBed.inject(HttpClient);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {RouterTestingModule} from '@angular/router/testing';
import {NaturalPanelsComponent} from './panels.component';
import {providePanels} from './panels.module';
import {naturalProviders} from '@ecodev/natural';
import {provideRouter} from '@angular/router';

describe('PanelComponent', () => {
let component: NaturalPanelsComponent;
let fixture: ComponentFixture<NaturalPanelsComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [providePanels({}), naturalProviders],
providers: [provideRouter([]), providePanels({}), naturalProviders],
}).compileComponents();
fixture = TestBed.createComponent(NaturalPanelsComponent);
component = fixture.componentInstance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {
NATURAL_DROPDOWN_CONTAINER_DATA,
NaturalDropdownContainerComponent,
NaturalDropdownContainerData,
} from './dropdown-container.component';
import {provideNoopAnimations} from '@angular/platform-browser/animations';

describe('NaturalDropdownContainerComponent', () => {
let component: NaturalDropdownContainerComponent;
let fixture: ComponentFixture<NaturalDropdownContainerComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [NoopAnimationsModule],
providers: [
provideNoopAnimations(),
{
provide: NATURAL_DROPDOWN_CONTAINER_DATA,
useValue: {showValidateButton: false} satisfies NaturalDropdownContainerData,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {NaturalGroupComponent} from './group.component';
import {provideNoopAnimations} from '@angular/platform-browser/animations';

describe('GroupComponent', () => {
let component: NaturalGroupComponent;
let fixture: ComponentFixture<NaturalGroupComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [NoopAnimationsModule],
providers: [provideNoopAnimations()],
}).compileComponents();
fixture = TestBed.createComponent(NaturalGroupComponent);
component = fixture.componentInstance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {NaturalInputComponent} from './input.component';
import {provideNoopAnimations} from '@angular/platform-browser/animations';

describe('NaturalInputComponent', () => {
let component: NaturalInputComponent;
let fixture: ComponentFixture<NaturalInputComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [NoopAnimationsModule],
providers: [provideNoopAnimations()],
}).compileComponents();
fixture = TestBed.createComponent(NaturalInputComponent);
component = fixture.componentInstance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {NaturalSearchComponent} from './search.component';
import {provideNoopAnimations} from '@angular/platform-browser/animations';

describe('NaturalSearchComponent', () => {
let component: NaturalSearchComponent;
let fixture: ComponentFixture<NaturalSearchComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [NoopAnimationsModule],
providers: [provideNoopAnimations()],
}).compileComponents();
fixture = TestBed.createComponent(NaturalSearchComponent);
component = fixture.componentInstance;
Expand Down
Loading

0 comments on commit b550025

Please sign in to comment.