Skip to content

Commit

Permalink
fix(MockBuilder): respects pipe-transform in early mocks #5239
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Mar 26, 2023
1 parent fac29e3 commit 979d42b
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default (mockDef: Set<any>, defValue: Map<any, any>): void => {
const deleteTouch = !ngMocksUniverse.touches.has(def);

resolutions.set(def, 'mock');
tryMockDeclaration(def, defValue);
tryMockDeclaration(def);
tryMockProvider(def, defValue);

if (deleteTouch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default ({
ngMocksUniverse.config.set(k, {
...ngMocksUniverse.getConfigMock().get(k),
...v,
defValue: defValue.get(k),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ import { MockComponent } from '../../mock-component/mock-component';
import { MockDirective } from '../../mock-directive/mock-directive';
import { MockPipe } from '../../mock-pipe/mock-pipe';

export default (def: any, defValue: Map<any, any>): void => {
export default (def: any): void => {
if (isNgDef(def, 'c')) {
ngMocksUniverse.builtDeclarations.set(def, MockComponent(def));
}
if (isNgDef(def, 'd')) {
ngMocksUniverse.builtDeclarations.set(def, MockDirective(def));
}
if (isNgDef(def, 'p')) {
const instance = defValue.get(def);
ngMocksUniverse.builtDeclarations.set(
def,
typeof instance?.transform === 'function' ? MockPipe(def, instance.transform) : MockPipe(def),
);
ngMocksUniverse.builtDeclarations.set(def, MockPipe(def));
}
};
4 changes: 3 additions & 1 deletion libs/ng-mocks/src/lib/mock-pipe/mock-pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export function MockPipes(...pipes: Array<Type<PipeTransform>>): Array<Type<Pipe
return pipes.map(pipe => MockPipe(pipe, undefined));
}

const getMockClass = (pipe: Type<any>, transform?: PipeTransform['transform']): Type<any> => {
const getMockClass = (pipe: Type<any>, transformValue?: PipeTransform['transform']): Type<any> => {
const config = ngMocksUniverse.config.get(pipe);
const transform = transformValue ?? config?.defValue?.transform;
const mock = extendClass(Mock);
Pipe(coreReflectPipeResolve(pipe))(mock);
decorateMock(mock, pipe, {
Expand Down
89 changes: 89 additions & 0 deletions tests/issue-5239/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {
Component,
Pipe,
PipeTransform,
VERSION,
} from '@angular/core';
import { TestBed } from '@angular/core/testing';

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

// A simple standalone pipe we are going to mock.
@Pipe(
{
name: 'pipe',
standalone: true,
} as never /* TODO: remove after upgrade to a14 */,
)
class StandalonePipe implements PipeTransform {
transform(value: string | null): string {
return `${value}:${this.constructor.name}`;
}

public pipe5239() {}
}

// A simple dependency component we are going to mock that imports the standalone pipe.
@Component(
{
selector: 'dependency',
template: 'dependency',
standalone: true,
imports: [StandalonePipe],
} as never /* TODO: remove after upgrade to a14 */,
)
class DependencyComponent {
public dependency5239() {}
}

// A standalone component we are going to test.
@Component(
{
selector: 'standalone',
template: `<dependency></dependency> {{ 'test' | pipe }}`,
standalone: true,
imports: [DependencyComponent, StandalonePipe],
} as never /* TODO: remove after upgrade to a14 */,
)
class StandaloneComponent {
public standalone5239() {}
}

// @see https://github.com/help-me-mom/ng-mocks/issues/5239
// The problem here was because of mocks of DependencyComponent.
// It has StandalonePipe too, so it mocked it without the custom function and cached,
// whereas the mock with the custom function was ignored due to existing cache of the pipe.
describe('issue-5239', () => {
if (Number.parseInt(VERSION.major, 10) < 14) {
it('needs >=a14', () => {
expect(true).toBeTruthy();
});

return;
}

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
// our component for testing
StandaloneComponent,

// the dependent component we want to mock
MockComponent(DependencyComponent),

// the pipe we want to mock with a custom transform
MockPipe(StandalonePipe, () => 'mock'),
],
}).compileComponents();
});

it('renders dependencies', () => {
const fixture = MockRender(StandaloneComponent);
expect(ngMocks.formatHtml(fixture)).toContain('mock');
});
});

0 comments on commit 979d42b

Please sign in to comment.