Enzyme: Failed to Parse data attribute selector

Created on 14 Apr 2019  路  2Comments  路  Source: enzymejs/enzyme

I am trying to achieve a test case based on data-test attribute but i am getting the following error

Failed to parse selector: [data-test = 'HeaderComponent']

can anyone help me out what is the mistake here

component:

<header data-test = "HeaderComponent">
      <div className ="wrapper" >
              <div className = "heading">
                     <h2 data-test="Title">Learning Jest Testing </h2>
               </div>
        </div>
</header>

Test case:

const setUp = (props = {}) => {
    const component = shallow(<Header {...props} />);
    return component;
}

const findByTestAtrr = (component, attr) =>{
    const wrapper = component.find(`[data-test = '${attr}']`);  //test failing line
    return wrapper;
}

describe('Header Component', () => {

    //call the setUp that is the shallow rendering before each test case
    let component;
    beforeEach(() => {
        component = setUp();
    })

    it('should render without error', () => {
        //testing the component
        const wrapper = findByTestAtrr(component,'HeaderComponent');
        expect(wrapper.length).toBe(1); 
    });

    it('should render the title', () => {
        const wrapper = component.find(`[data-test = 'Title']`);
        expect(wrapper.length).toBe(1);
    })

});

Most helpful comment

Remove the spaces around the equals sign; i don鈥檛 think that鈥檚 valid in querySelector.

All 2 comments

Remove the spaces around the equals sign; i don鈥檛 think that鈥檚 valid in querySelector.

It worked thanks!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blainekasten picture blainekasten  路  3Comments

modemuser picture modemuser  路  3Comments

aweary picture aweary  路  3Comments

abe903 picture abe903  路  3Comments

amcmillan01 picture amcmillan01  路  3Comments