Skip to content

Commit 2f79cb2

Browse files
committed
Adds Type module tests.
1 parent 29c19c0 commit 2f79cb2

1 file changed

Lines changed: 149 additions & 0 deletions

File tree

__tests__/Type.test.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*!
2+
* Type.test.js - tests for Type 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 Type = require('../lib/Type.js');
20+
21+
const mock = {
22+
$eval: jest.fn((selector, callback, value) => new Promise(async (resolve, reject) => {
23+
if (this.action === 'error') {
24+
reject(Error('error'));
25+
} else {
26+
resolve(callback(mock.element, value));
27+
}
28+
})),
29+
debug: jest.spyOn(Type.__test__.logger, 'debug'),
30+
element: {},
31+
};
32+
33+
describe('Type module test', () => {
34+
describe('ifExists', () => {
35+
beforeAll(() => {
36+
this.action = '';
37+
this.context = [];
38+
this.context.trigger = jest.fn(() => this.context);
39+
this.context.val = 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+
mock.element.type = 'text';
54+
const actual = await Type.ifExists.call(mock, 'selector', 'value', 'label');
55+
expect(actual).toBeTruthy();
56+
expect(mock.$eval).toHaveBeenCalledTimes(2);
57+
expect(mock.$eval).toHaveBeenNthCalledWith(1, 'selector', expect.any(Function), 'value');
58+
expect(mock.$eval).toHaveBeenNthCalledWith(2, 'selector', expect.any(Function));
59+
expect(mock.debug).toHaveBeenCalledTimes(1);
60+
expect(mock.debug).toHaveBeenNthCalledWith(1, '%s on %s for %s', 'value', 'selector', 'label');
61+
expect(angular.element).toHaveBeenCalledTimes(1);
62+
expect(angular.element).toHaveBeenNthCalledWith(1, mock.element);
63+
expect(this.context.val).toHaveBeenCalledTimes(1);
64+
expect(this.context.val).toHaveBeenNthCalledWith(1, 'value');
65+
expect(this.context.trigger).toHaveBeenCalledTimes(3);
66+
expect(this.context.trigger).toHaveBeenNthCalledWith(1, 'input');
67+
expect(this.context.trigger).toHaveBeenNthCalledWith(2, 'change');
68+
expect(this.context.trigger).toHaveBeenNthCalledWith(3, 'focusout');
69+
});
70+
71+
it('should masked password value and return truthy', async () => {
72+
this.action = '';
73+
this.context.length = 1;
74+
mock.element.type = 'password';
75+
const actual = await Type.ifExists.call(mock, 'selector', 'value', 'label');
76+
expect(actual).toBeTruthy();
77+
expect(mock.$eval).toHaveBeenCalledTimes(2);
78+
expect(mock.$eval).toHaveBeenNthCalledWith(1, 'selector', expect.any(Function), 'value');
79+
expect(mock.$eval).toHaveBeenNthCalledWith(2, 'selector', expect.any(Function));
80+
expect(mock.debug).toHaveBeenCalledTimes(1);
81+
expect(mock.debug).toHaveBeenNthCalledWith(1, '%s on %s for %s', '*****', 'selector', 'label');
82+
expect(angular.element).toHaveBeenCalledTimes(1);
83+
expect(angular.element).toHaveBeenNthCalledWith(1, mock.element);
84+
expect(this.context.val).toHaveBeenCalledTimes(1);
85+
expect(this.context.val).toHaveBeenNthCalledWith(1, 'value');
86+
expect(this.context.trigger).toHaveBeenCalledTimes(3);
87+
expect(this.context.trigger).toHaveBeenNthCalledWith(1, 'input');
88+
expect(this.context.trigger).toHaveBeenNthCalledWith(2, 'change');
89+
expect(this.context.trigger).toHaveBeenNthCalledWith(3, 'focusout');
90+
});
91+
92+
it('should use default value and return truthy', async () => {
93+
this.action = '';
94+
this.context.length = 1;
95+
mock.element.type = 'text';
96+
const actual = await Type.ifExists.call(mock, 'selector');
97+
expect(actual).toBeTruthy();
98+
expect(mock.$eval).toHaveBeenCalledTimes(2);
99+
expect(mock.$eval).toHaveBeenNthCalledWith(1, 'selector', expect.any(Function), '');
100+
expect(mock.$eval).toHaveBeenNthCalledWith(2, 'selector', expect.any(Function));
101+
expect(mock.debug).toHaveBeenCalledTimes(1);
102+
expect(mock.debug).toHaveBeenNthCalledWith(1, '%s on %s for %s', '', 'selector', 'type');
103+
expect(angular.element).toHaveBeenCalledTimes(1);
104+
expect(angular.element).toHaveBeenNthCalledWith(1, mock.element);
105+
expect(this.context.val).toHaveBeenCalledTimes(1);
106+
expect(this.context.val).toHaveBeenNthCalledWith(1, '');
107+
expect(this.context.trigger).toHaveBeenCalledTimes(3);
108+
expect(this.context.trigger).toHaveBeenNthCalledWith(1, 'input');
109+
expect(this.context.trigger).toHaveBeenNthCalledWith(2, 'change');
110+
expect(this.context.trigger).toHaveBeenNthCalledWith(3, 'focusout');
111+
});
112+
113+
it('should return falsy', async () => {
114+
this.action = '';
115+
this.context.length = 0;
116+
mock.element.type = 'text';
117+
const actual = await Type.ifExists.call(mock, 'selector', 'value', 'label');
118+
expect(actual).toBeFalsy();
119+
expect(mock.$eval).toHaveBeenCalledTimes(1);
120+
expect(mock.$eval).toHaveBeenNthCalledWith(1, 'selector', expect.any(Function), 'value');
121+
expect(mock.debug).toHaveBeenCalledTimes(1);
122+
expect(mock.debug).toHaveBeenNthCalledWith(1, '%s for %s not found', 'selector', 'label');
123+
expect(angular.element).toHaveBeenCalledTimes(1);
124+
expect(angular.element).toHaveBeenNthCalledWith(1, mock.element);
125+
expect(this.context.val).toHaveBeenCalledTimes(1);
126+
expect(this.context.val).toHaveBeenNthCalledWith(1, 'value');
127+
expect(this.context.trigger).toHaveBeenCalledTimes(3);
128+
expect(this.context.trigger).toHaveBeenNthCalledWith(1, 'input');
129+
expect(this.context.trigger).toHaveBeenNthCalledWith(2, 'change');
130+
expect(this.context.trigger).toHaveBeenNthCalledWith(3, 'focusout');
131+
});
132+
133+
it('should log error', async () => {
134+
this.action = 'error';
135+
this.context.length = 1;
136+
mock.element.type = 'text';
137+
const actual = await Type.ifExists.call(mock, 'selector', 'value', 'label');
138+
expect(actual).toBeFalsy();
139+
expect(mock.$eval).toHaveBeenCalledTimes(1);
140+
expect(mock.$eval).toHaveBeenNthCalledWith(1, 'selector', expect.any(Function), 'value');
141+
expect(mock.debug).toHaveBeenCalledTimes(2);
142+
expect(mock.debug).toHaveBeenNthCalledWith(1, 'el.type %s for %s error: %s', 'selector', 'label', expect.any(Error));
143+
expect(mock.debug).toHaveBeenNthCalledWith(2, '%s for %s not found', 'selector', 'label');
144+
expect(angular.element).not.toHaveBeenCalled();
145+
expect(this.context.val).not.toHaveBeenCalled();
146+
expect(this.context.trigger).not.toHaveBeenCalled();
147+
});
148+
});
149+
});

0 commit comments

Comments
 (0)