// @flow
import React from 'react';
import { renderSnapshot } from '../utils/tests/test-utils';

/**
 * This file serves two purposes:
 * - make sure we don't have stories that throw exceptions on render
 * - give people coverage for stories, since they will be executed by Eyebot.
 *
 * Note that we are not checking the actual output of the stories, only that
 * they render without throwing exceptions. Eyebot is responsible for actual
 * presentational verification.
 */

// $FlowFixMe - Flow doesn't like dynamic requires because it can't analyze them.
const req = require.context(`${process.cwd()}/src`, true, /\.stories\.js$/);
function loadStories() {
  return req.keys().map((key) => ({
    fileName: key,
    exports: req(key),
  }));
}

const allStories = loadStories();

allStories.forEach(({ fileName, exports }) => {
  describe(`Story sanity check: ${fileName}`, () => {
    Object.entries(exports).forEach(([name, Story]) => {
      if (typeof Story === 'function') {
        it(`${name} renders without errors`, () => {
          // $FlowFixMe - dynamic code - type unknown
          renderSnapshot(<Story />);
        });
      }
    });
  });
});
