bonsai-analyzer
Version:
Trim your dependency tree.
136 lines (132 loc) • 4.91 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _defaults = require("../../__test_helpers__/defaults");
var _filterModules = _interopRequireDefault(require("../filterModules"));
const emptyFilters = {
moduleName: '',
cumulativeSizeMin: '',
cumulativeSizeMax: '',
requiredByCountMin: '',
requiredByCountMax: '',
requirementsCountMin: '',
requirementsCountMax: ''
};
describe('filterModules', () => {
it('should return everything when there are no filters', () => {
const modules = [(0, _defaults.defaultExtendedModule)({
id: 1
}), (0, _defaults.defaultExtendedModule)({
id: 2
}), (0, _defaults.defaultExtendedModule)({
id: 3
})];
expect((0, _filterModules.default)(modules, emptyFilters)).toEqual(modules);
});
describe('name filter', () => {
it('keep modules when the name matches an alpha/num string', () => {
const indexJS = (0, _defaults.defaultExtendedModule)({
id: 1,
name: 'index'
});
const buttonJS = (0, _defaults.defaultExtendedModule)({
id: 1,
name: 'button'
});
const filters = { ...emptyFilters,
moduleName: 'index'
};
expect((0, _filterModules.default)([indexJS, buttonJS], filters)).toEqual([indexJS]);
});
it('keep modules when the name matches a string fragment', () => {
const indexJS = (0, _defaults.defaultExtendedModule)({
id: 1,
name: 'src/app/index'
});
const buttonJS = (0, _defaults.defaultExtendedModule)({
id: 1,
name: 'src/node_modules/button/index'
});
const filters = { ...emptyFilters,
moduleName: 'app'
};
expect((0, _filterModules.default)([indexJS, buttonJS], filters)).toEqual([indexJS]);
});
it('keep modules when the name matches a string with special chars', () => {
const indexJS = (0, _defaults.defaultExtendedModule)({
id: 1,
name: 'src/app/index'
});
const buttonJS = (0, _defaults.defaultExtendedModule)({
id: 1,
name: 'src/node_modules/button/index'
});
const filters = { ...emptyFilters,
moduleName: 'src/(?!node_modules)'
};
expect((0, _filterModules.default)([indexJS, buttonJS], filters)).toEqual([indexJS]);
});
});
describe('number range filters', () => {
const smallModule = (0, _defaults.defaultExtendedModule)({
id: 'small',
cumulativeSize: 10,
requiredByCount: 3,
requirementsCount: 5
});
const mediumModule = (0, _defaults.defaultExtendedModule)({
id: 'medium',
cumulativeSize: 200,
requiredByCount: 17,
requirementsCount: 19
});
const largeModule = (0, _defaults.defaultExtendedModule)({
id: 'large',
cumulativeSize: 3000,
requiredByCount: 23,
requirementsCount: 29
});
const allModuleSizes = [smallModule, mediumModule, largeModule];
it('should remove nothing when cumulativeSizeMin & cumulativeSizeMax are empty', () => {
const filters = { ...emptyFilters,
cumulativeSizeMin: '',
cumulativeSizeMax: ''
};
expect((0, _filterModules.default)(allModuleSizes, filters)).toEqual([smallModule, mediumModule, largeModule]);
});
it('should remove small & large when cumulativeSizeMin & cumulativeSizeMax is non-empty', () => {
const filters = { ...emptyFilters,
cumulativeSizeMin: '100',
cumulativeSizeMax: '1000'
};
expect((0, _filterModules.default)(allModuleSizes, filters)).toEqual([mediumModule]);
});
it('should remove just the small when cumulativeSizeMin is non-empty', () => {
const filters = { ...emptyFilters,
cumulativeSizeMin: '100',
cumulativeSizeMax: ''
};
expect((0, _filterModules.default)(allModuleSizes, filters)).toEqual([mediumModule, largeModule]);
});
it('should remove just the large when cumulativeSizeMax is non-empty', () => {
const filters = { ...emptyFilters,
cumulativeSizeMin: '',
cumulativeSizeMax: '1000'
};
expect((0, _filterModules.default)(allModuleSizes, filters)).toEqual([smallModule, mediumModule]);
});
it('should remove small & large when requiredByCountMin & requiredByCountMax is non-empty', () => {
const filters = { ...emptyFilters,
requiredByCountMin: '12',
requiredByCountMax: '20'
};
expect((0, _filterModules.default)(allModuleSizes, filters)).toEqual([mediumModule]);
});
it('should remove small & large when requirementsCountMin & requirementsCountMax is non-empty', () => {
const filters = { ...emptyFilters,
requirementsCountMin: '12',
requirementsCountMax: '20'
};
expect((0, _filterModules.default)(allModuleSizes, filters)).toEqual([mediumModule]);
});
});
});