blob: f0c536936d0492d036de274f48a95e8ad7035b0a [file] [log] [blame]
Wyatt Allen60ea95f2016-06-03 09:35:07 -07001<!DOCTYPE html>
2<!--
3Copyright (C) 2016 The Android Open Source Project
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16-->
17
18<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
19<title>gr-settings-view</title>
20
21<script src="../../../bower_components/webcomponentsjs/webcomponents.min.js"></script>
22<script src="../../../bower_components/web-component-tester/browser.js"></script>
23
24<link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html">
25<link rel="import" href="gr-settings-view.html">
26
27<test-fixture id="basic">
28 <template>
29 <gr-settings-view></gr-settings-view>
30 </template>
31</test-fixture>
32
Wyatt Allen2dc5c362016-06-13 21:05:45 -070033<test-fixture id="blank">
34 <template>
35 <div></div>
36 </template>
37</test-fixture>
38
Wyatt Allen60ea95f2016-06-03 09:35:07 -070039<script>
40 suite('gr-settings-view tests', function() {
41 var element;
42 var account;
43 var preferences;
44
Wyatt Allend14895b2016-06-06 12:37:24 -070045 function valueOf(title, fieldsetid) {
46 var sections = element.$[fieldsetid].querySelectorAll('section');
47 var titleEl;
48 for (var i = 0; i < sections.length; i++) {
49 titleEl = sections[i].querySelector('.title');
50 if (titleEl.textContent === title) {
51 return sections[i].querySelector('.value');
52 }
53 }
54 }
55
56 // Because deepEqual isn't behaving in Safari.
57 function assertMenusEqual(actual, expected) {
58 assert.equal(actual.length, expected.length);
59 for (var i = 0; i < actual.length; i++) {
60 assert.equal(actual[i].name, expected[i].name);
61 assert.equal(actual[i].url, expected[i].url);
62 }
63 }
64
Wyatt Allen60ea95f2016-06-03 09:35:07 -070065 setup(function(done) {
66 account = {
67 _account_id: 123,
68 name: 'user name',
69 email: 'user@email',
70 username: 'user username',
71 registered: '2000-01-01 00:00:00.000000000',
72 };
Wyatt Allend14895b2016-06-06 12:37:24 -070073 preferences = {
74 changes_per_page: 25,
75 date_format: 'UK',
76 time_format: 'HHMM_12',
77 diff_view: 'UNIFIED_DIFF',
78 email_strategy: 'ENABLED',
79
80 my: [
81 {url: '/first/url', name: 'first name', target: '_blank'},
82 {url: '/second/url', name: 'second name', target: '_blank'},
83 ],
84 };
Wyatt Allenbd472172016-06-09 17:44:51 -070085 watchedProjects = [];
Wyatt Allen60ea95f2016-06-03 09:35:07 -070086
87 stub('gr-rest-api-interface', {
88 getLoggedIn: function() { return Promise.resolve(true); },
89 getAccount: function() { return Promise.resolve(account); },
90 getPreferences: function() { return Promise.resolve(preferences); },
Wyatt Allenbd472172016-06-09 17:44:51 -070091 getWatchedProjects: function() {
92 return Promise.resolve(watchedProjects);
93 },
Wyatt Allen60ea95f2016-06-03 09:35:07 -070094 });
95 element = fixture('basic');
96
97 // Allow the element to render.
98 element.async(done, 1);
99 });
100
Wyatt Allend14895b2016-06-06 12:37:24 -0700101 test('account info render', function() {
Wyatt Allen60ea95f2016-06-03 09:35:07 -0700102 assert.isFalse(element._loading);
103
Wyatt Allend14895b2016-06-06 12:37:24 -0700104 assert.equal(valueOf('ID', 'profile').textContent, account._account_id);
105 assert.equal(valueOf('Name', 'profile').textContent, account.name);
106 assert.equal(valueOf('Email', 'profile').textContent, account.email);
107 assert.equal(valueOf('Username', 'profile').textContent,
108 account.username);
Wyatt Allen60ea95f2016-06-03 09:35:07 -0700109 assert.equal(element._computeRegistered(element.account.registered),
110 'Sat, 01 Jan 2000 00:00:00 GMT');
111 });
Wyatt Allend14895b2016-06-06 12:37:24 -0700112
Wyatt Allen2dc5c362016-06-13 21:05:45 -0700113 test('calls the title-change event', function() {
114 var titleChangedStub = sinon.stub();
115
116 // Create a new view.
117 var newElement = document.createElement('gr-settings-view');
118 newElement.addEventListener('title-change', titleChangedStub);
119
120 // Attach it to the fixture.
121 var blank = fixture('blank');
122 blank.appendChild(newElement);
123
124 assert.isTrue(titleChangedStub.called);
125 assert.equal(titleChangedStub.getCall(0).args[0].detail.title,
126 'Settings');
127 });
128
Wyatt Allend14895b2016-06-06 12:37:24 -0700129 test('user preferences', function(done) {
130 // Rendered with the expected preferences selected.
Wyatt Allenbd472172016-06-09 17:44:51 -0700131 assert.equal(valueOf('Changes Per Page', 'preferences')
132 .firstElementChild.bindValue, preferences.changes_per_page);
Wyatt Allend14895b2016-06-06 12:37:24 -0700133 assert.equal(valueOf('Date/Time Format', 'preferences')
Wyatt Allenbd472172016-06-09 17:44:51 -0700134 .firstElementChild.bindValue, preferences.date_format);
Wyatt Allend14895b2016-06-06 12:37:24 -0700135 assert.equal(valueOf('Date/Time Format', 'preferences')
Wyatt Allenbd472172016-06-09 17:44:51 -0700136 .lastElementChild.bindValue, preferences.time_format);
Wyatt Allend14895b2016-06-06 12:37:24 -0700137 assert.equal(valueOf('Email Notifications', 'preferences')
Wyatt Allenbd472172016-06-09 17:44:51 -0700138 .firstElementChild.bindValue, preferences.email_strategy);
Wyatt Allend14895b2016-06-06 12:37:24 -0700139 assert.equal(valueOf('Diff View', 'preferences')
Wyatt Allenbd472172016-06-09 17:44:51 -0700140 .firstElementChild.bindValue, preferences.diff_view);
Wyatt Allend14895b2016-06-06 12:37:24 -0700141
142 assert.isFalse(element._prefsChanged);
143 assert.isFalse(element._menuChanged);
144
145 // Change the diff view element.
146 var diffSelect = valueOf('Diff View', 'preferences').firstElementChild;
Wyatt Allenbd472172016-06-09 17:44:51 -0700147 diffSelect.bindValue = 'SIDE_BY_SIDE';
Wyatt Allend14895b2016-06-06 12:37:24 -0700148 diffSelect.fire('change');
149
150 assert.isTrue(element._prefsChanged);
151 assert.isFalse(element._menuChanged);
152
153 stub('gr-rest-api-interface', {
154 savePreferences: function(prefs) {
155 assert.equal(prefs.diff_view, 'SIDE_BY_SIDE');
156 assertMenusEqual(prefs.my, preferences.my);
157 return Promise.resolve();
158 }
159 });
160
161 // Save the change.
162 element._handleSavePreferences().then(function() {
163 assert.isFalse(element._prefsChanged);
164 assert.isFalse(element._menuChanged);
165 done();
166 });
167 });
168
169 test('menu', function(done) {
170 assert.isFalse(element._menuChanged);
171 assert.isFalse(element._prefsChanged);
172
173 assertMenusEqual(element._localMenu, preferences.my);
174
175 var menu = element.$.menu.firstElementChild;
176 var tableRows = Polymer.dom(menu.root).querySelectorAll('tbody tr');
177 assert.equal(tableRows.length, preferences.my.length);
178
179 // Add a menu item:
180 element.splice('_localMenu', 1, 0, {name: 'foo', url: 'bar', target: ''});
181 Polymer.dom.flush();
182
183 tableRows = Polymer.dom(menu.root).querySelectorAll('tbody tr');
184 assert.equal(tableRows.length, preferences.my.length + 1);
185
186 assert.isTrue(element._menuChanged);
187 assert.isFalse(element._prefsChanged);
188
189 stub('gr-rest-api-interface', {
190 savePreferences: function(prefs) {
191 assertMenusEqual(prefs.my, element._localMenu);
192 return Promise.resolve();
193 }
194 });
195
196 element._handleSaveMenu().then(function() {
197 assert.isFalse(element._menuChanged);
198 assert.isFalse(element._prefsChanged);
199 assertMenusEqual(element.prefs.my, element._localMenu);
200 done();
201 });
202 });
Wyatt Allen60ea95f2016-06-03 09:35:07 -0700203 });
204</script>