universal_bot-ts
404 строки · 11.7 Кб
1import {
2Button,
3Buttons,
4IAlisaButton,
5ITelegramKeyboard,
6IViberButtonObject,
7ViberButton,
8IVkButtonObject,
9VkButton,
10mmApp
11} from '../../src';
12
13const DEFAULT_URL = 'https://test.ru';
14
15describe('Buttons test', () => {
16let defaultButtons: Buttons;
17
18beforeEach(() => {
19mmApp.params.utm_text = '';
20defaultButtons = new Buttons();
21for (let i = 0; i < 3; i++) {
22defaultButtons.addBtn(`${i + 1}`);
23defaultButtons.addLink(`${i + 1}`, DEFAULT_URL);
24}
25});
26
27it('Button utm text', () => {
28const button = new Button();
29mmApp.params.utm_text = null;
30button.initBtn('btn', 'https://google.com');
31expect(button.url).toEqual('https://google.com?utm_source=Yandex_Alisa&utm_medium=cpc&utm_campaign=phone');
32
33button.initBtn('btn', 'https://google.com?utm_source=test');
34expect(button.url).toEqual('https://google.com?utm_source=test');
35
36button.initBtn('btn', 'https://google.com?data=test');
37expect(button.url).toEqual('https://google.com?data=test&utm_source=Yandex_Alisa&utm_medium=cpc&utm_campaign=phone');
38
39mmApp.params.utm_text = 'my_utm_text';
40button.initBtn('btn', 'https://google.com');
41expect(button.url).toEqual('https://google.com?my_utm_text');
42});
43
44it('Get buttons Alisa', () => {
45const alisaButtons: IAlisaButton[] = [
46{
47title: '1',
48hide: true
49},
50{
51title: '1',
52hide: false,
53url: DEFAULT_URL
54},
55{
56title: '2',
57hide: true
58},
59{
60title: '2',
61hide: false,
62url: DEFAULT_URL
63},
64{
65title: '3',
66hide: true
67},
68{
69title: '3',
70hide: false,
71url: DEFAULT_URL
72}
73];
74expect(defaultButtons.getButtons(Buttons.T_ALISA_BUTTONS)).toEqual(alisaButtons);
75
76defaultButtons.btns = [
77{
78title: 'btn',
79url: DEFAULT_URL,
80payload: 'test'
81}
82];
83defaultButtons.links = [
84{
85title: 'link',
86url: DEFAULT_URL,
87payload: 'test'
88}
89];
90
91alisaButtons.push({
92title: 'btn',
93url: DEFAULT_URL,
94payload: 'test',
95hide: true
96});
97alisaButtons.push({
98title: 'link',
99url: DEFAULT_URL,
100payload: 'test',
101hide: false
102});
103expect(defaultButtons.getButtons(Buttons.T_ALISA_BUTTONS)).toEqual(alisaButtons);
104});
105it('Get buttons Alisa card', () => {
106expect(defaultButtons.getButtons(Buttons.T_ALISA_CARD_BUTTON)).toEqual({
107text: '1'
108});
109});
110
111it('Get buttons Vk', () => {
112const vkButtons: IVkButtonObject = {
113one_time: true,
114buttons: [
115{
116action: {
117type: Button.VK_TYPE_TEXT,
118label: '1'
119}
120},
121{
122action: {
123type: Button.VK_TYPE_LINK,
124link: DEFAULT_URL,
125label: '1'
126}
127},
128{
129action: {
130type: Button.VK_TYPE_TEXT,
131label: '2'
132}
133},
134{
135action: {
136type: Button.VK_TYPE_LINK,
137link: DEFAULT_URL,
138label: '2'
139}
140},
141{
142action: {
143type: Button.VK_TYPE_TEXT,
144label: '3'
145}
146},
147{
148action: {
149type: Button.VK_TYPE_LINK,
150link: DEFAULT_URL,
151label: '3'
152}
153}
154]
155};
156expect(defaultButtons.getButtons(Buttons.T_VK_BUTTONS)).toEqual(vkButtons);
157
158defaultButtons.btns = [
159{
160title: 'btn',
161url: DEFAULT_URL,
162payload: 'test'
163}
164];
165defaultButtons.links = [
166{
167title: 'link',
168url: DEFAULT_URL,
169payload: 'test'
170}
171];
172vkButtons.buttons.push({
173action: {
174type: Button.VK_TYPE_LINK,
175link: DEFAULT_URL,
176label: 'btn',
177payload: 'test'
178}
179});
180vkButtons.buttons.push({
181action: {
182type: Button.VK_TYPE_LINK,
183link: DEFAULT_URL,
184label: 'link',
185payload: 'test'
186}
187});
188expect(defaultButtons.getButtons(Buttons.T_VK_BUTTONS)).toEqual(vkButtons);
189
190defaultButtons.clear();
191expect(defaultButtons.getButtons(Buttons.T_VK_BUTTONS)).toEqual({one_time: false, buttons: []});
192
193});
194it('Get buttons Vk group', () => {
195const vkButtons: IVkButtonObject = {
196one_time: true,
197buttons: [
198[
199{
200action: {
201type: Button.VK_TYPE_TEXT,
202label: '1',
203payload: '{}'
204}
205},
206{
207action: {
208type: Button.VK_TYPE_LINK,
209link: DEFAULT_URL,
210label: '1',
211payload: '{}'
212}
213},
214{
215action: {
216type: Button.VK_TYPE_TEXT,
217label: '2',
218payload: '{}'
219}
220},
221{
222action: {
223type: Button.VK_TYPE_LINK,
224link: DEFAULT_URL,
225label: '2',
226payload: '{}'
227}
228},
229],
230[
231{
232action: {
233type: Button.VK_TYPE_TEXT,
234label: '3',
235payload: '{}'
236}
237},
238],
239{
240action: {
241type: Button.VK_TYPE_LINK,
242link: DEFAULT_URL,
243label: '3'
244}
245}
246]
247};
248defaultButtons.clear();
249defaultButtons.addBtn('1', null, {}, {[VkButton.GROUP_NAME]: 0});
250defaultButtons.addLink('1', DEFAULT_URL, {}, {[VkButton.GROUP_NAME]: 0});
251defaultButtons.addBtn('2', null, {}, {[VkButton.GROUP_NAME]: 0});
252defaultButtons.addLink('2', DEFAULT_URL, {}, {[VkButton.GROUP_NAME]: 0});
253
254defaultButtons.addBtn('3', null, {}, {[VkButton.GROUP_NAME]: 1});
255defaultButtons.addLink('3', DEFAULT_URL);
256expect(defaultButtons.getButtons(Buttons.T_VK_BUTTONS)).toEqual(vkButtons);
257
258defaultButtons.btns = [
259{
260title: 'btn',
261url: DEFAULT_URL,
262payload: {},
263options: {
264[VkButton.GROUP_NAME]: 1
265}
266}
267];
268defaultButtons.links = [
269{
270title: 'link',
271url: DEFAULT_URL,
272payload: 'test'
273}
274];
275
276vkButtons.buttons[1].push(
277{
278action: {
279type: Button.VK_TYPE_LINK,
280link: DEFAULT_URL,
281label: 'btn',
282payload: '{}'
283}
284}
285);
286vkButtons.buttons.push({
287action: {
288type: Button.VK_TYPE_LINK,
289link: DEFAULT_URL,
290label: 'link',
291payload: 'test'
292}
293});
294expect(defaultButtons.getButtons(Buttons.T_VK_BUTTONS)).toEqual(vkButtons);
295});
296
297it('Get buttons Viber', () => {
298const viberButtons: IViberButtonObject = {
299DefaultHeight: true,
300BgColor: '#FFFFFF',
301Buttons: [
302{
303Text: '1',
304ActionType: ViberButton.T_REPLY,
305ActionBody: '1'
306},
307{
308Text: '1',
309ActionType: ViberButton.T_OPEN_URL,
310ActionBody: DEFAULT_URL
311},
312{
313Text: '2',
314ActionType: ViberButton.T_REPLY,
315ActionBody: '2'
316},
317{
318Text: '2',
319ActionType: ViberButton.T_OPEN_URL,
320ActionBody: DEFAULT_URL
321},
322{
323Text: '3',
324ActionType: ViberButton.T_REPLY,
325ActionBody: '3'
326},
327{
328Text: '3',
329ActionType: ViberButton.T_OPEN_URL,
330ActionBody: DEFAULT_URL
331},
332]
333};
334expect(defaultButtons.getButtons(Buttons.T_VIBER_BUTTONS)).toEqual(viberButtons);
335
336defaultButtons.btns = [
337{
338title: 'btn',
339url: DEFAULT_URL,
340payload: 'test'
341}
342];
343defaultButtons.links = [
344{
345title: 'link',
346url: DEFAULT_URL,
347payload: 'test'
348}
349];
350viberButtons.Buttons.push({
351Text: 'btn',
352ActionType: ViberButton.T_OPEN_URL,
353ActionBody: DEFAULT_URL
354},);
355viberButtons.Buttons.push({
356Text: 'link',
357ActionType: ViberButton.T_OPEN_URL,
358ActionBody: DEFAULT_URL
359},);
360expect(defaultButtons.getButtons(Buttons.T_VIBER_BUTTONS)).toEqual(viberButtons);
361});
362
363it('Get buttons Telegram', () => {
364const telegramButtons: ITelegramKeyboard = {
365keyboard: [
366'1', '2', '3'
367]
368};
369
370expect(defaultButtons.getButtons(Buttons.T_TELEGRAM_BUTTONS)).toEqual(telegramButtons);
371
372defaultButtons.btns = [
373{
374title: 'btn',
375url: DEFAULT_URL,
376payload: 'test'
377}
378];
379defaultButtons.links = [
380{
381title: 'link',
382url: DEFAULT_URL,
383payload: 'test'
384}
385];
386
387telegramButtons.inline_keyboard = [];
388telegramButtons.inline_keyboard.push({
389text: 'btn',
390url: DEFAULT_URL,
391callback_data: 'test'
392});
393telegramButtons.inline_keyboard.push({
394text: 'link',
395url: DEFAULT_URL,
396callback_data: 'test'
397});
398
399expect(defaultButtons.getButtons(Buttons.T_TELEGRAM_BUTTONS)).toEqual(telegramButtons);
400
401defaultButtons.clear();
402expect(defaultButtons.getButtons(Buttons.T_TELEGRAM_BUTTONS)).toEqual({remove_keyboard: true});
403});
404});
405