All files / src/templates ListTemplate.js

100% Statements 11/11
100% Branches 15/15
100% Functions 1/1
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 45 46 47 48                      6x 1x     5x 1x     4x 1x     3x 1x     2x 1x     1x                                
import {
  TOP_ELEMENT_STYLES,
  LIST_TEMPLATE_MIN_ELEMENTS,
  LIST_TEMPLATE_MAX_ELEMENTS,
  LIST_TEMPLATE_MAX_BUTTONS,
} from '../constants';
 
class ListTemplate {
  constructor({
    elements, buttons = [], top_element_style = 'large', sharable = true,
  }) {
    if (!Array.isArray(elements)) {
      throw new Error('elements must be an array.');
    }
 
    if (elements.length < LIST_TEMPLATE_MIN_ELEMENTS) {
      throw new Error(`You must have more than ${LIST_TEMPLATE_MIN_ELEMENTS} elements.`);
    }
 
    if (elements.length > LIST_TEMPLATE_MAX_ELEMENTS) {
      throw new Error(`You cannot have more than ${LIST_TEMPLATE_MAX_ELEMENTS} elements.`);
    }
 
    if (TOP_ELEMENT_STYLES.indexOf(top_element_style) === -1) {
      throw new Error('Invalid top_element_style provided.');
    }
 
    if (buttons && buttons.length > 1) {
      throw new Error(`You can have a maximum of ${LIST_TEMPLATE_MAX_BUTTONS} button`);
    }
 
    return {
      attachment: {
        type: 'template',
        payload: {
          template_type: 'list',
          top_element_style,
          elements,
          sharable,
          buttons,
        },
      },
    };
  }
}
 
export default ListTemplate;