pwa-helpers
Version:
Small helper methods or mixins to help you build web apps.
111 lines (100 loc) • 3.61 kB
HTML
<!--
@license
Copyright (c) 2018 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
-->
<html lang="en">
<head>
<meta charset="utf-8">
<title>connect-mixin</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script>
<script src="../node_modules/chai/chai.js"></script>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/wct-mocha/wct-mocha.js"></script>
</head>
<body>
<test-fixture id="basic">
<template>
<div>
<unconnected-element></unconnected-element>
<connected-element></connected-element>
</div>
</template>
</test-fixture>
<script>
// Redux assumes `process.env.NODE_ENV` exists in the ES module build.
// https://github.com/reactjs/redux/issues/2907
window.process = { env: { NODE_ENV: 'production' } };
</script>
<script type="module">
import '@polymer/test-fixture';
import { connect } from '../connect-mixin.js';
import { createStore } from 'redux';
const store = createStore(
(state, action) => {
switch (action.type) {
case 'INCREMENT':
return {
...state,
count: state.count + action.value
};
default:
return state;
}
},
{ count: 5 });
class UnconnectedElement extends HTMLElement {
constructor() {
super();
this.count = 0;
}
increment(value) {
this.dispatchEvent(new CustomEvent('increment', { detail: value }));
}
}
customElements.define('unconnected-element', UnconnectedElement);
class ConnectedElement extends connect(store)(UnconnectedElement) {
stateChanged(state) {
this.count = state.count;
}
increment(value) {
store.dispatch({
type: 'INCREMENT',
value
});
}
}
customElements.define('connected-element', ConnectedElement);
suite('connect-mixin tests', () => {
let unconnected, connected;
setup(() => {
const div = fixture('basic');
unconnected = div.querySelector('unconnected-element');
connected = div.querySelector('connected-element');
});
test('unconnected element uses default implementation', () => {
assert.equal(unconnected.count, 0);
let value = 0;
unconnected.addEventListener('increment', e => value = e.detail);
unconnected.increment(42);
assert.equal(value, 42);
assert.equal(unconnected.count, 0);
});
test('connected element is connected to the store', () => {
assert.equal(connected.count, 5);
connected.addEventListener('increment', () => {
throw new Error('should not be called');
});
connected.increment(42);
assert.equal(connected.count, 47);
});
});
</script>
</body>
</html>