简体   繁体   English

React 功能组件测试文件无法更新 state 值并调用其 props function

[英]React functional component test file unable to update state value and call its props function

i do have a functional parent component and it do have a state value, which have default value as false and it is calling another component which is visible only when the state value changes to true and the child component do have 2 function in it.我确实有一个功能性父组件,它确实有一个 state 值,其默认值为 false 并且它正在调用另一个组件,该组件仅在 state 值更改为 true 并且子组件在其中有 2 个 ZC1C425268E68384F14AB5079 时才可见。

Illustrating with code用代码说明

      export const MainSearch = (props) => {
        const [search, setSearch] = useState(false);
        
          const closeSearch = () => {
            setSearch(false);
           
            ANALYTICS.trackEvent('popup_collpsed');
          }
        
          const toggleSearch = async () => {
        
            await setSearch(true);
           
              ANALYTICS.trackEvent('popup_expanded');
            }
          };
            return (
             <React.Fragment>   
             <searchBar toggleSearch={toggleSearch} />
                {search  &&
                      <childSearch
                        toggleSearch={toggleSearch}
                        closeSearch={closeSearch}
                      />}
             </React.Fragment>
             )
        }

And its test file with one test case

describe('MainSearch',()=>{
 it('AdvancedSearch - Toggle popup_expanded  and popup_collapsed ', async () => {

     const component = shallow(<MainSearch {...props} />);
     const searchBar = component.find('searchBar');
     await searchBar.props().toggleSearch(); // calling function
     expect(ANALYTICS.trackEvent).toHaveBeenCalledWith('popup_expanded');
     const childSearchComponent = component.find('childSearch'); // not working ,since state value hides
     expect(childSearchComponent).toBeDefined();
     await advancedSearchComponent.props().closeSearch();// here getting null for .props()
     expect(ANALYTICS.page.trackEvent).toHaveBeenCalledWith('popup_collapsed');
  });
});

i know its possible with component.update for CLASS COMPONENTS, but here am using functional components and am getting error我知道 CLASS COMPONENTS 的 component.update 是可能的,但这里使用的是功能组件并且出现错误

if i remove the state value search, am getting my test case PASS, but cant remove that, its needed.如果我删除 state 值搜索,我的测试用例通过了,但不能删除它,它需要。 so my test case need to make the state value true and call the function closeSearch and then check the analytics.所以我的测试用例需要使 state 值为真并调用 function closeSearch 然后检查分析。

BUT am getting error Method “props” is meant to be run on 1 node.但我收到错误方法“道具”意味着在 1 个节点上运行。 0 found instead.找到了 0 个。
I guess state value if false and its not getting that particular node.我猜 state 值如果为 false 并且它没有得到那个特定的节点。

Can you guys please help me on same, since am stuck with it and can give more info if needed你们能帮我吗,因为我坚持下去,如果需要可以提供更多信息

Take a look at your toggleSearch function.看看你的toggleSearch function。 You're awaiting setSearch , which isn't a promise.您正在等待setSearch ,它不是 promise。 Remove the await Keyword and you should be fine!删除await关键字,你应该没问题! If you would want to trigger your Analytics call only after the State has been set, you would need to hook in to Reacts useEffect Hook.如果您只想在设置 State 后触发Analytics调用,则需要挂钩到 Reacts useEffect Hook。

Then you could do something like this:然后你可以做这样的事情:

useEffect(() => {
  if(search){
   ANALYTICS.trackEvent('popup_expanded');
 }
}, [search])

https://reactjs.org/docs/hooks-effect.html https://reactjs.org/docs/hooks-effect.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应:在功能组件中更改 State 也更改功能组件的道具值,及其父 Class State - React: Changing State in functional component also Change functional component's props value, and its Parent Class State 无法在反应功能组件中的道具上设置状态 - Unable to setState on props in react functional component React Functional Component props 不更新 - React Functional Component props does not update 如何在 React 功能组件中正确更新 props - How to update props correctly in React functional component 如何使用带有钩子的反应功能组件测试句柄 function 调用 - How to test a handle function call with a react functional component with hooks 调用 state 更新 function 在 state 更新 ZC1C425268E68384 组件4F 中使用功能状态钩子1 - Calling a state update function within a state update function - useState hook in React Functional component 反应递归功能组件更新 state - react recursive functional component update state 如何在功能组件中立即更新反应状态? - How to update react state immediately in functional component? 如何在 React 功能组件中立即更新状态? - How to update the state immediately in React functional component? 如何基于道具更新React组件状态 - How to update react component state based on props
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM