reactjs

Date mock with two or more tests

拜拜、爱过 提交于 2021-02-19 08:17:07
问题 We are using jest for mocking. I have a function which will greet us based on the time that file looks like below: export default function getGreetingMessage() { const today = new Date(); const curHr = today.getHours(); if (curHr < 12) { return 'Good morning'; } else if (curHr < 18) { return 'Good afternoon'; } return 'Good evening'; } And My test file will look like below import getGreetingMessage from '../messages'; describe('messages', () => { function setup(date) { const DATE_TO_USE = new

msal in React SPA - use access token received from AcquireTokenRedirect

大憨熊 提交于 2021-02-19 08:04:21
问题 I have a React SPA and I'm using msal to authenticate Microsoft users using loginRedirect. After the login, I'm acquiring an access token silently using acquireTokenSilent to call a web API. When acquiring the access token fails and interaction is required, I'm using acquireTokenRedirect. When I use acquireTokenRedirect, what I see is: 1. The user is redirected to login.microsoftonline.com. 2. A 302 response is returned with Location header that contains the redirect url + the access token. 3

iOS 11 Location permission with three options from the beginning

こ雲淡風輕ζ 提交于 2021-02-19 07:53:47
问题 I am trying to ask for location permission on iOS 11 with React Native. There is a change from Apple coming with iOS 11 where we have a third option for using the location permission only when using the app. Our app is location based and need the user's coordinates even when the app is in the background so I would like the user to have the option to click 'Always' the first time the popup shows up. The problem is that the only popup which shows up is not having the three options (the first

Using React hooks to prevent React re-rendering of a D3 chart

不想你离开。 提交于 2021-02-19 07:35:23
问题 I have been using React and D3 separately and now have a project where I need low level control over the plotting function of an application. Basically, I need to be able to go and fetch higher resolution data from a database as the user zooms in, and vice versa as the user zooms out, on a plot. I have found a few methods to use D3 and React together. I wanted to try and keep all of my React code based around the hooks API as that is what is used for the rest of the code base. I am struggling

Testing parent methods in stateless child component in Jest

假如想象 提交于 2021-02-19 07:35:14
问题 I currently have a react component with an onChange method. I want to test the onChange method like if someone were to type in, or paste things in. The problem is the handleChange is dependent on the parent components using it. What's the best way to test for the simulation? Do I need to reference the parent component's handleChange method and simulate that? CodeSandbox (i didn't add the tests here) https://codesandbox.io/s/react-basic-example-cwdh1?from-embed Updated Current Test: const

how to mock npm uniqid for react unit test when using Jest?

走远了吗. 提交于 2021-02-19 07:10:45
问题 I'm trying to mock npm uniqid library for react unit test. I have used this uniqid for check box. This is for component render test. 回答1: You can mock it like this: jest.mock('uniqid', ()=>(i)=> i + 'someUniqId') or if you use one of the other functions jest.mock('uniqid', () = > ({ process: (i) => i + 'someProcess, time: (i) => i + ' someTime, })) 来源: https://stackoverflow.com/questions/44537755/how-to-mock-npm-uniqid-for-react-unit-test-when-using-jest

how to mock npm uniqid for react unit test when using Jest?

自作多情 提交于 2021-02-19 07:10:36
问题 I'm trying to mock npm uniqid library for react unit test. I have used this uniqid for check box. This is for component render test. 回答1: You can mock it like this: jest.mock('uniqid', ()=>(i)=> i + 'someUniqId') or if you use one of the other functions jest.mock('uniqid', () = > ({ process: (i) => i + 'someProcess, time: (i) => i + ' someTime, })) 来源: https://stackoverflow.com/questions/44537755/how-to-mock-npm-uniqid-for-react-unit-test-when-using-jest

classNames is not defined

China☆狼群 提交于 2021-02-19 06:50:14
问题 I am newbie in react development and trying to understand how classNames work in react. This is the react code from my book. I just copied it. const MOUNT = document.getElementById('root'); class App extends React.Component { render() { const klasses = classNames({ box: true, // always apply the box class alert: this.props.isAlert, // if the prop is set severity: this.state.onHighAlert, // with state timed: false // never apply this class }); return React.createElement( 'div', {className:

Material-UI 4.8.1 API change - new way to specify the `component` property?

久未见 提交于 2021-02-19 06:49:05
问题 Edit : This was a bug in the typing of 4.8.x, upgrade to 4.9.0+ to fix the issue. On version 4.8.0 , the following code compiles and runs fine: <DialogContent> {/* use a span so we can nest div, p, etc. */} <DialogContentText component="span"> {confirmContent} </DialogContentText> </DialogContent> After upgrading to 4.8.1 the code now fails type-checking with: No overload matches this call. Overload 1 of 2, '(props: DialogContentTextProps, context?: any): ReactElement<any, string | ((props:

Showing list of buttons displaying modals in ReactJS

故事扮演 提交于 2021-02-19 06:41:07
问题 I have a problem showing a list of buttons where each would display a popup. I have taken react-modal (https://github.com/reactjs/react-modal) component and able to display a single popup on button click, but how would i do it for list of buttons? I ve read about refs collection, but there is seem no way of changing props collection accessing and components by refs name? Having a show button: <button type="button" onClick={this.openModal}>Show</button> openModal function: openModal: function