import { test } from 'blue-tape';
import { findLastIndex } from './utils';
import orderItems from './order-items';
const arbitraryItems = () => {
const template = {
type: 'template',
};
const page = {
type: 'page',
};
const container = {
type: 'container',
};
const feature = {
type: 'feature',
};
const widget = {
type: 'widget',
};
const arbitraryOtherItem = {
type: 'arbitraryOtherType',
};
return [page, template, arbitraryOtherItem, container, feature, template, widget, feature];
};
const allBefore = (items, dependencyType, dependentType) => {
const firstDependentIndex = items.findIndex(item => item.type === dependentType);
const lastDependencyIndex = findLastIndex(items, item => item.type === dependencyType);
return (lastDependencyIndex <= firstDependentIndex);
};
test('unit :: order-items :: default', (assert) => {
const items = arbitraryItems();
const ordered = orderItems(items);
assert.ok(allBefore(ordered, 'template', 'container'), 'templates are before the containers');
assert.ok(allBefore(ordered, 'template', 'page'), 'templates are before the pages');
assert.ok(allBefore(ordered, 'feature', 'widget'), 'features are before the widgets');
assert.ok(allBefore(ordered, 'arbitraryOtherType', 'template'), 'unknown types are sorted first');
assert.end();
});
|