blob: a3f8548f198d44dc896aa7a349c389f004435f6a [file] [log] [blame]
Logan Hanks25f49af2016-10-10 17:26:43 -07001<!DOCTYPE html>
2<!--
Dave Borowitz8cdc76b2018-03-26 10:04:27 -04003@license
Logan Hanks25f49af2016-10-10 17:26:43 -07004Copyright (C) 2016 The Android Open Source Project
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17-->
18
19<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
Tao Zhoud90e7f42020-04-29 17:07:14 +020020<meta charset="utf-8">
Logan Hanks25f49af2016-10-10 17:26:43 -070021<title>gr-registration-dialog</title>
Tao Zhou8ef16f72019-11-18 14:14:36 -080022
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010023<script src="/node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js"></script>
Logan Hanks25f49af2016-10-10 17:26:43 -070024
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010025<script src="/node_modules/@webcomponents/webcomponentsjs/webcomponents-lite.js"></script>
26<script src="/components/wct-browser-legacy/browser.js"></script>
Viktar Donich29e1ce52017-03-28 17:02:44 -070027
Logan Hanks25f49af2016-10-10 17:26:43 -070028<test-fixture id="basic">
29 <template>
30 <gr-registration-dialog></gr-registration-dialog>
31 </template>
32</test-fixture>
33
34<test-fixture id="blank">
35 <template>
36 <div></div>
37 </template>
38</test-fixture>
39
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010040<script type="module">
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010041import '../../../test/common-test-setup.js';
42import './gr-registration-dialog.js';
43suite('gr-registration-dialog tests', () => {
44 let element;
45 let account;
46 let sandbox;
47 let _listeners;
Logan Hanks25f49af2016-10-10 17:26:43 -070048
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010049 setup(() => {
50 sandbox = sinon.sandbox.create();
51 _listeners = {};
Logan Hanks25f49af2016-10-10 17:26:43 -070052
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010053 account = {
54 name: 'name',
55 username: null,
56 email: 'email',
57 secondary_emails: [
58 'email2',
59 'email3',
60 ],
61 };
Logan Hanks25f49af2016-10-10 17:26:43 -070062
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010063 stub('gr-rest-api-interface', {
64 getAccount() {
65 return Promise.resolve(account);
66 },
67 setAccountName(name) {
68 account.name = name;
69 return Promise.resolve();
70 },
71 setAccountUsername(username) {
72 account.username = username;
73 return Promise.resolve();
74 },
75 setPreferredAccountEmail(email) {
76 account.email = email;
77 return Promise.resolve();
78 },
79 getConfig() {
80 return Promise.resolve(
81 {auth: {editable_account_fields: ['USER_NAME']}});
82 },
Logan Hanks25f49af2016-10-10 17:26:43 -070083 });
84
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010085 element = fixture('basic');
86
87 return element.loadData();
88 });
89
90 teardown(() => {
91 sandbox.restore();
92 for (const eventType in _listeners) {
93 if (_listeners.hasOwnProperty(eventType)) {
94 element.removeEventListener(eventType, _listeners[eventType]);
Logan Hanks25f49af2016-10-10 17:26:43 -070095 }
Logan Hanks25f49af2016-10-10 17:26:43 -070096 }
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010097 });
Logan Hanks25f49af2016-10-10 17:26:43 -070098
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010099 function listen(eventType) {
100 return new Promise(resolve => {
101 _listeners[eventType] = function() { resolve(); };
102 element.addEventListener(eventType, _listeners[eventType]);
103 });
104 }
105
106 function save(opt_action) {
107 const promise = listen('account-detail-update');
108 if (opt_action) {
109 opt_action();
110 } else {
111 MockInteractions.tap(element.$.saveButton);
Logan Hanks25f49af2016-10-10 17:26:43 -0700112 }
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100113 return promise;
114 }
Logan Hanks25f49af2016-10-10 17:26:43 -0700115
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100116 function close(opt_action) {
117 const promise = listen('close');
118 if (opt_action) {
119 opt_action();
120 } else {
121 MockInteractions.tap(element.$.closeButton);
Logan Hanks25f49af2016-10-10 17:26:43 -0700122 }
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100123 return promise;
124 }
Logan Hanks25f49af2016-10-10 17:26:43 -0700125
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100126 test('fires the close event on close', done => {
127 close().then(done);
128 });
Logan Hanks25f49af2016-10-10 17:26:43 -0700129
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100130 test('fires the close event on save', done => {
131 close(() => {
132 MockInteractions.tap(element.$.saveButton);
133 }).then(done);
134 });
Logan Hanks25f49af2016-10-10 17:26:43 -0700135
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100136 test('saves account details', done => {
137 flush(() => {
138 element.$.name.value = 'new name';
139 element.$.username.value = 'new username';
140 element.$.email.value = 'email3';
Logan Hanks25f49af2016-10-10 17:26:43 -0700141
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100142 // Nothing should be committed yet.
143 assert.equal(account.name, 'name');
144 assert.isNotOk(account.username);
145 assert.equal(account.email, 'email');
Logan Hanks25f49af2016-10-10 17:26:43 -0700146
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100147 // Save and verify new values are committed.
148 save()
149 .then(() => {
150 assert.equal(account.name, 'new name');
151 assert.equal(account.username, 'new username');
152 assert.equal(account.email, 'email3');
153 })
154 .then(done);
Kasper Nilsson55cf8c12017-11-09 15:45:09 -0800155 });
Logan Hanks25f49af2016-10-10 17:26:43 -0700156 });
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100157
158 test('email select properly populated', done => {
159 element._account = {email: 'foo', secondary_emails: ['bar', 'baz']};
160 flush(() => {
161 assert.equal(element.$.email.value, 'foo');
162 done();
163 });
164 });
165
166 test('save btn disabled', () => {
167 const compute = element._computeSaveDisabled;
168 assert.isTrue(compute('', '', false));
169 assert.isTrue(compute('', 'test', false));
170 assert.isTrue(compute('test', '', false));
171 assert.isTrue(compute('test', 'test', true));
172 assert.isFalse(compute('test', 'test', false));
173 });
174
175 test('_computeUsernameMutable', () => {
176 assert.isTrue(element._computeUsernameMutable(
177 {auth: {editable_account_fields: ['USER_NAME']}}, null));
178 assert.isFalse(element._computeUsernameMutable(
179 {auth: {editable_account_fields: ['USER_NAME']}}, 'abc'));
180 assert.isFalse(element._computeUsernameMutable(
181 {auth: {editable_account_fields: []}}, null));
182 assert.isFalse(element._computeUsernameMutable(
183 {auth: {editable_account_fields: []}}, 'abc'));
184 });
185});
Logan Hanks25f49af2016-10-10 17:26:43 -0700186</script>