Skip to content

Custom find-queries are lost when put through 'replaceFindWithFindAndDetectChanges' (>=v9.1.0) #147

Closed
@dgr-m

Description

@dgr-m

Starting from v9.1.0, the queries passed to the render function are first run through replaceFindWithFindAndDetectChanges to trigger a change detection cycle when using the find-queries. When looking at the code it seems the queries are properly iterated, but the getQueryBy is assigned from the dtlQueries, not the queries passed as argument (originalQueriesForContainer). This means that any custom find-query is 'lost' (as there is no get-query equivalent for it in the dtlQueries). Replacing dtlQueries with originalQueriesForContainer and removing the container argument from getByQuery(container, text, options) should fix this issue.

function replaceFindWithFindAndDetectChanges<T>(container: HTMLElement, originalQueriesForContainer: T): T {
  return Object.keys(originalQueriesForContainer).reduce((newQueries, key) => {
    if (key.startsWith('find')) {
      const getByQuery = dtlQueries[key.replace('find', 'get')];
      newQueries[key] = async (text, options, waitOptions) => {
        // original implementation at https://github.com/testing-library/dom-testing-library/blob/master/src/query-helpers.js
        const result = await waitForWrapper(
          detectChangesForMountedFixtures,
          () => getByQuery(container, text, options),
          waitOptions,
        );
        return result;
      };
    } else {
      newQueries[key] = originalQueriesForContainer[key];
    }

    return newQueries;
  }, {} as T);
}

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