开玩笑/反应-快照中未显示组件

UNl*_*nel 5 javascript reactjs jestjs

我有一个组件,其中包含一个基于if语句呈现的组件。如果为false,则语句将css类添加到容器组件,而不呈现内部组件;否则,为true。是否以正确的方式添加了该类。另一方面,无论如何,内部组件都不会呈现在快照中。该组件在浏览器中工作正常。

这是容器:

 <React.Fragment>
            <div className={containerClasses}>
                <div className="conditions-types">
                    <TableHeaderCell title={texts.bookingReferenceInformation1}/>
                    {isTrue(displayFlightBookingReference) && <TableHeaderCell title={texts.bookingReferenceInformation2}/>}
                    {isTrue(displayBookingDate) && <TableHeaderCell title={texts.bookingReferenceInformation3}/>}
                    <TableHeaderCell title={texts.bookingReferenceInformation4}/>
                </div>
                <div className="condition-values">
                    <TableCell value={reservation.bookingCode}/>
                    {isTrue(displayFlightBookingReference) && <TableCell value={bookingNumber}/>}
                    {isTrue(displayBookingDate) && <TableCell value={getHolidaySummaryDate(reservation.datUserCreated)}/>}
                    <TableCell value={departureDateTime}/>
                </div>
            </div> 

             //following is the missing component

            {isTrue(displayCheckInButton) && <CheckInButton btnText={texts.checkInNow} links={links}/>}
        </React.Fragment>
Run Code Online (Sandbox Code Playgroud)

这是内部组件:

const CheckInButton = ({btnText ='' ,links= []}) =>
<div className="btn-section thankyouSection">
    <div className="checkin-now-btn">
        <a data-hook="checkInNow" target="_blank" itemProp="url" href={links && links.CheckInNow}>{btnText}</a>
    </div>
</div>;
Run Code Online (Sandbox Code Playgroud)

当调试包含if语句“ displayCheckInButton”的值的const时,需要时它为true。组件应渲染内部组件。

考试:

it(`should render BookingReference component with check-in button`, () => {
    const bookingReferenceComponent = mount(<BookingReference {...props} />);
    const wrapper = mount(<BookingReference {...props} />);
    expect(wrapper.find('.btn-section').length).toEqual(1);
    expect(bookingReferenceComponent).toMatchSnapshot();
});
Run Code Online (Sandbox Code Playgroud)

我试过包装器,看它是否包含该组件。包装程序确实找到了一个具有内部组件类的元素,因为它应该保留快照:

exports[`BookingReference Tests should render BookingReference component 
with check-in button 1`] = `
<div class="condition-table thankyouSection">
<div class="conditions-types">
    <div class="type">
        <div></div>
    </div>
    <div class="type">
        <div></div>
    </div>
</div>
<div class="condition-values">
    <div class="value">ABCDE12345</div>
    <div class="value">1 June 2018</div>
</div>

 // in here should be the missing component


 </div>
 `;
Run Code Online (Sandbox Code Playgroud)

希望我能提供足够的信息

UNl*_*nel 1

解决方案:

return (
        //instead of fragment i tried div

        <div>
            <div className={containerClasses}>
                <div className="conditions-types">
                    <TableHeaderCell title={texts.bookingReferenceInformation1}/>
                    {isTrue(displayFlightBookingReference) && <TableHeaderCell title={texts.bookingReferenceInformation2}/>}
                    {isTrue(displayBookingDate) && <TableHeaderCell title={texts.bookingReferenceInformation3}/>}
                    <TableHeaderCell title={texts.bookingReferenceInformation4}/>
                </div>
                <div className="condition-values">
                    <TableCell value={reservation.bookingCode}/>
                    {isTrue(displayFlightBookingReference) && <TableCell value={bookingNumber}/>}
                    {isTrue(displayBookingDate) && <TableCell value={getHolidaySummaryDate(reservation.datUserCreated)}/>}
                    <TableCell value={departureDateTime}/>
                </div>
            </div>
            {isTrue(displayCheckInButton) && <CheckInButton btnText={texts.checkInNow} links={links}/>}
        </div>
    );
Run Code Online (Sandbox Code Playgroud)

谢谢大家