• Robert Knight's avatar
    Change mocking approach for imported components in tests · 147f561c
    Robert Knight authored
    Shallow rendering with Enzyme's `shallow` function conflates two changes in
    rendering behavior:
    
     1. Only rendering "one level deep" so that a test for a component is
        isolated from the details of any child components
    
     2. In React (but not Preact) rendering to a data structure instead of
        real DOM nodes. Even though Preact still renders real DOM nodes,
        Enzyme's shallow rendering API removes many of the features of
        `mount` rendering
    
    In practice, we only used shallow rendering for (1) and we have found
    that there are use cases where we want (1) but still want to be able to
    interact with the real DOM nodes (ie. we don't want (2)). The need to
    use different mocking approaches and understand the conflated behaviors
    mentioned above has been a source of confusion for developers.
    
    This commit changes the mocking approach to always use a pattern that we
    have until now only used selectively. Enzyme's `mount` rendering mode is
    always used, but a utility function, `mockImportedComponents`, is used
    with our existing import mocking (via babel-plugin-mockable-imports) to
    semi-automatically mock all imported components.
    
    Each test has been changed as follows:
    
     1. Replace `shallow` with `mount`
    
     2. Call `ComponentUnderTest.$imports.$mock(mockImportedComponents())`
        in the test setup to mock all imported components, but not helper
        components defined in the same file.
    
     3. Call `ComponentUnderTest.$imports.$restore()` in test cleanup, if
        the test was not already doing that.
    
     4. Replace uses of (`wrapper.find(SomeChildComponent)`) with
        (`wrapper.find('SomeChildComponent')`. This is necessary because the
        mocked component no longer has the same identity as the original, so
        we look it up by name instead.
    147f561c
Name
Last commit
Last update
..
test Loading commit data...
annotation-action-button.js Loading commit data...
annotation-document-info.js Loading commit data...
annotation-header.js Loading commit data...
annotation-publish-control.js Loading commit data...
annotation-share-dialog.js Loading commit data...
annotation-share-info.js Loading commit data...
annotation-thread.js Loading commit data...
annotation-user.js Loading commit data...
annotation-viewer-content.js Loading commit data...
annotation.js Loading commit data...
excerpt.js Loading commit data...
focused-mode-header.js Loading commit data...
group-list-item.js Loading commit data...
group-list-section.js Loading commit data...
group-list.js Loading commit data...
help-link.js Loading commit data...
help-panel.js Loading commit data...
hypothesis-app.js Loading commit data...
logged-out-message.js Loading commit data...
markdown-editor.js Loading commit data...
markdown-view.js Loading commit data...
menu-item.js Loading commit data...
menu-section.js Loading commit data...
menu.js Loading commit data...
moderation-banner.js Loading commit data...
new-note-btn.js Loading commit data...
search-input.js Loading commit data...
search-status-bar.js Loading commit data...
selection-tabs.js Loading commit data...
share-annotations-panel.js Loading commit data...
sidebar-content-error.js Loading commit data...
sidebar-content.js Loading commit data...
sidebar-panel.js Loading commit data...
sidebar-tutorial.js Loading commit data...
slider.js Loading commit data...
sort-menu.js Loading commit data...
spinner.js Loading commit data...
stream-content.js Loading commit data...
stream-search-input.js Loading commit data...
svg-icon.js Loading commit data...
tag-editor.js Loading commit data...
thread-list.js Loading commit data...
timestamp.js Loading commit data...
top-bar.js Loading commit data...
user-menu.js Loading commit data...