Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Playing around with React/Redux/Unit testing

Featured Replies

Hey all.

 

I'm building out a project purely for educational purposes, it is yet another todo app, but will have a load of features. The App itself - what it does is kind of irrelevant as it's intentions are to help cement my learning of React, Redux, React-router and how to test such an application with both unit and acceptance tests - where I will be using Ava and NYC

 

I thought I would share the work-in progress repository as I think others can learn from the good things I do in here along with the mistakes I will inevitably make. @@citypaul I am sure you will be able to add some good feedback on how the testing can be improved.

 

 

This is a WIP that I started a few days ago so you can watch how it gets built out. If anybody has any Qs I'll be happy to answer

https://github.com/rbrtsmith/redux-advanced-todo-app

  • Author

Ava is a JavaScript unit testing framework, I literally started using it today https://github.com/avajs/ava it is quite minimal and runs unit tests concurrently which means if you've a lot of tests they will run much faster.

 

NYC a command line interface for Istanbul https://github.com/gotwarlost/istanbul which is a test coverage suite. Test coverage tells you how much of your code is covered by your unit tests in the terminal, and it is also configured in my project to output a HTML file inside of /coverage/ that will display the results in a more human readable form.

 

I am sure @@citypaul can expand more being the testing guru that he is :D

  • Author

Updated. Now have the ability to edit existing todos. Unit tests have been added for this functionality and I have adhered to the write tests first best practice.

  • Author

One thing to note: You will only see 'class' in one component and no createClass. All other components are functional which should always be prefered unless you need to use a components lifecycle hooks or use state. In the case of this App Redux handles all state. In the case where I used 'class' I have to use the componentDidMount() lifecycle hook.

I recommend you do the same. Class is considered an antipattern in JS and unfortunately in my case here React provides no alternative. But they are looking at changing that in future releases...

  • Author

NPM scripts is fine if you are just running a couple of tasks but things become unweildy as complexity increases. I prefer something like Gulp here. Webpack is good too but I just chose Gulp here.

 

As for testing React components I will ad some tests for them but Dan Abramhov has spoken about not unit testing dumb components and focus more on testing the logic consumed by Redux. Apparently this is why Facebook don't pay much attention to the react-test-utils.

Whether or not this is is right or wrong it's what facebook appear to be doing. Although I do plan to add in enzyme and test non-dumb components.

Thank you very much for sharing this! I think, we need "pinned" theme where forum members can post theirs React app and questions about creating and testing React components.

  • Author

 

Ha. Well I'm using nigh****ch at the moment and have found it nicer than webdriver io. I'd be interested in knowing why he/she recommended webdriver io? We use webdriver for some things internally and it's just a massive source of pain for us - the docs are often either wrong or just missing things, often it just doesn't work as documented and the api itself is really inconsistent and just plain nasty to work with. I can tell you I'm certainly not the only person with that opinion in our workplace.

 

I should add that we've only used nigh****ch sparingly for some quite lightweight end to end tests so far, but so far I've preferred it over webdriver.

 

I shall ask him tomorrow, he's used both pretty extensively.

  • Author

Another update - I've added the ability to add new todos and cleaned up the interface and navigation a little. Tomorrow I plan to look at swapping out some of the text fields for select boxes, and investigate unit testing React components with Enzyme. All of the Redux reducers, action-creators and utility functions are tested.

  • Author

Added some more updates. Refactored the remove button into its own component so it can be dropped in on both the single todo view and the grid of todods.

  • Author

Added a preview component inside the <AddTodo /> component which updates the preview todo as you type. Added more unit testing now focusing on the components themselves using Enzyme.
https://github.com/rbrtsmith/redux-advanced-todo-app

Edited by rbrtsmith

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.