Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #29 from PolymerElements/fix-events
Browse files Browse the repository at this point in the history
clean up shared code and add tests
  • Loading branch information
notwaldorf committed Nov 17, 2015
2 parents 0326b92 + 32dd028 commit 6d24f0c
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 24 deletions.
6 changes: 4 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
"iron-icons": "PolymerElements/iron-icons#^1.0.0",
"paper-checkbox": "PolymerElements/paper-checkbox#^1.0.0",
"paper-icon-button": "PolymerElements/paper-icon-button#^1.0.0",
"paper-input": "PolymerElements/paper-input#^1.0.0",
"paper-toggle-button": "PolymerElements/paper-toggle-button#^1.0.0",
"test-fixture": "PolymerElements/test-fixture#^1.0.0",
"web-component-tester": "Polymer/web-component-tester#^3.3.0",
"webcomponentsjs": "webcomponents/webcomponentsjs#^0.7.0"
"web-component-tester": "Polymer/web-component-tester#^3.4.0",
"webcomponentsjs": "webcomponents/webcomponentsjs#^0.7.0",
"iron-test-helpers": "PolymerElements/iron-test-helpers#^1.0.0"
}
}
4 changes: 3 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<link rel="import" href="../../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../../paper-styles/demo-pages.html">

<link rel="import" href="../../paper-input/paper-input.html">

<style is="custom-style">
.list {
padding-top: 12px;
Expand Down Expand Up @@ -88,7 +90,7 @@
<div>
<h4>Single line items</h4>
<div class="list short" role="listbox">
<paper-item>Inbox</paper-item>
<paper-item><paper-input></paper-input></paper-item>
<paper-item>Starred</paper-item>
<paper-item>Sent mail</paper-item>
<paper-item>Drafts</paper-item>
Expand Down
13 changes: 3 additions & 10 deletions paper-icon-item.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
<link rel="import" href="../iron-behaviors/iron-button-state.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../paper-styles/typography.html">

<link rel="import" href="paper-item-behavior.html">
<link rel="import" href="paper-item-shared-styles.html">

<!--
`<paper-icon-item>` is a convenience element to make an item with icon. It is a non interactive list
`<paper-icon-item>` is a convenience element to make an item with icon. It is an interactive list
item with a fixed-width icon area, according to Material Design. This is useful if the icons are of
varying widths, but you want the item bodies to line up. Use this like a `<paper-item>`. The child
node with the attribute `item-icon` is placed in the icon area.
Expand Down Expand Up @@ -45,7 +45,6 @@
`--paper-item-disabled` | Mixin applied to disabled paper-items | `{}`
`--paper-item-focused` | Mixin applied to focused paper-items | `{}`
`--paper-item-focused-before` | Mixin applied to :before focused paper-items | `{}`
-->

<dom-module id="paper-icon-item">
Expand Down Expand Up @@ -78,14 +77,8 @@
Polymer({
is: 'paper-icon-item',

hostAttributes: {
'role': 'option',
'tabindex': '0'
},

behaviors: [
Polymer.IronControlState,
Polymer.IronButtonState
Polymer.PaperItemBehavior
]
});
</script>
Expand Down
37 changes: 37 additions & 0 deletions paper-item-behavior.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!--
@license
Copyright (c) 2015 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
-->

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-behaviors/iron-control-state.html">
<link rel="import" href="../iron-behaviors/iron-button-state.html">

<!--
`PaperItemBehavior` is a convenience behavior shared by <paper-item> and
<paper-icon-item> that manages the shared control states and attributes of
the items.
-->

<script>
/** @polymerBehavior Polymer.PaperItemBehavior */
Polymer.PaperItemBehaviorImpl = {
hostAttributes: {
role: 'option',
tabindex: '0'
}
};

/** @polymerBehavior */
Polymer.PaperItemBehavior = [
Polymer.IronControlState,
Polymer.IronButtonState,
Polymer.PaperItemBehaviorImpl
];

</script>
12 changes: 3 additions & 9 deletions paper-item.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
<link rel="import" href="../iron-behaviors/iron-button-state.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../paper-styles/default-theme.html">
<link rel="import" href="paper-item-behavior.html">
<link rel="import" href="paper-item-shared-styles.html">

<!--
Material design: [Lists](https://www.google.com/design/spec/components/lists.html)
`<paper-item>` is a non-interactive list item. By default, it is a horizontal flexbox.
`<paper-item>` is an interactive list item. By default, it is a horizontal flexbox.
<paper-item>Item</paper-item>
Expand Down Expand Up @@ -48,7 +49,6 @@
`--paper-item-focused` | Mixin applied to focused paper-items | `{}`
`--paper-item-focused-before` | Mixin applied to :before focused paper-items | `{}`
### Accessibility
This element has `role="listitem"` by default. Depending on usage, it may be more appropriate to set
Expand Down Expand Up @@ -86,14 +86,8 @@
Polymer({
is: 'paper-item',

hostAttributes: {
role: 'option',
tabindex: '0'
},

behaviors: [
Polymer.IronControlState,
Polymer.IronButtonState
Polymer.PaperItemBehavior
]
});
</script>
Expand Down
136 changes: 134 additions & 2 deletions test/paper-item.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>

<script src="../../web-component-tester/browser.js"></script>
<script src="../../test-fixture/test-fixture-mocha.js"></script>
<script src="../../iron-test-helpers/mock-interactions.js"></script>

<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="../../paper-input/paper-input.html">
<link rel="import" href="../paper-item.html">
<link rel="import" href="../paper-icon-item.html">

Expand All @@ -45,7 +45,139 @@
</template>
</test-fixture>

<test-fixture id="item-with-input">
<template>
<div role="list">
<paper-item><input></paper-item>
</div>
</template>
</test-fixture>

<test-fixture id="item-with-paper-input">
<template>
<div role="list">
<paper-item><paper-input></paper-input></paper-item>
</div>
</template>
</test-fixture>

<test-fixture id="iconItem-with-input">
<template>
<div role="list">
<paper-icon-item><input></paper-icon-item>
</div>
</template>
</test-fixture>

<script>
suite('paper-item basic', function() {
var item, clickHandler;

setup(function() {
item = fixture('item').querySelector('paper-item');
clickHandler = sinon.spy();
item.addEventListener('click', clickHandler);
});

test('space triggers a click event', function(done) {
MockInteractions.pressSpace(item);
Polymer.Base.async(function(){
// You need two ticks, one for the MockInteractions event, and one
// for the button event.
Polymer.Base.async(function(){
expect(clickHandler.callCount).to.be.equal(1);
done();
}, 1);
}, 1);
});

test('click triggers a click event', function(done) {
MockInteractions.tap(item);
Polymer.Base.async(function(){
expect(clickHandler.callCount).to.be.equal(1);
done();
}, 1);
});
});

suite('paper-icon-item basic', function() {
var item, clickHandler;

setup(function() {
item = fixture('iconItem').querySelector('paper-icon-item');
clickHandler = sinon.spy();
item.addEventListener('click', clickHandler);
});

test('space triggers a click event', function(done) {
MockInteractions.pressSpace(item);
Polymer.Base.async(function(){
// You need two ticks, one for the MockInteractions event, and one
// for the button event.
Polymer.Base.async(function(){
expect(clickHandler.callCount).to.be.equal(1);
done();
}, 1);
}, 1);
});

test('click triggers a click event', function(done) {
MockInteractions.tap(item);
Polymer.Base.async(function(){
expect(clickHandler.callCount).to.be.equal(1);
done();
}, 1);
});
});

suite('clickable element inside item', function() {
test('paper-item: space in child native input does not trigger a click event', function(done) {
var f = fixture('item-with-input');
var outerItem = f.querySelector('paper-item');
var innerInput = f.querySelector('input');

var itemClickHandler = sinon.spy();
outerItem.addEventListener('click', itemClickHandler);

innerInput.focus();
MockInteractions.pressSpace(innerInput);
Polymer.Base.async(function(){
expect(itemClickHandler.callCount).to.be.equal(0);
done();
}, 1);
});

test('paper-item: space in child paper-input does not trigger a click event', function(done) {
var f = fixture('item-with-paper-input');
var outerItem = f.querySelector('paper-item');
var innerInput = f.querySelector('paper-input');

var itemClickHandler = sinon.spy();
outerItem.addEventListener('click', itemClickHandler);

innerInput.focus();
MockInteractions.pressSpace(innerInput);
Polymer.Base.async(function(){
expect(itemClickHandler.callCount).to.be.equal(0);
done();
}, 1);
});

test('paper-icon-item: space in child input does not trigger a click event', function(done) {
var f = fixture('iconItem-with-input');
var outerItem = f.querySelector('paper-icon-item');
var innerInput = f.querySelector('input');

var itemClickHandler = sinon.spy();
outerItem.addEventListener('click', itemClickHandler);

MockInteractions.pressSpace(innerInput);
Polymer.Base.async(function(){
expect(itemClickHandler.callCount).to.be.equal(0);
done();
}, 1);
});
});

suite('item a11y tests', function() {
var item, iconItem;
Expand Down

0 comments on commit 6d24f0c

Please sign in to comment.