Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: MockPipe with custom transform function on a standalone angular pipe #4564

Closed
bornes opened this issue Dec 19, 2022 · 4 comments · Fixed by #4565 or #4576
Closed

Bug: MockPipe with custom transform function on a standalone angular pipe #4564

bornes opened this issue Dec 19, 2022 · 4 comments · Fixed by #4565 or #4576
Assignees
Labels
bug Something isn't working released v14.5.1

Comments

@bornes
Copy link

bornes commented Dec 19, 2022

Using standalone angular pipe, it is not possible to mock the transforme function since it always return an empty string.

stackblitz: https://stackblitz.com/edit/angular-ivy-ov8yzw

It should be possible to mock the return of the transform function in a standalone angular pipe

@bornes bornes added the bug Something isn't working label Dec 19, 2022
@satanTime
Copy link
Member

Hi,

the problem in your test is that you don't change the definition of the standalone component.

Here you can find information how to mock dependencies of a standalone component: https://ng-mocks.sudo.eu/guides/component-standalone

https://stackblitz.com/edit/angular-ivy-8anmhe?file=src%2Fapp%2Fapp.component.spec.ts

import { Component, Pipe, PipeTransform } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { MockBuilder } from 'ng-mocks';

@Pipe({
  name: 'mockedPipe',
  pure: false,
  standalone: true,
})
export class MockedPipe implements PipeTransform {
  transform() {
    return 'hi';
  }
}

@Component({
  selector: 'mocked-component',
  template: '<span> {{ test | mockedPipe }}</span>',
  imports: [MockedPipe],
  standalone: true,
})
export class MockedComponent {}

describe('test standalone mocked pipe', () => {
  beforeEach(() => MockBuilder(MockedComponent).mock(MockedPipe, () => 'bye'));

  it('should have the attribute and method of the extended class', () => {
    const fixture = TestBed.createComponent(MockedComponent);
    const component = fixture.componentInstance;
    fixture.detectChanges();
    expect(fixture.nativeElement.innerText).toContain('bye');
  });
});

@satanTime
Copy link
Member

However, you found a different bug.

@bornes
Copy link
Author

bornes commented Dec 20, 2022

But I am using TestBed.configureTestingModule in my example with a mocked transform function in a standalone pipe. The same approach, in a not standalone pipe , works well. So, maybe it should work on standalone pipes too 👍

satanTime added a commit to satanTime/ng-mocks that referenced this issue Dec 24, 2022
satanTime added a commit that referenced this issue Dec 24, 2022
fix(core): correctly defines TestBed with multiple declarations with the same selector #4564
@satanTime
Copy link
Member

v14.5.1 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released v14.5.1
Projects
None yet
2 participants