Debian Codegraph @

packages/controls/test/src/lumino/currentselection_test.ts

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3
4 import { expect } from 'chai';
5 import { spy } from 'sinon';
6
7 import { ArrayExt } from '@lumino/algorithm';
8
9 import { Selection } from '../../../lib/lumino/currentselection';
10
11 function getLastMessage(subscriber: {
12 getCall: (arg0: number) => { args: [any, any] };
13 }): any {
14 const [, message] = subscriber.getCall(0).args;
15 return message;
16 }
17
18 describe('Selection with items', function () {
19 let selection: Selection<string>;
20 let subscriber: any; // subscribe to signals from selection
21 let sequence: string[];
22
23 beforeEach(function () {
24 sequence = ['value-0', 'value-1'];
25 selection = new Selection(sequence) as Selection<string>;
26 selection.index = null;
27 subscriber = spy();
28 selection.selectionChanged.connect(subscriber);
29 });
30
31 it('be unselected', function () {
32 expect(selection.index).to.be.null;
33 expect(selection.value).to.be.null;
34 });
35
36 it('set an item', function () {
37 selection.index = 1;
38 expect(selection.index).to.equal(1);
39 expect(selection.value).to.equal('value-1');
40 });
41
42 it('dispatch a signal when setting an item', function () {
43 selection.index = 1;
44 expect(subscriber.calledOnce).to.be.true;
45 const message = getLastMessage(subscriber);
46 expect(message).to.deep.equal({
47 previousIndex: null,
48 previousValue: null,
49 currentIndex: 1,
50 currentValue: 'value-1',
51 });
52 });
53
54 it('set a value', function () {
55 selection.value = 'value-0';
56 expect(selection.value).to.equal('value-0');
57 expect(selection.index).to.equal(0);
58 });
59
60 it('dispatch a signal when setting a value', function () {
61 selection.value = 'value-0';
62 expect(subscriber.calledOnce).to.be.true;
63 const [, message] = subscriber.getCall(0).args;
64 expect(message).to.deep.equal({
65 previousIndex: null,
66 previousValue: null,
67 currentIndex: 0,
68 currentValue: 'value-0',
69 });
70 });
71
72 it('set to null if the index is out of bounds', function () {
73 selection.index = 22;
74 expect(selection.index).to.be.null;
75 expect(selection.value).to.be.null;
76 });
77
78 it('set to null if the value is not present', function () {
79 selection.value = 'not-a-value';
80 expect(selection.index).to.be.null;
81 expect(selection.value).to.be.null;
82 });
83
84 it('adjust after inserting an item', function () {
85 const insertedValue = 'value-before-1';
86 ArrayExt.insert(sequence, 1, insertedValue);
87 // sequence is now [value-0, value-before-1, value-1]
88 selection.adjustSelectionForInsert(1, insertedValue);
89 expect(selection.index).to.equal(1);
90 expect(selection.value).to.equal(insertedValue);
91 });
92
93 it('adjust after removing an item', function () {
94 ArrayExt.removeAt(sequence, 0);
95 selection.adjustSelectionForRemove(0, 'value-0');
96 expect(selection.index).to.be.null;
97 expect(selection.value).to.be.null;
98 });
99
100 it('clear the selection', function () {
101 selection.clearSelection();
102 expect(selection.index).to.be.null;
103 expect(selection.value).to.be.null;
104 });
105 });
106
107 describe('Selection with items with an item selected', function () {
108 let selection: Selection<string>;
109 let subscriber: any; // subscribe to signals from selection
110 let sequence: string[];
111
112 beforeEach(function () {
113 sequence = ['value-0', 'value-1'];
114 selection = new Selection(sequence);
115 selection.index = 1;
116 subscriber = spy();
117 selection.selectionChanged.connect(subscriber);
118 });
119
120 it('set another item', function () {
121 selection.index = 0;
122 expect(selection.index).to.equal(0);
123 expect(selection.value).to.equal('value-0');
124 const message = getLastMessage(subscriber);
125 expect(message).to.deep.equal({
126 previousIndex: 1,
127 previousValue: 'value-1',
128 currentIndex: 0,
129 currentValue: 'value-0',
130 });
131 });
132
133 it('adjust after inserting an item', function () {
134 const insertedValue = 'value-before-1';
135 ArrayExt.insert(sequence, 1, insertedValue);
136 selection.adjustSelectionForInsert(1, insertedValue);
137 expect(selection.index).to.equal(2);
138 expect(selection.value).to.equal('value-1');
139 });
140
141 it('adjust after removing an item', function () {
142 ArrayExt.removeAt(sequence, 0);
143 selection.adjustSelectionForRemove(0, 'value-0');
144 expect(selection.index).to.equal(0);
145 expect(selection.value).to.equal('value-1');
146 });
147
148 it('adjust after removing the selected item', function () {
149 ArrayExt.removeAt(sequence, 1);
150 selection.adjustSelectionForRemove(1, 'value-1');
151 expect(selection.index).to.equal(0);
152 expect(selection.value).to.equal('value-0');
153 const message = getLastMessage(subscriber);
154 expect(message).to.deep.equal({
155 previousIndex: 1,
156 previousValue: 'value-1',
157 currentIndex: 0,
158 currentValue: 'value-0',
159 });
160 });
161 });