Skip to content

Commit

Permalink
fix: auto-spy covers pipes with default transform
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Dec 5, 2020
1 parent 0f838b5 commit 980b4d7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 27 deletions.
19 changes: 8 additions & 11 deletions lib/mock-pipe/mock-pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getMockedNgDefOf } from '../common/func.get-mocked-ng-def-of';
import { Mock } from '../common/mock';
import { MockOf } from '../common/mock-of';
import ngMocksUniverse from '../common/ng-mocks-universe';
import helperMockService from '../mock-service/helper.mock-service';

import { MockedPipe } from './types';

Expand All @@ -17,24 +18,20 @@ export function MockPipes(...pipes: Array<Type<PipeTransform>>): Array<Type<Pipe
return pipes.map(pipe => MockPipe(pipe, undefined));
}

const defaultTransform = (): void => undefined;

const getMockClass = (pipe: Type<any>, transform: PipeTransform['transform']): Type<any> => {
const getMockClass = (pipe: Type<any>, transform?: PipeTransform['transform']): Type<any> => {
@Pipe(coreReflectPipeResolve(pipe))
@MockOf(pipe)
class PipeMock extends Mock implements PipeTransform {
class PipeMock extends Mock {
public constructor(@Optional() injector?: Injector) {
super(injector);

// need to override overrides
if (transform !== defaultTransform) {
this.transform = transform;
if (transform) {
(this as any).transform = transform;
} else if (!(this as any).transform) {
helperMockService.mock(this, 'transform', `${this.constructor.name}.transform`);
}
}

public transform(value: any, ...args: any[]): any {
return transform(value, ...args);
}
}

return PipeMock;
Expand All @@ -50,7 +47,7 @@ export function MockPipe<TPipe extends PipeTransform>(

export function MockPipe<TPipe extends PipeTransform>(
pipe: Type<TPipe>,
transform: TPipe['transform'] = defaultTransform,
transform?: TPipe['transform'],
): Type<MockedPipe<TPipe>> {
// We are inside of an 'it'. It's fine to return a mock copy.
if ((getTestBed() as any)._instantiated) {
Expand Down
18 changes: 10 additions & 8 deletions tests/issue-240/test.builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ describe('issue-240:builder', () => {
const pure = ngMocks.findInstance(PurePipe);
const impure = ngMocks.get(fixture.point, ImpurePipe);

spyOn(pure, 'transform');
spyOn(impure, 'transform');
// Without auto-spy we need the code below.
// Calls would start with 0.
// spyOn(pure, 'transform');
// spyOn(impure, 'transform');

expect(pure.transform).toHaveBeenCalledTimes(0);
expect(impure.transform).toHaveBeenCalledTimes(0);

fixture.detectChanges();
expect(pure.transform).toHaveBeenCalledTimes(0);
expect(pure.transform).toHaveBeenCalledTimes(1);
expect(impure.transform).toHaveBeenCalledTimes(2);

fixture.detectChanges();
expect(pure.transform).toHaveBeenCalledTimes(0);
expect(pure.transform).toHaveBeenCalledTimes(1);
expect(impure.transform).toHaveBeenCalledTimes(4);

fixture.detectChanges();
expect(pure.transform).toHaveBeenCalledTimes(1);
expect(impure.transform).toHaveBeenCalledTimes(6);
});
});
2 changes: 2 additions & 0 deletions tests/issue-240/test.classic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ describe('issue-240:classic', () => {
const pure = ngMocks.get(fixture.point, PurePipe);
const impure = ngMocks.findInstance(ImpurePipe);

// We don't have auto-spies, because we provided callbacks.
// Now we need to install spies.
spyOn(pure, 'transform');
spyOn(impure, 'transform');

Expand Down
18 changes: 10 additions & 8 deletions tests/issue-240/test.guts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ describe('issue-240:guts', () => {
const pure = ngMocks.get(fixture.point, PurePipe);
const impure = ngMocks.findInstance(ImpurePipe);

spyOn(pure, 'transform');
spyOn(impure, 'transform');
// Without auto-spy we need the code below.
// Calls would start with 0.
// spyOn(pure, 'transform');
// spyOn(impure, 'transform');

expect(pure.transform).toHaveBeenCalledTimes(0);
expect(impure.transform).toHaveBeenCalledTimes(0);

fixture.detectChanges();
expect(pure.transform).toHaveBeenCalledTimes(0);
expect(pure.transform).toHaveBeenCalledTimes(1);
expect(impure.transform).toHaveBeenCalledTimes(2);

fixture.detectChanges();
expect(pure.transform).toHaveBeenCalledTimes(0);
expect(pure.transform).toHaveBeenCalledTimes(1);
expect(impure.transform).toHaveBeenCalledTimes(4);

fixture.detectChanges();
expect(pure.transform).toHaveBeenCalledTimes(1);
expect(impure.transform).toHaveBeenCalledTimes(6);
});
});
22 changes: 22 additions & 0 deletions tests/pipe-auto-spy/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Pipe, PipeTransform } from '@angular/core';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

@Pipe({
name: 'target',
})
class TargetPipe implements PipeTransform {
public transform(value: string): number {
return value.length;
}
}

describe('pipe-auto-spy', () => {
beforeEach(() => MockBuilder(null, TargetPipe));

it('has spy out of the box', () => {
MockRender(`{{ 'test' | target }}`);
const pipe = ngMocks.findInstance(TargetPipe);
expect(pipe.transform).toHaveBeenCalledWith('test');
expect(pipe.transform).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 980b4d7

Please sign in to comment.