1 | const { Button, TextButton, LinkButton, LocationButton, VKPayButton, VKAppButton } = require('../index');
|
2 | const { TypeError } = require('../src/exceptions');
|
3 |
|
4 | const encode = (data) => {
|
5 | return JSON.stringify(data);
|
6 | };
|
7 |
|
8 | describe('TextButton', () => {
|
9 | it('should create button', () => {
|
10 | const { button } = new TextButton('label', 'default');
|
11 |
|
12 | expect(button.action.type).toBe('text');
|
13 | expect(button.action.label).toBe('label');
|
14 | expect(button.action.payload).toBe('[]');
|
15 | expect(button.color).toBe('default');
|
16 | });
|
17 |
|
18 | it('should create button with payload', () => {
|
19 | const { button } = new TextButton('label', 'default', 'action');
|
20 |
|
21 | expect(button.action.payload).toBe(encode('action'));
|
22 | });
|
23 |
|
24 | it('should create button with payload and args', () => {
|
25 | const { button } = new TextButton('label', 'default', [
|
26 | 'action', ['arg1', 'arg2']
|
27 | ]);
|
28 |
|
29 | expect(button.action.payload).toBe(encode([
|
30 | 'action', ['arg1', 'arg2']
|
31 | ]));
|
32 | });
|
33 | });
|
34 |
|
35 | describe('LinkButton', () => {
|
36 | it('should create button', () => {
|
37 | const { button } = new LinkButton('test', 'https://google.com');
|
38 |
|
39 | expect(button.action.type).toBe('open_link');
|
40 | expect(button.action.label).toBe('test');
|
41 | expect(button.action.link).toBe('https://google.com');
|
42 | expect(button.action.payload).toBe('[]');
|
43 | });
|
44 | });
|
45 |
|
46 | describe('LocationButton', () => {
|
47 | it('should create button', () => {
|
48 | const { button } = new LocationButton();
|
49 |
|
50 | expect(button.action.type).toBe('location');
|
51 | expect(button.action.payload).toBe('[]');
|
52 | });
|
53 | });
|
54 |
|
55 | describe('VKPayButton', () => {
|
56 | it('should create button', () => {
|
57 | const { button } = new VKPayButton('hash');
|
58 |
|
59 | expect(button.action.type).toBe('vkpay');
|
60 | expect(button.action.hash).toBe('hash');
|
61 | expect(button.action.payload).toBe('[]');
|
62 | });
|
63 |
|
64 | it('should create button from string', () => {
|
65 | const { button } = new VKPayButton('action=transfer-to-group&group_id=1&aid=10');
|
66 |
|
67 | expect(button.action.hash).toBe('action=transfer-to-group&group_id=1&aid=10');
|
68 | });
|
69 |
|
70 | it('should create button from object', () => {
|
71 | const { button } = new VKPayButton({ action: 'transfer-to-group', group_id: 1, aid: 1 });
|
72 |
|
73 | expect(button.action.hash).toBe('action=transfer-to-group&group_id=1&aid=1');
|
74 | });
|
75 | });
|
76 |
|
77 | describe('VKAppButton', () => {
|
78 | it('should create button', () => {
|
79 | const { button } = new VKAppButton('label', {});
|
80 |
|
81 | expect(button.action.type).toBe('open_app');
|
82 | expect(button.action.label).toBe('label');
|
83 | expect(button.action.payload).toBe('[]');
|
84 |
|
85 | expect(button.action.app_id).not.toBeDefined();
|
86 | expect(button.action.owner_id).not.toBeDefined();
|
87 | expect(button.action.hash).not.toBeDefined();
|
88 | });
|
89 |
|
90 | it('should create button with params', () => {
|
91 | const { button } = new VKAppButton('label', { app_id: 1, owner_id: -1, hash: 'hash' });
|
92 |
|
93 | expect(button.action.app_id).toBe(1);
|
94 | expect(button.action.owner_id).toBe(-1);
|
95 | expect(button.action.hash).toBe('hash');
|
96 | });
|
97 | });
|
98 |
|
99 | describe('Button', () => {
|
100 | it('should not allow to create default button', () => {
|
101 | expect(() => new Button('asd')).toThrow(TypeError);
|
102 | });
|
103 | });
|