Skip to content

Commit

Permalink
fix(core): respecting transform in mock pipes help-me-mom#4564
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Dec 19, 2022
1 parent a12f089 commit d80267b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
3 changes: 2 additions & 1 deletion libs/ng-mocks/src/lib/common/mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter, Injector, Optional, Self } from '@angular/core';
import { EventEmitter, Injector, Optional, PipeTransform, Self } from '@angular/core';

import { IMockBuilderConfig } from '../mock-builder/types';
import mockHelperStub from '../mock-helper/mock-helper.stub';
Expand Down Expand Up @@ -116,6 +116,7 @@ export type ngMocksMockConfig = {
outputs?: string[];
queryScanKeys?: string[];
setControlValueAccessor?: boolean;
transform?: PipeTransform['transform'];
};

const applyOverrides = (instance: any, mockOf: any, injector?: Injector): void => {
Expand Down
11 changes: 7 additions & 4 deletions libs/ng-mocks/src/lib/common/ng-mocks-global-overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ const configureTestingModule =
// 0b10 - mock exist
// 0b01 - real exist
let hasMocks = 0;
const mockBuilder: Array<[any, boolean]> = [];
const mockBuilder: Array<[any, any, boolean]> = [];
for (const key of useMockBuilder ? ['imports', 'declarations'] : []) {
for (const declaration of flatten(moduleDef[key as never]) as any[]) {
if (!declaration) {
Expand All @@ -205,19 +205,22 @@ const configureTestingModule =
providers: declaration.providers,
}
: getSourceOfMock(declaration),
isNgModuleDefWithProviders(declaration) ? declaration.ngModule : declaration,
isMockNgDef(funcGetType(declaration)),
]);
if (key === 'imports') {
hasMocks |= mockBuilder[mockBuilder.length - 1][1] ? 0b10 : 0b01;
hasMocks |= mockBuilder[mockBuilder.length - 1][2] ? 0b10 : 0b01;
}
}
}
// We should do magic only then both mock and real exist.
let finalModuleDef = hasMocks === 0b11 ? undefined : moduleDef;
if (!finalModuleDef) {
let builder = MockBuilder(NG_MOCKS_ROOT_PROVIDERS);
for (const [def, isMock] of mockBuilder) {
builder = isMock ? builder.mock(def) : builder.keep(def);
for (const [source, def, isMock] of mockBuilder) {
const transform = def.prototype.__ngMocksConfig?.transform;
builder =
isMock && transform ? builder.mock(source, transform) : isMock ? builder.mock(source) : builder.keep(source);
}
finalModuleDef = builder.build();
finalModuleDef = {
Expand Down
1 change: 1 addition & 0 deletions libs/ng-mocks/src/lib/mock-pipe/mock-pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const getMockClass = (pipe: Type<any>, transform?: PipeTransform['transform']):
helperMockService.mock(instance, 'transform', `${funcGetName(instance)}.transform`);
}
},
transform,
});

return mock;
Expand Down
65 changes: 65 additions & 0 deletions tests/issue-4564/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
Component,
NgModule,
Pipe,
PipeTransform,
} from '@angular/core';
import { TestBed } from '@angular/core/testing';

import { MockModule, MockPipe, MockRender, ngMocks } from 'ng-mocks';

@Pipe({
name: 'target',
})
class TargetPipe implements PipeTransform {
transform() {
return 'real';
}
}

@Pipe({
name: 'standard',
})
class StandardPipe implements PipeTransform {
transform() {
return 'standard';
}
}

@NgModule({
declarations: [TargetPipe, StandardPipe],
exports: [TargetPipe, StandardPipe],
})
class PipeModule {}

@Component({
selector: 'target',
template: '<span> {{ null | target }}</span>',
})
class TargetComponent {}

@NgModule({
imports: [PipeModule],
declarations: [TargetComponent],
exports: [TargetComponent],
})
class ComponentModule {}

// @see https://github.com/help-me-mom/ng-mocks/issues/4564
// mixed imports forget pipe customizations.
describe('issue-4564', () => {
beforeEach(() =>
TestBed.configureTestingModule({
imports: [ComponentModule, MockModule(PipeModule)],
declarations: [
MockPipe(TargetPipe, () => 'mock'),
MockPipe(StandardPipe),
],
}).compileComponents(),
);

it('customizes pipe', () => {
const fixture = MockRender(TargetComponent);
expect(ngMocks.formatText(fixture)).toEqual('mock');
});
});

0 comments on commit d80267b

Please sign in to comment.