Skip to content

Commit adbdb8e

Browse files
committed
Fixes
- Fix tests for Firefox - Remove functions from element API
1 parent 866f690 commit adbdb8e

File tree

3 files changed

+68
-46
lines changed

3 files changed

+68
-46
lines changed

palindrom-error-catcher.html

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,40 @@
7575
location.reload();
7676
}
7777

78+
function bindToElement(instance) {
79+
instance.target = instance.getRootNode().querySelector(instance.targetSelector) || document.querySelector(instance.targetSelector);
80+
}
81+
82+
function subscribeToEvents(instance) {
83+
unsubscribeToEvents(instance);
84+
instance.target.addEventListener('generic-error', instance.handleGenericError);
85+
instance.target.addEventListener('connection-error', instance.handleConnectionError);
86+
instance.target.addEventListener('reconnection-countdown', instance.handleReconnectionCountdownTick);
87+
instance.target.addEventListener('reconnection-end', instance.handleReconnectionEnd);
88+
instance.cancelReloadingBtn.addEventListener('click', instance.cancelReloading);
89+
instance.reloadNowBtn.addEventListener('click', reload);
90+
instance.genericErrorPane.addEventListener('click', reload);
91+
instance.subscribed = true;
92+
}
93+
94+
function unsubscribeToEvents(instance) {
95+
if (instance.subscribed) {
96+
instance.target.removeEventListener('generic-error', instance.handleGenericError);
97+
instance.target.removeEventListener('connection-error', instance.handleConnectionError);
98+
instance.target.removeEventListener('reconnection-countdown', instance.handleReconnectionCountdownTick);
99+
instance.target.removeEventListener('reconnection-end', instance.handleReconnectionEnd);
100+
instance.genericErrorPane.removeEventListener('click', reload);
101+
instance.cancelReloadingBtn.removeEventListener('click', instance.cancelReloading);
102+
instance.reloadNowBtn.removeEventListener('click', reload);
103+
instance.subscribed = false;
104+
}
105+
}
106+
78107
class PalindromErrorCatcher extends HTMLElement {
79108
static get observedAttributes() {
80109
return ['target-selector']
81110
}
111+
82112
constructor() {
83113
super();
84114

@@ -95,50 +125,29 @@
95125

96126
bindCallbacks(this);
97127
}
98-
_bindToElement() {
99-
this.target = this.getRootNode().querySelector(this.targetSelector) || document.querySelector(this.targetSelector);
100-
}
101128

102-
_subscribeToEvents() {
103-
this._unsubscribeToEvents();
104-
this.target.addEventListener('generic-error', this.handleGenericError);
105-
this.target.addEventListener('connection-error', this.handleConnectionError);
106-
this.target.addEventListener('reconnection-countdown', this.handleReconnectionCountdownTick);
107-
this.target.addEventListener('reconnection-end', this.handleReconnectionEnd);
108-
this.cancelReloadingBtn.addEventListener('click', this.cancelReloading);
109-
this.reloadNowBtn.addEventListener('click', reload);
110-
this.genericErrorPane.addEventListener('click', reload);
111-
this.subscribed = true;
112-
}
113-
_unsubscribeToEvents() {
114-
if (this.subscribed) {
115-
this.target.removeEventListener('generic-error', this.handleGenericError);
116-
this.target.removeEventListener('connection-error', this.handleConnectionError);
117-
this.target.removeEventListener('reconnection-countdown', this.handleReconnectionCountdownTick);
118-
this.target.removeEventListener('reconnection-end', this.handleReconnectionEnd);
119-
this.genericErrorPane.removeEventListener('click', reload);
120-
this.cancelReloadingBtn.removeEventListener('click', this.cancelReloading);
121-
this.reloadNowBtn.removeEventListener('click', reload);
122-
this.subscribed = false;
123-
}
124-
}
129+
125130
connectedCallback() {
126131
this.targetSelector = this.getAttribute('target-selector') || 'palindrom-client';
127-
this._bindToElement();
128-
this._subscribeToEvents();
132+
bindToElement(this);
133+
subscribeToEvents(this);
129134
}
135+
130136
disconnectedCallback() {
131-
this._unsubscribeToEvents();
137+
unsubscribeToEvents(this);
132138
}
139+
133140
handleGenericError(event) {
134141
this.genericErrorPane.removeAttribute('hidden');
135142
console.error(event.detail.error);
136143
}
144+
137145
cancelReloading() {
138146
this.connectionErrorPane.setAttribute('hidden', '');
139147
clearInterval(this.reloadInterval);
140148
delete this.reloadInterval;
141149
}
150+
142151
handleConnectionError(event) {
143152
this.connectionErrorPane.removeAttribute('hidden');
144153
let timeout = 4;
@@ -155,16 +164,16 @@
155164
}
156165
handleReconnectionCountdownTick(event) {
157166
/* you can use this method to add your logic to handle reconnection timer ticks */
158-
console.log('Reconnection tick', event)
167+
console.info('Reconnection tick', event)
159168
}
160169
handleReconnectionEnd() {
161170
/* you can use this method to add your logic to handle reconnection initiation. */
162-
console.log('Reconnection will happen now...')
171+
console.info('Reconnection will happen now...')
163172
}
164173
attributeChangedCallback(name, oldVal, newVal) {
165174
this.targetSelector = newVal;
166-
this._bindToElement();
167-
this._subscribeToEvents();
175+
this.bindToElement(this);
176+
this.subscribeToEvents(this);
168177
}
169178
}
170179
customElements.define('palindrom-error-catcher', PalindromErrorCatcher)

test/index.html

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
<!DOCTYPE html>
2-
<html><head>
3-
<meta charset="utf-8">
4-
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
5-
<script src="../../web-component-tester/browser.js"></script>
6-
</head>
7-
<body>
8-
<script>
9-
WCT.loadSuites([
10-
'simple.html'
11-
]);
12-
</script>
2+
<html>
133

4+
<head>
5+
<meta charset="utf-8">
6+
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
7+
<script src="../../web-component-tester/browser.js"></script>
8+
</head>
149

15-
</body></html>
10+
<body>
11+
<script>
12+
WCT.loadSuites([
13+
'simple.html'
14+
]);
15+
</script>
16+
</body>
17+
18+
</html>

test/simple.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,25 @@
2525
</test-fixture>
2626
<script>
2727
describe('palindrom-error-catcher', function () {
28-
var textFixture, ppclient, palindromErrorCatcher, errorCatcherShadowRoot;
28+
var textFixture, ppclient, palindromErrorCatcher, errorCatcherShadowRoot, originalConsoleError;
2929
before(function () {
3030
chai.use(palindromChaiPlugin);
31+
/* for some strange reason I couldn't find a trace for,
32+
when console.error is called, mocha assumes an exception happened,
33+
even if it is intended. This fixes the issue but doesn't affect the test scenario */
34+
originalConsoleError = console.error;
35+
console.error = function noop () {};
3136
});
37+
after(function() {
38+
// just to be clean
39+
console.error = originalConsoleError;
40+
})
3241
beforeEach(function () {
3342
textFixture = fixture('text-fixture');
3443
ppclient = textFixture[0];
3544
palindromErrorCatcher = textFixture[1];
3645
errorCatcherShadowRoot = palindromErrorCatcher.shadowRoot;
46+
3747
});
3848
describe('Connection errors: UI', function () {
3949
it('Should show UI after connection error', function () {

0 commit comments

Comments
 (0)