Tomasz Łakomy: Currently, we have a simple Cypress test. It's going to open up the home page and check whether the Emoji Search header exists over here. Next up, we would like to test whether all those emojis are displayed in this list. In order to test that, we have to select those items somehow.
Right now, we can see in the DevTools that each one of those items has a class of component-emoji-result-row. We could just copy this class and use it in our test. In order to get an item using a class, we can use the get command.
We can do cy.get, paste in the name of this class and assert on it, so .should('have.length', 20) because there are 20 items on the list. If I save that, the test is going to run successfully. We can see over here that we have successfully asserted that.
In fact, there are 20 items on this list over here, but this is not a perfect test. The issue is that if we decided to change the name of this class, this test would fail, even though the functionality of the app is not affected, but we can do better.
Let's go to our emoji-result-row component. Over here, instead of using this class name, we're going to add a new data property. We're going to do data-cy "emoji-row".
The idea behind that is to add a separate specific data property to an element that we want to assert with Cypress test. Right now, we are free to modify or even remove those class names. This test is going to continue to pass because the functionality of the app is not affected by those changes.
Let's go back to our test. Let me move it to a different test because the first one is going to test whether the page header has been successfully rendered, and the second one is going to check whether it('renders the list of emojis').
It's going to take a function. We're going to visit the home page. We're going to copy that, paste it over here. Instead of using this class, we're going to get the element via data-cy property. So, data-cy='emoji-row'. Let me close that, save it, and now, the test is going to pass even though we decided to rename the class name.
If I rename the class name to emoji-result-row and apply those changes to all files, after restarting the test, it's going to successfully pass, and we are no longer asserting via a class name. This is how we are able to write a better test.