lundi 4 septembre 2017

Unit Testing - Save the DOM Ref for an Header and pass it to a Component

I am saving the dog ref for the header and pass it an other component ( RacecardContainer).

ref={(el: HTMLElement) => { PageHeaderRef = el; }}

onRender={() => PageHeaderRef && PageHeaderRef.scrollIntoView()}

Here the all the code together.

let PageHeaderRef;

return (
    <ListItem>
        <PageHeader style="primary" ref={(el: HTMLElement) => { PageHeaderRef = el; }}>
            { header }
        </PageHeader>
      <RacecardContainer onRender={() => PageHeaderRef && PageHeaderRef.scrollIntoView()} />
        </div>
    </ListItem>
);

It works fine but it is not passing the Unit Test.

In the last line it returns false instead of true after the onRender().

   const header = '.sb-page-header--primary';

    it('saves the DOM ref to the header and passes it to RacecardContainer', () => {
        const racecardContainerStub = sinon.stub().returns(null);
        RewireAPI.__set__('RacecardContainer', racecardContainerStub);
        const wrapper = mount(<ListRowOpen />);
        const headerEl = wrapper.find(header).getDOMNode();
        const onRender = racecardContainerStub.firstCall.args[0].onRender;

        const scrollSpy = headerEl.scrollIntoView = sinon.spy();

        expect(scrollSpy.called).to.equal(false);
        onRender();
        expect(scrollSpy.called).to.equal(true);

What am i doing wrong?

Aucun commentaire:

Enregistrer un commentaire