Instructor: In this test, we're using cy.server and cy.route to stub out an API call that's made when this page loads. We're then calling cy.visit to visit the root of our application. We're using cy.wait to make sure we wait for that load to complete before we make any assertions or interact with our application in another way.
We'll probably want to perform the same set of steps, or at least some variation of this set of steps, in most of our tests across all of our spec files. Luckily, Cypress gives us a way to very easily create custom commands that we can use right off the global cy object.
Let's create a custom command that encapsulates all of these commands. I'll start by copying this code. Then, under the Cypress directory, I'm going to expand the Support directory and open commands.js. Commands.js has a bunch of comments that tell us how we can create different commands.
We're going to scroll down to the bottom. On a new line, we'll create our own custom command. We'll start with the global CypressObject.Commands.add.
The first argument to add is going to be the name of our command. We're going to seed and visit our application through this command. We'll just call it seedAndVisit.
The second argument is a function. This is where the logic for our command is going to live.
I copied that code. I'm going to paste it right in here. I'll format it. This is our custom command.
We'll save this. Back in my spec, I'm going to take these lines and delete them. I'm going to replace them with a call to cy.seedAndVisit.
With this in place, I'm going to save this, switch back to the runner, and we'll see that our test still runs. Everything's working the same way it was before.
Now we know our command is working. Let's switch back to the code. I'm going to open up our form input spec file.
This is using a beforeEach to visit the root of our application. It doesn't really say anything about the initial API call.
In this case, since that's not stubbed, it's going to call our actual API. Let's replace this call to cy.visit with a call to our custom command.
We'll save it. Then I'm going to switch back to Cypress and back to my test. I'm going to run the form input spec file now.
Now we'll see that we have our fixture data which is being loaded by a custom command. If I expand out the command log, we'll see that our XHR call for our initial get to api/todos is being stubbed out.
This is great. Everything's working the way I expect it to. Neither one of these tests really care about the list. Let's update our custom command so that we can specific what to seed our application with.
I'll switch back to the code. In the case of these input form tests, I'd really like to be able to see the application with an empty array. The list isn't populated. We don't have to worry about that part.
Ideally, I could pass an empty array here. That would be our seed data for our application. In order to make this work, we need to refactor our custom command, so we switch back into the commands.js file.
We'll see that we have a function here. This function can take arguments. I'm going give this an argument, which I'll call SeedData.
Then I'm going to come down here. I'm going to cut this fixture todos. I'm going to use SeedData in my route as the response.
If everything's working properly, that form input spec is going to pass in that empty array. That'll be our seed.
I'm going to save the command. Then I'm going to save the tests. When the tests run again, we'll see that our tests are passing because they're testing the form behavior. We'll also notice that the preview doesn't contain any todo items because that initial API call is stubbed out. It's receiving an empty array as its response.
Now I'm going to go back to tests. I'm going to open up the app and its spec.js file. We'll run that. We'll see this time we have a problem because we're getting a null or undefined passed in because we never passed this in an initial argument.
Let's switch back to the code. I'm going to update our command with a reasonable default. If seed data is not passed in, I want to set it to equal that fixture:todo string.
I'm going to save this. Switch back to the test. We'll see we're getting an error message here. We need to wrap this in some parens.
This time when we run it, everything's going to work just fine. Our default is to load our todos from the fixture. If we need to override that behavior, all we have to do is pass a custom argument into that seedAndVisit command.