123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- module('Data adapters - Maximum selection length');
- var MaximumSelectionLength = require('select2/data/maximumSelectionLength');
- var $ = require('jquery');
- var Options = require('select2/options');
- var Utils = require('select2/utils');
- function MaximumSelectionStub () {
- this.called = false;
- this.currentData = [];
- }
- MaximumSelectionStub.prototype.current = function (callback) {
- callback(this.currentData);
- };
- MaximumSelectionStub.prototype.val = function (val) {
- this.currentData.push(val);
- };
- MaximumSelectionStub.prototype.query = function (params, callback) {
- this.called = true;
- };
- var MaximumSelectionData = Utils.Decorate(
- MaximumSelectionStub,
- MaximumSelectionLength
- );
- test('0 never displays the notice', function (assert) {
- var zeroOptions = new Options({
- maximumSelectionLength: 0
- });
- var data = new MaximumSelectionData(null, zeroOptions);
- data.trigger = function () {
- assert.ok(false, 'No events should be triggered');
- };
- data.query({
- term: ''
- });
- assert.ok(data.called);
- data = new MaximumSelectionData(null, zeroOptions);
- data.trigger = function () {
- assert.ok(false, 'No events should be triggered');
- };
- data.val('1');
- data.query({
- term: ''
- });
- assert.ok(data.called);
- data = new MaximumSelectionData(null, zeroOptions);
- data.trigger = function () {
- assert.ok(false, 'No events should be triggered');
- };
- data.val('1');
- data.val('2');
- data.query({
- term: ''
- });
- assert.ok(data.called);
- });
- test('< 0 never displays the notice', function (assert) {
- var negativeOptions = new Options({
- maximumSelectionLength: -1
- });
- var data = new MaximumSelectionData(null, negativeOptions);
- data.trigger = function () {
- assert.ok(false, 'No events should be triggered');
- };
- data.query({
- term: ''
- });
- assert.ok(data.called);
- data = new MaximumSelectionData(null, negativeOptions);
- data.trigger = function () {
- assert.ok(false, 'No events should be triggered');
- };
- data.val('1');
- data.query({
- term: ''
- });
- assert.ok(data.called);
- data = new MaximumSelectionData(null, negativeOptions);
- data.trigger = function () {
- assert.ok(false, 'No events should be triggered');
- };
- data.val('1');
- data.val('2');
- data.query({
- term: ''
- });
- assert.ok(data.called);
- });
- test('triggers when >= 1 selection' , function (assert) {
- var maxOfOneOptions = new Options({
- maximumSelectionLength: 1
- });
- var data = new MaximumSelectionData(null, maxOfOneOptions);
- data.trigger = function () {
- assert.ok(false, 'No events should be triggered');
- };
- data.query({
- term: ''
- });
- assert.ok(data.called);
- data = new MaximumSelectionData(null, maxOfOneOptions);
- data.trigger = function () {
- assert.ok(true, 'The event should be triggered.');
- };
- data.val('1');
- data.query({
- term: ''
- });
- assert.ok(!data.called);
- });
- test('triggers when >= 2 selections' , function (assert) {
- var maxOfTwoOptions = new Options({
- maximumSelectionLength: 2
- });
- var data = new MaximumSelectionData(null, maxOfTwoOptions);
- data.trigger = function () {
- assert.ok(false, 'No events should be triggered');
- };
- data.query({
- term: ''
- });
- assert.ok(data.called);
- data = new MaximumSelectionData(null, maxOfTwoOptions);
- data.trigger = function () {
- assert.ok(false, 'No events should be triggered');
- };
- data.val('1');
- data.query({
- term: ''
- });
- assert.ok(data.called);
- data = new MaximumSelectionData(null, maxOfTwoOptions);
- data.trigger = function () {
- assert.ok(true, 'The event should be triggered.');
- };
- data.val('1');
- data.val('2');
- data.query({
- term: ''
- });
- assert.ok(!data.called);
- });
|