Skip to content

fix: do cleanup when removeAngularAttributes is enabled #69

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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
]
},
"dependencies": {
"@angular/animations": "^9.0.0",
"@angular/cdk": "^9.0.0",
"@angular/common": "^9.0.0",
"@angular/compiler": "^9.0.0",
"@angular/core": "^9.0.0",
"@angular/forms": "^9.0.0",
"@angular/material": "^9.0.0",
"@angular/platform-browser": "^9.0.0",
"@angular/platform-browser-dynamic": "^9.0.0",
"@angular/router": "^9.0.0",
"@angular/animations": "^9.0.3",
"@angular/cdk": "^9.1.0",
"@angular/common": "^9.0.3",
"@angular/compiler": "^9.0.3",
"@angular/core": "^9.0.3",
"@angular/forms": "^9.0.3",
"@angular/material": "^9.1.0",
"@angular/platform-browser": "^9.0.3",
"@angular/platform-browser-dynamic": "^9.0.3",
"@angular/router": "^9.0.3",
"@ngrx/store": "^8.0.0-rc.0",
"@phenomnomnominal/tsquery": "^3.0.0",
"@testing-library/dom": "^6.12.2",
Expand All @@ -44,14 +44,14 @@
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.900.1",
"@angular-devkit/build-ng-packagr": "~0.900.1",
"@angular/cli": "~9.0.1",
"@angular/compiler-cli": "^9.0.0",
"@angular/language-service": "^9.0.0",
"@angular-devkit/build-angular": "~0.900.3",
"@angular-devkit/build-ng-packagr": "~0.900.3",
"@angular/cli": "~9.0.3",
"@angular/compiler-cli": "^9.0.3",
"@angular/language-service": "^9.0.3",
"@testing-library/jest-dom": "^4.1.0",
"@types/jest": "~24.0.11",
"@types/node": "^12.11.1",
"@types/node": "^13.7.6",
"codelyzer": "^5.1.2",
"husky": "^2.3.0",
"jest": "^24.1.0",
Expand Down
26 changes: 0 additions & 26 deletions projects/jest-utils/src/lib/configure-test-suite.ts

This file was deleted.

1 change: 0 additions & 1 deletion projects/jest-utils/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './configure-test-suite';
export * from './create-mock';
2 changes: 1 addition & 1 deletion projects/jest-utils/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"experimentalDecorators": true,
"importHelpers": true,
"allowSyntheticDefaultImports": true,
"types": [],
"types": ["@types/jest"],
"lib": ["dom", "es2015"]
},
"angularCompilerOptions": {
Expand Down
12 changes: 12 additions & 0 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { createSelectOptions, createType, tab } from './user-events';
@Component({ selector: 'wrapper-component', template: '' })
class WrapperComponent {}

let cleanup: () => void;

export async function render<ComponentType>(
component: Type<ComponentType>,
renderOptions?: RenderComponentOptions<ComponentType>,
Expand Down Expand Up @@ -73,6 +75,7 @@ export async function render<SutType, WrapperType = SutType>(
const idAttribute = fixture.nativeElement.getAttribute('id');
if (idAttribute && idAttribute.startsWith('root')) {
fixture.nativeElement.removeAttribute('id');
cleanup = () => fixture.nativeElement.setAttribute('id', idAttribute);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I restore only the id, because it's the only attribute that Angular need to remove old roots. Should do we restore ng-version too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I wasn't clear, I will follow-up with another PR

}
}

Expand Down Expand Up @@ -236,3 +239,12 @@ function addAutoImports({ imports, routes }: Pick<RenderComponentOptions<any>, '

return [...imports, ...animations(), ...routing()];
}

if (typeof afterEach === 'function' && !process.env.ATL_SKIP_AUTO_CLEANUP) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When process.env.ATL_SKIP_AUTO_CLEANUP is going to be neccessary?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's to be consistent with the rest of the testing libraries

afterEach(() => {
if (cleanup) {
cleanup();
cleanup = null;
}
});
}
29 changes: 29 additions & 0 deletions projects/testing-library/tests/issues/67.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// https://github.com/testing-library/angular-testing-library/issues/67
import { Component } from '@angular/core';
import { render } from '../../src/public_api';

@Component({
template: `
<div>
<!-- if remove for="name" no error happens -->
<label for="name">
<input type="checkbox" id="name" data-testid="checkbox" />
TEST
</label>
</div>
`,
})
export class BugGetByLabelTextComponent {}

it('first step to reproduce the bug: skip this test to avoid the error or remove the for attribute of label', async () => {
expect(await render(BugGetByLabelTextComponent)).toBeDefined();
});

it('second step: bug happens :`(', async () => {
const { getByLabelText, getByTestId } = await render(BugGetByLabelTextComponent);

const checkboxByTestId = getByTestId('checkbox');
const checkboxByLabelTest = getByLabelText('TEST');

expect(checkboxByTestId).toBe(checkboxByLabelTest);
});
2 changes: 1 addition & 1 deletion projects/testing-library/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"types": [],
"types": ["./types", "@types/node"],
"lib": ["dom", "es2015", "es2018.promise"]
},
"angularCompilerOptions": {
Expand Down
1 change: 1 addition & 0 deletions projects/testing-library/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare function afterEach(fn: () => {}, timeout?: number): void;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed to run tests in CI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the IDE it shows an error (duplicate identifier)
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to be able to build

Loading