Files
superset2/superset/assets/spec/javascripts/dashboard/util/isValidChild_spec.js
Christine Chambers 9029701f24 [refactor] Migrate from Mocha+Chai to Jest (#6079)
* [refactor] Migrate from Mocha+Chai to Jest

This change migrates all the existing unit tests
- to Jest's global expect and matchers from chai's imported expect, asserts and matchers.
- to Jest's describe/test from mocha's describe/it

The majority of the mechanical changes to tests are achieved through running jest-codemods. The only two note-worthy manual tweaks:
1. Setting a testURL of http://localhost in jest config and adjusting a few tests to leverage this value instead of relying on about:blank.
2. Re-enabling ExploreChartPanel_spec which was previously commented out as we cannot have empty tests with nothing in it with Jest. :)

This change also removes dependencies to Mocha and Chai.

* Remove the test:one command as it now does the same thing as test.

* Fixing lint errors. The diff looks large but is large done through `yarn run lint --fix`

The only noteworthy change is the one in eslintrc for tests. The env has been updated from mocha to jest.

* Adding eslint-plugin-jest and further modify tests.

- One small fix in sqllab's Timer Spec for a test that is not using the spy it created for testing.
- Deletion of a duplicated test caught by eslint-plugin-jest.

* - Make istanbul coverage work with Jest.

- Remove dependency on stand-alone istanbul and babel-istanbul as they're built-into jest. Yes!

* Attempt to fix dynamic imports in tests.

* run sequentially and log heap usage

* - tweaking maxworkers for travis and specifying coverageDirectory for codecov

- remove dynamic import in shim.js now that it is set in babelrc for tests only.
2018-10-15 13:10:18 -07:00

145 lines
4.9 KiB
JavaScript

import isValidChild from '../../../../src/dashboard/util/isValidChild';
import {
CHART_TYPE as CHART,
COLUMN_TYPE as COLUMN,
DASHBOARD_GRID_TYPE as GRID,
DASHBOARD_ROOT_TYPE as ROOT,
DIVIDER_TYPE as DIVIDER,
HEADER_TYPE as HEADER,
MARKDOWN_TYPE as MARKDOWN,
ROW_TYPE as ROW,
TABS_TYPE as TABS,
TAB_TYPE as TAB,
} from '../../../../src/dashboard/util/componentTypes';
const getIndentation = depth =>
Array(depth * 3)
.fill('')
.join('-');
describe('isValidChild', () => {
describe('valid calls', () => {
// these are representations of nested structures for easy testing
// [ROOT (depth 0) > GRID (depth 1) > HEADER (depth 2)]
// every unique parent > child relationship is tested, but because this
// test representation WILL result in duplicates, we hash each test
// to keep track of which we've run
const didTest = {};
const validExamples = [
[ROOT, GRID, CHART], // chart is valid because it is wrapped in a row
[ROOT, GRID, MARKDOWN], // markdown is valid because it is wrapped in a row
[ROOT, GRID, COLUMN], // column is valid because it is wrapped in a row
[ROOT, GRID, HEADER],
[ROOT, GRID, ROW, MARKDOWN],
[ROOT, GRID, ROW, CHART],
[ROOT, GRID, ROW, COLUMN, HEADER],
[ROOT, GRID, ROW, COLUMN, DIVIDER],
[ROOT, GRID, ROW, COLUMN, CHART],
[ROOT, GRID, ROW, COLUMN, MARKDOWN],
[ROOT, GRID, ROW, COLUMN, ROW, CHART],
[ROOT, GRID, ROW, COLUMN, ROW, MARKDOWN],
[ROOT, GRID, ROW, COLUMN, ROW, COLUMN, CHART],
[ROOT, GRID, ROW, COLUMN, ROW, COLUMN, MARKDOWN],
[ROOT, GRID, TABS, TAB, ROW, COLUMN, ROW, COLUMN, MARKDOWN],
// tab equivalents
[ROOT, TABS, TAB, CHART],
[ROOT, TABS, TAB, MARKDOWN],
[ROOT, TABS, TAB, COLUMN],
[ROOT, TABS, TAB, HEADER],
[ROOT, TABS, TAB, ROW, MARKDOWN],
[ROOT, TABS, TAB, ROW, CHART],
[ROOT, TABS, TAB, ROW, COLUMN, HEADER],
[ROOT, TABS, TAB, ROW, COLUMN, DIVIDER],
[ROOT, TABS, TAB, ROW, COLUMN, CHART],
[ROOT, TABS, TAB, ROW, COLUMN, MARKDOWN],
[ROOT, TABS, TAB, ROW, COLUMN, ROW, CHART],
[ROOT, TABS, TAB, ROW, COLUMN, ROW, MARKDOWN],
[ROOT, TABS, TAB, ROW, COLUMN, ROW, COLUMN, CHART],
[ROOT, TABS, TAB, ROW, COLUMN, ROW, COLUMN, MARKDOWN],
[ROOT, TABS, TAB, TABS, TAB, ROW, COLUMN, ROW, COLUMN, MARKDOWN],
];
validExamples.forEach((example, exampleIdx) => {
let childDepth = 0;
example.forEach((childType, i) => {
const parentDepth = childDepth - 1;
const parentType = example[i - 1];
const testKey = `${parentType}-${childType}-${parentDepth}`;
if (i > 0 && !didTest[testKey]) {
didTest[testKey] = true;
it(`(${exampleIdx})${getIndentation(
childDepth,
)}${parentType} (depth ${parentDepth}) > ${childType}`, () => {
expect(
isValidChild({
parentDepth,
parentType,
childType,
}),
).toBe(true);
});
}
// see isValidChild.js for why tabs do not increment the depth of their children
childDepth += childType !== TABS && childType !== TAB ? 1 : 0;
});
});
});
describe('invalid calls', () => {
// In order to assert that a parent > child hierarchy at a given depth is invalid
// we also define some valid hierarchies in doing so. we indicate which
// parent > [child] relationships should be asserted as invalid using a nested array
const invalidExamples = [
[ROOT, [DIVIDER]],
[ROOT, [CHART]],
[ROOT, [MARKDOWN]],
[ROOT, GRID, [TAB]],
[ROOT, GRID, TABS, [ROW]],
// [ROOT, GRID, TABS, TAB, [TABS]], // @TODO this needs to be fixed
[ROOT, GRID, ROW, [TABS]],
[ROOT, GRID, ROW, [TAB]],
[ROOT, GRID, ROW, [DIVIDER]],
[ROOT, GRID, ROW, COLUMN, [TABS]],
[ROOT, GRID, ROW, COLUMN, [TAB]],
[ROOT, GRID, ROW, COLUMN, ROW, [DIVIDER]],
[ROOT, GRID, ROW, COLUMN, ROW, COLUMN, [ROW]], // too nested
];
invalidExamples.forEach((example, exampleIdx) => {
let childDepth = 0;
example.forEach((childType, i) => {
const shouldTestChild = Array.isArray(childType);
if (i > 0 && shouldTestChild) {
const parentDepth = childDepth - 1;
const parentType = example[i - 1];
it(`(${exampleIdx})${getIndentation(
childDepth,
)}${parentType} (depth ${parentDepth}) > ${childType}`, () => {
expect(
isValidChild({
parentDepth,
parentType,
childType,
}),
).toBe(false);
});
}
// see isValidChild.js for why tabs do not increment the depth of their children
childDepth += childType !== TABS && childType !== TAB ? 1 : 0;
});
});
});
});