Skip to content

[Request] expose detectChanges function #38

Closed
@GregOnNet

Description

@GregOnNet

Before we start

🙏 Thank you for providing this library. It really helps my team to create solid maintainable tests.

Description

The render function gives access of the fixture corresponding to the Component that is tested.
In many tests I find my self using fixture just to use detectChanges.
It would be nice to use detectChanges directly

const { detectChanges } = render(Component)

Benefit

If fixture is only used for execute detectChanges the direct use of detectChanges makes the test more readable. In other words detectChanges goes hand in hand with the existing helper-functions (e.g. type, click, dblClick).

// Before
const { getByTestId, click, fixture } = render(Component);

const btn = getByTestId('my-id');
click(btn);
tick(600);
fixtue.detectChanges();
expect(btn).toBeDisabled();

// After
const { getByTestId, click, detectChanges } = render(Component);

const btn = getByTestId('my-id');
click(btn);
detectChanges();
expect(btn).toBeDisabled();

Use Case

Entering a value to an input triggers an asynchronous operation that might be delayed by debounceTime.

  1. The first detectChanges coming from type triggers the asynchronous operation
  2. After flushing the async tasks a second detectChanges needs to be called updating the view with the loaded data.
describe('When an entered company name/ticker has at least 2 characters', () => {
  it(
    'searches and lists duplicate candidates as the user types', 
    fakeAsync(async () => 
  {
    const { type, getByTestId, getAllByTestId, fixture } = await 
    render(CompanyCreateComponent, renderOptions);
    
    type(getByTestId('name-or-ticker'), 'GO');
    tick(600);
    fixture.detectChanges();

    expect(getAllByTestId('name-duplicate')).toHaveLength(1);
  }));
});

Contribution

  • I would create an PR for this feature if you find it valuable, too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions