How to use jsdoc to describe export default ({ Label, initialValue, })=>{ ... } - jsdoc

How to use jsdoc to describe export default ({
Label,
initialValue,
})=>{
...
}
How to use jsdoc to describe export default ({
Label,
initialValue,
})=>{
...
}

Related

React-testing-library with Ionic v5 (react) and react-hook-form- change events do not fire

I am trying to test a component rendered with Controller from react-hook-form with react-testing-library
<Controller
render={({ onChange, onBlur, value }) => (
<IonInput
onIonChange={onChange}
onIonBlur={onBlur}
value={value}
type="text"
data-testid="firstname-field"
/>
)}
name="firstName"
control={control}
defaultValue={firstName}
/>
Default values are as expected when I render the component with a some mock data. However, when I go about changing values, it seems the events are not firing. From this blog post it looks like ionic exports a set of test utils to handle ionic's custom events. After setting that up in my setupTests.ts I'm attempting to use both the ionFireEvent and the fireEvent from RTU, neither of which reflect changes in the component when I use debug(). I've set it up so I can use both fireEvent and ionFireEvent to test:
import { render, screen, wait, fireEvent } from "#testing-library/react";
import { ionFireEvent } from "#ionic/react-test-utils";
// using RTL fireEvent - no change
it("fires change event on firstname", () => {
const { baseElement } = renderGolferContext(mockGolfer);
const firstNameField = screen.getByTestId("firstname-field") as HTMLInputElement;
fireEvent.change(firstNameField, { target: { detail: { value: "Jill" } } });
expect(firstNameField.value).toBe("Jill");
});
// using IRTL ionFireEvent/ionChange - no change
it("fires change event on firstname", () => {
const { baseElement } = renderGolferContext(mockGolfer);
const firstNameField = screen.getByTestId("firstname-field") as HTMLInputElement;
ionFireEvent.ionChange(firstNameField, "Jill");
expect(firstNameField.value).toBe("Jill");
});
screen.debug(baseElement);
I've also tried moving the data-testid property to the controller rather than the IonInput suggested here, with the result being the same: no event is fired.
Here are the versions I'm using:
Using Ionic 5.1.1
#ionic/react-test-utils 0.0.3
jest 24.9
#testing-library/react 9.5
#testing-library/dom 6.16
Here is a repo I've created to demonstrate.
Any help would be much appreciated!
this line appears to be incorrect...
expect(firstNameField.value).toBe("Jill");
It should be looking at detail.value since that is what you set
expect((firstNameField as any).detail.value).toBe("Jill");
this is my test,
describe("RTL fireEvent on ion-input", () => {
it("change on firstname", () => {
const { baseElement, getByTestId } = render(<IonicHookForm />);
const firstNameField = screen.getByTestId(
"firstname-field"
) as HTMLInputElement;
fireEvent.change(firstNameField, {
target: { detail: { value: "Princess" } },
});
expect((firstNameField as any).detail.value).toEqual("Princess");
});
});

Can't change the pagination 'next' and 'prev' labels to my language in grid.js

There is no such option as change the prev and next button label in the documentation, and when i try to use string replacement or change the button innerHTML via Javascript, it doesn't work, is there any way that I can safely change the label?
You can use the language config (added since v1.5.0) to customize this:
new Grid({
columns: ['Name', 'Email', 'Title'],
sort: true,
search: true,
pagination: {
limit: 5
},
data: Array(50).fill().map(x => [
faker.name.findName(),
faker.internet.email(),
faker.name.title(),
]),
language: {
'search': {
'placeholder': '🔍 Search...'
},
'pagination': {
'previous': '⬅️',
'next': '➡️',
'showing': '😃 Displaying'
}
}
});
Also see this example: https://gridjs.io/docs/examples/i18n/

Testing Material UI components using Hooks API & Enzyme shallow rendering

The latest versions of Material UI now have a Hooks alternative for styling components, instead of the HoC. So instead of
const styles = theme => ({
...
});
export const AppBarHeader = ({ classes, title }) => (
...
);
export default withStyles(styles)(AppBarHeader);
you can choose to do this instead:
const useStyles = makeStyles(theme => ({
xxxx
}));
const AppBarHeader = ({ title }) => {
const classes = useStyles();
return (
....
)
};
export default AppBarHeader;
In some ways this is nicer, but as with all hooks you can no longer inject a 'stub' dependency to the component. Previously, for testing with Enzyme I just tested the non-styled component:
describe("<AppBarHeader />", () => {
it("renders correctly", () => {
const component = shallow(
<AppBarHeader title="Hello" classes="{}" />
);
expect(component).toMatchSnapshot();
});
});
However, if you use hooks, without the 'stub' dependency for classes, and you get:
Warning: Material-UI: the `styles` argument provided is invalid.
You are providing a function without a theme in the context.
One of the parent elements needs to use a ThemeProvider.
because you always need a provider in place. I can go and wrap this up:
describe("<AppBarHeader />", () => {
it("renders correctly", () => {
const component = shallow(
<ThemeProvider theme={theme}>
<AppBarHeader title="Hello" classes="{}" />
</ThemeProvider>
).dive();
expect(component).toMatchSnapshot();
});
});
but that no longer seems to render the children of the component (even with the dive call). How are folks doing this?
EDIT: As per the comments below, this implementation presents some timing issues. Consider testing with mount instead of shallow testing, or use the withStyles HOC and export your component for shallow rendering.
So I have been grappling with this for a day now. Here is what I have come up with.
There are some issues trying to stub makeStyles since it appears MUI has made it readonly. So instead of creating a useStyles hook in each component, I created my own custom useStyles hook that calls makeStyles. In this way I can stub my useStyles hook for testing purposes, with minimal impact to the flow of my code.
// root/utils/useStyles.js
// My custom useStyles hook
import makeStyles from '#material-ui/styles/makeStyles';
export const useStyles = styles => makeStyles(theme => styles(theme))();
Its almost like using the withStyles HOC
// root/components/MyComponent.js
import React from 'react';
import { useStyles } from '../util/useStyles';
// create styles like you would for withStyles
const styles = theme => ({
root: {
padding: 0,
},
});
export const MyComponent = () => {
const classes = useStyles(styles);
return(
</>
);
}
// root/component/MyComponent.spec.js
import { MyComponent } from './MyComponent';
import { shallow } from 'enzyme';
import { stub } from 'sinon';
describe('render', () => {
it('should render', () => {
let useStylesStub;
useStylesStub = stub(hooks, 'useStyles');
useStylesStub.returns({ });
const wrapper = shallow(<MyComponent />);
console.log('profit');
});
});
This is the best I can come up with for now, but always open to suggetions.

How do you enable rendering of attributes in Cycle.js/dom?

I have the following snippet:
button('.textbutton', {
type: "button",
onclick: `toggleVisibility('#abs-${submission.submission_id}');`
},
'Abstract'
),
a( {href: "https://localhost:8080"}, 'View Article'),
div(`#abs-${submission.submission_id}`,
{style: 'display:none'}, submission.abstract
),
This seems to render as just:
<button class="textbutton">Abstract</button>
<a>View Article</a>
<div id="abs-1405603">Text not shown on SO...</div>
Note that none of the attributes are being rendered. My cycle.js imports in this file are simply:
import {VNode, div, a, button, h3, img, hr, b, p, span} from "#cycle/dom";
It's snabbdom.
It should be
a({
attrs: {
href: '#'
}
}, ['link'])
And events go under on, like
button('.textbutton', {
attrs: {
type: 'button'
},
on: {
click: () => {} // here goes function
},
}, ['Abstract'])
You have to create object with key attrs and then attributes.
The only case when something like this will work are modules class and style. class takes CSS class as key and condition as value, e.g.
div({
class: {
'block': true,
'hidden': isVisible === false
}
}, [/**/])
When condition is falsy then class will not be present.
style is just like CSS styles key - value:
div({
style: {
'display': 'none'
}
}, [/**/])
Also with Cycle you should not attach events directly to DOM by yourself but call source driver DOM to do that, e.g. sources.DOM.select('a').events('click') and then you have clicks stream.

How to access ngModelController in Angular1.5 components?

When I use angular.component() to create the brand new component that angular 1.5 provides, there's no link function, so the old way of injecting ngModelController or any other controllers doesn't work.
require: 'ngModel',
link: function(scope, element, attrs, ctrls)
The above code is for a directive to access ngModelController.
How do we access it in the component now?
Instead of getting a ctrls array, you get them now by name, just like bindings use to:
class MyComponentController implements ng.IComponentController {
public modelCtrl: ng.INgModelController;
...
...
// use modelCtrl here
// instead of ctrls[0]
...
...
}
const MyComponent: ng.IComponentOptions = {
template: '...',
bindings: {...},
require: {modelCtrl: 'ngModel'},
controller: MyComponentController
}
angular.module('myModule').component('MyComponent', MyComponent);
Or, if you prefer plain JS:
function MyComponentController() {
...
...
// use this.modelCtrl here
...
...
}
var MyComponent = {
template: '...',
bindings: {...},
require: { modelCtrl: 'ngModel' },
controller: MyComponentController
};
angular.module('myModule').component('MyComponent', MyComponent);