React Testing Library Tutorial #8 - Assertions

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·4y ago

Key Takeaways

Writes assertions for tests using React Testing Library

Full Transcript

all right in this video we are going to cover another unit test but here we're going to be focusing on the many different assertions that we can utilize to test the outcome of our test so we're going to be doing that by testing another component so i went ahead and i just commented out every single test except for the first one which tests our header so i will keep that and we should see here that one test has passed i also went ahead and i commented out the second header you can either comment it out or get rid of it all right so the second component that we're going to test is works very similarly to the first component that we tested which is this one over here the to do footer so the way that it works is that we are going to pass a number into this component as a prop and then is going to render a specific test or text rather so over here we're passing in zero and it's rendering zero tasks left now if we were to add a task then we're going to be passing in one and notice that it changed to one task left now if we were to add another one then we're going to be passing in two and then the text changes so that is what we are going to be testing again very very similar to this one so that is going to live inside if our to do footer and you can see here that is going to take in a prop number of incomplete tasks and you can see here that the text lives in a paragraph tag and you can see that there's just a quick regular expression to see if we should use plural or or sorry plural or singular and so that way if we have just one task left it's just one task left instead of one tasks left and we're also utilizing react router dom and this is a good thing because it's going to introduce how we can utilize react router and all these other external libraries when writing our tests because we're going to notice a quick error that we get when writing our tests so let's actually go ahead and let's create a underscore underscore test directory and in here we're going to as usual create our test so let's go and let's create the to do footer.test.js all right so now let's just copy this let's copy this format let's paste that in there and let's test uh let's test let's just change the text of the test block so over here we are basically testing should render the correct amount of incomplete tasks so should render the correct amount of incomplete tasks now the component that we are going to be utilizing is the to do footer so over here to do footer and over here let's say we're going to render the to do footer and then remember the prop is number of incomplete tasks so let's go ahead and let's say that the number of incomplete tasks is uh i don't know five and so over here what we should expect we should be able to get a paragraph element let's just go ahead here and just say para graph element and that should say well that should say let's actually just go ahead and let's just add five in there we should see five tasks left so we can say five tasks left and we should expect this paragraph element to be in the document so this seems like a very valid test but it is going to fail now why is it going to fail well the reason why it's going to fail and we can actually scroll up let's just scroll up you can see here that we are utilizing link outside of router and if we go to our header we're utilizing this link from react router dom to basically change the the url and change the page of our application now we cannot do that without let's just go over here to index.js without wrapping the component with browser router now because inside of our test we're testing this component in complete isolation it is not going to be wrapped with browser router so what we could do is create a component that wraps this component with browser router and what i like to do is call this a mock component so we can do something like mock to do footer and here this is just a function component and what we can do is we can import browser router from react router or dom so let's just import browser router and over here we can do browser router and now we have browser router and so now what we can do is we can pass in this component so in here we can wrap browser router within this component let's actually just make this a little bit prettier so it looks like this but now we need a way to pass the number that we want to pass in through the mock to do component so we can do that through props as well so we can go over here and just say number of incomplete tasks we're going to pass it in as a prop to the mock footer which is ultimately going to pass it in as a prop to the to do footer so you can see here that is how that works so now what we can do instead here is instead of rendering the to-do footer we could just render the mock to do footer that contains browser router so let's go over here and let us i'm just going to do it in a separate line i'm just going to do mock to do footer all right mock to do footer and over here we're going to do number of incomplete tasks and then we are going to pass in that number so we're going to pass in five so now if we save this and we go back to our tests and we scroll all the way down they're still failing for some reason that's strange i wasn't expecting that actually this i should go here and let's see why that's failing and it's saying um so nothing was rendered from render this usually means that the return statement so returns null okay i kind of find that strange actually so over here we have okay so nothing was rendered from render huh that is strange it's over oh and that's because well we forgot to return so you of course have to return our component so remember this has to be a valid component makes you have to return the jsx so let's go over here and let's quickly save that and now our tests pass okay cool all right so now that we got that out of the way let's start looking at some of the different assertions because now all we're doing right now is just saying hey is this element in the document and we might want to do many many other assertions so another assertion that we might do is if we have only one task left so if we pass in one we expect it to use the singular task so let's actually copy this and let's go here and let's just change this block so let's just change this block we're going to say should render we're going to say task singular when the number of incomplete tasks incomplete tasks is one and so if we pass in one we should see the singular version so one task left so we can also assert this so we can go ahead and this is a very valid test so this is these are two very valid tests so now let's just go and let's write some other tests that we're just going to use to learn more about assertions but i think for this particular component these two tests really do the trick but again let's just go ahead and let's just write some more tests just to test more about uh assertions so i'm just going to go ahead and copy this and i'm going to go and paste it and now let's start working with some other assertions so over here what we can do to look at the other assertions we can just do dot and we can see some of the many assertions that we can utilize to be to be called with to be falsy to be invalid to be less than most of these assertions they really speak from themselves and they're very self-documented to be null that makes sense to be truthy so to be truthy well it returns a true value and actually what we can do is we can actually hover over this and we can read more about the assertion when used when you don't care what the value is you just want to assert that it returns a true value so it shouldn't be false it shouldn't be zero it shouldn't be an empty string should it be null shouldn't be undefined shouldn't be not a number and well this is exactly going to be to be truthy so we can actually assert this and that is fine so you can see here that that test passed now we can also assert multiple different things and i won't be unfortunately i won't be able to go through all of the different methods because there are so many but again you can always hover over it and just read the documentation for it so the next one i want to talk about is to be visible this one's a very interesting one so again you can go ahead and hover over it and you can see here that this one allows you to check that the element is actually visible to the user so here what we're doing is we're checking if the element is in the document but here we're checking if the element is visible to the user so the element could still be in the document but not visible to the user so for instance for instance right now the element of course is going to be visible to the user but what if i went to that element that footer element and i added these styles so let's add these styles of opacity of zero so to me that's not going to be visible because the opacity is zero now it is still in the document but the opacity is zero so we can actually use to be visible to test hey is it visible to my user and you can see here that it fails so that is a really really good test so let me just go ahead and just get rid of that and now let's talk about some of the other some of the other ones that i'm going to introduce again won't be able to introduce all of them so let's go over here and let's do dot and then we can say here to contain so we are going to get a specific element and in this element we can assert that hey this element is going to contain the following html element so ep tag so we can go ahead and save this and this is a valid assertion now if we say hey we expect to get a h3 tag then that's not a valid assertion because what we all what we get back is a p tag so let's go here and let's just change the p tag all right so let's talk about some of the other ones i'm just going to go ahead and just explore some of the other ones let's see here so let's go let's do dot and we can also say to have text content so we can say to have text content so instead of saying that hey we want to assert that this is in the document we can say to have text content where is it to have text content and this text content is going to be one task left so let's say we weren't able to uh get the element by the text so we we got it by test id then we can assert that hey it has the text content of one task left now here we're kind of doing a double assertion but you can imagine maybe we got this you know get by we got it by the roll and paragraph and then we want to test the text content of that paragraph and that has failed for some reason because it's not in here i guess p tags don't have the role of paragraphs which really doesn't make that much sense to me again you can read more about the documentation for the role but you guys get the gist just to just to 100 prove this to you let me just add a test id so let's do data test id and i'm just going to call this para for paragraph so i'm going to go ahead and just copy this and i'm going to say here get by and then get by test id and i'm going to say para so go ahead i'm just going to quickly save this and we should get everything passing all right so let us now talk about the not a method that we can use so we can actually get the opposite of everything by adding dot not and then dot that method and that gets the opposite of what we want so we can over here if we save this well here we're saying that hey this should not be visible now it is visible so it is going to pass again if i went to this and i changed the styles so i changed these styles to opacity of zero then it should not be visible and then this assertion should pass because here what we're saying is hey this shouldn't be visible now let's just get rid of these styles let's get rid of that not so we can see here that we can utilize that so let's just copy a test and let's just use that not if we want to so again we can put that not to get the opposite of the assertions that we have specified so we can say here something like dot not dot to be falsy so here we expect it to not be falsy so we can go ahead and save this and this should pass and it does all right so let me just check my notes um it seems like we have one more that i want to talk about so what we can also do is inside of here what we can do is this is a element and we can actually get some of the attributes of that element so we can get some the value for instance of that element and then over here what we can do at that point is do just a simple assertion a dot 2b assertion and then in here we're going to say hey we expect the value of the paragraph element to be well one task left so one task left so we can get the attributes and actually just assert the attributes themselves now why did why didn't that work uh let's see why didn't that work i'm a little confused actually um so we're getting by para getting by data test id which is para and we're it's a little confusing actually oh sorry we're not getting by value we're getting by text content so value is actually something that we only get from an input element but if you want to get the text content of a specific element then that's that's we would use text content instead you can see here that that passes all right so that is the assertions and we can actually assert multiple times inside of our test and so over here we can actually have multiple assertions now i don't recommend this i like to have one assertion per test because that really isolates where the error is happening inside of the test now let's say that these three pass but this one fails then that whole test is going to fail and then i have to diagnose which assertion is going which assertion is the one that actually failed so for instance over here let's just say i added five tasks left well we can actually do all these assertions but now we have one failed test and i have to figure out that well actually i guess it tells you that this is the one that fails so you can add multiple assertions in there if you want to so if i were to do one task left then everything is going to pass but again i this is not a very common approach you could do it if you want to but i recommend one assertion per test all right cool so that is pretty much it and i hope that introduces assertions to you in a very well manner and i'll see you guys in the next lecture

Original Description

Check out Laith's YouTube channel for more tutorials: https://www.youtube.com/channel/UCyLNhHSiEVkVwPSFKxJAfSA 🐱‍💻 Access the course files on GitHub: https://github.com/harblaith7/React-Testing-Library-Net-Ninja 🐱‍👤 Get access to extra premium courses on Net Ninja Pro: https://netninja.dev 🐱‍💻 Full React Course: https://www.youtube.com/watch?v=j942wKiXFu8&list=PL4cUxeGkcC9gZD-Tvwfod2gaISzfRiP9d 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →