OpenReplay
Navigate back to the homepage
BLOG
Browse Repo
Back

Getting Started with React Cosmos

Origho Precious
August 23rd, 2021 · 7 min read

As React developers, we break complex components into smaller bits to reduce complexity and also to make testing easier. React Cosmos further reduces that complexity by giving us a sandbox to easily test, see and iterate quickly while building these components.

React Cosmos is a library that provides a sandbox environment for developing and testing React components in Isolation.

Think of it like a library that provides a way to isolate components and be able to see what they look like (their UI), make changes, and iterate quickly. In the sandbox, we can change props and see our components reflex the changes instantly.

It only supports CRA and Nextjs officially, It also supports the use of Typescript.

The only requirements to use react cosmos are React 16.8 (or newer) Node 10 (or newer).

Why Use React Cosmos

There are many ways to test component UIs and some testing frameworks help us achieve that, to mention but a few react-testing-library, where we write tests to check what a component has, for example, if we are testing a form component, we will write tests to check it a button is rendered, if there are input and/or select tags, etc and we usually see the results in our terminals but with React cosmos, we have a visual way to test our components (Visual TDD) which makes testing easier.

React cosmos also comes with hot-reload so we can see changes made to our components in the sandbox instantly, and it provides a medium for quick testing with prop changes and state changes right there in the cosmos explorer.

Getting Started with React cosmos

Now that we have an overview of React cosmos, let’s see how to work with it by testing components of a food ordering app (no checkout & authentication) with it in isolation.

To get started, we will clone the food ordering app from here. You can do that by running the command below in your terminal (make sure you have git installed in your machine).

1git clone https://github.com/Origho-precious/cosmos-food.git

After cloning the repository, navigate into the project folder and open it up in your favorite code editor.

In this project, we are using tailwind for styling, and react-router-dom for routing.

Install project dependencies by running yarn install or npm install in your terminal.

Let’s quickly go over the components we have in this project.

In the src/components folder, we have 4 components:

  • Button - a simple button component
  • CartCard - is used to show details of a cart item, and we can also update cart item quantity from this component.
  • Checkout - shows a summary of the cart (sub-total price, delivery fee, and total price).
  • FoodCard - shows food details and a button to add food to the cart.

And in the src/pages, we have 2 components, these are the 2 pages in the app.

  • cart - this component renders all cart items and the checkout component
  • home - this is the app’s landing page, it primarily renders food items and a navbar.

Now let’s start the app to see what it looks like. Start your dev server with

1yarn start

OR

1npm start

You should see these on your browser.

/ route

/cart route when cart is empty

/cart route  after adding 3 items to cart

In the next section, we will start testing components in this app with React cosmos.

Using React Cosmos

Let’s install the React cosmos package with the command below.

1yarn add react-cosmos -D

To use react cosmos with create-react-app, we need to add and change a few things. Firstly, create a cosmos config file, In our project root folder, create a file called cosmos.config.json, inside it add the code block below

1{
2 "staticPath": "public",
3 "watchDirs": ["src"],
4 "webpack": {
5 "configPath": "react-scripts/config/webpack.config"
6 },
7 "globalImports": ["src/styles/main.css"]
8}

We are telling cosmos to watch the src folder and watch out for static assets in our public folder, we are also pointing cosmos to where to get our webpack config file from, and finally, we add our CSS import as seen in line 7.

The next step is to add a package that makes cosmos work well with create-react-app, without this we will run into an issue with cosmos and create-react-app hot-reload. learn more about it here. Run the command below in your terminal

1yarn add cross-env -D

Let’s update our package.json file, to include cosmos scripts

1"scripts": {
2 "start": "npm run watch:css && react-scripts start",
3 "build": "npm run watch:css && react-scripts build",
4 "test": "react-scripts test",
5 "eject": "react-scripts eject",
6 "watch:css": "postcss src/styles/tailwind.css -o src/styles/main.css",
7 "cosmos": "cross-env FAST_REFRESH=false cosmos",
8 "cosmos:export": "cross-env FAST_REFRESH=false cosmos-export"
9 }

Start our cosmos server with

1yarn cosmos

OR

1npm run cosmos

After the server starts, go to http://localhost/5000 in your browser, you should see this.

React Cosmos explorer landing page with no fixture

If you see any error in your terminal, please go through the steps again.

Open Source Session Replay

Debugging a web application in production may be challenging and time-consuming. OpenReplay is an Open-source alternative to FullStory, LogRocket and Hotjar. It allows you to monitor and replay everything your users do and shows how your app behaves for every issue. It’s like having your browser’s inspector open while looking over your user’s shoulder. OpenReplay is the only open-source alternative currently available.

OpenReplay

Happy debugging, for modern frontend teams - Start monitoring your web app for free.

Writing Fixtures

I know you might be wondering what fixtures are, don’t sweat it. Fixtures are files containing default export of your React Components or React Node that will be shown in React cosmos explorer/sandbox.

There are two options to creating fixtures

  1. files are to end with .fixture.{js,jsx,ts,tsx} e.g Button.fixture.js or
  2. Create a folder inside src called __fixtures__ to house all your fixtures inside it like __fixtures/Button.jsx.

We will be using the second option to keep things a bit more organized. Let’s create our first fixture,

Button Fixture Inside src create a folder called __fixture__ inside create a file called Button.jsx. We are going to create a Button fixture with the code below:

1import Button from "../components/Button/Button";
2
3export default (
4 <div className="w-60 mx-auto">
5 <Button>Cosmos</Button>
6 </div>
7);
  • First we imported the component we want to test
  • We exported it as default, notice the div and classes, I did that to resize the width of the button and center it horizontally.

Go to your Cosmos explorer, you should see this

null

Notice the Button looking exactly like it is in our app, and the children prop we passed showing there.

On the right panel, we can directly update the prop passed to the button component, play around with it and see how it changes immediately,

To further test the Button’s UI, let’s add a variant prop so we will have a primary and secondary Button variant.

Go to src/components/Button/Button.jsx and update it to look like this

1const Button = ({ children, variant = "primary" }) => {
2 return (
3 <button
4 className={`block w-full h-12 ${
5 variant === 'primary' ? "bg-black" : "bg-blue-600"
6 } hover:opacity-70 mt-3 text-sm rounded-sm`}
7 style={{ color: '#fff', border: "none", outline: "none" }}
8 type="button"
9 >
10 {children}
11 </button>
12 );
13};
14export default Button;

We just updated our Button component to have a prop called variant which updates the background color of the Button.

Now to go src/__fixtures__/Button.jsx and update it to look like this

1import { useSelect } from 'react-cosmos/fixture';
2import Button from "../components/Button/Button";
3
4export default () => {
5 const [variant] = useSelect('variant', {
6 options: ['primary', 'secondary'],
7 });
8
9return (
10 <div className="w-60 mx-auto">
11 <Button variant={variant}>Cosmos</Button>
12 </div>
13)};

Above we updated our Button fixture to include a variant prop as we did the Button component and also we introduce a Control panel hook useSelect, with this hook we are able to set options of the variant prop, so in our sandbox, we can select from the options we have other than typing it manually. There’s also another control panel hook React cosmos offers useValues with it, we can update data like count, etc

Let’s see Button component in the explorer. On the right, you can select either of the two variant options to change the variant prop and see the background-color change accordingly.

updated Button fixture

We have now successfully created a Button fixture and tested it in our Cosmos sandbox (explorer).

Let’s continue by creating a Checkout fixture.

Checkout Fixture Just like we created the Button fixture, in src/__fixtures__/ create a new file called Checkout.jsx and add the code below

1import Checkout from "../components/Checkout/Checkout";
2
3const data = {
4 subTotal: 100,
5 deliveryFee: 5,
6 total: 105,
7};
8
9export default (
10 <div className="w-60 mx-auto">
11 <Checkout
12 subTotal={data.subTotal}
13 deliveryFee={data.deliveryFee}
14 total={data.total}
15 />
16 </div>
17);

Above we created a fixture for our Checkout component using mock data. Let’s check what it looks like in our Cosmos sandbox, Click on it on the left panel. It should look like

Checkout fixture

Simple right? Now you can test it by passing different prop values to it. or play with it in the explorer.

Cards Fixture Here, we will create a different type of fixture called Multi fixture, It’s like the regular fixture but here, we can create more than one fixture from the same file. It’s easier than it sounds, let’s do it below to see how that works.

Create a file called Cards.js in the __fixtures__ folder and add the following code

1import CartCard from "../components/CartCard/CartCard";
2import FoodCard from "../components/FoodCard/FoodCard";
3
4const food = {
5 id: 1,
6 name: "Peppered Chicken",
7 imgURL: "https://sisijemimah.com/wp-content/uploads/2015/07/image70.jpg",
8 price: "8.00",
9};
10
11const order = {
12 id: 1,
13 name: "Peppered Chicken",
14 imgURL: "https://sisijemimah.com/wp-content/uploads/2015/07/image70.jpg",
15 price: "8.00",
16 qty: 1,
17 updateList: () => {},
18};
19
20export default {
21 FoodCard: (
22 <FoodCard
23 id={food.id}
24 name={food.name}
25 price={food.price}
26 imgURL={food.imgURL}
27 />
28 ),
29 CartCard: (
30 <CartCard
31 id={order.id}
32 name={order.name}
33 price={order.price}
34 imgURL={order.imgURL}
35 qty={order.qty}
36 updateList={order.updateList}
37 />
38 ),
39};

Above we imported, FoodCard and CartCard components and created two objects to contain mock data for the FoodCard and CartCard components and passed them to the respective components as props.

Let’s see what a multi fixture looks like in our cosmos sandbox,

Cards fixture -  A multi fixture

You see now we have a Cards fixture and in it we have FoodCard and CartCard components, and we can click on them to see them. It’s more organized now right? Yeah, that’s how you can organize your sandbox as it gets larger.

Lastly, let us build a Page fixture - A multi fixture for pages (home and cart pages). we will do that below.

Pages Fixture Create a new file in __fixtures__ called Pages.js add the code below

1import Cart from "../pages/cart";
2import Home from "../pages/home";
3
4export default {
5 cart: <Cart />,
6 home: <Home />
7};

A very simple fixture to show what a whole page looks, They should look like this in our sandbox;

Home page fixture

Cart page fixture

We now have home and cart page fixtures, click on any of the cart icons on the FoodCard to add to the cart and navigate to the cart page to see what you add. You can also update the quantity of the items ordered on the CartCard and see the values update accordingly as well as the checkout values.

We can visually test our app now, we see how it functions by testing the components in isolation and testing how they work together also in isolation without starting our app’s dev server. This is the visual TDD React cosmos offers. Other than writing tests for every component and checking the results in our terminal, with React cosmos, we can see the component working instead.

Decorators Decorators help us wrap fixtures with some shared properties like CSS styles. To create a decorator we just need to create a cosmos.decorator.js file in the same folder as the fixtures we want to be wrapped with that decorator. Let’s create a decorator for all our pages, to just change the background color a bit. In src/__fixtures__/ create a file called cosmos.decorator.js and add the code below.

1export default ({ children }) => (
2 <div className="bg-gray-100 h-screen">{children}</div>
3);

We just wrapped our fixtures with a gray background color and made the page height 100vh. We have now successfully wrapped our fixtures with a decorator. Go over to the cosmos explorer to see the change.

Extending Responsiveness We can specify the viewports we want in our sandbox, specifying the devices as well as their height and width. Let do that below

Goto src/cosmos.config.json, update the file to look like this

1{
2 "staticPath": "public",
3 "watchDirs": ["src"],
4 "webpack": {
5 "configPath": "react-scripts/config/webpack.config"
6 },
7 "globalImports": ["src/styles/main.css"],
8 "ui": {
9 "responsivePreview": {
10 "devices": [
11 { "label": "iPhone 5", "width": 320, "height": 568 },
12 { "label": "iPhone 6", "width": 375, "height": 667 },
13 { "label": "iPhone 6 Plus", "width": 414, "height": 736 },
14 { "label": "Medium", "width": 1024, "height": 768 },
15 { "label": "Large", "width": 1440, "height": 900 },
16 { "label": "1080p", "width": 1920, "height": 1080 }
17 ]
18 }
19 }
20}

Now restart the cosmos server for it to reflect and go over the sandbox. On the top right, we have 4 icons. Click on the second from the left, you can now toggle the different viewports we just passed to it.

Cosmos sandbox icons

Static Export We can export the fixtures we created if we want to host it as a component library with any static hosting service like netlify. Simply run the cosmos:export command

1yarn cosmos:export

OR

1npm run cosmos:export

Conclusion

We have now completely tested our components using react cosmos and we have a component library running alongside our app with which we can quickly test components and we can add more fixtures as we keep building the app.

In this tutorial, we built a food ordering service and testing all the components using React cosmos to learn how it works.

Resources

More articles from OpenReplay Blog

Forever Functional: Waiting with Promises

Learn how to use promises to handle waiting for certain conditions to happen

August 22nd, 2021 · 5 min read

Optimizing the Performance of Your Vue Apps Using Web Workers

Take advantage of the power of Web Workers to optimize the performance of your Vue applications

August 17th, 2021 · 6 min read
© 2021 OpenReplay Blog
Link to $https://twitter.com/OpenReplayHQLink to $https://github.com/openreplay/openreplayLink to $https://www.linkedin.com/company/18257552