export function generateLayerTests()

in modules/test-utils/src/generate-layer-tests.js [31:113]


export function generateLayerTests({
  Layer,
  sampleProps = {},
  assert = defaultAssert,
  onBeforeUpdate,
  onAfterUpdate = () => {},
  runDefaultAsserts = true
}) {
  assert(Layer.layerName, 'Layer should have display name');

  function wrapTestCaseTitle(title) {
    return `${Layer.layerName}#${title}`;
  }

  const testCases = [
    {
      title: 'Empty props',
      props: {}
    },
    {
      title: 'Null data',
      updateProps: {data: null}
    },
    {
      title: 'Sample data',
      updateProps: sampleProps
    }
  ];

  try {
    // Calling constructor for the first time resolves default props
    // eslint-disable-next-line
    new Layer({});
  } catch (error) {
    assert(false, `Construct ${Layer.layerName} throws: ${error.message}`);
  }

  const {_propTypes: propTypes, _mergedDefaultProps: defaultProps} = Layer;

  // Test alternative data formats
  testCases.push(...makeAltDataTestCases(sampleProps, propTypes));

  for (const propName in Layer.defaultProps) {
    if (!(propName in sampleProps)) {
      // Do not override user provided props - they may be layer-specific
      const newTestCase =
        makeAltPropTestCase({propName, propTypes, defaultProps, sampleProps, assert}) || [];
      testCases.push(...newTestCase);
    }
  }

  testCases.forEach(testCase => {
    testCase.title = wrapTestCaseTitle(testCase.title);
    const beforeFunc = testCase.onBeforeUpdate || noop;
    const afterFunc = testCase.onAfterUpdate || noop;
    testCase.onBeforeUpdate = params => {
      // Generated callback
      beforeFunc(params);
      // User callback
      onBeforeUpdate(params);
    };
    testCase.onAfterUpdate = params => {
      // Generated callback
      afterFunc(params);
      // User callback
      onAfterUpdate(params);

      // Default assert
      if (runDefaultAsserts) {
        if (params.layer.isComposite) {
          const {data} = params.layer.props;
          if (data && typeof data === 'object' && count(data)) {
            assert(params.subLayers.length, 'Layer should have sublayers');
          }
        } else {
          assert(params.layer.getModels().length, 'Layer should have models');
        }
      }
    };
  });

  return testCases;
}