Accessibility in JavaScript Applications

Coding Integration Tests

Marcy Sutton Todd

Marcy Sutton Todd

Principle Studios
Accessibility in JavaScript Applications

Check out a free preview of the full Accessibility in JavaScript Applications course

The "Coding Integration Tests" Lesson is part of the full, Accessibility in JavaScript Applications course featured in this preview video. Here's what you'd learn in this lesson:

Marcy utilizes Cypress to test the skip link created earlier on in the course, checking that the focus is being sent to the skip link when the route changes.


Transcript from the "Coding Integration Tests" Lesson

>> Marcy Sutton: So, I'm gonna spend a little bit of time writing some integration tests in Cyprus. So the convention for this, and Cyprus kind of sets this up for you by default. Close this components directory and the example. So when you run Cyprus, it will add this cyprus directory for you.

I think you can configure it. You can add fixtures in here and plugins and things. The commands, it adds this file by default and to use Cyprus Axe, which is a binding for the Axe accessibility testing API for Cyprus. I just import the commands in this commands JS file in the support directory.

And then I've got the integration test directory is where my tests would go. And so, this is pulling in those Cyprus bindings for the underlying API. So I'm just gonna start writing some tests. So let's do some accessibility tests. Can give it whatever name I want. I am going to, I'm just gonna write my first assertion.

Let's see, what am I gonna test? That's probably a better thing to look at. So we've got our application here. Yeah, I was going to test the client side routing that I added with this little skip link thing that we created earlier in the day. So, from an interaction.

I mean, I think about the testing in terms of, what am I trying to mimic that a user would do? And for this, I want to reach one of the items that is actually going to execute this skip link behavior. So I want to go onto a link.

I want to click it. I went back, I hit it twice. I want to assert that the focus is sent to a skip link. Seems pretty straightforward. I am gonna pull up the cyber stocks. So it's I like to have the the docs for whatever API I'm working on because especially when you're kind of going back and forth.

Let's see, guides. API is probably where I want to go. So there's assertions here. I think commands might be the way to go. Commands, yes. So there's things like click. So if I was or side.get, whatever you're trying to get and then click on it. This is where that focused API comes in handy.

Okay, so that helps me, I'm going to get this in here. So I'm going to say it should
>> Marcy Sutton: It should send focus to a skip link. Let's see, let's think about this. So.
>> Marcy Sutton: It should.
>> Marcy Sutton: Take the user's focus [LAUGH] But keep it the way it was.

It should send focus to a skip link. When the route changes. So that's mostly just indicating that I'm going to test the mechanics of sending it, focuses skip link. It's not really talking about the the link click that I'm coming from. So I'm trying to think of a way to capture that.

It's like it should, when the route changes from a link click. This is a very long test title all of a sudden but I'm just going to leave it. I'm going to wrap text, too. I try and keep my test names short. Maybe I write, maybe these are two different tests.

I can play around with that. So the first thing I need to do is go and tell it what page I'm going to. And that API. Now let's see. We had it in our, I'm gonna cheat and go back just in the interest of time. So I'm gonna do cy.visit.

You could do it before each test. I'm just gonna do it in the test cuz I'm just gonna write the one. So I'm gonna say cy.visit and I'm gonna give it the local host, port 8000 rather than the slash for the homepage. I've just had more success with giving it the local host URL in this case.

Something that would be a nice enhancement would be to make this port more dynamic. Like go read it from node process or something, but I'm just gonna leave it for now. Cuz that way, say if I have two gatsby sites and it gets opened in 8001 or something like that, then it's nice to know that I'm on the right one.

So cy.visit, I want to cy.getByText. Maybe, is that the one I want, getByText Dropdown. Let's see, is there more than one, that seems sort of brutal. Because on homepage, there might be more than one of those, so maybe. Let's see, get. Let's go look at that API. So I can use a selector, that's probably the best, so cy.get instead of getByText.

So I'm gonna say, let's go look at this element and see what it's got on it.
>> Marcy Sutton: So I could go, maybe, add a class name to this, I could probably say, page navigation and then go get it that way. Page navigation and then the first anchor. That might return me a collection, I guess we'll just see.

So I'm gonna click it. And then I'm gonna keep going, just try and write the rest of this test. So I'm gonna expect then that cy.focused. Let's go look at that API, go back.
>> Marcy Sutton: Focused. So I think that that gives me, cy.focus should have so I can assert, I could say should have class probably.

I think that's what I'm going to do, cy.focused. And, how quickly I forget, .should have .class and it was routeSkipLink, I believe. And because that was being baked into the component, that's probably pretty stable. This get page-navigation ones, maybe not as stable but let's try it. So to run these tests, I'm gonna go kill off my server.

I'm gonna do npm run e2e. It's test:e2e. So this is going to open Cyprus. Run it, firing up my application cuz that's what it's actually gonna test against. Instead of testing a virtual in-memory snapshot, it's actually going to fire this up in a browser. And I'm gonna turn my doc on so we can see it.

Try and open. There it goes, Cyprus open, so it's opening the Cyprus application. Here it goes. Okay, so now this is the interface that took so much energy to download. So I'm gonna click this. And this is when I can actually open it in a Chrome instance. It's gonna go hit that page.

Yeah, I was right. So, that can only be called on a single element. So, if i just want to get a single element, I could probably get it from an array. Maybe? Let's see a dog first of its in a list item. So I probably want to do LI first of type.

So this will kind of leverage CSS. See if that works. Just use CSS to go and get it instead of trying to do something. Okay, so that worked. That actually, I got the first link. It was focused, I'm pretty satisfied with that. So to get out of Cypress, I'm gonna kill off the browser.

I can hit Stop or I could just close it. So I'm gonna do that and that is a test. That has inserted that feature that we added earlier today which I'm pretty psyched about. So if I were shipping that real component and I were implementing it on a site, that's when I would write an integration test for that.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now