tests/core/utils/helpers.tsx   A
last analyzed

Complexity

Total Complexity 11
Complexity/F 0

Size

Lines of Code 114
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 88
mnd 11
bc 11
fnc 0
dl 0
loc 114
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import React from "react";
2
import { mount, shallow } from "enzyme";
3
import PKG from "../../../package.json";
4
5
var fs = require("fs");
6
var path = require("path");
7
8
export const describeSection = (section: string, test?: () => void) => {
9
  describe(`\n\n** ${section} **\n`, test ? test : () => {});
10
};
11
12
export const describeComponent = (component: string, test?: () => void) => {
13
  describe(`\n\n     - ${component}\n`, test ? test : () => {});
14
};
15
16
export const describeTest = (component: string, testSuite: () => void) => {
17
  describe(`\n        - ${component}\n`, testSuite ? testSuite : () => {});
18
};
19
20
export const renderingTest = (
21
  TestComponent: (props: any) => JSX.Element,
22
  params?: Record<string, any>,
23
  mountComponent?: boolean
24
) => {
25
  const method = mountComponent ? mount : shallow;
26
  describeTest("rendering test", () => {
27
    test("no parameters", () => {
28
      const wrapper = method(<TestComponent />);
29
      expect(wrapper);
30
    });
31
    params &&
32
      test("with parameters", () => {
33
        const wrapper = method(
34
          <TestComponent
35
            hide
36
            dark
37
            unstyled
38
            shadow={false}
39
            id="test-id"
40
            className="test-className"
41
            {...params}
42
          />
43
        );
44
        expect(wrapper);
45
      });
46
  });
47
};
48
49
export const getFilesList = (dir: string, showOnlyDirs?: boolean) => {
50
  const testDir = path.resolve(__dirname, dir);
51
  let result = [];
52
  let openedDir = fs.opendirSync(testDir);
53
54
  let filesLeft = true;
55
  while (filesLeft) {
56
    let fileDirent = openedDir.readSync();
57
    if (fileDirent != null) {
58
      const isDir = fs
59
        .lstatSync(path.join(openedDir.path, fileDirent.name))
60
        .isDirectory();
61
      if (isDir && showOnlyDirs) {
62
        result.push({
63
          name: fileDirent.name.split(".")[0],
64
          path: path.join(openedDir.path, fileDirent.name),
65
        });
66
      } else if (!isDir) {
67
        result.push({
68
          name: fileDirent.name.split(".")[0],
69
          path: path.join(openedDir.path, fileDirent.name),
70
        });
71
      }
72
    } else filesLeft = false;
73
  }
74
75
  result = result.sort((elem1, elem2) => {
76
    if (elem1.name < elem2.name) return -1;
77
    if (elem1.name === elem2.name) return 0;
78
    return 1;
79
  });
80
81
  openedDir.close();
82
  return result;
83
};
84
85
export const executeTests = () => {
86
  const path = require("path");
87
88
  const TEST_SUITES_PATH = path.resolve(__dirname, "../../test-suites/");
89
  const dirs = getFilesList(TEST_SUITES_PATH, true);
90
91
  describe(`\n                       ## ${PKG.name
92
    .replace("@cianciarusocataldo/", "")
93
    .toLowerCase()} - v.${PKG.version} - Unit tests ##`, () => {
94
    dirs.forEach(({ name: category }) =>
95
      describeSection(
96
        category.charAt(0).toUpperCase() + category.slice(1),
97
        () => {
98
          const elements = getFilesList(path.join(TEST_SUITES_PATH, category));
99
          elements.map((element) => {
100
            describeComponent(element.name, () => {
101
              try {
102
                require(element.path);
103
              } catch (error) {
104
                console.log(error);
105
                console.log(`${element.path} not found`);
106
              }
107
            });
108
          });
109
        }
110
      )
111
    );
112
  });
113
};
114