summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/change/gr-account-entry/gr-account-entry_test.html
blob: a7bf1e481fbc48a0f8994f0cf8a45a63415e6a45 (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
<!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-account-entry</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"/>
<script src="../../../scripts/util.js"></script>

<link rel="import" href="gr-account-entry.html">

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

<test-fixture id="basic">
  <template>
    <gr-account-entry></gr-account-entry>
  </template>
</test-fixture>

<script>
  suite('gr-account-entry tests', () => {
    let sandbox;
    let _nextAccountId = 0;
    const makeAccount = function(opt_status) {
      const accountId = ++_nextAccountId;
      return {
        _account_id: accountId,
        name: 'name ' + accountId,
        email: 'email ' + accountId,
        status: opt_status,
      };
    };
    let _nextAccountId2 = 0;
    const makeAccount2 = function(opt_status) {
      const accountId2 = ++_nextAccountId2;
      return {
        _account_id: accountId2,
        email: 'email ' + accountId2,
        status: opt_status,
      };
    };

    let owner;
    let existingReviewer1;
    let existingReviewer2;
    let suggestion1;
    let suggestion2;
    let suggestion3;
    let element;

    setup(() => {
      owner = makeAccount();
      existingReviewer1 = makeAccount();
      existingReviewer2 = makeAccount();
      suggestion1 = {account: makeAccount()};
      suggestion2 = {account: makeAccount()};
      suggestion3 = {
        group: {
          id: 'suggested group id',
          name: 'suggested group',
        },
      };

      element = fixture('basic');
      element.change = {
        _number: 42,
        owner,
        reviewers: {
          CC: [existingReviewer1],
          REVIEWER: [existingReviewer2],
        },
      };
      sandbox = sinon.sandbox.create();
    });

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

    suite('stubbed values for _getReviewerSuggestions', () => {
      setup(() => {
        stub('gr-rest-api-interface', {
          getChangeSuggestedReviewers() {
            const redundantSuggestion1 = {account: existingReviewer1};
            const redundantSuggestion2 = {account: existingReviewer2};
            const redundantSuggestion3 = {account: owner};
            return Promise.resolve([redundantSuggestion1, redundantSuggestion2,
              redundantSuggestion3, suggestion1, suggestion2, suggestion3]);
          },
        });
      });

      test('_makeSuggestion formats account or group accordingly', () => {
        let account = makeAccount();
        const account2 = makeAccount2();
        let suggestion = element._makeSuggestion({account});
        assert.deepEqual(suggestion, {
          name: account.name + ' <' + account.email + '>',
          value: {account},
        });

        const group = {name: 'test'};
        suggestion = element._makeSuggestion({group});
        assert.deepEqual(suggestion, {
          name: group.name + ' (group)',
          value: {group},
        });

        suggestion = element._makeSuggestion(account);
        assert.deepEqual(suggestion, {
          name: account.name + ' <' + account.email + '>',
          value: {account, count: 1},
        });

        element._config = {
          user: {
            anonymous_coward_name: 'Anonymous Coward',
          },
        };
        assert.deepEqual(element._accountOrAnon(account2), 'Anonymous');

        account = makeAccount('OOO');

        suggestion = element._makeSuggestion({account});
        assert.deepEqual(suggestion, {
          name: account.name + ' <' + account.email + '> (OOO)',
          value: {account},
        });

        suggestion = element._makeSuggestion(account);
        assert.deepEqual(suggestion, {
          name: account.name + ' <' + account.email + '> (OOO)',
          value: {account, count: 1},
        });
      });

      test('_getReviewerSuggestions excludes owner+reviewers', done => {
        element._getReviewerSuggestions().then(reviewers => {
          // Default is no filtering.
          assert.equal(reviewers.length, 6);

          // Set up filter that only accepts suggestion1.
          const accountId = suggestion1.account._account_id;
          element.filter = function(suggestion) {
            return suggestion.account &&
                suggestion.account._account_id === accountId;
          };

          element._getReviewerSuggestions().then(reviewers => {
            assert.deepEqual(reviewers, [element._makeSuggestion(suggestion1)]);
          }).then(done);
        });
      });
    });

    test('allowAnyUser', done => {
      const suggestReviewerStub =
          sandbox.stub(element.$.restAPI, 'getChangeSuggestedReviewers')
          .returns(Promise.resolve([]));
      const suggestAccountStub =
          sandbox.stub(element.$.restAPI, 'getSuggestedAccounts')
          .returns(Promise.resolve([]));

      element._getReviewerSuggestions('').then(() => {
        assert.isTrue(suggestReviewerStub.calledOnce);
        assert.isTrue(suggestReviewerStub.calledWith(42, ''));
        assert.isFalse(suggestAccountStub.called);
        element.allowAnyUser = true;

        element._getReviewerSuggestions('').then(() => {
          assert.isTrue(suggestReviewerStub.calledOnce);
          assert.isTrue(suggestAccountStub.calledOnce);
          assert.isTrue(suggestAccountStub.calledWith('cansee:42 '));
          done();
        });
      });
    });

    test('setText', () => {
      // Spy on query, as that is called when _updateSuggestions proceeds.
      const suggestSpy = sandbox.spy(element.$.input, 'query');
      element.setText('test text');
      flushAsynchronousOperations();

      assert.equal(element.$.input.$.input.value, 'test text');
      assert.isFalse(suggestSpy.called);
    });
  });
</script>