The role of storybook when testing of React application is conducted - reactjs

I am new to React and recently I found out there is the tool called storybook which basically helps to test our React components in isolated manner. However, I also found out that storybook is used when we test our React application, that is, there happens the comparison of snapshots. The question is what is the reason for testing the difference of snapshots? I mean, is it like we create a certain component and create a snapshot for it to ensure that other developers do not change that component accidentally, that is, snapshot in storybook helps us ensure there is no accidental change of components that we created. Is it true?

You create snapshot tests to ensure that your Components renders correctly given a certain input.
This is a way to make sure your code still behaves the way it was intended to. It's to ensure you don't accidentally break something in your code base, but also to alert you when anything changes.
An example:
Say I have a component called ProfileLink that generates a link to a user account:
function ProfileLink(props) {
return <a href ={get_url(user.id)}>{props.user.profileName}</a>;
}
and somewhere else I have a function called get_url:
function get_url(id) {
return "www.example.com/user/" + id;
}
Now, to ensure that my ProfileLink is always working, I can create snapshot, and everytime my tests run, the snapshot will be compared to the current rendered component.
Say someone were to change the get_url function, they might not be aware that it's used in ProfileLink, but as soon as the tests are run, you'll know that something has changed in the ProfileLink component as well.

Related

Set new Props in testcafe for the test case

I'm using reactJS and testcafe as one of the testing frameworks.
I've a default configuration - 'showCalculatedValue' set to true(it is always passed a prop), for my application component for which the testcafe test case runs fine.
However, I need to set that config - 'showCalculatedValue' (through props) to false in testcafe and then run another test case.
And yes, that config doesn't require any sort of action to trigger.
It is all about if you want to have it as a feature or not.
It is just that when 'showCalculatedValue' is set to true, it renders a specific dom element and when it is not, it doesn't render that element.
So my question is, how could I pass in new props in testcafe to set that 'showCalculated' value to false?
I do not know how your config works, but I assume that you can modify it on the client side somehow. If so, I suggest you take a look at the ClientFunctions mechanism. Using ClientFunctions, you will be able to run your custom JS-code with TestCafe.
If this does not help, please share a sample project with a detailed explanation with us.

Add `console.log` to all `useEffect`

For development purposes I’d like to have the option to log all uses of useEffect inside my React app. Is there an easy way to extend the behaviour of this function? I’m using Webpack, in case this would provide us with an extra way of doing so.
Also, because this will lead to many console.logs, is there a way to tell them apart by providing information identifying the component that’s calling useEffect?
I would like to have this behaviour to “visually” check (in the console) if the application redundantly rerenders.
I would like to have this behaviour to “visually” check (in the console) if the application redundantly rerenders.
There is a library that does this for you: https://github.com/welldone-software/why-did-you-render#readme
The repo has a simple example to set it up:
import React from 'react';
if (process.env.NODE_ENV === 'development') {
const whyDidYouRender = require('#welldone-software/why-did-you-render');
whyDidYouRender(React);
}

In React, how would i convert certain aspects of a stateful component to work within a function component?

My code is in React & is quite extensive, so i have provided a codesandbox link: My transactions section of my "under construction" app: https://codesandbox.io/s/agitated-sea-96048. A Chegg expert had given me the code that renders what you'll see above my transaction cards (his ModalEdit, for now, was placed inside of the TransactionsLayout component--this is just so you can visualize, somewhat, of what i'm looking for).
i like the way that the edit does what my question is asking. however, i do not know how to make it work within the framework of my app, because my TransactionCard.jsx file is a function component without state. and, the expert's code is with state.
i have tried putting his code as a separate component & calling to it with the button onClick (line#69 on the TransactionsCard.jsx file). however, this doesn't work because i can't figure out 'how' to convert his code to work within the framework of the transactions' files.

Subscription on child component not working in ReactJS

I created a publication(on server) and subscription (on client) in MEteor + React application. My problem is, I can't receive the array returned by the publication. I used console.log in publication and it logs the subscription key and option. I check also documents before returning and it is correct. But in client, still cant get array of documents.
Here is my code for componentDidMount,
Meteor.subscribe("messages",{},{},function(err){
console.log("err",err);
console.log(Messages.find().fetch());
});
console.log here are not called.
Here is my code for publication,
if (Meteor.isServer) {
Meteor.publish('messages', function (key,option) {
console.log(key);
return Messages.find(key,option);
});
}
console.log here is working and also i checked the documents before returning and it is correct.
I used also react-router in my application.I add route for my parent component. The problem i have is in child component.
What is wrong with my code? or how to solve this problem?
I used msavin:mongol and still having 0 for the count of documents.
I think Meteor Chef is promoting a nice folder structure. If you happen to use that, just to make sure your server side actions reach the server, meaning they are imported to the server. Just sharing a screen from one of my projects:
register-api.js is further down imported in main.js in my server folder.
Secondly, what if you do your console.log(Messages.find().fetch()) outside the subscription. You put it into the callback function and should not really be there. What you normally do is to pass the result of Messages.find().fetch() to props in the component's container, so the result needs to be captured outside of your subscription.

How React Component expose data to external app?

Let's say i have a React Component <forecast id="test"/>. And i want import this component into a legacy project which only have jquery involved.
Is it possible to get the value of this component like document.querySelector('#test').value?
I got some information from React website, that we cannot access data from outside the component. The recommended way is dispatching data from inside of the component.
My question is, the way to dispatching data is behind of the component implementation. Is it means that i have to read the source code of component in case i don't know how it works?
If this is true, i won't think React is free to inject to any product, it cost too much.
If you want to inject some React to your project you should do it with some independent part of your system.
If you have tightly coupled code base its always high cost to add any new technology to it. So its not a React problem. Try to find some independent module or subapplication in your system and move it to React. If you cannot find one, try to refactor existing code first.
You need to write a plain JS wrapper to do it. Something like this might work
function Forecast(element) {
this.value = initialValue;
React.render(<forecast onChange={onChange.bind(this)}/>, element);
function onChange(newValue) {
this.value = newValue;
}
}

Resources