React And Test Driven Development: Difference between revisions
Line 131: | Line 131: | ||
==Inverse Date Flow== | ==Inverse Date Flow== | ||
Events for a react project flow up (inverse). We use callbacks to update State in a React Application as React uses one-way data binding. | Events for a react project flow up (inverse). We use callbacks to update State in a React Application as React uses one-way data binding. | ||
React uses one-way data binding | React uses one-way data binding. | ||
Revision as of 04:44, 11 July 2021
Introduction
Requirements
On the course we got requirements
- Display map
- Select shop
- Marker show location
- User can search
- Detect user location
- Get Directions
- Scalable
Prototype
A prototype was developed on paper which created a web page with buttons for the first two sites.
Introducing TDD
TDD follows the three processes. Test, Code, Refactor. The module demonstrated how to start building the App by create sections of the App to components. E.g. Component Header. They split the containers and components where components were stateless. Tests were written to make sure the components rendered.
Need for TDD
With TDD you write the test and then the supporting code. It walked through what to test which I liked as a list known but nice to be reminded of.
- Does it render
- Does it render correctly
- Does it handle events correctly
- Do conditionals work
- Are edge cases handled.
Next they broken down ideas for the prototype requirements above.
1/ There should be a button for each store location
2/ Each store button should display the name of the location
3/ Clicking the button passes the value to the function
4/ A map chooser function returns an image file name based on input given to it
5/ When no input is given to the map chooser function it returns a default image file name
6/ When a bad input is given to the map chooser function, it returns a default image file name
With this done they identified 4/ as a requirement to write the test for. So created mapChooser.
import mapChooser from '../mapChooser';
describe("mapChooser", function(){
it("returns an image based on input given to it", function() {
let expected = "portland.jpg";
let actual = mapChooser("portland");
expect(actual).toEqual(expected);
});
it("returns an default image when no input is given", function() {
let expected = "default.jpg";
let actual = mapChooser("");
expect(actual).toEqual(expected);
});
});
And finally the code.
function mapChooser(locationName){
if (!locationName) {
locationName = "default";
}
let imageName = locationName + ".jpg";
return (imageName);
}
export default mapChooser;
This took several iterations where the simplest implementation was to return "portland".
Need for React
Imperative vs. Declarative
For a treasure map
Imperative Is the process to achieve something, Start at the dot, Walk to the Lake, Turn right and continue for 5km, Turn left and walk 2 km.
Declarative Is what it is. Go to X
Virtual DOM
React has two virtual DOMs. A copy of the actual DOM and the new DOM. React calculates what needs to change and applies it appropriately.
React Development Process
Here is how to approach React development
- Create a component hierarchy
- Build a Static Version
- Identify the minimal UI State
- Identify where the state should live
- Add Inverse Data Flow (Passing State upwards)
For lot of information on this go to https://reactjs.org/docs/thinking-in-react.html
Build up the UI
They then focused on the app UI todo list
There should be a button for each store location
Each store button should display the name of the location
There should be a map
There should be a header above the buttons
Clicking a button passes a value to a function
The components tests were created using enzyme. This is the first example of enzyme at the time of this page and demonstrates how to check if the UI renders.
import React from 'react';
import {shallow} from 'enzyme';
import StoreLocator from '../StoreLocator';
describe("StoreLocator", function () {
let mountedStoreLocator;
beforeEach(() => {
mountedStoreLocator = shallow(<StoreLocator />);
});
it('renders without crashing', () => {
let mountedStoreLocator = shallow(<StoreLocator />);
});
it('renders a header', () => {
const headers = mountedStoreLocator.find('Header');
expect(headers.length).toBe(1);
});
it('renders a map', () => {
const maps = mountedStoreLocator.find('Map');
expect(maps.length).toBe(1);
})
});
describe('chooseMap', ()=> {
it('updates this.state.currentMap using the location passed to it', ()=>{
let mountedStoreLocator = shallow(<StoreLocator />);
let mockEvent = {target:{value:'testland'}};
mountedStoreLocator.instance().chooseMap(mockEvent);
expect(mountedStoreLocator.instance().state.currentMap).toBe('testland.png');
})
});
Refactoring with React
Minimal State
On the React page above but to think about state consider.
- Is it not passed from the parent
- Does it change over time
- Is it not possible to compute based on other state of props
Inverse Date Flow
Events for a react project flow up (inverse). We use callbacks to update State in a React Application as React uses one-way data binding. React uses one-way data binding.