summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/shared/gr-cursor-manager/gr-cursor-manager_test.html
blob: adbe6181d39b46d31a7dfc4126692d965f9dedc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<!DOCTYPE html>
<!--
@license
Copyright (C) 2016 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-cursor-manager</title>

<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<link rel="import" href="gr-cursor-manager.html">

<script>void(0);</script>

<test-fixture id="basic">
  <template>
    <gr-cursor-manager cursor-target-class="targeted"></gr-cursor-manager>
    <ul>
      <li>A</li>
      <li>B</li>
      <li>C</li>
      <li>D</li>
    </ul>
  </template>
</test-fixture>

<script>
  suite('gr-cursor-manager tests', () => {
    let sandbox;
    let element;
    let list;

    setup(() => {
      sandbox = sinon.sandbox.create();
      const fixtureElements = fixture('basic');
      element = fixtureElements[0];
      list = fixtureElements[1];
    });

    teardown(() => {
      sandbox.restore();
    });

    test('core cursor functionality', () => {
      // The element is initialized into the proper state.
      assert.isArray(element.stops);
      assert.equal(element.stops.length, 0);
      assert.equal(element.index, -1);
      assert.isNotOk(element.target);

      // Initialize the cursor with its stops.
      element.stops = list.querySelectorAll('li');

      // It should have the stops but it should not be targeting any of them.
      assert.isNotNull(element.stops);
      assert.equal(element.stops.length, 4);
      assert.equal(element.index, -1);
      assert.isNotOk(element.target);

      // Select the third stop.
      element.setCursor(list.children[2]);

      // It should update its internal state and update the element's class.
      assert.equal(element.index, 2);
      assert.equal(element.target, list.children[2]);
      assert.isTrue(list.children[2].classList.contains('targeted'));
      assert.isFalse(element.isAtStart());
      assert.isFalse(element.isAtEnd());

      // Progress the cursor.
      element.next();

      // Confirm that the next stop is selected and that the previous stop is
      // unselected.
      assert.equal(element.index, 3);
      assert.equal(element.target, list.children[3]);
      assert.isTrue(element.isAtEnd());
      assert.isFalse(list.children[2].classList.contains('targeted'));
      assert.isTrue(list.children[3].classList.contains('targeted'));

      // Progress the cursor.
      element.next();

      // We should still be at the end.
      assert.equal(element.index, 3);
      assert.equal(element.target, list.children[3]);
      assert.isTrue(element.isAtEnd());

      // Wind the cursor all the way back to the first stop.
      element.previous();
      element.previous();
      element.previous();

      // The element state should reflect the end of the list.
      assert.equal(element.index, 0);
      assert.equal(element.target, list.children[0]);
      assert.isTrue(element.isAtStart());
      assert.isTrue(list.children[0].classList.contains('targeted'));

      const newLi = document.createElement('li');
      newLi.textContent = 'Z';
      list.insertBefore(newLi, list.children[0]);
      element.stops = list.querySelectorAll('li');

      assert.equal(element.index, 1);

      // De-select all targets.
      element.unsetCursor();

      // There should now be no cursor target.
      assert.isFalse(list.children[1].classList.contains('targeted'));
      assert.isNotOk(element.target);
      assert.equal(element.index, -1);
    });


    test('_moveCursor', () => {
      // Initialize the cursor with its stops.
      element.stops = list.querySelectorAll('li');
      // Select the first stop.
      element.setCursor(list.children[0]);
      const getTargetHeight = sinon.stub();

      // Move the cursor without an optional get target height function.
      element._moveCursor(1);
      assert.isFalse(getTargetHeight.called);

      // Move the cursor with an optional get target height function.
      element._moveCursor(1, null, getTargetHeight);
      assert.isTrue(getTargetHeight.called);
    });

    test('_moveCursor from -1 does not check height', () => {
      element.stops = list.querySelectorAll('li');
      const getTargetHeight = sinon.stub();
      element._moveCursor(1, () => false, getTargetHeight);
      assert.isFalse(getTargetHeight.called);
    });

    test('opt_noScroll', () => {
      sandbox.stub(element, '_targetIsVisible', () => false);
      const scrollStub = sandbox.stub(window, 'scrollTo');
      element.stops = list.querySelectorAll('li');
      element.scrollBehavior = 'keep-visible';

      element.setCursorAtIndex(1, true);
      assert.isFalse(scrollStub.called);

      element.setCursorAtIndex(2);
      assert.isTrue(scrollStub.called);
    });

    test('_getNextindex', () => {
      const isLetterB = function(row) {
        return row.textContent === 'B';
      };
      element.stops = list.querySelectorAll('li');
      // Start cursor at the first stop.
      element.setCursor(list.children[0]);

      // Move forward to meet the next condition.
      assert.equal(element._getNextindex(1, isLetterB), 1);
      element.index = 1;

      // Nothing else meets the condition, should be at last stop.
      assert.equal(element._getNextindex(1, isLetterB), 3);
      element.index = 3;

      // Should stay at last stop if try to proceed.
      assert.equal(element._getNextindex(1, isLetterB), 3);

      // Go back to the previous condition met. Should be back at.
      // stop 1.
      assert.equal(element._getNextindex(-1, isLetterB), 1);
      element.index = 1;

      // Go back. No more meet the condition. Should be at stop 0.
      assert.equal(element._getNextindex(-1, isLetterB), 0);
    });

    test('focusOnMove prop', () => {
      const listEls = list.querySelectorAll('li');
      for (let i = 0; i < listEls.length; i++) {
        sandbox.spy(listEls[i], 'focus');
      }
      element.stops = listEls;
      element.setCursor(list.children[0]);

      element.focusOnMove = false;
      element.next();
      assert.isFalse(element.target.focus.called);

      element.focusOnMove = true;
      element.next();
      assert.isTrue(element.target.focus.called);
    });

    suite('_scrollToTarget', () => {
      let scrollStub;
      setup(() => {
        element.stops = list.querySelectorAll('li');
        element.scrollBehavior = 'keep-visible';

        // There is a target which has a targetNext
        element.setCursor(list.children[0]);
        element._moveCursor(1);
        scrollStub = sandbox.stub(window, 'scrollTo');
        window.innerHeight = 60;
      });

      test('Called when top and bottom not visible', () => {
        sandbox.stub(element, '_targetIsVisible').returns(false);
        element._scrollToTarget();
        assert.isTrue(scrollStub.called);
      });

      test('Not called when top and bottom visible', () => {
        sandbox.stub(element, '_targetIsVisible').returns(true);
        element._scrollToTarget();
        assert.isFalse(scrollStub.called);
      });

      test('Called when top is visible, bottom is not, scroll is lower', () => {
        const visibleStub = sandbox.stub(element, '_targetIsVisible',
            () => visibleStub.callCount === 2);
        sandbox.stub(element, '_getWindowDims').returns({
          scrollX: 123,
          scrollY: 15,
          innerHeight: 1000,
          pageYOffset: 0,
        });
        sandbox.stub(element, '_calculateScrollToValue').returns(20);
        element._scrollToTarget();
        assert.isTrue(scrollStub.called);
        assert.isTrue(scrollStub.calledWithExactly(123, 20));
        assert.equal(visibleStub.callCount, 2);
      });

      test('Called when top is visible, bottom not, scroll is higher', () => {
        const visibleStub = sandbox.stub(element, '_targetIsVisible',
            () => visibleStub.callCount === 2);
        sandbox.stub(element, '_getWindowDims').returns({
          scrollX: 123,
          scrollY: 25,
          innerHeight: 1000,
          pageYOffset: 0,
        });
        sandbox.stub(element, '_calculateScrollToValue').returns(20);
        element._scrollToTarget();
        assert.isFalse(scrollStub.called);
        assert.equal(visibleStub.callCount, 2);
      });

      test('_calculateScrollToValue', () => {
        sandbox.stub(element, '_getWindowDims').returns({
          scrollX: 123,
          scrollY: 25,
          innerHeight: 300,
          pageYOffset: 0,
        });
        assert.equal(element._calculateScrollToValue(1000, {offsetHeight: 10}),
            905);
      });
    });
  });
</script>