Node only has a global object. And as you probably know, global object is not the window object. And our components use — and this is just a sample — CreateElement, QuerySelector, getElementById, OnClick, IntersectionObserver, InnerHeight, Location, LocalStorage, Navigator, History, and many, many more. So this is just a sample. And all of these are not supported within Node, so we need some way to implement them or to use them. And that's a problem.
So our components use window functionalities. It means that we need an object. So let me help you with this. I present to you TestEnvironment.js DOM. I believe most of you have this line in your Just Config file. What this line does is it creates a window object for everyone to use. It means that once we have this line in our Just Config, we'll be able to use window within our testing. But what is js-dom? Js-dom or happy-dom, which are basically competitors, are JavaScript implementation of the DOM. Unfortunately, they don't contain all functionalities. If you ever tried to click on an anchor within a test, you probably saw that the location isn't changing. That's because it's not implemented within js-dom. So there's no navigation, and there's also no layout, meaning that all of the components are sitting one on top of the other, and there's no actual layout within that specific environment.
Another problem, maybe the most serious problem, is that js-dom and happy-dom are slower than browsers. And the reason for that is that both of them are a JavaScript implementation of the DOM, and the browsers, real browsers, are usually written with a lower level language, so they are probably much, much faster. Just for an example, a query selector all run within js-dom takes about four milliseconds to complete, and that's quite a lot when we're talking about tests. It should probably be as fast as possible. So now that we've understood where we're rendering to, let's look at the code for the specific render within the Arc testing library. This is the GOAT logo, and this is the React testing library logo. It means that the code we're seeing now is in fact specific to React testing library, and every framework we have has a specific implementation for these specific functionalities. So what we have here, this is a set of code. It means that I removed some stuff that weren't relevant for this slide because it would only make confuse us. At the first line, what we're doing is we're creating a react route. React route is what you're also probably creating within index.js or index.js because we must have a React route to render React components. Right after we have that React route, we're pushing references to the container and the route so later on we'll be able to clean it up.
Comments