0

I’m creating test in cypress using typescript, I’m not sure how to assert that all options in drop down are unique - I found bug where is situation that values are duplicated multiple times - it’s a drop down with search results. I’m not sure how check that each value in this drop down is unique. Can you help me or direct how to do it in the easiest and best way?

1
  • 2
    I wouldn't do that at an E2E level, don't you have any component tests that could check this? – jonrsharpe Sep 8 '20 at 20:11
4

Get the text and add to a array

Convert array to set and compare the size

Set will have only unique elements (Removes the duplicates)

So if length of set and array are different , it means there where duplicate entries in the array.

var arr = [55, 44, 65, 55];
var set = new Set(arr);
console.log(set.size === arr.length);
set.forEach(x => {console.log(x)});
3
  • Can you explain to op why comparing a set to array will do the job? – Moro Sep 9 '20 at 8:05
  • 1
    @moro added that to the answer thank you – PDHide Sep 9 '20 at 8:06
  • Thanks,it works:) – ciupakabrans Sep 10 '20 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.