Skip to content
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

DebugElement.queryAll can return results in different order than DOM. #13066

Open
jjstreet opened this issue Nov 23, 2016 · 8 comments
Open

DebugElement.queryAll can return results in different order than DOM. #13066

jjstreet opened this issue Nov 23, 2016 · 8 comments
Assignees
Labels
area: testing Issues related to Angular testing features, such as TestBed freq3: high hotlist: google P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent type: bug/fix
Milestone

Comments

@jjstreet
Copy link

I'm submitting a ... (check one with "x")

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
DebugElements are returned from DebugElement.queryAll() in the same order they were first created in the ViewContainer regardless if they were moved by a structural directive like NgFor. If they were moved, then the order is out of sync with with the DOM.

Expected behavior
DebugElements should be returned from a query in the order they appear within the DOM. Order should be updated when underlying ViewRefs are moved around in a ViewContainer

Minimal reproduction of the problem with instructions
Plunker Reproducing the Issue

Running plunker will run tests that are almost identical to the unit tests for NgFor. Tests illustrate how adding and removing ViewRefs from a ViewContainer work as expected. Two tests illustrate how moving ViewRefs and using DebugElement to query for the moved items fails. Two tests illustrate how the DOM is in the correct order by using the native querySelectorAll() method.

What is the motivation / use case for changing the behavior?
DebugElements should be returned in the order they appear in the DOM when queried for.

Please tell us about your environment:
Windows 10 x64

  • Angular version: 2.2.1 with angular-cli 1.0.0-beta.20-4 (plunker not at this version)

  • Browser: all

  • Language: Typescript

  • Node (for AoT issues): node --version = 7.1.0

@fawind
Copy link

fawind commented Nov 25, 2016

I have the same issue using angular 2.2.1.

They are also in the wrong order when using the DebugElement.children property.

@jjstreet
Copy link
Author

I believe it may have to do with the following line:
https://github.com/angular/angular/blob/master/modules/%40angular/core/src/debug/debug_node.ts#L165

Map of debug nodes to element nodes is kept but may not be updated if nodes are moved around.

@vsavkin vsavkin added the area: testing Issues related to Angular testing features, such as TestBed label Nov 29, 2016
@vikerman vikerman self-assigned this Dec 21, 2016
@victornoel
Copy link
Contributor

@vikerman any news on that? I don't know why but since I upgraded from 4.1.3 to 4.3.6, this bug now appears…

@jshoudy11
Copy link

This is affecting my project as well. Is it being worked on?

@juliemr
Copy link
Member

juliemr commented Oct 19, 2017

The place to fix this is probably

class DebugRenderer2 implements Renderer2 {
, although I admit I haven't kept up with the renderer changes.

@amitpchigadani
Copy link

Has anyone found a solution for this?

@jshoudy11
Copy link

No clean solution but I found a workaround. Something like:

const debugEls = element.queryAll('foo');
const domEls = Array.from(element.nativeElement.querySelectorAll(selector));
debugEls.sort((a: DebugElement, b: DebugElement) => {
return domEls.indexOf(a.nativeElement) - domEls.indexOf(b.nativeElement);
});
return debugEls;

@amitpchigadani
Copy link

Yes, Thanks @jshoudy11 , We can verify our view template directly by fetching DOM elements instead of DebugElements. That's a nice alternative as of now. Hope this issue resolves sooner.

@jelbourn jelbourn added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent and removed severity3: broken labels Oct 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: testing Issues related to Angular testing features, such as TestBed freq3: high hotlist: google P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent type: bug/fix
Projects
None yet
Development

No branches or pull requests

10 participants