summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-change-actions-js-api_test.ts
blob: 4dfc63811666b64c244232dce8744d49c425918e (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
/**
 * @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.
 */

import '../../../test/common-test-setup-karma';
import '../../change/gr-change-actions/gr-change-actions';
import {
  query,
  queryAll,
  queryAndAssert,
  resetPlugins,
} from '../../../test/test-utils';
import {getPluginLoader} from './gr-plugin-loader';
import {GrChangeActions} from '../../change/gr-change-actions/gr-change-actions';
import {fixture, html} from '@open-wc/testing-helpers';
import {PluginApi} from '../../../api/plugin';
import {
  ActionType,
  ChangeActionsPluginApi,
  PrimaryActionKey,
} from '../../../api/change-actions';
import {GrButton} from '../gr-button/gr-button';
import {IronIconElement} from '@polymer/iron-icon';
import {ChangeViewChangeInfo} from '../../../types/common';
import {GrDropdown} from '../gr-dropdown/gr-dropdown';

suite('gr-change-actions-js-api-interface tests', () => {
  let element: GrChangeActions;
  let changeActions: ChangeActionsPluginApi;
  let plugin: PluginApi;

  suite('early init', () => {
    setup(async () => {
      resetPlugins();
      window.Gerrit.install(
        p => {
          plugin = p;
        },
        '0.1',
        'http://test.com/plugins/testplugin/static/test.js'
      );
      // Mimic all plugins loaded.
      getPluginLoader().loadPlugins([]);
      changeActions = plugin.changeActions();
      element = await fixture<GrChangeActions>(html`
        <gr-change-actions></gr-change-actions>
      `);
    });

    teardown(() => {
      resetPlugins();
    });

    test('does not throw', () => {
      assert.doesNotThrow(() => {
        changeActions.add(ActionType.CHANGE, 'foo');
      });
    });
  });

  suite('normal init', () => {
    setup(async () => {
      resetPlugins();
      element = await fixture<GrChangeActions>(html`
        <gr-change-actions></gr-change-actions>
      `);
      element.change = {} as ChangeViewChangeInfo;
      element._hasKnownChainState = false;
      window.Gerrit.install(
        p => {
          plugin = p;
        },
        '0.1',
        'http://test.com/plugins/testplugin/static/test.js'
      );
      changeActions = plugin.changeActions();
      // Mimic all plugins loaded.
      getPluginLoader().loadPlugins([]);
    });

    teardown(() => {
      resetPlugins();
    });

    test('property existence', () => {
      const properties = ['ActionType', 'ChangeActions', 'RevisionActions'];
      for (const p of properties) {
        // Have to type as any to prevent 'has no index signature.'
        assert.deepEqual((changeActions as any)[p], (element as any)[p]);
      }
    });

    test('add/remove primary action keys', () => {
      element.primaryActionKeys = [];
      changeActions.addPrimaryActionKey('foo' as PrimaryActionKey);
      assert.deepEqual(element.primaryActionKeys, ['foo']);
      changeActions.addPrimaryActionKey('bar' as PrimaryActionKey);
      assert.deepEqual(element.primaryActionKeys, ['foo', 'bar']);
      changeActions.removePrimaryActionKey('foo');
      assert.deepEqual(element.primaryActionKeys, ['bar']);
      changeActions.removePrimaryActionKey('baz');
      assert.deepEqual(element.primaryActionKeys, ['bar']);
      changeActions.removePrimaryActionKey('bar');
      assert.deepEqual(element.primaryActionKeys, []);
    });

    test('action buttons', async () => {
      const key = changeActions.add(ActionType.REVISION, 'Bork!');
      const handler = sinon.spy();
      changeActions.addTapListener(key, handler);
      await element.updateComplete;
      queryAndAssert<GrButton>(element, `[data-action-key="${key}"]`).click();
      await element.updateComplete;
      assert(handler.calledOnce);
      changeActions.removeTapListener(key, handler);
      await element.updateComplete;
      queryAndAssert<GrButton>(element, `[data-action-key="${key}"]`).click();
      await element.updateComplete;
      assert(handler.calledOnce);
      changeActions.remove(key);
      await element.updateComplete;
      assert.isUndefined(
        query<GrButton>(element, `[data-action-key="${key}"]`)
      );
    });

    test('action button properties', async () => {
      const key = changeActions.add(ActionType.REVISION, 'Bork!');
      await element.updateComplete;
      const button = queryAndAssert<GrButton>(
        element,
        `[data-action-key="${key}"]`
      );
      assert.isOk(button);
      assert.equal(button.getAttribute('data-label'), 'Bork!');
      assert.isNotOk(button.disabled);
      changeActions.setLabel(key, 'Yo');
      changeActions.setTitle(key, 'Yo hint');
      changeActions.setEnabled(key, false);
      changeActions.setIcon(key, 'pupper');
      await element.updateComplete;
      assert.equal(button.getAttribute('data-label'), 'Yo');
      assert.equal(button.parentElement!.getAttribute('title'), 'Yo hint');
      assert.isTrue(button.disabled);
      assert.equal(
        queryAndAssert<IronIconElement>(button, 'iron-icon').icon,
        'gr-icons:pupper'
      );
    });

    test('hide action buttons', async () => {
      const key = changeActions.add(ActionType.REVISION, 'Bork!');
      await element.updateComplete;
      let button = query<GrButton>(element, `[data-action-key="${key}"]`);
      assert.isOk(button);
      assert.isFalse(button!.hasAttribute('hidden'));
      changeActions.setActionHidden(ActionType.REVISION, key, true);
      await element.updateComplete;
      button = query<GrButton>(element, `[data-action-key="${key}"]`);
      assert.isNotOk(button);
    });

    test('move action button to overflow', async () => {
      const key = changeActions.add(ActionType.REVISION, 'Bork!');
      await element.updateComplete;
      assert.isTrue(queryAndAssert<GrDropdown>(element, '#moreActions').hidden);
      assert.isOk(
        queryAndAssert<GrButton>(element, `[data-action-key="${key}"]`)
      );
      changeActions.setActionOverflow(ActionType.REVISION, key, true);
      await element.updateComplete;
      assert.isNotOk(query<GrButton>(element, `[data-action-key="${key}"]`));
      assert.isFalse(
        queryAndAssert<GrDropdown>(element, '#moreActions').hidden
      );
      assert.strictEqual(
        queryAndAssert<GrDropdown>(element, '#moreActions').items![0].name,
        'Bork!'
      );
    });

    test('change actions priority', async () => {
      const key1 = changeActions.add(ActionType.REVISION, 'Bork!');
      const key2 = changeActions.add(ActionType.CHANGE, 'Squanch?');
      await element.updateComplete;
      let buttons = queryAll<GrButton>(element, '[data-action-key]');
      assert.equal(buttons[0].getAttribute('data-action-key'), key1);
      assert.equal(buttons[1].getAttribute('data-action-key'), key2);
      changeActions.setActionPriority(ActionType.REVISION, key1, 10);
      await element.updateComplete;
      buttons = queryAll<GrButton>(element, '[data-action-key]');
      assert.equal(buttons[0].getAttribute('data-action-key'), key2);
      assert.equal(buttons[1].getAttribute('data-action-key'), key1);
    });
  });
});