purefacts-widgets
v1.0.0
Published
This project is a library containing React widgets that can be re-used in PureReporting NextGen applications.
Downloads
1
Readme
Overview
This project is a library containing React widgets that can be re-used in PureReporting NextGen applications.
Two important things to take notice of:
To publish this project as an NPM package into a dummy repository, this project uses
verdaccio
. See below on how to use it (there are links and resources available to you to become familiar with this tool). And yes, you can use any non-public repository running locally or in PureFacts, but we recommend this Verdaccio & cover only that one in the tutorials below.To test the components without needing a host app, we use
React Storybook
components. Again there are links and resources provided below.
A note about creating components
Please create components using command line - for example:
npx generate-react-cli component AssetGraph
npx generate-react-cli component --path=src/components/test HelloWorld
We use this plugin to make sure the syntax of components are the same. This keeps this project clean and consistent.
If you are familiar with Angular, you know that Angular command line client helps Angular developers maintain their code and keep it clean; however, there are just too many flavours and tools to do the equivalent job in React world. Here we are using this tool: https://github.com/arminbro/generate-react-cli
Remark:
Using this plugin allows us to add storybook templates by default. That and more are all configured inside generate-react-cli.json
(something you should know by now if you had studied their doc in above link).
Installing Verdaccio
Verdaccio is a dummy local repository that we can use to test. The idea is to publish this package into your local repo, and to install it in your host app.
For example, after publishing this package into your local verdaccio instance, you can install it into our PureReporting Nextgen UI app.
Verdaccio is well documented and your best friend is the YouTUBE video found in their readme. Please see this link: https://verdaccio.org/ and click on docs on top menu.
Using React Storybook To Test
React Storybook allows developers of plugins and components to test their plugins right there.
As you know, if you are developing a library such as this one, your time would be wasted if you were to compile the plugin, publish the npm, update the plugin in host, and refresh just to see your change.
That's why Storybook is useful.
I think this video does a great job explaining what Storybook is all about. Please review this tutorial fully. https://www.youtube.com/watch?v=FUKpWgRyPlU&t=932s
Storybook (AKA @storybook/react) is currently broken on 6.4
The version has been temporarily set to 6.3
until https://github.com/storybookjs/storybook/issues/16880 is fixed
Using Snapshot testing
Snapshot testing is used to confirm that the output of a component for a given props stays the same. To create a new snapshot test (Or to update the old one, when you know that the new snapshot state is correct).
npm run update-snapshots
There are 2 major ways to do snapshot testing. enzyme and react-test-renderer.
The different is that react-test-renderer
only tests the HTML
outputted by a given component,
but enzyme
allows you to compare more detailed things.
A good example of the difference, when testing is testing anything with recharts
. The output of recharts
will not show up with react-test-renderer
, so we cannot test what we made.
With enzyme
, we can see the props
which have been given to recharts
, so we can at least be sure the props we passed to recharts
did not change.
How to use some of the more advanced hooks
useMemo
and merging default parameters
A perfect useCase for useMemo
is this
const Component = props => {
const propsMergedWithDefault = useMemo(() => {
return merge(props, defaultProps);
}, [props]
)
// ...
}
Which can replace the unclear and incorrect following pattern
const Component => props => {
const [propsMergedWithDefault, setPropsMergedWithDefault] = useState(defaultProps);
// ...
useEffect(() => {
setPropsMergedWithDefault(merge(props, defaultProps)
}, [props])
}
Jest and snapshot tests
npm run test-jst
will execute all tests. Many of these are snapshot tests which will check that your changes didn't change a component in an indesired way. If you changed a component on purpose, then use npm run update-snapshots
and commit the changed files.