|
1 | 1 | module('Data adapters - Maximum selection length'); |
2 | 2 |
|
| 3 | +var SelectData = require('select2/data/select'); |
3 | 4 | var MaximumSelectionLength = require('select2/data/maximumSelectionLength'); |
4 | 5 |
|
5 | 6 | var $ = require('jquery'); |
6 | 7 | var Options = require('select2/options'); |
7 | 8 | var Utils = require('select2/utils'); |
8 | 9 |
|
9 | | -function MaximumSelectionStub () { |
10 | | - this.called = false; |
11 | | - this.currentData = []; |
12 | | -} |
| 10 | +var MaximumSelectionData = Utils.Decorate(SelectData, MaximumSelectionLength); |
13 | 11 |
|
14 | | -MaximumSelectionStub.prototype.current = function (callback) { |
15 | | - callback(this.currentData); |
16 | | -}; |
17 | | - |
18 | | -MaximumSelectionStub.prototype.val = function (val) { |
19 | | - this.currentData.push(val); |
20 | | -}; |
21 | | - |
22 | | -MaximumSelectionStub.prototype.query = function (params, callback) { |
23 | | - this.called = true; |
24 | | -}; |
| 12 | +test('0 never displays the notice', function (assert) { |
| 13 | + assert.expect(3); |
25 | 14 |
|
26 | | -var MaximumSelectionData = Utils.Decorate( |
27 | | - MaximumSelectionStub, |
28 | | - MaximumSelectionLength |
29 | | -); |
| 15 | + var $select = $('#qunit-fixture .multiple'); |
30 | 16 |
|
31 | | -test('0 never displays the notice', function (assert) { |
32 | 17 | var zeroOptions = new Options({ |
33 | 18 | maximumSelectionLength: 0 |
34 | 19 | }); |
35 | 20 |
|
36 | | - var data = new MaximumSelectionData(null, zeroOptions); |
| 21 | + var container = new MockContainer(); |
| 22 | + var data = new MaximumSelectionData($select, zeroOptions); |
37 | 23 |
|
38 | | - data.trigger = function () { |
39 | | - assert.ok(false, 'No events should be triggered'); |
40 | | - }; |
| 24 | + data.bind(container, null); |
| 25 | + |
| 26 | + data.on('results:message', function () { |
| 27 | + assert.ok(false, 'The message should not be displayed'); |
| 28 | + }); |
41 | 29 |
|
42 | 30 | data.query({ |
43 | 31 | term: '' |
| 32 | + }, function () { |
| 33 | + assert.ok(true, 'The results should be queried'); |
44 | 34 | }); |
45 | 35 |
|
46 | | - assert.ok(data.called); |
47 | | - |
48 | | - data = new MaximumSelectionData(null, zeroOptions); |
49 | | - |
50 | | - data.trigger = function () { |
51 | | - assert.ok(false, 'No events should be triggered'); |
52 | | - }; |
53 | | - |
54 | | - data.val('1'); |
| 36 | + $select.val(['One']); |
55 | 37 |
|
56 | 38 | data.query({ |
57 | 39 | term: '' |
| 40 | + }, function () { |
| 41 | + assert.ok(true, 'The results should be queried'); |
58 | 42 | }); |
59 | 43 |
|
60 | | - assert.ok(data.called); |
61 | | - |
62 | | - data = new MaximumSelectionData(null, zeroOptions); |
63 | | - |
64 | | - data.trigger = function () { |
65 | | - assert.ok(false, 'No events should be triggered'); |
66 | | - }; |
67 | | - |
68 | | - data.val('1'); |
69 | | - data.val('2'); |
| 44 | + $select.val(['One', 'Two']); |
70 | 45 |
|
71 | 46 | data.query({ |
72 | 47 | term: '' |
| 48 | + }, function () { |
| 49 | + assert.ok(true, 'The results should be queried'); |
73 | 50 | }); |
74 | | - |
75 | | - assert.ok(data.called); |
76 | 51 | }); |
77 | 52 |
|
78 | 53 | test('< 0 never displays the notice', function (assert) { |
| 54 | + assert.expect(3); |
| 55 | + |
| 56 | + var $select = $('#qunit-fixture .multiple'); |
| 57 | + |
79 | 58 | var negativeOptions = new Options({ |
80 | 59 | maximumSelectionLength: -1 |
81 | 60 | }); |
82 | 61 |
|
83 | | - var data = new MaximumSelectionData(null, negativeOptions); |
| 62 | + var container = new MockContainer(); |
| 63 | + var data = new MaximumSelectionData($select, negativeOptions); |
| 64 | + |
| 65 | + data.bind(container, null); |
84 | 66 |
|
85 | | - data.trigger = function () { |
86 | | - assert.ok(false, 'No events should be triggered'); |
87 | | - }; |
| 67 | + data.on('results:message', function () { |
| 68 | + assert.ok(false, 'The message should not be displayed'); |
| 69 | + }); |
88 | 70 |
|
89 | 71 | data.query({ |
90 | 72 | term: '' |
| 73 | + }, function () { |
| 74 | + assert.ok(true, 'The results should be queried'); |
91 | 75 | }); |
92 | 76 |
|
93 | | - assert.ok(data.called); |
94 | | - |
95 | | - data = new MaximumSelectionData(null, negativeOptions); |
96 | | - |
97 | | - data.trigger = function () { |
98 | | - assert.ok(false, 'No events should be triggered'); |
99 | | - }; |
100 | | - |
101 | | - data.val('1'); |
| 77 | + $select.val(['One']); |
102 | 78 |
|
103 | 79 | data.query({ |
104 | 80 | term: '' |
| 81 | + }, function () { |
| 82 | + assert.ok(true, 'The results should be queried'); |
105 | 83 | }); |
106 | 84 |
|
107 | | - assert.ok(data.called); |
108 | | - |
109 | | - data = new MaximumSelectionData(null, negativeOptions); |
110 | | - |
111 | | - data.trigger = function () { |
112 | | - assert.ok(false, 'No events should be triggered'); |
113 | | - }; |
114 | | - |
115 | | - data.val('1'); |
116 | | - data.val('2'); |
| 85 | + $select.val(['One', 'Two']); |
117 | 86 |
|
118 | 87 | data.query({ |
119 | 88 | term: '' |
| 89 | + }, function () { |
| 90 | + assert.ok(true, 'The results should be queried'); |
120 | 91 | }); |
121 | | - |
122 | | - assert.ok(data.called); |
123 | 92 | }); |
124 | 93 |
|
125 | 94 | test('triggers when >= 1 selection' , function (assert) { |
126 | | - var maxOfOneOptions = new Options({ |
127 | | - maximumSelectionLength: 1 |
128 | | - }); |
129 | | - var data = new MaximumSelectionData(null, maxOfOneOptions); |
| 95 | + assert.expect(2); |
130 | 96 |
|
131 | | - data.trigger = function () { |
132 | | - assert.ok(false, 'No events should be triggered'); |
133 | | - }; |
| 97 | + var $select = $('#qunit-fixture .multiple'); |
134 | 98 |
|
135 | | - data.query({ |
136 | | - term: '' |
| 99 | + var maxOfOneOptions = new Options({ |
| 100 | + maximumSelectionLength: 1 |
137 | 101 | }); |
138 | 102 |
|
139 | | - assert.ok(data.called); |
| 103 | + var container = new MockContainer(); |
| 104 | + var data = new MaximumSelectionData($select, maxOfOneOptions); |
140 | 105 |
|
141 | | - data = new MaximumSelectionData(null, maxOfOneOptions); |
| 106 | + data.bind(container, null); |
142 | 107 |
|
143 | | - data.trigger = function () { |
144 | | - assert.ok(true, 'The event should be triggered.'); |
145 | | - }; |
| 108 | + data.on('results:message', function () { |
| 109 | + assert.ok(true, 'The message should be displayed'); |
| 110 | + }); |
146 | 111 |
|
147 | | - data.val('1'); |
| 112 | + $select.val(['One']); |
148 | 113 |
|
149 | 114 | data.query({ |
150 | 115 | term: '' |
| 116 | + }, function () { |
| 117 | + assert.ok(false, 'The results should not be queried'); |
151 | 118 | }); |
152 | 119 |
|
153 | | - assert.ok(!data.called); |
154 | | - |
155 | | -}); |
156 | | - |
157 | | -test('triggers when >= 2 selections' , function (assert) { |
158 | | - var maxOfTwoOptions = new Options({ |
159 | | - maximumSelectionLength: 2 |
160 | | - }); |
161 | | - var data = new MaximumSelectionData(null, maxOfTwoOptions); |
162 | | - |
163 | | - data.trigger = function () { |
164 | | - assert.ok(false, 'No events should be triggered'); |
165 | | - }; |
| 120 | + $select.val(['One', 'Two']); |
166 | 121 |
|
167 | 122 | data.query({ |
168 | 123 | term: '' |
| 124 | + }, function () { |
| 125 | + assert.ok(false, 'The results should not be queried'); |
169 | 126 | }); |
| 127 | +}); |
170 | 128 |
|
171 | | - assert.ok(data.called); |
172 | | - |
173 | | - data = new MaximumSelectionData(null, maxOfTwoOptions); |
174 | | - |
175 | | - data.trigger = function () { |
176 | | - assert.ok(false, 'No events should be triggered'); |
177 | | - }; |
| 129 | +test('triggers after selection' , function (assert) { |
| 130 | + assert.expect(1); |
178 | 131 |
|
179 | | - data.val('1'); |
| 132 | + var $select = $('#qunit-fixture .multiple'); |
180 | 133 |
|
181 | | - data.query({ |
182 | | - term: '' |
| 134 | + var maxOfOneOptions = new Options({ |
| 135 | + maximumSelectionLength: 1 |
183 | 136 | }); |
184 | 137 |
|
185 | | - assert.ok(data.called); |
186 | | - |
187 | | - data = new MaximumSelectionData(null, maxOfTwoOptions); |
188 | | - |
189 | | - data.trigger = function () { |
190 | | - assert.ok(true, 'The event should be triggered.'); |
191 | | - }; |
| 138 | + var container = new MockContainer(); |
| 139 | + var data = new MaximumSelectionData($select, maxOfOneOptions); |
192 | 140 |
|
193 | | - data.val('1'); |
194 | | - data.val('2'); |
| 141 | + data.bind(container, null); |
195 | 142 |
|
196 | | - data.query({ |
197 | | - term: '' |
| 143 | + data.on('results:message', function () { |
| 144 | + assert.ok(true, 'The message should be displayed'); |
198 | 145 | }); |
199 | 146 |
|
200 | | - assert.ok(!data.called); |
| 147 | + $select.val(['One']); |
201 | 148 |
|
| 149 | + container.trigger('select', { |
| 150 | + data: {} |
| 151 | + }); |
202 | 152 | }); |
0 commit comments