UNPKG

5.68 kBJavaScriptView Raw
1$(function(){
2 var $b = $('#builder');
3
4 QUnit.module('plugins-gui', {
5 afterEach: function() {
6 $b.queryBuilder('destroy');
7 }
8 });
9
10 /**
11 * Test bt-checkbox
12 */
13 QUnit.test('bt-checkbox', function(assert) {
14 $b.queryBuilder({
15 plugins: ['bt-checkbox'],
16 filters: [{
17 id: 'no-color',
18 type: 'integer',
19 input: 'checkbox',
20 values: {
21 10: 'foo',
22 20: 'bar'
23 }
24 }, {
25 id: 'one-color',
26 type: 'integer',
27 input: 'checkbox',
28 values: {
29 1: 'one',
30 2: 'two',
31 3: 'three'
32 },
33 color: 'primary'
34 }, {
35 id: 'multi-color',
36 type: 'integer',
37 input: 'radio',
38 values: {
39 0: 'no',
40 1: 'yes',
41 2: 'perhaps'
42 },
43 colors: {
44 0: 'danger',
45 1: 'success'
46 }
47 }],
48 rules: {
49 condition: 'AND',
50 rules: [{
51 id: 'no-color',
52 value: 10
53 }, {
54 id: 'one-color',
55 value: [1,2,3]
56 }, {
57 id: 'multi-color',
58 value: 2
59 }]
60 }
61 });
62
63 assert.ok(
64 $('#builder_rule_0 .checkbox.checkbox-default').length == 2,
65 'Should have 2 checkboxes with default color'
66 );
67
68 assert.ok(
69 $('#builder_rule_1 .checkbox.checkbox-primary').length == 3,
70 'Should have 3 checkboxes with primary color'
71 );
72
73 assert.ok(
74 $('#builder_rule_2 .radio.radio-danger').length == 1 &&
75 $('#builder_rule_2 .radio.radio-success').length == 1 &&
76 $('#builder_rule_2 .radio.radio-default').length == 1,
77 'Should have 3 radios with danger, success and default colors'
78 );
79 });
80
81 /**
82 * Test bt-selectpicker
83 */
84 QUnit.test('bt-selectpicker', function(assert) {
85 $b.queryBuilder({
86 plugins: ['bt-selectpicker'],
87 filters: basic_filters,
88 rules: basic_rules
89 });
90
91 assert.ok(
92 $b.find('.bootstrap-select').length == 8,
93 'Should have initialized Bootstrap Select on all filters and operators selectors'
94 );
95 });
96
97 /**
98 * Test bt-tooltip-errors
99 */
100 QUnit.test('bt-tooltip-errors', function(assert) {
101 $b.queryBuilder({
102 plugins: ['bt-tooltip-errors'],
103 filters: basic_filters,
104 rules: {
105 condition: 'AND',
106 rules: [{
107 id: 'id',
108 operator: 'equal',
109 value: ''
110 }]
111 }
112 });
113
114 $b.queryBuilder('validate');
115
116 assert.equal(
117 $('#builder_group_0 .error-container').eq(0).data('toggle'),
118 'tooltip',
119 'Should have added data-toggle="tooltip" in the template'
120 );
121
122 assert.equal(
123 $('#builder_rule_0 .error-container').data('originalTitle'),
124 'Empty value',
125 'Error title should be "Empty value"'
126 );
127 });
128
129 /**
130 * Test filter-description
131 */
132 QUnit.test('filter-description', function(assert) {
133 var filters = [{
134 id: 'name',
135 type: 'string',
136 description: '<b>Lorem Ipsum</b> sit amet.'
137 }, {
138 id: 'age',
139 type: 'integer',
140 description: function(rule) {
141 return 'Description of operator ' + rule.operator.type;
142 }
143 }];
144
145 var rules = {
146 condition: 'AND',
147 rules: [{
148 id: 'name',
149 value: 'Mistic'
150 }, {
151 id: 'age',
152 value: 25
153 }]
154 };
155
156 $b.queryBuilder({
157 plugins: {
158 'filter-description': { mode: 'inline' }
159 },
160 filters: filters,
161 rules: rules
162 });
163
164 assert.match(
165 $('#builder_rule_0 p.filter-description').html(),
166 new RegExp('<b>Lorem Ipsum</b> sit amet.'),
167 'Paragraph should contain filter description'
168 );
169
170 assert.match(
171 $('#builder_rule_1 p.filter-description').html(),
172 new RegExp('Description of operator equal'),
173 'Paragraph should contain filter description after function execution'
174 );
175
176 $b.queryBuilder('destroy');
177
178 $b.queryBuilder({
179 plugins: {
180 'filter-description': { mode: 'popover' }
181 },
182 filters: filters,
183 rules: rules
184 });
185
186 assert.ok(
187 $('#builder_rule_0 button.filter-description').data('toggle') == 'popover',
188 'Rule should contain a new button enabled with Popover'
189 );
190
191 $b.queryBuilder('destroy');
192
193 $b.queryBuilder({
194 plugins: {
195 'filter-description': { mode: 'bootbox' }
196 },
197 filters: filters,
198 rules: rules
199 });
200
201 assert.ok(
202 $('#builder_rule_0 button.filter-description').data('toggle') == 'bootbox',
203 'Rule should contain a new button enabled with Bootbox'
204 );
205 });
206});