Skip to content

Circular References in React Native #1509

Closed
@w0nche0l

Description

@w0nche0l

I'm not sure if this is intended behavior that I should post on StackOverflow to find a workaround, or a bug in the very new react native test renderer.

I'm seeing a very strange behavior from my current jest mocks for ListView and RefreshControl. Within the snapshot test, the wrapping component (which is RandomView in the example I wrote below) ListView is rendered multiple times from the RefreshControl's reference.

The resulting snapshot file is here, and you can see that the RandomView is being printed out multiple times, and not being resolved to a Circular reference.

What is the correct solution to this problem? Am I doing something unsupported or just doing something incorrectly?

Mocks:

jest.mock('RefreshControl', () => 'RefreshControl')

jest.mock('ListView', () => {
  const React = require('React')
  const View = require('react-native').View
  return React.createClass({
    statics: {
      DataSource: require.requireActual('ListView').DataSource
    },
    render () {
      // Renders all the elements in the ListView without regards for optimization
      let components = []
      for (let sectionIndex = 0; sectionIndex < this.props.dataSource.sectionIdentities.length; sectionIndex++) {
        if (this.props.renderSectionHeader) {
          components.push(this.props.renderSectionHeader(this.props.dataSource.getSectionHeaderData(sectionIndex), this.props.sectionIdentities[sectionIndex]))
        }
        if (this.props.renderRow) {
          for (let rowIndex = 0; rowIndex < this.props.dataSource.rowIdentities[sectionIndex].length; rowIndex++) {
            components.push(this.props.renderRow(this.props.dataSource.getRowData(sectionIndex, rowIndex)))
          }
        }
      }

      // passes the props to the rendered view
      return (<View {...this.props}>{components}</View>)
    }
  })
})

Component

import React, {PropTypes} from 'react'
import {
  ListView,
  RefreshControl,
  View
} from 'react-native'

var RandomView = React.createClass({
  propTypes: {
    list: PropTypes.array.isRequired
  },

  getDataSource: function (list) {
    return new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})
  },

  render: function () {
    return (
      <ListView
        enableEmptySections
        dataSource={this.getDataSource(this.props.list)}
        renderRow={this._renderRow}
        refreshControl={
          <RefreshControl />
        }
      />
    )
  },

  _renderRow: function (rowData) {
    return (
      <View />
    )
  }
})

export default RandomView

Test

import 'react-native'
import React from 'react'
import RandomView from '../RandomView'

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer'

describe('<RandomView />', () => {
  it('renders correctly', () => {
    const tree = renderer.create(
      <RandomView list={[{a: 1}, {a: 2}]} />
    ).toJSON()
    expect(tree).toMatchSnapshot()
  })
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions