universal_bot-ts

Форк
0
/
buttons.test.ts 
404 строки · 11.7 Кб
1
import {
2
    Button,
3
    Buttons,
4
    IAlisaButton,
5
    ITelegramKeyboard,
6
    IViberButtonObject,
7
    ViberButton,
8
    IVkButtonObject,
9
    VkButton,
10
    mmApp
11
} from '../../src';
12

13
const DEFAULT_URL = 'https://test.ru';
14

15
describe('Buttons test', () => {
16
    let defaultButtons: Buttons;
17

18
    beforeEach(() => {
19
        mmApp.params.utm_text = '';
20
        defaultButtons = new Buttons();
21
        for (let i = 0; i < 3; i++) {
22
            defaultButtons.addBtn(`${i + 1}`);
23
            defaultButtons.addLink(`${i + 1}`, DEFAULT_URL);
24
        }
25
    });
26

27
    it('Button utm text', () => {
28
        const button = new Button();
29
        mmApp.params.utm_text = null;
30
        button.initBtn('btn', 'https://google.com');
31
        expect(button.url).toEqual('https://google.com?utm_source=Yandex_Alisa&utm_medium=cpc&utm_campaign=phone');
32

33
        button.initBtn('btn', 'https://google.com?utm_source=test');
34
        expect(button.url).toEqual('https://google.com?utm_source=test');
35

36
        button.initBtn('btn', 'https://google.com?data=test');
37
        expect(button.url).toEqual('https://google.com?data=test&utm_source=Yandex_Alisa&utm_medium=cpc&utm_campaign=phone');
38

39
        mmApp.params.utm_text = 'my_utm_text';
40
        button.initBtn('btn', 'https://google.com');
41
        expect(button.url).toEqual('https://google.com?my_utm_text');
42
    });
43

44
    it('Get buttons Alisa', () => {
45
        const alisaButtons: IAlisaButton[] = [
46
            {
47
                title: '1',
48
                hide: true
49
            },
50
            {
51
                title: '1',
52
                hide: false,
53
                url: DEFAULT_URL
54
            },
55
            {
56
                title: '2',
57
                hide: true
58
            },
59
            {
60
                title: '2',
61
                hide: false,
62
                url: DEFAULT_URL
63
            },
64
            {
65
                title: '3',
66
                hide: true
67
            },
68
            {
69
                title: '3',
70
                hide: false,
71
                url: DEFAULT_URL
72
            }
73
        ];
74
        expect(defaultButtons.getButtons(Buttons.T_ALISA_BUTTONS)).toEqual(alisaButtons);
75

76
        defaultButtons.btns = [
77
            {
78
                title: 'btn',
79
                url: DEFAULT_URL,
80
                payload: 'test'
81
            }
82
        ];
83
        defaultButtons.links = [
84
            {
85
                title: 'link',
86
                url: DEFAULT_URL,
87
                payload: 'test'
88
            }
89
        ];
90

91
        alisaButtons.push({
92
            title: 'btn',
93
            url: DEFAULT_URL,
94
            payload: 'test',
95
            hide: true
96
        });
97
        alisaButtons.push({
98
            title: 'link',
99
            url: DEFAULT_URL,
100
            payload: 'test',
101
            hide: false
102
        });
103
        expect(defaultButtons.getButtons(Buttons.T_ALISA_BUTTONS)).toEqual(alisaButtons);
104
    });
105
    it('Get buttons Alisa card', () => {
106
        expect(defaultButtons.getButtons(Buttons.T_ALISA_CARD_BUTTON)).toEqual({
107
            text: '1'
108
        });
109
    });
110

111
    it('Get buttons Vk', () => {
112
        const vkButtons: IVkButtonObject = {
113
            one_time: true,
114
            buttons: [
115
                {
116
                    action: {
117
                        type: Button.VK_TYPE_TEXT,
118
                        label: '1'
119
                    }
120
                },
121
                {
122
                    action: {
123
                        type: Button.VK_TYPE_LINK,
124
                        link: DEFAULT_URL,
125
                        label: '1'
126
                    }
127
                },
128
                {
129
                    action: {
130
                        type: Button.VK_TYPE_TEXT,
131
                        label: '2'
132
                    }
133
                },
134
                {
135
                    action: {
136
                        type: Button.VK_TYPE_LINK,
137
                        link: DEFAULT_URL,
138
                        label: '2'
139
                    }
140
                },
141
                {
142
                    action: {
143
                        type: Button.VK_TYPE_TEXT,
144
                        label: '3'
145
                    }
146
                },
147
                {
148
                    action: {
149
                        type: Button.VK_TYPE_LINK,
150
                        link: DEFAULT_URL,
151
                        label: '3'
152
                    }
153
                }
154
            ]
155
        };
156
        expect(defaultButtons.getButtons(Buttons.T_VK_BUTTONS)).toEqual(vkButtons);
157

158
        defaultButtons.btns = [
159
            {
160
                title: 'btn',
161
                url: DEFAULT_URL,
162
                payload: 'test'
163
            }
164
        ];
165
        defaultButtons.links = [
166
            {
167
                title: 'link',
168
                url: DEFAULT_URL,
169
                payload: 'test'
170
            }
171
        ];
172
        vkButtons.buttons.push({
173
            action: {
174
                type: Button.VK_TYPE_LINK,
175
                link: DEFAULT_URL,
176
                label: 'btn',
177
                payload: 'test'
178
            }
179
        });
180
        vkButtons.buttons.push({
181
            action: {
182
                type: Button.VK_TYPE_LINK,
183
                link: DEFAULT_URL,
184
                label: 'link',
185
                payload: 'test'
186
            }
187
        });
188
        expect(defaultButtons.getButtons(Buttons.T_VK_BUTTONS)).toEqual(vkButtons);
189

190
        defaultButtons.clear();
191
        expect(defaultButtons.getButtons(Buttons.T_VK_BUTTONS)).toEqual({one_time: false, buttons: []});
192

193
    });
194
    it('Get buttons Vk group', () => {
195
        const vkButtons: IVkButtonObject = {
196
            one_time: true,
197
            buttons: [
198
                [
199
                    {
200
                        action: {
201
                            type: Button.VK_TYPE_TEXT,
202
                            label: '1',
203
                            payload: '{}'
204
                        }
205
                    },
206
                    {
207
                        action: {
208
                            type: Button.VK_TYPE_LINK,
209
                            link: DEFAULT_URL,
210
                            label: '1',
211
                            payload: '{}'
212
                        }
213
                    },
214
                    {
215
                        action: {
216
                            type: Button.VK_TYPE_TEXT,
217
                            label: '2',
218
                            payload: '{}'
219
                        }
220
                    },
221
                    {
222
                        action: {
223
                            type: Button.VK_TYPE_LINK,
224
                            link: DEFAULT_URL,
225
                            label: '2',
226
                            payload: '{}'
227
                        }
228
                    },
229
                ],
230
                [
231
                    {
232
                        action: {
233
                            type: Button.VK_TYPE_TEXT,
234
                            label: '3',
235
                            payload: '{}'
236
                        }
237
                    },
238
                ],
239
                {
240
                    action: {
241
                        type: Button.VK_TYPE_LINK,
242
                        link: DEFAULT_URL,
243
                        label: '3'
244
                    }
245
                }
246
            ]
247
        };
248
        defaultButtons.clear();
249
        defaultButtons.addBtn('1', null, {}, {[VkButton.GROUP_NAME]: 0});
250
        defaultButtons.addLink('1', DEFAULT_URL, {}, {[VkButton.GROUP_NAME]: 0});
251
        defaultButtons.addBtn('2', null, {}, {[VkButton.GROUP_NAME]: 0});
252
        defaultButtons.addLink('2', DEFAULT_URL, {}, {[VkButton.GROUP_NAME]: 0});
253

254
        defaultButtons.addBtn('3', null, {}, {[VkButton.GROUP_NAME]: 1});
255
        defaultButtons.addLink('3', DEFAULT_URL);
256
        expect(defaultButtons.getButtons(Buttons.T_VK_BUTTONS)).toEqual(vkButtons);
257

258
        defaultButtons.btns = [
259
            {
260
                title: 'btn',
261
                url: DEFAULT_URL,
262
                payload: {},
263
                options: {
264
                    [VkButton.GROUP_NAME]: 1
265
                }
266
            }
267
        ];
268
        defaultButtons.links = [
269
            {
270
                title: 'link',
271
                url: DEFAULT_URL,
272
                payload: 'test'
273
            }
274
        ];
275

276
        vkButtons.buttons[1].push(
277
            {
278
                action: {
279
                    type: Button.VK_TYPE_LINK,
280
                    link: DEFAULT_URL,
281
                    label: 'btn',
282
                    payload: '{}'
283
                }
284
            }
285
        );
286
        vkButtons.buttons.push({
287
            action: {
288
                type: Button.VK_TYPE_LINK,
289
                link: DEFAULT_URL,
290
                label: 'link',
291
                payload: 'test'
292
            }
293
        });
294
        expect(defaultButtons.getButtons(Buttons.T_VK_BUTTONS)).toEqual(vkButtons);
295
    });
296

297
    it('Get buttons Viber', () => {
298
        const viberButtons: IViberButtonObject = {
299
            DefaultHeight: true,
300
            BgColor: '#FFFFFF',
301
            Buttons: [
302
                {
303
                    Text: '1',
304
                    ActionType: ViberButton.T_REPLY,
305
                    ActionBody: '1'
306
                },
307
                {
308
                    Text: '1',
309
                    ActionType: ViberButton.T_OPEN_URL,
310
                    ActionBody: DEFAULT_URL
311
                },
312
                {
313
                    Text: '2',
314
                    ActionType: ViberButton.T_REPLY,
315
                    ActionBody: '2'
316
                },
317
                {
318
                    Text: '2',
319
                    ActionType: ViberButton.T_OPEN_URL,
320
                    ActionBody: DEFAULT_URL
321
                },
322
                {
323
                    Text: '3',
324
                    ActionType: ViberButton.T_REPLY,
325
                    ActionBody: '3'
326
                },
327
                {
328
                    Text: '3',
329
                    ActionType: ViberButton.T_OPEN_URL,
330
                    ActionBody: DEFAULT_URL
331
                },
332
            ]
333
        };
334
        expect(defaultButtons.getButtons(Buttons.T_VIBER_BUTTONS)).toEqual(viberButtons);
335

336
        defaultButtons.btns = [
337
            {
338
                title: 'btn',
339
                url: DEFAULT_URL,
340
                payload: 'test'
341
            }
342
        ];
343
        defaultButtons.links = [
344
            {
345
                title: 'link',
346
                url: DEFAULT_URL,
347
                payload: 'test'
348
            }
349
        ];
350
        viberButtons.Buttons.push({
351
            Text: 'btn',
352
            ActionType: ViberButton.T_OPEN_URL,
353
            ActionBody: DEFAULT_URL
354
        },);
355
        viberButtons.Buttons.push({
356
            Text: 'link',
357
            ActionType: ViberButton.T_OPEN_URL,
358
            ActionBody: DEFAULT_URL
359
        },);
360
        expect(defaultButtons.getButtons(Buttons.T_VIBER_BUTTONS)).toEqual(viberButtons);
361
    });
362

363
    it('Get buttons Telegram', () => {
364
        const telegramButtons: ITelegramKeyboard = {
365
            keyboard: [
366
                '1', '2', '3'
367
            ]
368
        };
369

370
        expect(defaultButtons.getButtons(Buttons.T_TELEGRAM_BUTTONS)).toEqual(telegramButtons);
371

372
        defaultButtons.btns = [
373
            {
374
                title: 'btn',
375
                url: DEFAULT_URL,
376
                payload: 'test'
377
            }
378
        ];
379
        defaultButtons.links = [
380
            {
381
                title: 'link',
382
                url: DEFAULT_URL,
383
                payload: 'test'
384
            }
385
        ];
386

387
        telegramButtons.inline_keyboard = [];
388
        telegramButtons.inline_keyboard.push({
389
            text: 'btn',
390
            url: DEFAULT_URL,
391
            callback_data: 'test'
392
        });
393
        telegramButtons.inline_keyboard.push({
394
            text: 'link',
395
            url: DEFAULT_URL,
396
            callback_data: 'test'
397
        });
398

399
        expect(defaultButtons.getButtons(Buttons.T_TELEGRAM_BUTTONS)).toEqual(telegramButtons);
400

401
        defaultButtons.clear();
402
        expect(defaultButtons.getButtons(Buttons.T_TELEGRAM_BUTTONS)).toEqual({remove_keyboard: true});
403
    });
404
});
405

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.