Giter Club home page Giter Club logo

lm-code-testing-react-components-aliens-solution's Introduction

Testing React Components - Model Solution

This isn't a perfect solution, because such a thing doesn't exist!

But it is a decent solution.

โš ๏ธ We didn't expect you to use the techniques shown here in your own solutions! They go beyond the instructions given in the assignment and a new TypeScript operator keyof is used in a couple of places. But having worked on this form yourself, you can understand the approach taken in this repo and begin to integrate some of these techniques in your future projects. ๐Ÿ™Œ

Exploring this Solution

๐Ÿ‘‰ Run the app with npm install and npm start and try out the form.

๐Ÿ‘‰ Explore the code, reading the comments. Ask in Slack if there's anything you're not sure about!

Things worth Noticing ๐Ÿ‘€

๐Ÿ‘‰ Notice the simple state. useState is only used 3 times!

1๏ธ. An object to hold the data for W12MForm

  1. A boolean flag to track if the submit button has been pressed

  2. A boolean flag to track if the user has touched each input


๐Ÿ‘€ There are zero uses of useEffect - this app does not interact with the outside world!

๐Ÿ‘‰ Notice the validation uses a touched flag to only display messages once the user has interacted with an input.

๐Ÿ‘‰ Notice that the validation errors are broken into multiple specific errors using a string[] - in other words, we don't give users a single vague error like "Must be more than 2 chars and less than 23 chars and not contain numbers or special characters". We can say EXACTLY which rule (or rules) they are breaking, which is a much nicer user experience.

๐Ÿ‘‰ Notice the use of the new keyof operator to make our state updates very simple - each input uses the exact same onChangeHandler function, which is only defined once.

๐Ÿ‘‰ Notice the validation functions are defined for each input as an array of basic rules. This means we can write tests for those basic rules and then just combine them as we need for each input. It's very easy to look at the function for each input and see exactly what the rules are, or change them.

๐Ÿ‘‰ Notice the tests for the TextInput and SelectInput and understand all of the tests being applied. Explore all the other tests too!

lm-code-testing-react-components-aliens-solution's People

Contributors

neil-hughes avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.