1.0.1 • Published 5 years ago

fluent-wrapper v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

fluent-wrapper

Extends the ReactWrapper you get when you mount/shallow a component with Enzyme with a set of functions that let you find/click/change/etc elements in a fluent way.

fluent-wrapper demo

Currently supported events:

  • find
  • click
  • change
  • focus
  • blur
  • and all the events provided by enzyme's simulate

Why fluent-wrapper?

  • maintainability: keep all the selectors in a single place, the specs, so you don't mix this mapping with the test logic.
    const specs = [{
        name:'mainForm', 
        selector:'form.main',
        children:[
            {name:'email', selector:'input[name="email"]'}
        ]},
    ]
  • make your tests fluent
    ui.findMainForm().changeEmail('new@value.com')

Get started

npm install -D fluent-wrapper

import fluentWrapper from 'fluent-wrapper'

    //example of a spec
    const specs = [{name:'randomElement', selector:'p']; 
    
    //pass mounted component and specs to fluentWrapper
    const wrapper = fluentWrapper(mount(<YourComponent whatever={true} />, specs);
    
    //find your element
    const myElement = wrapper.findRandomElement(); // ReactWrapper 
    

API

Properties of a spec.

PropertyMandatoryTypeDescription
nameyesstringunique name you want the element to have. See here how name is used in generated functions.
selectoryescss selector or React elementIt's the same selector you would pass to enzyme
eventsnolist of stringsAll the events you want to simulate. I.e. events=['click', 'blur'] for confirmButton will generate clickConfirmButton and blurConfirmButton. You can pass any event supported by enzyme's simulate.
childrennoarray of specsYou can nest fluent functions, see Example 3.
click (will be deprecated)no (default=false)booleanIf enabled it generates the click event (i.e. wrapper.clickConfirmButton())
change (will be deprecated)no (default=false)booleanIf enabled generates the change function (i.e. wrapper.changeEmailInput(arg)). You can pass whatever you would pass to enzyme's simulate('change') function.

Spec name conversion

The name is converted to a camel case name and composed with the generated function. See examples below:

Spec nameGenerated functions
mybuttonfindMybutton, clickMybutton, etc.
MybuttonfindMybutton, clickMybutton, etc.
my buttonfindMybutton, clickMybutton, etc.
my ButtonfindMyButton, clickMyButton, etc.
my-buttonfindMyButton, clickMyButton, etc.

Example1: simple find

    import fluentWrapper from 'fluent-wrapper'
    import {mount} from 'enzyme'
    import MyComponent from '../MyComponent'
    import AnotherComponent from '../AnotherComponent'
    
    const specs = [{name: 'confirmButton',selector:'button'}, {name: 'another', selector: AnotherComponent}]
    
    const wrapper = fluentWrapper(mount(<MyComponent />), specs) 
    
    // The wrapper you get from "mount" has now been extended!
    
    wrapper.findConfirmButton() //returns confirm button's react wrapper
    wrapper.findAnotherComponent() //returns AnotherComponent's react wrapper

Example 2: click

    import fluentWrapper from 'fluent-wrapper'
    import {mount} from 'enzyme'
    import MyComponent from '../MyComponent'
    
    const specs = [{name: 'confirmButton',selector:'button', events:['click']}]
    
    const wrapper = fluentWrapper(mount(<MyComponent />), specs) 
    
    // The wrapper you get from "mount" has now been extended!
    
    wrapper.clickConfirmButton() //generated function will return the button

Example 3: element with children

    import fluentWrapper from 'fluent-wrapper'
    import {mount} from 'enzyme'
    import MyComponent from '../MyComponent'
    
    const specs = [{
        name: 'myForm',
        selector:'form', 
        children:[{name: 'emailField', selector:'[name="email"]'}]
    }]
    
    const wrapper = fluentWrapper(mount(<MyComponent />), specs) 
    
    const foundField = wrapper.findMyForm().findEmailField();

    //NOTE: findEmailField is avaiable only on the return value of findMyForm()
    

Example 4: change

    import fluentWrapper from 'fluent-wrapper'
    import {mount} from 'enzyme'
    import MyComponent from '../MyComponent'
    
    const specs = [{name: 'username',selector:'input[name="user"]', events:['change']}]
    
    const wrapper = fluentWrapper(mount(<MyComponent />), specs) 
    
    // The wrapper you get from "mount" has now been extended!
    
    wrapper.changeUsername({target:{value:'elisa'}}) 

Example 5: multiple events and child elements

    import fluentWrapper from 'fluent-wrapper';
    import {mount} from 'enzyme';
    import MyComponent from '../MyComponent'
    
    const specs = [{
        name: 'myForm',
        selector:'form', 
        events: ['focus'],
        children:[{
            name: 'emailField', 
            selector:'[name="email"]', 
            events['change','blur','focus']
        }],
    }]
    
    const wrapper = fluentWrapper(mount(<MyComponent />), specs) 
    
    wrapper.focusMyForm();
    const form = wrapper.findMyForm();
    form.focusEmailField();
    form.changeEmailField('me@giulioambrogi.com');
    form.blurEmailField();
    

More examples

If you want to see more examples have a look at the test folder

1.0.1

5 years ago

1.0.0

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago