Is there a way to access a specific instance of a component within an app that uses it multiple times in other components? Currently, when I use the query command "fixture.debugElement.query(By.directive(ComponentName)).componentInstance;", it only recognizes the first instance of ComponentName.
Here is a simplified version of the example:
random.test.component.html
<random-test-child>
</random-test-child>
<snack-time
[bestSnack]="cheeseAndCrackers">
</snack-time>
random.test.component.child.html
<snack-time
[bestSnack]="applesAndOranges">
</snack-time>
<snack-time
[bestSnack]="cookies">
</snack-time>
*all variable names are strings in their respective .ts files (e.g., applesAndOranges is "Apples and Oranges")
Currently, the query command selects the SnackTime component with the value "Apples and Oranges" as bestSnack. How can I specifically access the instances with "Cookies" or "Cheese and Crackers" without adding an id tag?
EDIT
Considering a different approach: In the spec file, I'm using fixture.debugElement.query(By.directive(SnackTime)).componentInstance to identify the SnackTime elements. I have now included another SnackTime in random.test.component.html:
<snack-time
[bestSnack]="cheeseAndCrackers">
</snack-time>
<snack-time
[bestSnack]="chipsAndSoda">
</snack-time>
I am thinking of creating a QueryList or array of the SnackTime elements and then selecting the specific instance from there. However, when I attempt this:
let componentsInThePage:SnackTime[] = fixture.debugElement.query(By.directive(SnackTime)).componentInstance;
I only get an array containing the first created SnackTime component, missing out on the other instances from random.test.component.html file (the ones in child.html are also not included). What would be the correct way to create a list/array of all SnackTime components?