Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <!-- |
| 3 | Copyright (C) 2016 The Android Open Source Project |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations 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 Donich | 29e1ce5 | 2017-03-28 17:02:44 -0700 | [diff] [blame] | 21 | <script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script> |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 22 | <script src="../../../bower_components/web-component-tester/browser.js"></script> |
Mike Samuel | e07c4b2 | 2017-06-02 13:08:19 -0400 | [diff] [blame] | 23 | <link rel="import" href="../../../test/common-test-setup.html"/> |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 24 | <link rel="import" href="gr-registration-dialog.html"> |
| 25 | |
Viktar Donich | 29e1ce5 | 2017-03-28 17:02:44 -0700 | [diff] [blame] | 26 | <script>void(0);</script> |
| 27 | |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 28 | <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 Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 41 | suite('gr-registration-dialog tests', () => { |
| 42 | let element; |
| 43 | let account; |
Kasper Nilsson | 55cf8c1 | 2017-11-09 15:45:09 -0800 | [diff] [blame^] | 44 | let sandbox; |
Kasper Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 45 | let _listeners; |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 46 | |
Kasper Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 47 | setup(done => { |
Kasper Nilsson | 55cf8c1 | 2017-11-09 15:45:09 -0800 | [diff] [blame^] | 48 | sandbox = sinon.sandbox.create(); |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 49 | _listeners = {}; |
| 50 | |
| 51 | account = { |
| 52 | name: 'name', |
Kasper Nilsson | 55cf8c1 | 2017-11-09 15:45:09 -0800 | [diff] [blame^] | 53 | username: 'username', |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 54 | email: 'email', |
| 55 | secondary_emails: [ |
| 56 | 'email2', |
| 57 | 'email3', |
| 58 | ], |
| 59 | }; |
| 60 | |
| 61 | stub('gr-rest-api-interface', { |
Kasper Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 62 | getAccount() { |
| 63 | // Once the account is resolved, we can let the test proceed. |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 64 | flush(done); |
| 65 | return Promise.resolve(account); |
| 66 | }, |
Kasper Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 67 | setAccountName(name) { |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 68 | account.name = name; |
| 69 | return Promise.resolve(); |
| 70 | }, |
Kasper Nilsson | 55cf8c1 | 2017-11-09 15:45:09 -0800 | [diff] [blame^] | 71 | setAccountUsername(username) { |
| 72 | account.username = username; |
| 73 | return Promise.resolve(); |
| 74 | }, |
Kasper Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 75 | setPreferredAccountEmail(email) { |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 76 | account.email = email; |
| 77 | return Promise.resolve(); |
| 78 | }, |
| 79 | }); |
| 80 | |
| 81 | element = fixture('basic'); |
| 82 | }); |
| 83 | |
Kasper Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 84 | teardown(() => { |
Kasper Nilsson | 55cf8c1 | 2017-11-09 15:45:09 -0800 | [diff] [blame^] | 85 | sandbox.restore(); |
Kasper Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 86 | for (const eventType in _listeners) { |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 87 | if (_listeners.hasOwnProperty(eventType)) { |
| 88 | element.removeEventListener(eventType, _listeners[eventType]); |
| 89 | } |
| 90 | } |
| 91 | }); |
| 92 | |
| 93 | function listen(eventType) { |
Kasper Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 94 | return new Promise(resolve => { |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 95 | _listeners[eventType] = function() { resolve(); }; |
| 96 | element.addEventListener(eventType, _listeners[eventType]); |
| 97 | }); |
| 98 | } |
| 99 | |
| 100 | function save(opt_action) { |
Kasper Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 101 | const promise = listen('account-detail-update'); |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 102 | 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 Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 111 | const promise = listen('close'); |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 112 | if (opt_action) { |
| 113 | opt_action(); |
| 114 | } else { |
| 115 | MockInteractions.tap(element.$.closeButton); |
| 116 | } |
| 117 | return promise; |
| 118 | } |
| 119 | |
Kasper Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 120 | test('fires the close event on close', done => { |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 121 | close().then(done); |
| 122 | }); |
| 123 | |
Kasper Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 124 | test('fires the close event on save', done => { |
| 125 | close(() => { |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 126 | MockInteractions.tap(element.$.saveButton); |
| 127 | }).then(done); |
| 128 | }); |
| 129 | |
Kasper Nilsson | 55cf8c1 | 2017-11-09 15:45:09 -0800 | [diff] [blame^] | 130 | test('saves account details', done => { |
Kasper Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 131 | flush(() => { |
Logan Hanks | 63aff83 | 2016-10-17 11:02:02 -0700 | [diff] [blame] | 132 | element.$.name.value = 'new name'; |
Kasper Nilsson | 55cf8c1 | 2017-11-09 15:45:09 -0800 | [diff] [blame^] | 133 | element.$.username.value = 'new username'; |
Logan Hanks | 63aff83 | 2016-10-17 11:02:02 -0700 | [diff] [blame] | 134 | element.$.email.value = 'email3'; |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 135 | |
Logan Hanks | 63aff83 | 2016-10-17 11:02:02 -0700 | [diff] [blame] | 136 | // Nothing should be committed yet. |
| 137 | assert.equal(account.name, 'name'); |
Kasper Nilsson | 55cf8c1 | 2017-11-09 15:45:09 -0800 | [diff] [blame^] | 138 | assert.equal(account.username, 'username'); |
Logan Hanks | 63aff83 | 2016-10-17 11:02:02 -0700 | [diff] [blame] | 139 | assert.equal(account.email, 'email'); |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 140 | |
Logan Hanks | 63aff83 | 2016-10-17 11:02:02 -0700 | [diff] [blame] | 141 | // Save and verify new values are committed. |
Kasper Nilsson | 6ff0a3b | 2017-05-15 17:42:46 -0700 | [diff] [blame] | 142 | save().then(() => { |
Logan Hanks | 63aff83 | 2016-10-17 11:02:02 -0700 | [diff] [blame] | 143 | assert.equal(account.name, 'new name'); |
Kasper Nilsson | 55cf8c1 | 2017-11-09 15:45:09 -0800 | [diff] [blame^] | 144 | assert.equal(account.username, 'new username'); |
Logan Hanks | 63aff83 | 2016-10-17 11:02:02 -0700 | [diff] [blame] | 145 | assert.equal(account.email, 'email3'); |
| 146 | }).then(done); |
| 147 | }); |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 148 | }); |
| 149 | |
Wyatt Allen | 96ae98d | 2017-09-27 18:06:10 -0400 | [diff] [blame] | 150 | 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 Nilsson | 55cf8c1 | 2017-11-09 15:45:09 -0800 | [diff] [blame^] | 157 | |
| 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 Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 167 | }); |
| 168 | </script> |