summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/settings/gr-menu-editor/gr-menu-editor_test.html
blob: c70ae88db65d1914bc2218043f1e2a4da94fd13f (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
<!DOCTYPE html>
<!--
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-settings-view</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-menu-editor.html">

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

<test-fixture id="basic">
  <template>
    <gr-menu-editor></gr-menu-editor>
  </template>
</test-fixture>

<script>
  suite('gr-settings-view tests', () => {
    let element;
    let menu;

    function assertMenuNamesEqual(element, expected) {
      const names = element.menuItems.map(i => { return i.name; });
      assert.equal(names.length, expected.length);
      for (let i = 0; i < names.length; i++) {
        assert.equal(names[i], expected[i]);
      }
    }

    // Click the up/down button (according to direction) for the index'th row.
    // The index of the first row is 0, corresponding to the array.
    function move(element, index, direction) {
      const selector = 'tr:nth-child(' + (index + 1) + ') .move' +
          direction + 'Button';
      const button =
          element.$$('tbody').querySelector(selector).$$('paper-button');
      MockInteractions.tap(button);
    }

    setup(() => {
      element = fixture('basic');
      menu = [
        {url: '/first/url', name: 'first name', target: '_blank'},
        {url: '/second/url', name: 'second name', target: '_blank'},
        {url: '/third/url', name: 'third name', target: '_blank'},
      ];
      element.set('menuItems', menu);
      Polymer.dom.flush();
    });

    test('renders', () => {
      const rows = element.$$('tbody').querySelectorAll('tr');
      let tds;

      assert.equal(rows.length, menu.length);
      for (let i = 0; i < menu.length; i++) {
        tds = rows[i].querySelectorAll('td');
        assert.equal(tds[0].textContent, menu[i].name);
        assert.equal(tds[1].textContent, menu[i].url);
      }

      assert.isTrue(element._computeAddDisabled(element._newName,
          element._newUrl));
    });

    test('_computeAddDisabled', () => {
      assert.isTrue(element._computeAddDisabled('', ''));
      assert.isTrue(element._computeAddDisabled('name', ''));
      assert.isTrue(element._computeAddDisabled('', 'url'));
      assert.isFalse(element._computeAddDisabled('name', 'url'));
    });

    test('add a new menu item', () => {
      const newName = 'new name';
      const newUrl = 'new url';

      element._newName = newName;
      element._newUrl = newUrl;
      assert.isFalse(element._computeAddDisabled(element._newName,
          element._newUrl));

      const originalMenuLength = element.menuItems.length;

      element._handleAddButton();

      assert.equal(element.menuItems.length, originalMenuLength + 1);
      assert.equal(element.menuItems[element.menuItems.length - 1].name,
          newName);
      assert.equal(element.menuItems[element.menuItems.length - 1].url, newUrl);
    });

    test('move items down', () => {
      assertMenuNamesEqual(element,
          ['first name', 'second name', 'third name']);

      // Move the middle item down
      move(element, 1, 'Down');
      assertMenuNamesEqual(element,
          ['first name', 'third name', 'second name']);

      // Moving the bottom item down is a no-op.
      move(element, 2, 'Down');
      assertMenuNamesEqual(element,
          ['first name', 'third name', 'second name']);
    });

    test('move items up', () => {
      assertMenuNamesEqual(element,
          ['first name', 'second name', 'third name']);

      // Move the last item up twice to be the first.
      move(element, 2, 'Up');
      move(element, 1, 'Up');
      assertMenuNamesEqual(element,
          ['third name', 'first name', 'second name']);

      // Moving the top item up is a no-op.
      move(element, 0, 'Up');
      assertMenuNamesEqual(element,
          ['third name', 'first name', 'second name']);
    });

    test('remove item', () => {
      assertMenuNamesEqual(element,
          ['first name', 'second name', 'third name']);

      // Tap the delete button for the middle item.
      MockInteractions.tap(element.$$('tbody')
          .querySelector('tr:nth-child(2) .remove-button').$$('paper-button'));

      assertMenuNamesEqual(element, ['first name', 'third name']);

      // Delete remaining items.
      for (let i = 0; i < 2; i++) {
        MockInteractions.tap(element.$$('tbody')
            .querySelector('tr:first-child .remove-button').$$('paper-button'));
      }
      assertMenuNamesEqual(element, []);

      // Add item to empty menu.
      element._newName = 'new name';
      element._newUrl = 'new url';
      element._handleAddButton();
      assertMenuNamesEqual(element, ['new name']);
    });
  });
</script>