All files / src/elements Button.js

100% Statements 25/25
100% Branches 23/23
100% Functions 1/1
100% Lines 25/25

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                              24x 1x     23x 2x     21x 1x     20x 20x 20x 20x 20x 20x 20x   20x         20x 19x     20x 1x     20x 6x     20x 2x     20x 2x     20x          
import {
  WEBVIEW_HEIGHT_RATIOS,
  BUTTON_TYPES,
} from '../constants';
 
class Button {
  constructor({
    type,
    title,
    url = '',
    payload = '',
    webview_height_ratio = '',
    messenger_extensions = '',
    fallback_url = '',
  }) {
    if (BUTTON_TYPES.indexOf(type) === -1) {
      throw new Error('Invalid button type provided.');
    }
 
    if (webview_height_ratio && WEBVIEW_HEIGHT_RATIOS.indexOf(webview_height_ratio) === -1) {
      throw new Error('Invalid webview_height_ratio provided.');
    }
 
    if (title.length > 20) {
      throw new Error('Title cannot be longer 20 characters.');
    }
 
    this.type = type;
    this.title = title;
    this.url = url;
    this.payload = payload;
    this.webview_height_ratio = webview_height_ratio;
    this.messenger_extensions = messenger_extensions;
    this.fallback_url = fallback_url;
 
    const button = {
      type: this.type,
      title: this.title,
    };
 
    if (this.url) {
      button.url = this.url;
    }
 
    if (this.payload) {
      button.payload = this.payload;
    }
 
    if (this.webview_height_ratio) {
      button.webview_height_ratio = this.webview_height_ratio;
    }
 
    if (this.messenger_extensions) {
      button.messenger_extensions = 'true';
    }
 
    if (this.fallback_url) {
      button.fallback_url = this.fallback_url;
    }
 
    return button;
  }
}
 
export default Button;