Is it possible to query from a previous result of a query? #386
Replies: 9 comments
-
What about array destructuring combined with const [animal1, , animal3] = spectator.queryAll('[data-animal]')
expect(animal1).toHaveDescendantWithText({ selector: '.data-name', 'Dog1'});
expect(animal3).toHaveDescendantWithText({ selector: '.data-name', 'Dog3'}); or, even simplier, but less accurate: const [animal1, , animal3] = spectator.queryAll('[data-animal]')
expect(animal1).toHaveText('Dog1');
expect(animal3).toHaveText('Dog3'); But, to answer your question, you could also use: const [animal1, , animal3] = spectator.queryAll('[data-animal]')
expect(animal1!.querySelector('.data-name')).toHaveExactText('Dog1');
expect(animal3!.querySelector('.data-name')).toHaveExactText('Dog3'); (the non-null assertion |
Beta Was this translation helpful? Give feedback.
-
Thanks @dirkluijk, that'll help. But I'll also give you another example. If I want to select by component, I can use the syntax Something like |
Beta Was this translation helpful? Give feedback.
-
I understand. The only way to do this, if that we patch the returned element with a custom @NetanelBasal what do you think? |
Beta Was this translation helpful? Give feedback.
-
I think it's a helpful feature that worth a try :) |
Beta Was this translation helpful? Give feedback.
-
The issue is that it will break the code. The code now returns the element. It could be something like |
Beta Was this translation helpful? Give feedback.
-
Yes, that's what I thought. However, is returning a subtype considered a breaking change? |
Beta Was this translation helpful? Give feedback.
-
I think it will break compilation when someone has defined return type for query const select = spectator.query('#test-onchange-select') as HTMLSelectElement; Although I don't like it but if query is optional it shouldn't break anything. |
Beta Was this translation helpful? Give feedback.
-
I'm closing this issue for now. |
Beta Was this translation helpful? Give feedback.
-
Can you explain why?
If there's no issue to track it, it will just be forgotten 🤔 |
Beta Was this translation helpful? Give feedback.
-
I'm submitting a...
Current behavior
When I use
queryAll
, I'd like to extract values to get an array of current values that I can compared to an array of expected values. For example if I've got:and I want to be able to test the name of every animal.
So I'd write something like:
Expected behavior
Any way of achieving that already?
Should I just write that test in a completely different way?
What is the motivation / use case for changing the behavior?
Being able to query from an element would be helpful and precise.
Beta Was this translation helpful? Give feedback.
All reactions