Exercise: Props
by Tomas Trescak· React Fundamentals

0 / 3280 XP

Presentation Transcript
If you prefer text to video, check out the transcript of the presentation above

Slide 1 ----------- Part 1: Let's practice! Slide 2 ----------- Part 1: In this exercise, you will create a reusable Greeting component. Initially, you will see these four failing tests. Slide 3 ----------- Part 1: Your goal is to make them all pass and render the following text on the screen. Slide 4 ----------- Part 1: These are the ways to use the Greeting component, with age being an optional prop. Part 2: Start with correctly defining the type of your props, providing names and types of the three component props. Part 3: Then, define your component. Make sure you specify that the default value of age prop is 44. Slide 5 ----------- Part 1: If you are not sure what the Greeting component has to show, check out the test file. There, you find a new type of test that uses a helper function "it" from the DOM library. This function accepts three parameters. Part 2: The first parameter is the name of the test, describing what the test expects. Part 3: The second parameter is the component that we test. This test helper temporarily renders the component in your browser and executes the test. After the component is tested, the component will be removed from the DOM. So, you don't need to render the expected texts in your main app, as the component is tested in isolation. Part 4: The last parameter is the implementation of the test function, where we test if the component renders everything that we expect.

Description
All the extra information about this section

In this exercise, I would like you to create a reusable Greeting component. Initially, you will see four failing tests: [https://skillpies.s3.ap-southeast-2.amazonaws.com/courses/full-stack-development/sections/props-exercise-in-react/Screenshot%202024-11-14%20at%2021.00.33.png] Your goal is to make them all pass: [https://skillpies.s3.ap-southeast-2.amazonaws.com/courses/full-stack-development/sections/props-exercise-in-react/Screenshot%202024-11-14%20at%2021.00.47.png]   🎯 GOALS To do so, you need to modify the component Greeting as per instructions in your test. Overall these ar the goals: 1. Show a greeting in the form Hello Tomas, male, age 50 2. Age is an optional prop, the default value is 44 ℹ️ HELP If unclear, please check out the index.test.tsx file. There, you find a new type of test that uses our helper function dom.it(name, component, test), which accepts three parameters: 1. name is the name of the test 2. component is the component that we render for the purpose of this test 3. test is the implmentation of the test function This test helper temporarily renders the component into DOM and executes the test. Afterward, this component will be removed from the DOM. // Example describe ("Test Greeting Component", function() { dom.it ("Renders Tomas", <Greeting name="Tomas" age={50} gender="male" />, async function() { assert.exists(await dom.findByText("Hello Tomas, male, age 50")); } ) }) 🤑 BONUS 1. Correctly define the type of props of the component Greeting 2. Make sure there are no errors 

Discuss with Others
Ask questions, share your thoughts, and discuss with other learners

Join the discussion to ask questions, share your thoughts, and discuss with other learners
Loading...
Ready ...
Content Locked
This content is only available to registered users.
Please register at the the home page.
Setup
React Fundamentals
10 points
Next.js
10 points
Advanced React
Databases
10 points
React Hooks
Authentication and Authorisation
10 points
APIs
CI/CD and DevOps
Testing React
Advanced Topics