Skip to content

Commit 29c19c0

Browse files
committed
Adds Click module tests.
1 parent 73242b2 commit 29c19c0

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

__tests__/Click.test.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*!
2+
* Click.test.js - tests for Click functionality.
3+
* Copyright (c) 2018 - 2019 Richard Huang <rickypc@users.noreply.github.com>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as
7+
* published by the Free Software Foundation, either version 3 of the
8+
* License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
const Click = require('../lib/Click.js');
20+
21+
const mock = {
22+
$eval: jest.fn((selector, callback) => new Promise(async (resolve, reject) => {
23+
if (this.action === 'error') {
24+
reject(Error('error'));
25+
} else {
26+
resolve(callback(mock.element));
27+
}
28+
})),
29+
debug: jest.spyOn(Click.__test__.logger, 'debug'),
30+
element: {},
31+
waitUntilActionReady: jest.fn(() => new Promise(resolve => setTimeout(resolve, 1000))),
32+
};
33+
34+
describe('Click module test', () => {
35+
describe('ifExists', () => {
36+
beforeAll(() => {
37+
this.action = '';
38+
this.context = [];
39+
this.context.trigger = jest.fn(() => this.context);
40+
global.angular = {
41+
element: jest.fn(() => this.context),
42+
};
43+
});
44+
45+
afterAll(() => {
46+
global.angular = null;
47+
this.context = null;
48+
});
49+
50+
it('should return truthy', async () => {
51+
this.action = '';
52+
this.context.length = 1;
53+
const actual = await Click.ifExists.call(mock, 'selector', 'label', 1100);
54+
expect(actual).toBeTruthy();
55+
expect(mock.$eval).toHaveBeenCalledTimes(1);
56+
expect(mock.$eval).toHaveBeenNthCalledWith(1, 'selector', expect.any(Function));
57+
expect(mock.waitUntilActionReady).toHaveBeenCalledTimes(1);
58+
expect(mock.waitUntilActionReady).toHaveBeenNthCalledWith(1, 1100);
59+
expect(mock.debug).toHaveBeenCalledTimes(1);
60+
expect(mock.debug).toHaveBeenNthCalledWith(1, '%s for %s', 'selector', 'label');
61+
expect(angular.element).toHaveBeenCalledTimes(1);
62+
expect(angular.element).toHaveBeenNthCalledWith(1, mock.element);
63+
expect(this.context.trigger).toHaveBeenCalledTimes(1);
64+
expect(this.context.trigger).toHaveBeenNthCalledWith(1, 'click');
65+
});
66+
67+
it('should use default value and return truthy', async () => {
68+
this.action = '';
69+
this.context.length = 1;
70+
const actual = await Click.ifExists.call(mock, 'selector');
71+
expect(actual).toBeTruthy();
72+
expect(mock.$eval).toHaveBeenCalledTimes(1);
73+
expect(mock.$eval).toHaveBeenNthCalledWith(1, 'selector', expect.any(Function));
74+
expect(mock.waitUntilActionReady).toHaveBeenCalledTimes(1);
75+
expect(mock.waitUntilActionReady).toHaveBeenNthCalledWith(1, 25000);
76+
expect(mock.debug).toHaveBeenCalledTimes(1);
77+
expect(mock.debug).toHaveBeenNthCalledWith(1, '%s for %s', 'selector', 'click');
78+
expect(angular.element).toHaveBeenCalledTimes(1);
79+
expect(angular.element).toHaveBeenNthCalledWith(1, mock.element);
80+
expect(this.context.trigger).toHaveBeenCalledTimes(1);
81+
expect(this.context.trigger).toHaveBeenNthCalledWith(1, 'click');
82+
});
83+
84+
it('should return falsy', async () => {
85+
this.action = '';
86+
this.context.length = 0;
87+
const actual = await Click.ifExists.call(mock, 'selector', 'label', 1100);
88+
expect(actual).toBeFalsy();
89+
expect(mock.$eval).toHaveBeenCalledTimes(1);
90+
expect(mock.$eval).toHaveBeenNthCalledWith(1, 'selector', expect.any(Function));
91+
expect(mock.waitUntilActionReady).toHaveBeenCalledTimes(1);
92+
expect(mock.waitUntilActionReady).toHaveBeenNthCalledWith(1, 1100);
93+
expect(mock.debug).toHaveBeenCalledTimes(1);
94+
expect(mock.debug).toHaveBeenNthCalledWith(1, '%s for %s not found', 'selector', 'label');
95+
expect(angular.element).toHaveBeenCalledTimes(1);
96+
expect(angular.element).toHaveBeenNthCalledWith(1, mock.element);
97+
expect(this.context.trigger).toHaveBeenCalledTimes(1);
98+
expect(this.context.trigger).toHaveBeenNthCalledWith(1, 'click');
99+
});
100+
101+
it('should log error', async () => {
102+
this.action = 'error';
103+
this.context.length = 1;
104+
const actual = await Click.ifExists.call(mock, 'selector', 'label', 1100);
105+
expect(actual).toBeFalsy();
106+
expect(mock.$eval).toHaveBeenCalledTimes(1);
107+
expect(mock.$eval).toHaveBeenNthCalledWith(1, 'selector', expect.any(Function));
108+
expect(mock.waitUntilActionReady).toHaveBeenCalledTimes(1);
109+
expect(mock.waitUntilActionReady).toHaveBeenNthCalledWith(1, 1100);
110+
expect(mock.debug).toHaveBeenCalledTimes(2);
111+
expect(mock.debug).toHaveBeenNthCalledWith(1, '%s for %s error: %s', 'selector', 'label', expect.any(Error));
112+
expect(mock.debug).toHaveBeenNthCalledWith(2, '%s for %s not found', 'selector', 'label');
113+
expect(angular.element).not.toHaveBeenCalled();
114+
expect(this.context.trigger).not.toHaveBeenCalled();
115+
});
116+
});
117+
});

0 commit comments

Comments
 (0)