blob: 15b4fa2f2a96f45eab8ee0665a52d086c0f68b21 [file] [log] [blame]
Logan Hanks25f49af2016-10-10 17:26:43 -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-registration-dialog</title>
20
Viktar Donich29e1ce52017-03-28 17:02:44 -070021<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
Logan Hanks25f49af2016-10-10 17:26:43 -070022<script src="../../../bower_components/web-component-tester/browser.js"></script>
Mike Samuele07c4b22017-06-02 13:08:19 -040023<link rel="import" href="../../../test/common-test-setup.html"/>
Logan Hanks25f49af2016-10-10 17:26:43 -070024<link rel="import" href="gr-registration-dialog.html">
25
Viktar Donich29e1ce52017-03-28 17:02:44 -070026<script>void(0);</script>
27
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
40<script>
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -070041 suite('gr-registration-dialog tests', () => {
42 let element;
43 let account;
Kasper Nilsson55cf8c12017-11-09 15:45:09 -080044 let sandbox;
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -070045 let _listeners;
Logan Hanks25f49af2016-10-10 17:26:43 -070046
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -070047 setup(done => {
Kasper Nilsson55cf8c12017-11-09 15:45:09 -080048 sandbox = sinon.sandbox.create();
Logan Hanks25f49af2016-10-10 17:26:43 -070049 _listeners = {};
50
51 account = {
52 name: 'name',
Kasper Nilsson55cf8c12017-11-09 15:45:09 -080053 username: 'username',
Logan Hanks25f49af2016-10-10 17:26:43 -070054 email: 'email',
55 secondary_emails: [
56 'email2',
57 'email3',
58 ],
59 };
60
61 stub('gr-rest-api-interface', {
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -070062 getAccount() {
63 // Once the account is resolved, we can let the test proceed.
Logan Hanks25f49af2016-10-10 17:26:43 -070064 flush(done);
65 return Promise.resolve(account);
66 },
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -070067 setAccountName(name) {
Logan Hanks25f49af2016-10-10 17:26:43 -070068 account.name = name;
69 return Promise.resolve();
70 },
Kasper Nilsson55cf8c12017-11-09 15:45:09 -080071 setAccountUsername(username) {
72 account.username = username;
73 return Promise.resolve();
74 },
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -070075 setPreferredAccountEmail(email) {
Logan Hanks25f49af2016-10-10 17:26:43 -070076 account.email = email;
77 return Promise.resolve();
78 },
79 });
80
81 element = fixture('basic');
82 });
83
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -070084 teardown(() => {
Kasper Nilsson55cf8c12017-11-09 15:45:09 -080085 sandbox.restore();
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -070086 for (const eventType in _listeners) {
Logan Hanks25f49af2016-10-10 17:26:43 -070087 if (_listeners.hasOwnProperty(eventType)) {
88 element.removeEventListener(eventType, _listeners[eventType]);
89 }
90 }
91 });
92
93 function listen(eventType) {
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -070094 return new Promise(resolve => {
Logan Hanks25f49af2016-10-10 17:26:43 -070095 _listeners[eventType] = function() { resolve(); };
96 element.addEventListener(eventType, _listeners[eventType]);
97 });
98 }
99
100 function save(opt_action) {
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -0700101 const promise = listen('account-detail-update');
Logan Hanks25f49af2016-10-10 17:26:43 -0700102 if (opt_action) {
103 opt_action();
104 } else {
105 MockInteractions.tap(element.$.saveButton);
106 }
107 return promise;
108 }
109
110 function close(opt_action) {
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -0700111 const promise = listen('close');
Logan Hanks25f49af2016-10-10 17:26:43 -0700112 if (opt_action) {
113 opt_action();
114 } else {
115 MockInteractions.tap(element.$.closeButton);
116 }
117 return promise;
118 }
119
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -0700120 test('fires the close event on close', done => {
Logan Hanks25f49af2016-10-10 17:26:43 -0700121 close().then(done);
122 });
123
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -0700124 test('fires the close event on save', done => {
125 close(() => {
Logan Hanks25f49af2016-10-10 17:26:43 -0700126 MockInteractions.tap(element.$.saveButton);
127 }).then(done);
128 });
129
Kasper Nilsson55cf8c12017-11-09 15:45:09 -0800130 test('saves account details', done => {
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -0700131 flush(() => {
Logan Hanks63aff832016-10-17 11:02:02 -0700132 element.$.name.value = 'new name';
Kasper Nilsson55cf8c12017-11-09 15:45:09 -0800133 element.$.username.value = 'new username';
Logan Hanks63aff832016-10-17 11:02:02 -0700134 element.$.email.value = 'email3';
Logan Hanks25f49af2016-10-10 17:26:43 -0700135
Logan Hanks63aff832016-10-17 11:02:02 -0700136 // Nothing should be committed yet.
137 assert.equal(account.name, 'name');
Kasper Nilsson55cf8c12017-11-09 15:45:09 -0800138 assert.equal(account.username, 'username');
Logan Hanks63aff832016-10-17 11:02:02 -0700139 assert.equal(account.email, 'email');
Logan Hanks25f49af2016-10-10 17:26:43 -0700140
Logan Hanks63aff832016-10-17 11:02:02 -0700141 // Save and verify new values are committed.
Kasper Nilsson6ff0a3b2017-05-15 17:42:46 -0700142 save().then(() => {
Logan Hanks63aff832016-10-17 11:02:02 -0700143 assert.equal(account.name, 'new name');
Kasper Nilsson55cf8c12017-11-09 15:45:09 -0800144 assert.equal(account.username, 'new username');
Logan Hanks63aff832016-10-17 11:02:02 -0700145 assert.equal(account.email, 'email3');
146 }).then(done);
147 });
Logan Hanks25f49af2016-10-10 17:26:43 -0700148 });
149
Wyatt Allen96ae98d2017-09-27 18:06:10 -0400150 test('email select properly populated', done => {
151 element._account = {email: 'foo', secondary_emails: ['bar', 'baz']};
152 flush(() => {
153 assert.equal(element.$.email.value, 'foo');
154 done();
155 });
156 });
Kasper Nilsson55cf8c12017-11-09 15:45:09 -0800157
158 test('save btn disabled', () => {
159 const compute = element._computeSaveDisabled;
160 assert.isTrue(compute('', '', '', false));
161 assert.isTrue(compute('', 'test', 'test', false));
162 assert.isTrue(compute('test', '', 'test', false));
163 assert.isTrue(compute('test', 'test', '', false));
164 assert.isTrue(compute('test', 'test', 'test', true));
165 assert.isFalse(compute('test', 'test', 'test', false));
166 });
Logan Hanks25f49af2016-10-10 17:26:43 -0700167 });
168</script>