Build test to custom hooks created with Atomico with an isolated instance and predictable
The "atomico/test-hooks" submodule is a direct part of Atomico's core and will allow you to run the customHook in a controlled way without the need for webcomponents.
function that creates an isolated context that is shared globally with Atomico hooks when executing the load method
Where:
optionalRender: Callback that allows restarting the hook's life cycle, this callback will be executed every time a hook like useState or useReducer requests the scope update, Atomico uses it to render the webcomponent again.
opcionalHost: allows to share an object for the hook useHost, Atomico uses it to share the instance of the webcomponent.
Where:
load: Function that allows to load the hooks to the temporary global context of Atomico.
clearEffect: Function that activates the collector of useLayoutEffect, when executing clear Effect a callback will be returned that allows ending the collector for useEffect, closing the cycle of secondary effects.
If clear Effect receives true as parameter, it communicates the unmounted.
./use-counter.js: customHook to test.
./use-counter.test.js: The example is based on the test environment from @web/test-runner.
function that allows to keep the render on a single node for each execution of it
, the container will be unmounted at the end of each execution of it
.
This allows each fixture execution inside it
to be related to the container used for the test, allowing you to fily test external DOM changes, example:
Atomico optimizes the execution of your script by minimizing resources through the rendering control.
Atomico's render cycle works like this:
First, when the component is instantiated, 3 promises will be created pending resolution:
componentInstance.mounted
: resolved once the connectedCallback
has been executed by the customElement.
componentInstance.updated
: the render cycle for first mount or update is encapsulated in componentInstance.updated
.
componentInstance.unmounted
: resolved once the disconnectedCallback
has been executed by the customElement.
Render or rerender cases are:
First render.
Updating a property or attribute.
Update dispatched by a hook
Remember all updates are stacked into a single big loop of componentInstance.updated.
This improves the testing experience since to observe the changes you only have to wait for the resolution of componentInstance.updated
, example:
The first rendering as the update of a prop will call to execute again the function that defines our component, Atomico internally stores the references to support the Hooks api and will render the virtualDOM returned by said function, this is encapsulated within componentInstance.updated
Atomico allows modifying its life cycle in cases of SSR, improving the rendering of the CSS in case of SSR and avoiding the effects of useEffect and useLayoutEffect
Technique to reuse the DOM generated by the SSR, Atomico only in the first render of the component that declares date-hydrate
will retrieve the existing DOM in the DOM not created by Atomico to relate it to the virtualDOM, avoiding creating the node again.
Atomico by default has a good performance, but it can be further optimized if certain techniques are applied.
A static node is equivalent to using node.cloneNode(true)
The renderOnce property renders the node only once, one of the advantages of this technique is that the virtualDOM can access the scope of the function.
We recommend the @web/test-runner tool, if your project was created with the command npm init @atomico
the test environment will be preconfigured, you can see this pre-configuration at https://github.com/atomicojs/base/tree/2-started.
To scale the test environment you can complement with tools such as: