Skip to content

Usage of rerender with OnPush ChangeDetection #157

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

Closed
rafaelss95 opened this issue Nov 4, 2020 · 2 comments · Fixed by #158
Closed

Usage of rerender with OnPush ChangeDetection #157

rafaelss95 opened this issue Nov 4, 2020 · 2 comments · Fixed by #158
Labels

Comments

@rafaelss95
Copy link
Contributor

rafaelss95 commented Nov 4, 2020

Hey, thanks for this lib :)

So, I'm facing an issue right now that when calling rerender function with component that uses OnPush doesn't cause component to change the properties. Here's an example:

@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  selector: 'app-test',
  template: `
    <div data-testid="number" [class.active]="activeField === 'number'">Number</div>
    <div data-testid="holder" [class.active]="activeField === 'holder'">Holder</div>
    <div data-testid="expiry" [class.active]="activeField === 'expiry'">Expiry</div>
    <div data-testid="cvv" [class.active]="activeField === 'cvv'">CVV</div>
  `,
})
export class TestComponent {
  @Input() activeField: string;
}

//////// Test file

async function setup() {
  const { rerender } = await render(TestComponent);
  const numberHtmlElementRef = screen.getByTestId('number');
  const holderHtmlElementRef = screen.getByTestId('holder');
  const expiryHtmlElementRef = screen.getByTestId('expiry');
  const cvvHtmlElementRef = screen.getByTestId('cvv');

  return {
    numberHtmlElementRef,
    holderHtmlElementRef,
    expiryHtmlElementRef,
    cvvHtmlElementRef,
    rerender,
  } as const;
}

describe(TestComponent.name, () => {
  test('someDescription', async () => {
    const {
      numberHtmlElementRef,
      holderHtmlElementRef,
      expiryHtmlElementRef,
      cvvHtmlElementRef,
      rerender,
    } = await setup();

    rerender({ activeField: 'number' });
    expect(numberHtmlElementRef).toHaveClass('active');
    rerender({ activeField: 'holder' });
    expect(holderHtmlElementRef).toHaveClass('active');
    rerender({ activeField: 'expiry' });
    expect(expiryHtmlElementRef).toHaveClass('active');
    rerender({ activeField: 'cvv' });
    expect(cvvHtmlElementRef).toHaveClass('active');
  });
}

All the tests above fail when using OnPush, but if I remove this, all pass.

@rafaelss95 rafaelss95 changed the title Usage of rerenderer with OnPush ChangeDetection Usage of rerender with OnPush ChangeDetection Nov 4, 2020
@timdeschryver
Copy link
Member

Thanks for creating this issue @rafaelss95
This seems like we'll need to trigger a change detection cycle.

If you want you can take a look yourself and create a pull request with the fix.

@github-actions
Copy link

github-actions bot commented Nov 5, 2020

🎉 This issue has been resolved in version 10.1.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants