All files / src/messenger_profile PersistentMenuItem.js

100% Statements 32/32
100% Branches 53/53
100% Functions 1/1
100% Lines 32/32

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89                                        17x 1x     16x 1x     15x 1x     14x 1x     13x 1x     12x 1x     11x 1x     10x         10x 5x     10x 3x     10x 2x 1x   1x     9x 1x     9x 2x 2x 1x       9x 1x     9x          
import {
  WEBVIEW_HEIGHT_RATIOS,
  PERSISTENT_MENU_TYPES,
  PERSISTENT_MENU_TITLE_MAX_LENGTH,
  PERSISTENT_MENU_PAYLOAD_MAX_LENGTH,
  PERSISTENT_MENU_SUB_LEVEL_CTA_LIMIT,
} from '../constants';
 
class PersistentMenuItem {
  constructor({
    type,
    title,
    url = '',
    payload = '',
    call_to_actions = [],
    webview_height_ratio = '',
    messenger_extensions = false,
    fallback_url = '',
    webview_share_button = '',
  }) {
    if (PERSISTENT_MENU_TYPES.indexOf(type) === -1) {
      throw new Error('Invalid type provided.');
    }
 
    if (title.length > PERSISTENT_MENU_TITLE_MAX_LENGTH) {
      throw new Error(`Title cannot be longer ${PERSISTENT_MENU_TITLE_MAX_LENGTH} characters.`);
    }
 
    if (payload && payload.length > PERSISTENT_MENU_PAYLOAD_MAX_LENGTH) {
      throw new Error(`Payload cannot be longer ${PERSISTENT_MENU_PAYLOAD_MAX_LENGTH} characters.`);
    }
 
    if (type === 'web_url' && !url) {
      throw new Error('`url` must be supplied for `web_url` type menu items.');
    }
 
    if (type === 'postback' && !payload) {
      throw new Error('`payload` must be supplied for `postback` type menu items.');
    }
 
    if (type === 'nested' && !call_to_actions.length) {
      throw new Error('`call_to_actions` must be supplied for `nested` type menu items.');
    }
 
    if (webview_height_ratio && WEBVIEW_HEIGHT_RATIOS.indexOf(webview_height_ratio) === -1) {
      throw new Error('Invalid `webview_height_ratio` provided.');
    }
 
    const res = {
      type,
      title,
    };
 
    if (url && type === 'web_url') {
      res.url = url;
    }
 
    if (payload && type === 'postback') {
      res.payload = payload;
    }
 
    if (call_to_actions && type === 'nested') {
      if (call_to_actions.length > PERSISTENT_MENU_SUB_LEVEL_CTA_LIMIT) {
        throw new Error(`call_to_actions is limited to ${PERSISTENT_MENU_SUB_LEVEL_CTA_LIMIT} for sub-levels`);
      }
      res.call_to_actions = call_to_actions;
    }
 
    if (webview_height_ratio) {
      res.webview_height_ratio = webview_height_ratio;
    }
 
    if (messenger_extensions) {
      res.messenger_extensions = Boolean(messenger_extensions);
      if (fallback_url) {
        res.fallback_url = fallback_url;
      }
    }
 
    if (webview_share_button) {
      res.webview_share_button = webview_share_button;
    }
 
    return res;
  }
}
 
export default PersistentMenuItem;