aboutsummaryrefslogtreecommitdiff
path: root/catapult/third_party/polymer/components/paper-dropdown-menu/test/paper-dropdown-menu-light.html
blob: 183ca86f4d6e97b4805154948826585691386711 (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
<!doctype html>
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
  <meta charset="UTF-8">
  <title>paper-dropdown-menu-light basic tests</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">

  <script src="../../webcomponentsjs/webcomponents-lite.js"></script>
  <script src="../../web-component-tester/browser.js"></script>
  <script src="../../test-fixture/test-fixture-mocha.js"></script>
  <script src="../../iron-test-helpers/mock-interactions.js"></script>

  <link rel="import" href="../../paper-listbox/paper-listbox.html">
  <link rel="import" href="../../paper-item/paper-item.html">
  <link rel="import" href="../../test-fixture/test-fixture.html">
  <link rel="import" href="../paper-dropdown-menu-light.html">
</head>
<body>

  <test-fixture id="TrivialDropdownMenu">
    <template>
      <paper-dropdown-menu-light no-animations>
        <paper-listbox class="dropdown-content">
          <paper-item>Foo</paper-item>
          <paper-item>Bar</paper-item>
        </paper-listbox>
      </paper-dropdown-menu-light>
    </template>
  </test-fixture>

  <test-fixture id="PreselectedDropdownMenu">
    <template>
      <paper-dropdown-menu-light no-animations>
        <paper-listbox class="dropdown-content" selected="1">
          <paper-item>Foo</paper-item>
          <paper-item>Bar</paper-item>
        </paper-listbox>
      </paper-dropdown-menu-light>
    </template>
  </test-fixture>

  <test-fixture id="LabelsDropdownMenu">
    <template>
      <paper-dropdown-menu-light no-animations>
        <paper-listbox class="dropdown-content">
          <paper-item label="Foo label property">Foo textContent</paper-item>
          <paper-item label="Foo label attribute">Foo textContent</paper-item>
          <paper-item>Foo textContent</paper-item>
        </paper-listbox>
      </paper-dropdown-menu-light>
    </template>
  </test-fixture>

  <script>

    function runAfterOpen(menu, callback) {
      menu.$.menuButton.$.dropdown.addEventListener('iron-overlay-opened', function() {
        Polymer.Base.async(callback, 1);
      });
      MockInteractions.tap(menu);
    }

    suite('<paper-dropdown-menu-light>', function() {
      var dropdownMenu;

      setup(function() {
        dropdownMenu = fixture('TrivialDropdownMenu');
        content = Polymer.dom(dropdownMenu).querySelector('.dropdown-content');
      });

      test('opens when tapped', function(done) {
        var contentRect = content.getBoundingClientRect();

        expect(contentRect.width).to.be.equal(0);
        expect(contentRect.height).to.be.equal(0);

        runAfterOpen(dropdownMenu, function() {
          contentRect = content.getBoundingClientRect();

          expect(dropdownMenu.opened).to.be.equal(true);

          expect(contentRect.width).to.be.greaterThan(0);
          expect(contentRect.height).to.be.greaterThan(0);
          done();
        });

        expect(dropdownMenu.opened).to.be.equal(true);
      });

      test('closes when an item is activated', function(done) {
        runAfterOpen(dropdownMenu, function() {
          var firstItem = Polymer.dom(content).querySelector('paper-item');

          MockInteractions.tap(firstItem);

          Polymer.Base.async(function() {
            expect(dropdownMenu.opened).to.be.equal(false);
            done();
          });
        });
      });

      test('sets selected item to the activated item', function(done) {
        runAfterOpen(dropdownMenu, function() {
          var firstItem = Polymer.dom(content).querySelector('paper-item');

          MockInteractions.tap(firstItem);

          Polymer.Base.async(function() {
            expect(dropdownMenu.selectedItem).to.be.equal(firstItem);
            done();
          });
        });
      });

      suite('when a value is preselected', function() {
        setup(function() {
          dropdownMenu = fixture('PreselectedDropdownMenu');
        });

        test('the input area shows the correct selection', function() {
          Polymer.dom.flush();
          var secondItem = Polymer.dom(dropdownMenu).querySelectorAll('paper-item')[1];
          expect(dropdownMenu.selectedItem).to.be.equal(secondItem);
        });
      });

      suite('deselecting', function() {
        var menu;

        setup(function() {
          dropdownMenu = fixture('PreselectedDropdownMenu');
          menu = Polymer.dom(dropdownMenu).querySelector('.dropdown-content');
        });

        test('an `iron-deselect` event clears the current selection', function() {
          Polymer.dom.flush();
          menu.selected = null;
          expect(dropdownMenu.selectedItem).to.be.equal(null);
        });
      });

      suite('validation', function() {
        test('a non required dropdown is valid regardless of its selection', function() {
          var dropdownMenu = fixture('TrivialDropdownMenu');
          menu = Polymer.dom(dropdownMenu).querySelector('.dropdown-content');

          // no selection.
          expect(dropdownMenu.validate()).to.be.true;
          expect(dropdownMenu.invalid).to.be.false;
          expect(dropdownMenu.value).to.not.be.ok;

          // some selection.
          menu.selected = 1;
          expect(dropdownMenu.validate()).to.be.true;
          expect(dropdownMenu.invalid).to.be.false;
          expect(dropdownMenu.value).to.be.equal('Bar');
        });

        test('a required dropdown is invalid without a selection', function() {
          var dropdownMenu = fixture('TrivialDropdownMenu');
          dropdownMenu.required = true;

          // no selection.
          expect(dropdownMenu.validate()).to.be.false;
          expect(dropdownMenu.invalid).to.be.true;
          expect(dropdownMenu.value).to.not.be.ok;
        });

        test('a required dropdown is valid with a selection', function() {
          var dropdownMenu = fixture('PreselectedDropdownMenu');
          Polymer.dom.flush();

          dropdownMenu.required = true;

          expect(dropdownMenu.validate()).to.be.true;
          expect(dropdownMenu.invalid).to.be.false;
          expect(dropdownMenu.value).to.be.equal('Bar');
        });
      });

      suite('selectedItemLabel', function() {
        test('label property', function() {
          var dropdownMenu = fixture('LabelsDropdownMenu');
          var menu = Polymer.dom(dropdownMenu).querySelector('.dropdown-content');
          menu.selected = 0;
          //Fake a label property since paper-item doesn't have one
          dropdownMenu.selectedItem.label = dropdownMenu.selectedItem.getAttribute('label');
          expect(dropdownMenu.selectedItemLabel).to.be.equal('Foo label property');
        });

        test('label attribute', function() {
          var dropdownMenu = fixture('LabelsDropdownMenu');
          var menu = Polymer.dom(dropdownMenu).querySelector('.dropdown-content');
          menu.selected = 1;
          expect(dropdownMenu.selectedItemLabel).to.be.equal('Foo label attribute');
        });

        test('textContent', function() {
          var dropdownMenu = fixture('LabelsDropdownMenu');
          var menu = Polymer.dom(dropdownMenu).querySelector('.dropdown-content');
          menu.selected = 2;
          expect(dropdownMenu.selectedItemLabel).to.be.equal('Foo textContent');
        });
      });
    });
  </script>

</body>
</html>