All files order-items.spec.js

100% Statements 25/25
100% Branches 0/0
100% Functions 5/5
100% Lines 23/23
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 451x 1x   1x   1x 1x     1x     1x     1x     1x     1x       1x     1x 19x 22x 4x     1x 1x 1x   1x 1x 1x 1x 1x    
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();
});