All files / tests/shared demoTest.tsx

46.88% Statements 15/32
24% Branches 6/25
57.14% Functions 4/7
50% Lines 14/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73                1x 1x               5x                                                                   1x   1x 5x 5x     5x 5x 5x   5x     5x   5x 5x        
import glob from 'glob';
import {render} from 'enzyme';
import MockDate from 'mockdate';
import moment from 'moment';
 
type CheerIO = ReturnType<typeof render>;
type CheerIOElement = CheerIO[0];
// We should avoid use it in 4.0. Reopen if can not handle this.
const USE_REPLACEMENT = false;
const testDist = process.env.LIB_DIR === 'dist';
 
/**
 * Rc component will generate id for aria usage. It's created as `test-uuid` when env === 'test'. Or
 * `f7fa7a3c-a675-47bc-912e-0c45fb6a74d9`(randomly) when not test env. So we need hack of this to
 * modify the `aria-controls`.
 */
function ariaConvert(wrapper: CheerIO) {
  Eif (!testDist || !USE_REPLACEMENT) return wrapper;
 
  const matches = new Map();
 
  function process(entry: CheerIOElement) {
    if (entry.type === 'text' || entry.type === 'tag') {
      return;
    }
    const {attribs, children} = entry;
    if (matches.has(entry)) return;
    matches.set(entry, true);
 
    // Change aria
    if (attribs && attribs['aria-controls']) {
      attribs['aria-controls'] = '' as never; // Remove all the aria to keep render sync in jest & jest node
    }
 
    // Loop children
    if (!children) {
      return;
    }
    (Array.isArray(children) ? children : [children]).forEach(process);
  }
 
  wrapper.each((_, entry) => process(entry));
 
  return wrapper;
}
 
type Options = {
  skip?: boolean;
};
 
export default function demoTest(component: string, options: Options = {}) {
  const files = glob.sync(`./src/components/${component}/demos/*.tsx`);
 
  files.forEach(file => {
    let testMethod = options.skip === true ? test.skip : test;
    Iif (Array.isArray(options.skip) && options.skip.some(c => file.includes(c))) {
      testMethod = test.skip;
    }
    testMethod(`renders ${file} correctly`, () => {
      MockDate.set(moment('2016-11-22').valueOf());
      const demo = require(`../.${file}`).default; // eslint-disable-line global-require, import/no-dynamic-require
 
      const wrapper = render(demo);
 
      // Convert aria related content
      ariaConvert(wrapper);
 
      expect(wrapper).toMatchSnapshot();
      MockDate.reset();
    });
  });
}