Skip to content

Commit

Permalink
fix: fixed a few mistakes & fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Jul 19, 2018
1 parent 9756ca3 commit 44a1dab
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 35 deletions.
9 changes: 4 additions & 5 deletions src/button.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ButtonParams, IButton } from './types/button'
const button = (params: ButtonParams | string): IButton => {
import { IButton } from './types/button'
const button = (params: IButton | string): IButton => {
// Button has been created from string
if (typeof params === 'string') {
return {
Expand All @@ -10,19 +10,18 @@ const button = (params: ButtonParams | string): IButton => {
if (typeof params === 'object') {
const {
title,
text,
tts,
url,
hide = false,
payload,
} = params

if (!title && !text) {
if (!title) {
throw new Error('text is a required parameter')
}

return {
title: title || text,
title: title,
tts,
url,
hide,
Expand Down
35 changes: 7 additions & 28 deletions src/buttonBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,18 @@
import { IButton } from './types/button'

interface ButtonConstructor {
title?: string // title and text — same
text?: string
}
export default class ButtonBuilder {
public button: {
title?: string,
url?: string,
hide?: boolean,
payload?: {},
}
constructor(buttonConstructor?: ButtonConstructor) {
button: IButton
constructor(buttonConstructor?: IButton) {
/* No button object passed to the constructor */
if (!buttonConstructor) {
this.button = {}
this.button = {
title: ''
}
return this
}

/* Object-constructor passed */
if (typeof buttonConstructor !== 'object') {
throw new Error('Invalid ButtonBuilder constructor type. Should be object')
}
const {
title, text,
} = buttonConstructor
if (!title && !text) {
throw new Error('Button [title] or [text] is required for ButtonBuilder constructor.')
}

this.button = buttonConstructor
return this.button
}

public text(text) {
return this._setTitle(text)
return this
}

public title(title) {
Expand Down
10 changes: 10 additions & 0 deletions src/tests/button.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import button from '../button'
import ButtonBuilder from '../buttonBuilder'

test('create button with string constructor', () => {
const expected = {
Expand All @@ -20,5 +21,14 @@ test('create button with object constructor', () => {
payload: expected.payload,
hide: expected.hide,
})

const btn2 = new ButtonBuilder()
.title(expected.title)
.payload(expected.payload)
.shouldHide(expected.hide)
.get()

expect(btn).toEqual(expected)
expect(btn2).toEqual(expected)
})

4 changes: 2 additions & 2 deletions src/tests/buttonBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('common test for buttonBuilder', () => {

// test using button builder factory
button
.text(expected.title)
.title(expected.title)
.url(expected.url)
.shouldHide(expected.hide)
.payload(expected.payload)
Expand All @@ -25,5 +25,5 @@ test('common test for buttonBuilder', () => {
// test using button builder constructor

const button2 = new ButtonBuilder(expected)
expect(button2).toEqual(expected)
expect(button2.get()).toEqual(expected)
})

0 comments on commit 44a1dab

Please sign in to comment.