SHARE
TWEET

Untitled

a guest Mar 18th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export class Section extends React.Component {
  2.   constructor(props) {
  3.     super(props)
  4.     this.formRef = React.createRef();
  5.     this.submitForm = this.submitForm.bind(this);
  6.     this.state = {
  7.       formSubmitted: false
  8.     }
  9.   }
  10.  
  11.   submitForm() {
  12.     console.log("form submitted");
  13.     this.setState({ formSubmitted: true })
  14.     if (this.formRef && this.formRef.current) {
  15.       this.formRef.current.submit();
  16.     }
  17.   }
  18.  
  19.   render() {
  20.     return (
  21.       <section onBlur={this.submitForm}>
  22.         <form ref={this.formRef} action={url} method='post'>
  23.           <input type="text" name="something" />
  24.         </form>
  25.       </section>
  26.     );
  27.   }
  28. }
  29.  
  30. export default connect(
  31.   mapStateToProps,
  32.   mapDispatchToProps
  33. )(Section);
  34.      
  35. import React from 'react';
  36. import { shallow } from 'enzyme';
  37. import { Section } from './Section';
  38.  
  39. describe('Section', () => {
  40.   it('should submit form on blur', () => {
  41.     const wrapper = shallow(<Section content={{ body: [] }} />);
  42.     const spy = spyOn(wrapper.instance(), 'submitForm');
  43.     const spy2 = spyOn(Section.prototype, 'submitForm');
  44.     const spy3 = jest.spyOn(wrapper.instance(), 'submitForm');
  45.     const spy4 = jest.spyOn(Section.prototype, 'submitForm');
  46.  
  47.     wrapper.find('section').simulate('blur');
  48.     expect(wrapper.state().formSubmitted).toEqual(true);
  49.     expect(spy).toHaveBeenCalled();
  50.     expect(spy2).toHaveBeenCalled();
  51.     expect(spy3).toHaveBeenCalled();
  52.     expect(spy4).toHaveBeenCalled();
  53.   })
  54. })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top