-
Notifications
You must be signed in to change notification settings - Fork 93
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from './configure-test-suite'; | ||
export * from './create-mock'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>, | ||
|
@@ -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); | ||
} | ||
} | ||
|
||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
}); | ||
} |
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); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare function afterEach(fn: () => {}, timeout?: number): void; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed to run tests in CI? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needed to be able to build |
There was a problem hiding this comment.
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 restoreng-version
too?There was a problem hiding this comment.
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