From 5d86dff520ce3ce08d687de31760b39f686373a9 Mon Sep 17 00:00:00 2001 From: timdeschryver <28659384+timdeschryver@users.noreply.github.com> Date: Fri, 16 Oct 2020 06:59:39 +0200 Subject: [PATCH] fix: only replace find query if they got a get query --- .github/workflows/ci.yml | 4 ++-- .github/workflows/release.yml | 4 ++-- package.json | 2 +- .../testing-library/src/lib/testing-library.ts | 15 ++++++--------- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 88c5e3fd..f3fd8735 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,6 @@ jobs: - name: install run: npm install - name: build - run: npm run build --skip-nx-cache + run: npm run build -- --skip-nx-cache - name: test - run: npm run test --ci --code-coverage + run: npm run test -- --ci --code-coverage diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6f774d97..0370418d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -22,9 +22,9 @@ jobs: - name: install run: npm install - name: build - run: npm run build --skip-nx-cache + run: npm run build -- --skip-nx-cache - name: test - run: npm run test --ci --code-coverage + run: npm run test -- --ci --code-coverage - name: Release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/package.json b/package.json index c2199161..04bf1628 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "scripts": { "ng": "ng", "start": "ng serve", - "build": "nx run-many --target=build --all", + "build": "nx run-many --target=build --projects=testing-library,jest-utils", "test": "nx run-many --target=test --all", "lint": "nx workspace-lint && ng lint", "e2e": "ng e2e", diff --git a/projects/testing-library/src/lib/testing-library.ts b/projects/testing-library/src/lib/testing-library.ts index ec776992..819f8760 100644 --- a/projects/testing-library/src/lib/testing-library.ts +++ b/projects/testing-library/src/lib/testing-library.ts @@ -121,7 +121,7 @@ export async function render( detectChanges(); }; - const inject = TestBed.inject || TestBed.get + const inject = TestBed.inject || TestBed.get; let router = routes ? inject(Router) : null; const zone = inject(NgZone); const navigate = async (elementOrPath: Element | string, basePath = '') => { @@ -167,10 +167,7 @@ export async function render( Array.isArray(element) ? element.forEach((e) => console.log(dtlPrettyDOM(e, maxLength, options))) : console.log(dtlPrettyDOM(element, maxLength, options)), - ...replaceFindWithFindAndDetectChanges( - fixture.nativeElement, - dtlGetQueriesForElement(fixture.nativeElement, queries), - ), + ...replaceFindWithFindAndDetectChanges(dtlGetQueriesForElement(fixture.nativeElement, queries)), }; } @@ -315,10 +312,10 @@ class WrapperComponent {} /** * Wrap findBy queries to poke the Angular change detection cycle */ -function replaceFindWithFindAndDetectChanges(container: HTMLElement, originalQueriesForContainer: T): T { +function replaceFindWithFindAndDetectChanges(originalQueriesForContainer: T): T { return Object.keys(originalQueriesForContainer).reduce((newQueries, key) => { - if (key.startsWith('find')) { - const getByQuery = originalQueriesForContainer[key.replace('find', 'get')]; + const getByQuery = originalQueriesForContainer[key.replace('find', 'get')]; + if (key.startsWith('find') && getByQuery) { 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( @@ -354,7 +351,7 @@ function detectChangesForMountedFixtures() { /** * Re-export screen with patched queries */ -const screen = replaceFindWithFindAndDetectChanges(document.body, dtlScreen); +const screen = replaceFindWithFindAndDetectChanges(dtlScreen); /** * Re-export waitFor with patched waitFor