I am currently developing a time selection widget in Angular to compare our current performance with previous data. This widget consists of two dropdown lists along with various buttons and input fields (for now, let's focus on the dropdown lists). I have a concern regarding the testing of these dropdowns. Below is an excerpt of my code:
timeselector.component.html
<select data-automation-id="timeselection-mode-primary">
<option *ngFor="let mode of modes" [attr.data-automation-id]="'options_' + mode">
{{ mode }}
</option>
</select>
<!--additional input fields and toggle button-->
<select data-automation-id="timeselection-mode-secondary">
<option *ngFor="let mode of modes" [attr.data-automation-id]="'previous_options_' + mode">
Previous {{ mode }}
</option>
</select>
Both dropdowns are fetching modes
from the same array in my typescript file:
timeselector.component.ts
modes=['Calendar year', 'Year-to-date', 'Quarter', 'Monthly Pipe', 'Weekly Sprint']
The issue arises when dealing with options like Calendar year
, Monthly Pipe
, and Weekly Sprint
due to the spaces in their names. Consequently, the data-automation-id
becomes tricky to handle.
To address this, I included index values for each option as shown below:
<option *ngFor="let mode of modes; let i=index"
[attr.data-automation-id]="'options_' +i">
and
<option *ngFor="let mode of modes; let i=index"
[attr.data-automation-id]="'previous_options_' +i">
This solution resolves the issue, but my tech lead prefers not to use indexes. She suggests utilizing methods like truncate
, trim
, or similar, without relying on indices.
While familiar with techniques such as truncate
, trim
, and subString
, I struggle to implement them at runtime within data-automation-id
. Various attempts have been unsuccessful so far. Any assistance would be greatly appreciated.
Here is how the widget appears: