KCD - Epic React / Articles Flashcards

1
Q

What is a nullish coalescing operator?

A

“<div>Sometimes you want to fall back to a default value if a value is null or undefined.</div><div><br></br></div><div>This would look like this:</div><div><img></img><br></br></div><div><br></br></div><div>But that was problematic for numbers or booleans where ““0”” or ““false”” could be valid values. So then we used to have to do this:</div><div><img></img><br></br></div><div><br></br></div><div>Now, we have a double question mark:</div><div><img></img><br></br></div><div><br></br></div><div>This returns the right side whenever the variable on the left side is null or undefined (not just falsey) - if the left side is 0 or falsey, it returns that instead.</div>”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you append directly to the page (instead of an element on the page)

A

“<div><img></img><br></br></div><div><span></span></div>”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is special about the children prop with React.createElement?

A

It is a special prop where if you have one element, that’s fine, but if you want multiple elements, you can either just keep adding arguments to createElement and they all get slurped up and thrown into children, or you can provide it as an array - either way it converts it into an array. (This will be useful when creating custom components)<br></br><br></br><br></br>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why do we have to use Babel?

A

Because JSX is not actually Javascript, so Babel is the code compiler that converts it into React.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What two things from JavaScript cannot go inside JSX curly braces?

A

You can’t do if/else conditionals, and you can’t define variables - that would have to happen above in Javascript land.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a component, really?

A

Basically just a JS function which returns something that is renderable (more React elements, strings, null, numbers, etc.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In order for JSX to interpret a function between angle brackets as a component, what is allowed?

A
  1. The function starts with a capital letter<div>2. OR a lower or uppercase with a dot proprty</div><div>3. OR an Upper_Snake_Case name</div>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of propTypes?

A

They give an error when we passed the wrong value (or no value) to a component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens if I pass the wrong kind of value into a prop typed component while in production?

A

Nothing. Prop Types add runtimee overhead, so they aren’t run in production.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you implement PropTypes?

A

“It’s a package made available through React. To use it, imagine a component named Message that requires it’s two props to be strings and to both be passed.<div><br></br></div><div><img></img><br></br></div>”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is the style prop different between HTML and React?

A

“In HTML you pass a string of CSS, while in React you pass an object of CSS.<div><img></img><br></br></div>”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Where does your form’s onSubmit function go?

A

On the form element itself, not on the submit button.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What do you need to add to an input and a label to associate them together?

A

The label needs an htmlFor= tag that matches the input’s id.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the browser do by default when a form is submitted?

A

It makes a GET request to the current URL with the values of the form as query parameters in the URL, which then causes a full page refresh. (only named values are submitted)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a SyntheticEvent?

A

Not a real event from the browser, but an object that React creates for us that looks and behaves exactly like a regular event. You likely won’t know the difference - they do this for performance reasons, and for event delegation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can you get the value of an individual input on a form?

A

event.target[0].value - where the index of the array will depend on which input you are trying to get it from.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What should you do instead of accessing inputs by targeting the order in which they appear on the form and why?

A

Instead, you should name your inputs, and then whatever you have given as the name or id is accessible by the form as form.name, you should do this because it is much more change resillient (if the order changes, you’re still ok.)<div><br></br></div><div><br></br></div>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Why is it better to give all form inputs an id instead of a name?

A

Either will associate the input as a call-able item on the form, but screen readers need the label to be associate to the input, and in addition, if your labels aren’t tied to inputs through their htmlFor matching the input’s id, then when you click the label, it won’t go to the input, which usually you do want to happen.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is a ref?

A

A ref is an object that stays consistent between renders of your React component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the value on a ref that can be updated to any value at any time?

A

ref.current

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Say you have an inputRef object created - how could you access the value?

A

inputRef.current.value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Describe useRef in terms of a box.

A

it’s like a box that can hold a mutable value in its .current value.<div><br></br></div>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What happens if you pass a ref object to React (like <div></div>

A

React will set its .current property to the corresponding DOM node whenever that node changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How is useRef helpful beyond just the ref attribute on an input?

A

useRef is handy for keeping any mutable values around, similar to an instance field in a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the difference between useRef and creating your own {current: …} object?

A

useRef will give you the same ref object on every render.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What should you keep in mind with regards to useRef?

A

It doesn’t notify you when its content changes. Mutating the .current property doesn’t cause a re-render. If you want to run some code when React attaches or detaches a ref to a DOM nodse, it’s probably better to use a callback ref instead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is a Controlled Form Input?

A

A Controlled Form Input allows you to programattically control your input. Maybe you want to set the value explicitly at the push of a button, or change what the value is as the user types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

How do you explicitly update the value of an input?

A

React allows us to to set a value prop on the input, like so:<div><div><br></br></div><div>With that done, the value set can only change if the value of the myInputValue variable changes.</div></div>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What happens if you have an input with a value but no onChange handler?

A

The input becomes read only since there is no way to change the value based on input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

How do you pre-fill a form input?

A

Just provide the defaultValue prop to the input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the issue with rendering elements by mapping over an array?

A

It’s totally fine, but when you’re interpolating an array of renderable elements, if you re-render that list with added or removed items, React isn’t able to know whether you added/removed from the middle, beginning, end, or whatever of that array. React just has to take its best guess - which sometimes works out ok, sometimes doesn’t.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What happens when rendering inputs by mapping over an array without a key?

A

There is state that exists in the that is managed by the browser, not by React. Without a key, React has no idea which element you return corresponds to the specific DOM node it removes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

How do you fix array rendered elements that don’t have a key getting all mixed up about which input goes with which element?

A

Just add a key to the list item (or whatever) being rendered - you can set it to the id of the item.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

How does React hold state?

A

With special functions called hooks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What are the two most prominent examples of React Hooks that return a value?

A

React.useRef & React.useContext

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What does React.useEffect return?

A

Nothing a’tall

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What is the definition of State?

A

Data that changes over time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Why doesn’t it work to just declare a variable and use it within the JSX where you’d like it to display on change?

A

Because a normal function is only run one time - when the component is initially rendered. There’s no mechanism to tell React that the value has changed - that’s where useState comes in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

What’s the use of React.useState?

A

It tells react to re-render anytime the value given changes (as opposed to just declaring a variable, which only renders on page load), and gives us an easy method with which to change it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

How do you set a default value for a destructured variable?

A

“<img></img>”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

How do we interact with local storage or make http requests?

A

Using the useEffect hook.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

What is React.useEffect?

A

“A built-in hook that allows you to run some custom code after React renders (and re-renders) your component to the DOM. It accepts a callback function which React calls after the DOM has updated.<div><br></br></div><div><img></img><br></br></div>”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

What’s a problem with reading from localStorage?

A

It can be slooow.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

Can you pass a function to the useState hook instead of an initial value?

A

Yes, and the benefit is - that function only gets run the first time, so things that are unnecessary in further renders can be put there.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What is lazy state initialization?

A

“React’s useState hook allows you to pass a function instead of the actual value, and then it will only call that function to get the state value when the component is rendered the first time.<br></br><div><br></br></div><div><img></img><br></br></div>”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

What if you only want useEffect to run when a particular value changes?

A

React.useEffect takes a second argument called the ‘dependency array’, which signals to React that your effect callback function should be called when and only when those dependencies change. This saves us from doing unnecessary work.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

What’s the catch when an object is in the dependency array of a useEffect?

A

React does a shallow comparison within the dependency array, so if you are dependent upon an object, and you have two objects with all the same properties, it will re-run the useEffect, even if they are exactly the same.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

What does useRef give you?

A

An object that you can mutate without triggering re-renders.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

Describe React’s hook flow

A

“<img></img>”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

What is the first thing React will do when the component has never been rendered before?

A

Run the lazy initializers (function arguments passed into useState or useReducer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

What is the hook flow on component mount?

A
  1. Run lazy initializers.<div>2. Render</div><div>3. React updates the DOM</div><div>4. Run LayoutEffects</div><div>5. Browser paints the screen.</div><div>6. Run Effects</div>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

What is the hook flow on Component Update?

A
  1. Render<div>2. React Updates DOM</div><div>3. Cleanup LayoutEffects from last time</div><div>4. Run LayoutEffects</div><div>5. Browser paints the screen</div><div>6. Cleanup effects.</div><div>7. Run Effects.</div>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

What is the React Hook Flow on component unmount?

A
  1. Cleanup LayoutEffects<div>2. Cleanup Effects.</div>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

How do you share state between two sibling components?

A

You lift the state to the lowest common parent between them, and place the state assignment there, and then passing the statee and a mechanism for updating it down to the child components that need it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

What is co-locating state?

A

Pushing state back down when it is no longer needed at a higher level - this isn’t strictly needed and will work fine passing it down, but it’s best to always keep state as close as possible to where it’s used. So, if it’s no longer used at a higher level, we have to push it back down to the lower levels.

56
Q

Why is it important to colocate state? (Keep state as close as possible to where it is used?)

A

It improves maintenence - you can use the component more easily with the state maintained within the component - you don’t have to do something above it each time to pass in. This makes the API much simpler, and encapsulates the management of the component’s own state to itself.<div><br></br></div><div>This also can lead to some performance improvement, as changes to the child won’t necessitate the parent to re-load, only the child would re-render.</div>

57
Q

Is each call to React.useState unique?

A

Yes, it gives you a unique state and updater function, even if they have the same name as the state and updater in a sibling component.

58
Q

What is the difference between managed state and derived state?

A

Managed state is state that you need to explicitly manage.<div><br></br></div><div>Whereas derived state is state that you can calculate based on the status of other state.</div>

59
Q

Why should you derive state instead of syncing it?

A

To avoid synchronization bugs and unnecessary complexity.

60
Q

What is derived state?

A

State that can be derived (or calculated) based on other values rather than managed on its own.

61
Q

What’s the first thing to do when building a new UI?

A

Make sure the state is managed, and that the UI is rendering properly.

62
Q

Why is mutation of state a problem in React?

A

It leads to unexpected bugs. React relies on the fact that any state change will trigger a re-render. Mutation can lead to stale, out of sync closures which reference the mutated value instead of the state value, and that leads to hard to track down bugs.<div><br></br></div><div>Instead, make copies of state, rather than mutating it.</div>

63
Q

What do you need to do to objects, array, etc. - things that aren’t strings - when saving to local storage?

A

JSON.stringify, and then JSON.parse when you pull them back out again.

64
Q

Is JSON parsing something out of localStorage on every update of state awesome?

A

Probably not, chief.

65
Q

What kind of access do you have to DOM nodes in your react render() method?

A

None. <div>hi</div> is just syntactic sugar for React.createElement() - you don’t actually have access.

66
Q

When are DOM nodes actually created within React?

A

Not until the ReactDOM.render method is called - you don’t have access to them prior to that (in the render() method, for instance)<br></br><br></br><div>Your component’s render method is really just responsible for creating and returning React Elements and has nothing to do with the DOM in particular.</div>

67
Q

How can you gain access to the DOM?

A

Using a special prop called ref.<div><br></br></div><div>This asks React to give you access to a particular DOM node when it renders your component.</div>

68
Q

How do you use useRef and useEffect together, and why?

A

“Here’s a simplified example:<div><img></img><br></br></div><div><br></br></div><div>After the component is rendered, that’s when it is considered mounted. After mount is when the useEffect callback is called - so, by that point, ref’s current property should be set to the DOM node. Thus, we have access to whatever element we needed within our useEffect.</div>”

69
Q

How do you give third-party libraries that need access directly to DOM nodes that access?

A

Using a combination of useRef and useEffect.

70
Q

What do you need to remember after you use direct DOM nodes with useRef & useEffect?

A

You’ll need to clean up after yourself on unMount, or you’ll have event handlers hanging around on DOM nodes that are no longer in the document.

71
Q

What happens if you put a useRef onto a node, but then try to call it directly, without a useEffect?

A

It would be undefined (or whatever initial value you gave it) because it hasn’t yet rendered to the page at that point - that’s why we end up using a useEffect to work with it, because that is called after mount, which is after render.

72
Q

When do you use the useRef hook?

A

Any time you want to maintain a reference to something, and you want to be able to make changes to that without triggering a re-render.<div><br></br></div><div>If you DO want to trigger a re-render, you should probably just use useState.</div>

73
Q

What can you return from the useEffect hook?

A

Nothing - except the cleanup function.

74
Q

How does the fact that you can’t return anything from a useEffect hook affect async/await?

A

“<img></img><div><br></br></div><div>Async/Await doesn’t work, because when you make a function async, it automatically returns a promise (whether you explicitly return a function or not).</div>”

75
Q

If you do want to use Async/Await within a useEffect, how should you?

A

“Just make sure it is fully contained within the useEffect - don’t declare the opening function as async, instead declare a separate async/await within the useEffect. Like thus:<br></br><br></br><img></img><br></br><div><br></br></div><div>That way, you return nothing, as useEffects should, rather than returning a promise from the async/await.</div>”

76
Q

What’s an easier way to do async/await stuff within useEffect hooks?

A

Extract all the Async code into a utility function, then call that and use the promise-based .then method instead of async/await syntax.

77
Q

Why should use a status or state machine instead of an isLoading boolean?

A

Because using isLoading can easily get out of sync and requires being extremely careful about when you set it in order to get the desired result.

78
Q

What statuses should you use?

A

idle, rejected, pending, resolved

79
Q

Where can you go to see Graphql errors when they aren’t showing anything in the display?

A

Queries that fail will be red on the network tab - click on those and it will show you the error.

80
Q

If you are setting multiple instances of state (say, setting date and a status at the same time), this can cause errors when you do it out of order.<div><br></br></div><div>What is a simpler way to handle this and make errors less likely?</div>

A

“Put all of your state into an object so that they are updated simultaneously.<div><br></br></div><div>aka not this:</div><div><img></img><br></br></div><div><br></br></div><div>but this:</div><div><img></img><br></br></div>”

81
Q

What happens if a render throws an error and it is unhandled by your application?

A

You application will be removed from the page, leaving the user with a blank screen…awkward.

82
Q

What is the special kind of component that is perfect for handling errors in your application?

A

An Error Boundary.

83
Q

How do you create an Error Boundary with a function call?

A

Currently, you can’t. You have to use a class component, unfortunately.

84
Q

How do you get React to hand an error to the error boundary?

A

You put ‘throw error’ in the code.

85
Q

What are Error Boundaries?

A

React component that<strong>catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI</strong>instead of the component tree that crashed.

86
Q

What do Error Boundaries not catch errors for?

A

<ul><li>Event handlers</li><li>Asynchronous code (e.g.<code>setTimeout</code>or<code>requestAnimationFrame</code>callbacks)</li><li>Server side rendering</li><li>Errors thrown in the error boundary itself (rather than its children)</li></ul>

87
Q

What makes a class component an Error Boundary?

A

It must define either (or both) of the lifecycle methods ‘static getDerivedStateFromError()’ or componentDidCatch()

88
Q

What are the uses of getDerivedStateFromError() and componentDidCatch() life cycle methods?

A

getDerivedStateFromError() is used to render a fallback UI after an error has been thrown.<div><br></br>componentDidCatch() is used to log error information.</div>

89
Q

Define Error Boundaries in terms of JS

A

They’re similar to a JS catch{} block, but for components.

90
Q

Why does React unmount the whole React component tree when an Error is not caught by an error boundary?

A

Because it is worse to leave corrupted UI in place than to completely remove it.

91
Q

Why use an Error Boundary over a try / catch?

A

Try / Catch is great, but only works for imperative code - React components are declarative and specify what should be rendered.

92
Q

Why don’t Error Boundaries work for event handlers?

A

Unlike render and lifecycle methods, event handlers don’t occur during rendering - so, if they throw an error, React still knows what to display on screen.<br></br><br></br>So, if you need to catch an error on an event handler, you should just use the regular JS try / catch.

93
Q

What must every class component have?

A

A render function

94
Q

How can you get a component to unmount and remount?

A

By sending it a unique key prop - then, whenever that prop changes, it will unmount and re-mount the component.

95
Q

What is the problem with using the key prop to reset a component?

A

It will re-mount not only the child element, but also the parent that is passing the key on.

96
Q

What is the react-error-boundary prop to allow it to reset state whenever certain keys change?

A

resetKeys (passed in as an array to ErrorBoundary)

97
Q

How can you tell ErrorBoundary what to do whenever the error is re-set?

A

You can pass an onReset prop into the ErrorBoundary component.

98
Q

How do you define what should be displayed by react-error-boundary whenever an error is hit?

A

The ErrorBoundary component takes a FallbackComponent prop which can be set to whatever component you would like displayed.<br></br><br></br>This component will then be supplied with the error as well as a resetErrorBoundary function which can be used to reset the ..error boundary (and clear the error from state so that it can be tried again)

99
Q

What is the basic definition of a test?

A

Code that throws an error when the actual result of something doesn’t match the expected output.

100
Q

What are the easiest things to test?

A

Pure functions - there’s no DB setup and nothing has to be rendered prior to testing

101
Q

What is an assertion?

A

The part of a test that says what result you expect the actual code to pass.

102
Q

What is the best way to ensure that your tests are actually covering your app?

A

The more your tests resemble the way your software is used, the more confidence they can give you.

103
Q

Why is testing implementation details bad?

A

“It can break when you refactor application code - false negative<div><br></br></div><div>or, it may not fail when you do break application code - false positive</div><div><br></br></div><div>The test is simply ““does the software work””</div>”

104
Q

Why is it important to clean up your DOM by removing whatever is on it?

A

If you don’t clean up, then it could impact other tests and/or cause a memory leak.

105
Q

How do you clean up your enviroment between each test so that your tests can run in total isolation from each other?

A

Make sure you wipe the DOM and unmount components, using a beforeEach block is handy.

106
Q

Instead of calling .click() within your tests, what should you do instead to more closely emulate how React works?

A

“You should, instead, dispatch an event. Not all events have dot methods available (like mouseover), and dispatching an event when a button is clicked is how Reacy actually does it.<div><br></br></div><div><img></img><br></br></div>”

107
Q

Within react-testing-library, what does container give you, and how do you use it?

A

“container is the div that your component has been mounted onto -<div><br></br></div><div><img></img><br></br></div>”

108
Q

What is the syntax to fire different events within react-testing-library?

A

“<img></img>”

109
Q

Within react-testing-library, how do you log the current state of the DOM (with syntax highlighting, no less)

A

screen.debug()

110
Q

Why don’t you have to clean up the DOM when using react-testing-library?

A

It has an auto-cleanup feature, so you’re gucci

111
Q

What all does react-testing-library’s render function do?

A

It creates the root div, it renders the div using ReactDOM.render, and then returns that div as the container property that we get back. In addition, it keeps track of that div and all components mounted to it and unmounts them and clears the div so we don’t have to worry about clean-up.

112
Q

In react-testing-library, what is the assertion to test the text on a page?

A

“<img></img>”

113
Q

What is the main thing to avoid if you want to test your apps in the way that they are used?

A

Don’t test implementation details.

114
Q

Why should you not use things like firstChild when accessing which elements on the container to test?

A

Because that is implementation - if we move the element around at all, our tests break, even though they still function fine.<div><br></br></div>

115
Q

What is a better solution than order-based than container.firstChild and the like?

A

screen.getByText() will grab whatever has that text content or even better:<div>screen.getByRole(‘button’, {name: ‘0’})</div>

116
Q

What are the two reasons that testing implementation details is bad?

A

<ol><li>They can break when you refactor application code.<strong>False negatives</strong></li><li>They may not fail when you break application code.<strong>False positives</strong></li></ol>

117
Q

“With react-testing-library, what is the following testing?<div><img></img><br></br></div>”

A

This tests, rather brilliantly, that the default is set, and that when you click on the accordion it hides the one and shows the other - it doesn’t care a bit about how that code was implemented.

118
Q

What should our tests typically deal with, given that our two users are users coming to the page and developers trying to use our component?

A

What’s rendered to the page, for our users - and the props that are passed in for the developers (easy cues when they forget a prop or misuse one)

119
Q

What is the process to know what to test?

A

“<ol><li>What part of your untested codebase would be really bad if it broke? (The checkout process)</li><li>Try to narrow it down to a unit or a few units of code (When clicking the ““checkout”” button a request with the cart items is sent to /checkout)</li><li>Look at that code and consider who the ““users”” are (The developer rendering the checkout form, the end user clicking on the button)</li><li>Write down a list of instructions for that user to manually test that code to make sure it’s not broken. (render the form with some fake data in the cart, click the checkout button, ensure the mocked /checkout API was called with the right data, respond with a fake successful response, make sure the success message is displayed).</li><li>Turn that list of instructions into an automated test.</li></ol>”

120
Q

What does react-testing-library getBy return?

A

The first matching node for a query, and they throw an error if it either founds no matching nodes, or if it finds more than one match.

121
Q

What is the react-testing-library query to return an array of all matching nodes and throw an error if none are found?

A

getAllBy

122
Q

How does the findBy and findAllByquery work within react-testing-library?

A

“<code>findBy*</code><span>queries return a promise which resolves when an element is found which matches the given query. The promise is rejected if no element is found or if more than one element is found after a default timeout of</span><code>1000</code><span>ms.</span>”

123
Q

What arguments can a react-testing-library query take?

A

A string, a regular expression, or a function

124
Q

What are the three types of queries (that work with all the options) in react-testing-library

A

getBy/getAllBy, queryBy/queryAllBy, findBy / findAllBy

125
Q

What is the screen object that is exported from from @testing-library/dom ?

A

“It gives you every query pre-bound to the document.body. You can use it like this:<div><br></br></div><div><pre><div><code><span>const</span> exampleInput <span>=</span> screen<span>.</span><span>getByLabelText</span><span>(</span><span>/example/i</span><span>)</span> </code></div><div></div></pre><br></br></div>”

126
Q

If you need to debug within your test how can you do it?

A

“If you import screen from @testing-library/dom then it makes available screen.debug() which is a shortcut for console.log(prettyDOM()) and can be used like this:<div><br></br></div><div><img></img><br></br></div>”

127
Q

“What is wrong with this react-testing-library query?<div><img></img><br></br></div>”

A

It assumes the buttons will always be in the same position and we won’t ever add any more buttons - it’d be better to query them by the text of their button.

128
Q

If you are grabbing elements by name in react-testing-library, what’s a good idea to make your test more flexible?

A

“Instead of just searching for a string, search for a case insensitive regex, because the user probably doesn’t care if it is capitalized or not.<div><br></br></div><div><img></img><br></br></div>”

129
Q

Which query should you use?

A

Generally, go down this order until you reach one possible:<div>1. Queries Accessible To Everyone</div><div> a. getByRole (almost always)</div><div> b. getByLabelText (Use for form fields)</div><div> c. getByPlaceholderText (If there’s no label for some reason)</div><div> d. getByText (not useful for a form, but how a user finds most</div><div> non-interactive elements.)</div><div> e. getByDisplayValue (useful for the current value of a form</div><div> element, useful when navigating a page with filled-in values.</div><div><br></br></div><div>2. Semantic Queries (getByAltText, getByTitle)</div><div> These are ARIA compliant, so that’s cool, but the user experience of these values varies greatly by browser and assistive technology.</div><div><br></br></div><div>3. Test IDs (getByTestId)</div><div> If you can’t match by role or text (maybe the text is dynamic) - ok, but the user can’t see or hear these, so you aren’t mimicking their experience.</div>

130
Q

What website can help if you aren’t sure how to query something?

A

testing-playground.com

131
Q

Instead of using fireEvent.click, what is a better tool that launches all of the different events that a real user would?

A

userEvent.click - but we have to import it from @testing-library/user-event for now, though it may one day be incorporated.

132
Q

If some input isn’t important to the functionality, what should you do for any variables involved?

A

“To clarify to future folk that the input doesn’t matter, you can generate it with a library called faker.<div><br></br></div><div><div><div><b>const {username, password} = buildLoginForm()</b></div><div><span></span></div></div></div>”

133
Q

What is an Object Mother or Test Factory Builder Function?

A

“It’s a function within your tests where you set up the data that will be necessary for testing.<div><br></br></div><div><img></img><br></br></div>”

134
Q

How can you build flexibility into a Test Mother (a function setting database fields)

A

“If you have it accept an overrides, and then just spread that at the bottom of the return, it will allow you to pass in any of the fields and have them be set instead of the default.<div><br></br></div><div><img></img><br></br></div>”

135
Q

What is a good way to think of complex components?

A

Think about and s (though that is HTML, not JS) - They don’t work without each other, but they are expressed in a way that is very readable and declarative.