/
decorators-test.ts
252 lines (214 loc) · 9.35 KB
/
decorators-test.ts
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* @license
* Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt The complete set of authors may be found
* at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
* be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
* Google as part of the polymer project is also subject to an additional IP
* rights grant found at http://polymer.github.io/PATENTS.txt
*/
import {PolymerElement} from '@polymer/polymer/polymer-element.js';
import {customElement} from '../decorators';
import {DeclarativeListenerTestElement} from './elements/declarative-listener-test-element';
import {ElementWithIs} from './elements/element-with-is';
import {GestureListenerTestElement} from './elements/gesture-listener-test-element';
import {MixinBehaviorsTestElement} from './elements/mixin-behaviors-test-element';
import {TestElement} from './elements/test-element';
declare function fixture(id: string): HTMLElement;
suite('TypeScript Decorators', function() {
let testElement: TestElement;
let declarativeListenerTestElement: DeclarativeListenerTestElement;
let gestureListenerTestElement: GestureListenerTestElement;
setup(function() {
testElement = fixture('test-element-fixture') as TestElement;
gestureListenerTestElement =
fixture('gesture-listener-test-element-fixture') as
GestureListenerTestElement;
declarativeListenerTestElement =
fixture('declarative-listener-test-element-fixture') as
DeclarativeListenerTestElement;
});
suite('@customElement', function() {
test('defines an element', function() {
chai.assert.instanceOf(testElement, TestElement);
});
test('defines an element with an "is" getter', function() {
const el = fixture('element-with-is') as ElementWithIs;
chai.assert.instanceOf(el, ElementWithIs);
});
test('defines an element with a mixin behavior', function() {
const el = fixture('mixin-behaviors-test-element-fixture') as
MixinBehaviorsTestElement;
chai.assert.instanceOf(el, MixinBehaviorsTestElement);
chai.assert.equal(el.elementProperty, 'elementPropertyValue');
chai.assert.equal(el.behaviorProperty, 'behaviorPropertyValue');
});
test('throws when element names do not match', function() {
chai.assert.throws(() => {
@customElement('test-name-1')
class TestElement extends PolymerElement {
static is = 'test-name-2';
}
return TestElement;
});
chai.assert.isUndefined(customElements.get('test-name-1'));
chai.assert.isUndefined(customElements.get('test-name-2'));
});
});
suite('@property', function() {
test('defines a property', function() {
testElement.aNum = 999;
const numDiv = testElement.shadowRoot!.querySelector('#num')!;
const numText = numDiv.textContent;
chai.assert.equal(numText, '999');
});
test('merges multiple definitions', function() {
chai.assert.deepEqual(
// tslint:disable-next-line:no-any The properties config is not typed.
(TestElement as any).properties.computedWithOptions,
{
computed: '__computecomputedWithOptions(dependencyOne)',
readOnly: true,
type: String
});
});
test('notify property should fire events', function() {
let fired = false;
const fn = function() {
fired = true;
};
testElement.addEventListener('a-num-changed', fn);
testElement.aNum = 999;
chai.assert.equal(fired, true);
testElement.removeEventListener('a-num-changed', fn);
});
test('notify should default to false', function() {
let fired = false;
const fn = function() {
fired = true;
};
testElement.addEventListener('doesnt-notify-changed', fn);
testElement.doesntNotify = false;
chai.assert.equal(fired, false);
testElement.removeEventListener('doesnt-notify-changed', fn);
});
test(
'reflectToAttribute property should be reflected as an attribute',
function() {
testElement.reflectedString = 'nice';
const attributeText = testElement.getAttribute('reflected-string');
chai.assert.equal(attributeText, 'nice');
});
test('readonly property should not be changeable', function() {
testElement.readOnlyString = 'new value';
const propValue = testElement.readOnlyString;
chai.assert.equal(propValue, 'initial value');
});
test('computed property should return computed value', function() {
testElement.computedString = 'new value';
const propValue = testElement.computedString;
chai.assert.equal(propValue, 'computed yahoo');
});
test('observer property function should be invoked', function() {
testElement.observedString = 'new value';
const propValue = testElement.lastChange;
chai.assert.equal(propValue, 'new value');
});
});
suite('@observe', function() {
test('calls a method when a single observed property changes', function() {
testElement.aNum = 999;
chai.assert.equal(testElement.lastNumChange, 999);
});
test('calls a method when multiple observed properties change', function() {
testElement.aNum = 999;
chai.assert.equal(testElement.lastMultiChange[0], 999);
testElement.aString = 'yahoo';
chai.assert.equal(testElement.lastMultiChange[1], 'yahoo');
});
});
suite('@computed', function() {
test('defines a computed property', function() {
testElement.dependencyOne = 'foo';
const compDiv = testElement.shadowRoot!.querySelector('#computed')!;
chai.assert.equal(compDiv.textContent, 'foo');
chai.assert.equal(testElement.computedOne, 'foo');
});
test('defines a computed property with multiple arguments', function() {
testElement.dependencyOne = 'foo';
const compDiv = testElement.shadowRoot!.querySelector('#computedTwo')!;
chai.assert.equal(compDiv.textContent, 'foo');
chai.assert.equal(testElement.computedTwo, 'foo');
testElement.dependencyTwo = 'bar';
chai.assert.equal(compDiv.textContent, 'foobar');
chai.assert.equal(testElement.computedTwo, 'foobar');
});
});
suite('@query', function() {
test('queries the shadow root', function() {
const numDiv = testElement.numDiv;
chai.assert.equal(numDiv.id, 'num');
});
});
suite('@queryAll', function() {
test('queries the shadow root', function() {
const divs = testElement.divs;
chai.assert.equal(divs.length, 4);
});
});
suite('@listen - declarative listeners', function() {
test('triggers declarative listener on a element', function() {
const tapRegion =
declarativeListenerTestElement.shadowRoot!.querySelector(
'#tapRegion')!;
chai.assert.equal(declarativeListenerTestElement.elementClickCounter, 0);
tapRegion.dispatchEvent(
new CustomEvent('element-event', {bubbles: false}));
chai.assert.equal(declarativeListenerTestElement.elementClickCounter, 1);
});
test('triggers declarative listener on a document', function() {
chai.assert.equal(declarativeListenerTestElement.documentClickCounter, 0);
document.dispatchEvent(
new CustomEvent('document-event', {bubbles: false}));
chai.assert.equal(declarativeListenerTestElement.documentClickCounter, 1);
});
test('triggers declarative listener on a window', function() {
chai.assert.equal(declarativeListenerTestElement.windowClickCounter, 0);
window.dispatchEvent(new CustomEvent('window-event', {bubbles: false}));
chai.assert.equal(declarativeListenerTestElement.windowClickCounter, 1);
});
});
suite('@listen - gesture listeners', function() {
test('triggers gesture listener on a element', function() {
const tapRegion =
gestureListenerTestElement.shadowRoot!.querySelector('#tapRegion')!;
chai.assert.equal(gestureListenerTestElement.elementClickCounter, 0);
tapRegion.dispatchEvent(new CustomEvent('click', {bubbles: true}));
chai.assert.equal(gestureListenerTestElement.elementClickCounter, 1);
});
test('triggers gesture listener on a document', function() {
chai.assert.equal(gestureListenerTestElement.documentClickCounter, 0);
document.dispatchEvent(new CustomEvent('click', {bubbles: true}));
chai.assert.equal(gestureListenerTestElement.documentClickCounter, 1);
});
test('triggers gesture listener on a window', function() {
chai.assert.equal(gestureListenerTestElement.windowClickCounter, 0);
window.dispatchEvent(new CustomEvent('click', {bubbles: true}));
chai.assert.equal(gestureListenerTestElement.windowClickCounter, 1);
});
test(
'triggers non-gesture event on a class extending gesture listeners',
function() {
const tapRegion =
gestureListenerTestElement.shadowRoot!.querySelector(
'#tapRegion')!;
chai.assert.equal(
gestureListenerTestElement.nonGestureElementClickCounter, 0);
tapRegion.dispatchEvent(
new CustomEvent('element-event', {bubbles: false}));
chai.assert.equal(
gestureListenerTestElement.nonGestureElementClickCounter, 1);
});
});
});