Fix tests and remove obsolete ones

This commit is contained in:
Valentin Palkovic 2023-02-16 18:14:16 +01:00
parent deedfae482
commit aa95dc47f0
4 changed files with 12 additions and 125 deletions

View File

@ -1,17 +1,6 @@
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({
imports: [
//
BrowserModule,
BrowserAnimationsModule,
],
})
export class WithAnimationsModule {}
@NgModule({
imports: [CommonModule, HttpClientModule],

View File

@ -7,10 +7,9 @@ import {
provideAnimations,
provideNoopAnimations,
} from '@angular/platform-browser/animations';
import { provideHttpClient } from '@angular/common/http';
import { NgModuleMetadata } from '../../types';
import { PropertyExtractor, REMOVED_MODULES } from './PropertyExtractor';
import { WithAnimationsModule, WithOfficialModule } from '../__testfixtures__/test.module';
import { WithOfficialModule } from '../__testfixtures__/test.module';
const TEST_TOKEN = new InjectionToken('testToken');
const TestTokenProvider = { provide: TEST_TOKEN, useValue: 123 };
@ -79,13 +78,11 @@ describe('PropertyExtractor', () => {
it('should remove Browser/Animations modules recursively', () => {
const metadata = {
imports: [WithAnimationsModule],
imports: [BrowserAnimationsModule, BrowserModule],
};
const { imports, providers, singletons } = analyzeMetadata(metadata);
expect(imports.flat(Number.MAX_VALUE)).toEqual([CommonModule]);
expect(providers.flat(Number.MAX_VALUE)).toEqual([
{ provide: REMOVED_MODULES, useValue: WithAnimationsModule, multi: true },
]);
expect(providers.flat(Number.MAX_VALUE)).toEqual([]);
expect(singletons.flat(Number.MAX_VALUE)).toEqual(provideAnimations());
});
@ -94,18 +91,16 @@ describe('PropertyExtractor', () => {
imports: [WithOfficialModule],
};
const { imports, providers, singletons } = analyzeMetadata(metadata);
expect(imports.flat(Number.MAX_VALUE)).toEqual([CommonModule]);
expect(providers.flat(Number.MAX_VALUE)).toEqual([
{ provide: REMOVED_MODULES, useValue: WithOfficialModule, multi: true },
]);
expect(singletons.flat(Number.MAX_VALUE)).toEqual([provideHttpClient()]);
expect(imports.flat(Number.MAX_VALUE)).toEqual([CommonModule, WithOfficialModule]);
expect(providers.flat(Number.MAX_VALUE)).toEqual([]);
expect(singletons.flat(Number.MAX_VALUE)).toEqual([]);
});
});
describe('extractImports', () => {
it('should return Angular official modules', () => {
const imports = extractImports({ imports: [TestModuleWithImportsAndProviders] });
expect(imports).toEqual([CommonModule]);
expect(imports).toEqual([CommonModule, TestModuleWithImportsAndProviders]);
});
it('should return standalone components', () => {
@ -115,17 +110,11 @@ describe('PropertyExtractor', () => {
},
StandaloneTestComponent
);
expect(imports).toEqual([CommonModule, StandaloneTestComponent]);
});
it('should not return any regular modules (they get destructured)', () => {
const imports = extractImports({
imports: [
TestModuleWithDeclarations,
{ ngModule: TestModuleWithImportsAndProviders, providers: [] },
],
});
expect(imports).toEqual([CommonModule]);
expect(imports).toEqual([
CommonModule,
TestModuleWithImportsAndProviders,
StandaloneTestComponent,
]);
});
});
@ -134,44 +123,6 @@ describe('PropertyExtractor', () => {
const declarations = extractDeclarations({ declarations: [TestComponent1] }, TestComponent2);
expect(declarations).toEqual([TestComponent1, TestComponent2]);
});
it('should ignore `storyComponent` if it is already declared', () => {
const declarations = extractDeclarations(
{
imports: [TestModuleWithImportsAndProviders],
declarations: [TestComponent2, StandaloneTestComponent, TestDirective],
},
TestComponent1
);
expect(declarations).toEqual([
TestComponent1,
TestComponent2,
StandaloneTestComponent,
TestDirective,
]);
});
it('should ignore `storyComponent` if it is standalone', () => {
const declarations = extractDeclarations(
{
imports: [TestModuleWithImportsAndProviders],
declarations: [TestComponent1, TestComponent2, TestDirective],
},
StandaloneTestComponent
);
expect(declarations).toEqual([TestComponent1, TestComponent2, TestDirective]);
});
it('should ignore `storyComponent` if it is not a component/directive/pipe', () => {
const declarations = extractDeclarations(
{
imports: [TestModuleWithImportsAndProviders],
declarations: [TestComponent1, TestComponent2, StandaloneTestComponent],
},
TestService
);
expect(declarations).toEqual([TestComponent1, TestComponent2, StandaloneTestComponent]);
});
});
describe('extractProviders', () => {
@ -182,39 +133,6 @@ describe('PropertyExtractor', () => {
expect(providers).toEqual([TestService]);
});
it('should return an array of providers extracted from ModuleWithProviders', () => {
const providers = extractProviders({
imports: [{ ngModule: TestModuleWithImportsAndProviders, providers: [TestService] }],
});
expect(providers).toEqual([
{ provide: TEST_TOKEN, useValue: 123 },
{ provide: REMOVED_MODULES, useValue: TestModuleWithDeclarations, multi: true },
TestService,
{ provide: REMOVED_MODULES, useValue: TestModuleWithImportsAndProviders, multi: true },
]);
});
it('should return an array of unique providers', () => {
const providers = extractProviders({
imports: [{ ngModule: TestModuleWithImportsAndProviders, providers: [TestService] }],
providers: [TestService, { provide: TEST_TOKEN, useValue: 123 }],
});
expect(providers).toEqual([
{ provide: TEST_TOKEN, useValue: 123 },
{ provide: REMOVED_MODULES, useValue: TestModuleWithDeclarations, multi: true },
TestService,
{
provide: new InjectionToken('testToken'),
useValue: 123,
},
{
provide: REMOVED_MODULES,
useValue: TestModuleWithImportsAndProviders,
multi: true,
},
]);
});
it('should return an array of singletons extracted', () => {
const singeltons = extractSingletons({
imports: [BrowserAnimationsModule],

View File

@ -1,5 +1,4 @@
import { CommonModule } from '@angular/common';
import { HttpClientModule, provideHttpClient } from '@angular/common/http';
import {
Component,
Directive,
@ -124,13 +123,6 @@ export class PropertyExtractor implements NgModuleMetadata {
return [true, provideNoopAnimations()];
}
/**
* HttpClient has to be provided manually as a singleton
*/
if (ngModule === HttpClientModule) {
return [true, provideHttpClient()];
}
return [false];
};

View File

@ -3395,18 +3395,6 @@ __metadata:
languageName: node
linkType: hard
"@ngxs/store@npm:^3.7.6":
version: 3.7.6
resolution: "@ngxs/store@npm:3.7.6"
dependencies:
tslib: ^1.9.0
peerDependencies:
"@angular/core": ">=6.1.0 <16.0.0"
rxjs: ">=6.5.5"
checksum: 38e9c0e9830712e9dfa0de6c0226e16fd5cf0cf9960d0eb1ebf67f35228225685536be12c920c0755298cc0e95f2eb673ede91f23ed78d8f0cdde04ae3c51cdd
languageName: node
linkType: hard
"@nodelib/fs.scandir@npm:2.1.5":
version: 2.1.5
resolution: "@nodelib/fs.scandir@npm:2.1.5"