<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Thor Würtzner</title>
    <description>The latest articles on DEV Community by Thor Würtzner (@thorwurtzner).</description>
    <link>https://dev.to/thorwurtzner</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F580760%2Ff0894c6d-4d76-401b-a9f9-d0790be81912.png</url>
      <title>DEV Community: Thor Würtzner</title>
      <link>https://dev.to/thorwurtzner</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thorwurtzner"/>
    <language>en</language>
    <item>
      <title>Cellular Automata - Wonders of Emergent Patterns from Biology to Computer Science</title>
      <dc:creator>Thor Würtzner</dc:creator>
      <pubDate>Wed, 31 Jul 2024 10:13:31 +0000</pubDate>
      <link>https://dev.to/thorwurtzner/cellular-automata-wonders-of-emergent-patterns-from-biology-to-computer-science-156f</link>
      <guid>https://dev.to/thorwurtzner/cellular-automata-wonders-of-emergent-patterns-from-biology-to-computer-science-156f</guid>
      <description>&lt;p&gt;I live with a molecular biologist, and her passion for topics like the brain and cells often leads to fascinating (if sometimes very &lt;em&gt;scientific&lt;/em&gt;) conversations. So when I started researching the concept of Cellular Automata, I saw a wonderful subject that combined both of our fields in an unexpected way. If there's one overall takeaway from this post, it's that &lt;strong&gt;math truly is everywhere!&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  So what is it?
&lt;/h2&gt;

&lt;p&gt;in a nutshell; Cellular Automata are mathematical models that simulate complex systems from &lt;em&gt;surprisingly&lt;/em&gt; simple rules. &lt;br&gt;
Developed initially by John von Neumann and later popularized by Stephen Wolfram. Ever heard of Wolfram|Alpha - the incredible math engine used by countless students and experts alike? The research company behind it was founded by this guy.&lt;/p&gt;

&lt;p&gt;His findings in 1983 showed incredibly complex and unpredictable behavior, and describing a very powerful tool for modeling real-world phenomena.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic principles
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Representation
&lt;/h3&gt;

&lt;p&gt;Cellular Automata are typically represented by a grid of cells. Each cell can be in one of a finite number of states. For example, in the simplest form of Cellular Automata, each cell is either "on" or "off" (binary states). The grid can be one-dimensional, two-dimensional, or even higher-dimensional, though two-dimensional grids are the most commonly studied.&lt;/p&gt;

&lt;h3&gt;
  
  
  States and Rules
&lt;/h3&gt;

&lt;p&gt;The rules of Cellular Automata determine how the state of each cell evolves over time. These rules are usually based on the states of neighboring cells. For example, a rule might specify that a cell turns "on" if exactly two of its neighbors are "on" and turns "off" otherwise. Despite the simplicity of these rules, they can produce insanely complex and varied behavior.&lt;/p&gt;

&lt;h4&gt;
  
  
  Game Of Life
&lt;/h4&gt;

&lt;p&gt;The Game of Life, developed by mathematician John Conway, is perhaps the most famous example of Cellular Automata . In this two-dimensional grid, each cell is either alive or dead. The rules for the Game of Life are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Any live cell with fewer than two live neighbors dies (underpopulation).&lt;/li&gt;
&lt;li&gt;Any live cell with two or three live neighbors lives on to the next generation.&lt;/li&gt;
&lt;li&gt;Any live cell with more than three live neighbors dies (overpopulation).&lt;/li&gt;
&lt;li&gt;Any dead cell with exactly three live neighbors becomes a live cell 
(reproduction).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;My rendition of the phenomena, built in Javascript&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fokjjaxhm28geoluv883w.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fokjjaxhm28geoluv883w.gif" alt="Game of Life" width="446" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Sand Simulation
&lt;/h4&gt;

&lt;p&gt;This simulation models the behavior of falling sand grains, showing how simple rules can create realistic-looking phenomena. Each cell represents a grain of sand, air, or a solid surface, and the rules dictate how sand grains move in response to gravity and collisions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;My rendition in Javascript and HTML canvas&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frzksccdbhde8s1u90xdy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frzksccdbhde8s1u90xdy.gif" alt="Sand simulation" width="566" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  So, how does it actually work?:
&lt;/h2&gt;

&lt;p&gt;From these examples, we can draw out three phases of life for a cell:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Initialization
&lt;/h3&gt;

&lt;p&gt;The starting conditions, or initial state, of the Cellular Automata grid are crucial. These conditions can be randomly generated, or they can be set up in a specific pattern to observe how different configurations evolve over time.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Rules
&lt;/h3&gt;

&lt;p&gt;The rules of Cellular Automata define the "neighborhood" of a cell (the set of cells that influence its state) and the state transitions (how a cell's state changes based on its neighbors). In the Game of Life, for example, the neighborhood consists of the eight surrounding cells, and the state transitions are defined by the rules mentioned earlier.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Iteration
&lt;/h3&gt;

&lt;p&gt;Cellular Automata evolve over discrete time steps, with each cell updating its state simultaneously based on the rules. This process is repeated for many iterations, often resulting in intricate and unexpected patterns.&lt;br&gt;
&lt;em&gt;We'll see soon this show up in weird ways!&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Emergent Patterns and Behavior:
&lt;/h2&gt;

&lt;p&gt;One of the most fascinating aspects of Cellular Automata is this weird emergence of complex behavior from simple rules. Where larger patterns and structures arise out of &lt;em&gt;local&lt;/em&gt; interactions.&lt;/p&gt;

&lt;h4&gt;
  
  
  Self-replication and Growth
&lt;/h4&gt;

&lt;p&gt;Some Cellular Automata rules can lead to self-replicating structures, which are patterns that produce copies of themselves. These structures can be used to model biological processes like cell division and growth.&lt;/p&gt;

&lt;h4&gt;
  
  
  Oscillators and Gliders
&lt;/h4&gt;

&lt;p&gt;In 2D Cellular Automata like the Game of Life, certain patterns repeat infinitely, called oscillators:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsddaibpsojl7fe8mtq94.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsddaibpsojl7fe8mtq94.gif" alt="Oscillator repeating" width="40" height="48"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is incredible when thinking in the aspect of biological life - these cells are in harmony, and in a way 'perfect'.&lt;/p&gt;

&lt;p&gt;Others travel across the grid, called gliders:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F72shoj47oysz07u0hrqb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F72shoj47oysz07u0hrqb.gif" alt="Glider travelling" width="114" height="114"&gt;&lt;/a&gt; Showing interesting applications in how human cells might be travelling through our insides.&lt;/p&gt;

&lt;p&gt;These patterns aren't just visually interesting but also demonstrate how complex the end result can actually get - and how we're moving in the direction of what Stephen Wolfram described as 'Scientific Philosophy'.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stability and Chaos
&lt;/h3&gt;

&lt;p&gt;Furthermore Cellular Automata can exhibit a range of behaviors from stable (where the grid reaches a steady state like the Oscillator mentioned above) to chaotic (where patterns change unpredictably over time). The study of these behaviors helps scientists understand the transition between order and chaos in complex systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Applications of Cellular Automata:
&lt;/h2&gt;

&lt;p&gt;Now here's where it gets interesting. Until now we've simply described how simple rules gives complex results, yada yada - but remember how i told you that the iterations showed up in &lt;em&gt;very weird ways&lt;/em&gt;?&lt;/p&gt;

&lt;h4&gt;
  
  
  Simulations of natural phenomena
&lt;/h4&gt;

&lt;p&gt;Phew, well here we go. Cellular Automata can even be used to model biological processes like the development of organisms, pattern formation, and the spread of all kinds of diseases. Take these patterns on seashells for example.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8j7jzc00zqvm4rt82vyu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8j7jzc00zqvm4rt82vyu.png" alt="A shell that shows Cellular Automation in nature" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Like, man, this is legit just Conway's Game of Life from above - I don't know about you, but I'm pretty blown away.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn7hxicu2g6znbawdzhyi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn7hxicu2g6znbawdzhyi.png" alt="Lizard scale" width="800" height="361"&gt;&lt;/a&gt;&lt;br&gt;
Seeing the rules we specified earlier of how each cell moves through different iterations, now in real life, is nothing short of incredible.&lt;/p&gt;

&lt;p&gt;To keep it 'Computer Sciency' and not just about Lizards, Cellular Automata have actually also been proposed for public-key cryptography, which makes sense given their complexity. The evolution of Cellular Automata patterns over numerous iterations is highly unpredictable, making it extremely difficult to reverse-engineer or predict previous states. This inherent unpredictability provides a robust foundation for encryption - but if I'm not wrong, it's still just a theory and haven't been implemented as of yet.&lt;/p&gt;

&lt;p&gt;Simply put, this stupid little thing finds its place in an incredible array of contexts—from complex computer systems to small lizards whose scales provide camouflage, helping them hide from predators while they search for food.&lt;/p&gt;

&lt;p&gt;As research in Cellular Automata continues, our understanding of these emergent behavior improves, and who knows where the next application will find itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Cellular Automata provide a unique lens through which we can explore the complexity that arises from simple rules. As we delve deeper into their properties and behaviors, we continue to uncover new insights that could revolutionize various scientific and technological fields.&lt;br&gt;
And it's also just freaking cool!&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading and Resources:
&lt;/h2&gt;

&lt;p&gt;I've only scratched the surface, there's all kinds of definitions like Wolfram's class system etc. - so a whole world is available to read about if this simple introduction was interesting.&lt;/p&gt;

&lt;p&gt;Here's are some influential papers and books:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stephen Wolfram's "A New Kind of Science"&lt;/li&gt;
&lt;li&gt;Toffoli and Margolus: "Cellular Automata Machines: A New Environment for Modeling"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://sandsimulatorthorw.netlify.app/" rel="noopener noreferrer"&gt;Experiment with my sand simulation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://game-of-life-thorw.netlify.app" rel="noopener noreferrer"&gt;Experiment with my version of 'Game of Life'&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'll end on a beautiful and relevant quote from the great Stephen Wolfram.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;...All the wonders of our universe can in effect be captured by simple rules, yet ... there can be no way to know all the consequences of these rules, except in effect just to watch and see how they unfold.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;

</description>
      <category>math</category>
      <category>computerscience</category>
      <category>lizards</category>
    </item>
    <item>
      <title>The modern way of setting up IndexedDB</title>
      <dc:creator>Thor Würtzner</dc:creator>
      <pubDate>Wed, 18 Aug 2021 13:04:12 +0000</pubDate>
      <link>https://dev.to/thorwurtzner/the-modern-way-of-setting-up-indexeddb-5bm</link>
      <guid>https://dev.to/thorwurtzner/the-modern-way-of-setting-up-indexeddb-5bm</guid>
      <description>&lt;p&gt;&lt;em&gt;Updated for Aug 2021&lt;/em&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Look! What is that? Is it a plane? is it localStorage?&lt;br&gt;
No, it's IndexedDB - an unnecessarily complicated low-level API for client-side storage of lots of structured data.&lt;br&gt;
I would like to meet the person who instantly understands how to implement it into their project, because he might have the ability to crack this modern version of the Enigma Code.&lt;/p&gt;

&lt;p&gt;Pathetic jokes aside; I was required to use this pretty cool way of storing data for a school project, and decided to document how I made it work. For my future self, and any other frustrated developer who has also started yelling at their rubber ducky for not giving the answer.&lt;/p&gt;

&lt;p&gt;To make it very clear, I know very little of what goes on behind the API, but simply how I personally made it work - and how you, future Thor, might also need to.&lt;br&gt;
SO! Let's get started.&lt;/p&gt;

&lt;p&gt;IndexedDB isn't exactly a normal SQL-based relational database, but more of a javascript-based object oriented one. Ever seen this type of data before? &lt;br&gt;
&lt;code&gt;key: "value"&lt;/code&gt;&lt;br&gt;
That's what we're working with, and it's great for simple applications like the Movie PWA I was building.&lt;br&gt;
Right out the box the API works by events, but I recommend ditching that and instead installing Jake Archibald's 'IndexedDB Promised Library', which pretty much mirrors the API, and uses... you guessed it. Promises.&lt;/p&gt;
&lt;h2&gt;
  
  
  Disclaimer
&lt;/h2&gt;

&lt;p&gt;Many tutorials like the one by Google, use an old version of the library, and will &lt;strong&gt;not&lt;/strong&gt; work if you follow it. Ask me how I figured that out.&lt;/p&gt;

&lt;p&gt;To understand how IndexedDB works, you first need to know the different ways of handling data therein.&lt;br&gt;
Before anything can be done, the database has to be 'opened'. I'm putting that in quotes, because you actually also need to 'open' it to create it, so you're kind of doing both at the same time.&lt;br&gt;
The way you'll probably see it imported in online tutorials is like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;idb&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;idb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You would use it like &lt;code&gt;idb.open()&lt;/code&gt;&lt;br&gt;
but since becoming a module we're instead required to import it like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;openDB&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;idb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h3&gt;
  
  
  Highest Level
&lt;/h3&gt;

&lt;p&gt;To start the whole process we'll open/create the actual database&lt;br&gt;
For that create an async function somewhere and write this inside.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;myDatabase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;openDB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Database_name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

   &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the highest level of the database.&lt;br&gt;
You can create as many as you want, but usually there's only one of these per app.&lt;/p&gt;
&lt;h3&gt;
  
  
  The Object Store
&lt;/h3&gt;

&lt;p&gt;This is one of the buckets that we're gonna store our data in, it's like a table in traditional relational databases.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;openDB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Database_name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nf"&gt;upgrade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name_of_store&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;keyPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id_of_the_thing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
     &lt;span class="p"&gt;});&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need the &lt;code&gt;upgrade&lt;/code&gt; if this version of the database hasn't been opened before, which it hasn't... I don't think.&lt;br&gt;
Honestly I would look this up if you need to, because the methods of IndexedDB are quite weirdly named and you might not need it in your project. But for the sake of not breaking mine, I'm keeping it for now.&lt;/p&gt;

&lt;p&gt;Inside of that, we're creating the &lt;em&gt;store&lt;/em&gt;, also called a bucket, or table. Well, It's where we &lt;em&gt;store&lt;/em&gt; our data. Ha.&lt;br&gt;
If your project breaks, or you need a random ID for your stuff, add an autoIncrement like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name_of_store&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;keyPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id_of_the_thing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;autoIncrement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Test time!
&lt;/h4&gt;

&lt;p&gt;Whenever or however you call your async function, an IndexedDB should show up in your browsers dev tools. You'll find it in the application tab, right next to localStorage.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdf5pwyswgmb3dt0uwyt9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdf5pwyswgmb3dt0uwyt9.png" alt="image" width="148" height="190"&gt;&lt;/a&gt;&lt;br&gt;
Cool. We have a database, and a store/bucket/table ready to receive our stuff.&lt;/p&gt;

&lt;p&gt;To add our first item to the store, stay inside our &lt;code&gt;async function&lt;/code&gt;, and write like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name_of_store&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;apple&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;delicious&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in the browser:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuivdqiywmvgpjl6z5m98.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuivdqiywmvgpjl6z5m98.png" alt="image" width="543" height="75"&gt;&lt;/a&gt;&lt;br&gt;
We have our first piece of data!&lt;/p&gt;

&lt;p&gt;But... how do you get the thing back you ask?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myThing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name_of_store&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 1 is the id of our apple in the store. If you specified it to be something specific instead of an auto incremented one, you of course use that instead.&lt;br&gt;
The variable &lt;code&gt;myThing&lt;/code&gt; now has an object ready to be used!&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvvr03d4jgxyewokw029m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvvr03d4jgxyewokw029m.png" alt="image" width="309" height="74"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Full code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;myDatabase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// It's a good idea to have this error handling just in case&lt;/span&gt;
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;indexedDB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This browser doesn&lt;/span&gt;&lt;span class="se"&gt;\'&lt;/span&gt;&lt;span class="s1"&gt;t support IndexedDB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;openDB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Database_name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;upgrade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name_of_store&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="na"&gt;keyPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id_of_the_thing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="na"&gt;autoIncrement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;

   &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name_of_store&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;apple&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;delicious&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;

   &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myThing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name_of_store&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myThing&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So that kind of ends this simple setup.&lt;br&gt;
It's incredible how little help you're able to find online regarding all of this, so hopefully this will help myself in the future, and maybe some lost soul will stumble upon it as well.&lt;br&gt;
There's loads of other features that can be used, but I won't get into that right now.&lt;br&gt;
You can find more examples on Jake Archibald's library readme:&lt;br&gt;
&lt;a href="https://github.com/jakearchibald/idb" rel="noopener noreferrer"&gt;https://github.com/jakearchibald/idb&lt;/a&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>webdev</category>
      <category>indexeddb</category>
      <category>storage</category>
    </item>
    <item>
      <title>React testing &amp; formatting made easy</title>
      <dc:creator>Thor Würtzner</dc:creator>
      <pubDate>Wed, 17 Feb 2021 19:14:39 +0000</pubDate>
      <link>https://dev.to/thorwurtzner/react-testing-formatting-made-easy-52o1</link>
      <guid>https://dev.to/thorwurtzner/react-testing-formatting-made-easy-52o1</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;It doesn't matter how beautiful your site is, if a push with some faulty code can crumble it all&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Testing fixes this.&lt;/p&gt;

&lt;p&gt;And luckily for us, react has already thought of it.&lt;/p&gt;

&lt;p&gt;Open up your favorite code editor and create a file named after one of your components, but add a .test. as a file extension.&lt;br&gt;
Example:&lt;br&gt;
&lt;code&gt;Category.test.js&lt;/code&gt;&lt;br&gt;
In here we're gonna be testing a mocked axios response.&lt;/p&gt;

&lt;p&gt;In here you wanna import everything you need, including but not limited to: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;your component&lt;/li&gt;
&lt;li&gt;mockedAxios from "axios"&lt;/li&gt;
&lt;li&gt;{ act, cleanup, render, screen, waitFor } from "@testing-library/react"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;we're gonna be using Jest, which is what facebook uses to test react apps - this means it's already included if we started our project with the create-react-app template.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;afterEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first function makes sure that the virtually rendered test is unmounted after each run, and the second tells the document that we're mocking an axios response.&lt;br&gt;
The whole point is to simulate what kind of response our component wants to use. This means that we're &lt;strong&gt;not&lt;/strong&gt; testing the actual API response, but our own mocked version.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shows our component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// This is where our test will be set up&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We wanna create an almost exact copy of what the real API response looks like, the reason it's not an exact copy is that the values don't matter - only the properties.&lt;/p&gt;

&lt;p&gt;So this could be our mocked response, but remember &lt;em&gt;it needs to have the exact same properties and nesting as the real thing&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;playlists&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                    &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="na"&gt;images&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                            &lt;span class="p"&gt;{&lt;/span&gt;
                                &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://via.placeholder.com/600&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                            &lt;span class="p"&gt;}&lt;/span&gt;
                        &lt;span class="p"&gt;],&lt;/span&gt;
                        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rock on&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;playlist&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                    &lt;span class="p"&gt;},&lt;/span&gt;
                    &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="na"&gt;images&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                            &lt;span class="p"&gt;{&lt;/span&gt;
                                &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://via.placeholder.com/600&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                            &lt;span class="p"&gt;}&lt;/span&gt;
                        &lt;span class="p"&gt;],&lt;/span&gt;
                        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dance moves&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;playlist&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                    &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to actually test this we will use an 'it' function, which takes a name parameter and callback function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shows a functional component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// Our test will run here&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside we need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mockedAxios.get.mockResolvedValue(response)&lt;/code&gt;
Which uses our fake response and simulates it as real.&lt;/li&gt;
&lt;li&gt;an &lt;code&gt;act&lt;/code&gt; function which closely simulates a browser environment by executing useEffects, and reduces amount of re-renders done. It takes a callback function as parameter.&lt;/li&gt;
&lt;li&gt;an &lt;code&gt;await waitFor&lt;/code&gt; function which also takes a callback function as parameter. We use await because of the outer &lt;code&gt;it&lt;/code&gt; function being asynchronous.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is what we're left with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this is a description of what the function does&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;mockedAxios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mockResolvedValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;act&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;waitFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside of the callback for the &lt;code&gt;act&lt;/code&gt; function, we need to render our component in the simulated browser.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;render &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; 
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside of the callback for the asynchronous &lt;code&gt;waitFor&lt;/code&gt; function, we need to declare a variable that looks for a specific piece of text on the screen in this simulated environment. For the test to pass, this piece of text obviously needs to be provided by the mocked axios response declared further above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/party/i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the premade script called "test", and it will get a pass!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To summarize&lt;/strong&gt;,&lt;br&gt;
we are not testing the actual finished product, or the real API response. We are simply making sure that the component is ready and able to &lt;strong&gt;use&lt;/strong&gt; the information it will eventually be provided and is designed for.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;afterEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shows our component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                    &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="na"&gt;images&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                            &lt;span class="p"&gt;{&lt;/span&gt;
                                &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://via.placeholder.com/600&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                            &lt;span class="p"&gt;}&lt;/span&gt;
                        &lt;span class="p"&gt;],&lt;/span&gt;
                        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;party&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;playlist&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                    &lt;span class="p"&gt;},&lt;/span&gt;
                    &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="na"&gt;images&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                            &lt;span class="p"&gt;{&lt;/span&gt;
                                &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://via.placeholder.com/600&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                            &lt;span class="p"&gt;}&lt;/span&gt;
                        &lt;span class="p"&gt;],&lt;/span&gt;
                        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dance moves&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;playlist&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                    &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shows a functional component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;mockedAxios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mockResolvedValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="nf"&gt;act&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;render &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;waitFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/party/i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Formatting
&lt;/h2&gt;

&lt;p&gt;The package &lt;code&gt;Prettier&lt;/code&gt; makes sure our code is formatted in a uniform way. Useful for examples like sharing code and working together, it creates a consistent style by making sure the files follow a specific set of rules.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install prettier -D&lt;/code&gt;&lt;br&gt;
This needs to be a dev-dependency for later!&lt;/p&gt;

&lt;p&gt;Now these files need to created in your root folder:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;prettierrc.json, to let your code editor know that you're using prettier. This is your config file.&lt;/li&gt;
&lt;li&gt;.prettierignore file so your code editor knows which files shouldnt be formatted&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There's a bunch of different options that can be input in the json config, so instead of writing them all out i'll link you to the official documentation&lt;br&gt;
&lt;a href="https://prettier.io/docs/en/options.html" rel="noopener noreferrer"&gt;prettier options&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An easy example could look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;printWidth&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;useTabs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;semi: true,
   &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="nx"&gt;singleQuote&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;: false,
   &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="nx"&gt;quoteProps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;consistent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bracketSpacing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;arrowParents&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;avoid&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes sure that even though Bob from your office loves to use 31 tabs between variable declarations and not use a single line break, you won't have to deal with it when he sends you the file for review. &lt;br&gt;
And there's potential options to deal with ALL of your co-workers "personal formatting preferences", and you don't even have to confront them about it - great right?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now for the ignore file&lt;/strong&gt;&lt;br&gt;
Most importantly, add your node_modules folder to the .prettierignore file.&lt;br&gt;
some other good ideas are 'build', 'coverage' and '.vscode', but like the official documentation recommends, you can mostly just copy everything from your .gitignore file.&lt;/p&gt;

&lt;p&gt;To format all files with prettier:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new script in package.json called "prettier", with a value of &lt;code&gt;"prettier --write ."&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Go to the console and &lt;code&gt;npm run prettier&lt;/code&gt;, this uses the default options and ones written in your config file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you so desire, theres the possibility of changing the script value to &lt;code&gt;"prettier --write src/"&lt;/code&gt; to only format files in the src folder etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  A bit of a warning!
&lt;/h2&gt;

&lt;p&gt;We automatically use eslint because of react, so we need to install a package that lets Prettier work nicely with it. This simply disables some eslint options that might interfere.&lt;br&gt;
&lt;code&gt;npm install eslint-config-prettier -D&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pre-commit hooks:
&lt;/h2&gt;

&lt;p&gt;Quick to set up, but incredibly useful.&lt;/p&gt;

&lt;p&gt;To make sure Prettier always formats your files before committing,&lt;br&gt;
go to the console and write as so:&lt;br&gt;
&lt;code&gt;npx mrm lint-staged&lt;/code&gt;&lt;br&gt;
This is a package that simply runs Prettier before every commit.&lt;/p&gt;

&lt;p&gt;If you also wanna make sure you don't commit something that has failed one of your tests, go to the console once again:&lt;br&gt;
&lt;code&gt;npm i husky&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npx husky install&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npx husky add .husky/pre-commit "npm test"&lt;/code&gt;&lt;br&gt;
Add cross-env CI=true to your test script like so:&lt;br&gt;
&lt;code&gt;npm i cross-env&lt;/code&gt;&lt;br&gt;
&lt;code&gt;"test": "cross-env CI=true react-scripts test"&lt;/code&gt;&lt;br&gt;
Now the "npm test" script will always be ran before a commit, and if it fails, the script will end itself.&lt;/p&gt;

&lt;p&gt;Now there's no way around formatting the code, take that -31 tabs Bob-&lt;/p&gt;

</description>
      <category>testing</category>
      <category>react</category>
      <category>jest</category>
      <category>mocking</category>
    </item>
  </channel>
</rss>
