all files / lib/elements/ Button.js

100% Statements 0/0
100% Branches 0/0
100% Functions 0/0
100% Lines 0/0
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                                                                                   
class Button {
  constructor({ type, title, url = '', payload = '' }) {
    const buttonTypes = [
      'web_url',
      'postback',
      'phone_number',
      'account_link',
      'account_unlink',
    ];
 
    if (buttonTypes.indexOf(type) === -1) {
      throw new Error('Invalid button type 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;
 
    const button = {
      type: this.type,
      title: this.title,
    };
 
    if (this.url) {
      button.url = this.url;
    }
 
    if (this.payload) {
      button.payload = this.payload;
    }
 
    return button;
  }
}
 
export default Button;