|
| 1 | +import { moduleForComponent, test } from 'ember-qunit'; |
| 2 | +import hbs from 'htmlbars-inline-precompile'; |
| 3 | +import { tap } from 'ember-native-dom-helpers/test-support/helpers'; |
| 4 | + |
| 5 | + |
| 6 | +moduleForComponent('tap', 'Integration | Test Helper | tap', { |
| 7 | + integration: true |
| 8 | +}); |
| 9 | + |
| 10 | +// Note: This test will fail in desktop browsers as it doesn't fire |
| 11 | +// touch events unless device emulation mode is enabled. |
| 12 | +test('It fires touchstart, touchend, and then the click sequence(mousedown -> focus -> mouseup -> click) events on the element with the given selector (in that order)', function(assert) { |
| 13 | + assert.expect(18); |
| 14 | + let index = 0; |
| 15 | + this.onTouchStart = (e) => { |
| 16 | + assert.equal(++index, 1, 'touchstart is fired first'); |
| 17 | + assert.ok(true, 'a touchstart event is fired'); |
| 18 | + assert.ok(e instanceof window.Event, 'It receives a native event'); |
| 19 | + } |
| 20 | + this.onTouchEnd = (e) => { |
| 21 | + assert.equal(++index, 2, 'touchend is fired second'); |
| 22 | + assert.ok(true, 'a touchend event is fired'); |
| 23 | + assert.ok(e instanceof window.Event, 'It receives a native event'); |
| 24 | + } |
| 25 | + this.onMouseDown = (e) => { |
| 26 | + assert.equal(++index, 3, 'mousedown is fired third'); |
| 27 | + assert.ok(true, 'a mousedown event is fired'); |
| 28 | + assert.ok(e instanceof window.Event, 'It receives a native event'); |
| 29 | + } |
| 30 | + this.onFocus = (e) => { |
| 31 | + assert.equal(++index, 4, 'focus is fired forth'); |
| 32 | + assert.ok(true, 'a focus event is fired'); |
| 33 | + assert.ok(e instanceof window.Event, 'It receives a native event'); |
| 34 | + } |
| 35 | + this.onMouseUp = (e) => { |
| 36 | + assert.equal(++index, 5, 'mouseup is fired fifth'); |
| 37 | + assert.ok(true, 'a mouseup event is fired'); |
| 38 | + assert.ok(e instanceof window.Event, 'It receives a native event'); |
| 39 | + } |
| 40 | + this.onClick = (e) => { |
| 41 | + assert.equal(++index, 6, 'click is fired sixth'); |
| 42 | + assert.ok(true, 'a click event is fired'); |
| 43 | + assert.ok(e instanceof window.Event, 'It receives a native event'); |
| 44 | + } |
| 45 | + |
| 46 | + this.render(hbs` |
| 47 | + <input class="target-element" |
| 48 | + ontouchstart={{onTouchStart}} |
| 49 | + ontouchend={{onTouchEnd}} |
| 50 | + onmouseup={{onMouseUp}} |
| 51 | + onfocus={{onFocus}} |
| 52 | + onclick={{onClick}} |
| 53 | + onmousedown={{onMouseDown}} /> |
| 54 | + `); |
| 55 | + |
| 56 | + tap('.target-element'); |
| 57 | +}); |
| 58 | + |
| 59 | +test('It the touchstart event is defaultPrevented, the focus and mouse events are not fired', function(assert) { |
| 60 | + assert.expect(6); |
| 61 | + let index = 0; |
| 62 | + this.onTouchStart = (e) => { |
| 63 | + assert.equal(++index, 1, 'touchstart is fired first'); |
| 64 | + assert.ok(true, 'a touchstart event is fired'); |
| 65 | + assert.ok(e instanceof window.Event, 'It receives a native event'); |
| 66 | + e.preventDefault(); |
| 67 | + } |
| 68 | + this.onTouchEnd = (e) => { |
| 69 | + assert.equal(++index, 2, 'touchend is fired second'); |
| 70 | + assert.ok(true, 'a touchend event is fired'); |
| 71 | + assert.ok(e instanceof window.Event, 'It receives a native event'); |
| 72 | + } |
| 73 | + this.onMouseDown = () => assert.ok(false, 'mousedown should not be fired'); |
| 74 | + this.onFocus = () => assert.ok(false, 'focus should not be fired'); |
| 75 | + this.onMouseUp = () => assert.ok(false, 'mouseup should not be fired'); |
| 76 | + this.onClick = () => assert.ok(false, 'click should not be fired'); |
| 77 | + |
| 78 | + this.render(hbs` |
| 79 | + <input class="target-element" |
| 80 | + ontouchstart={{onTouchStart}} |
| 81 | + ontouchend={{onTouchEnd}} |
| 82 | + onmouseup={{onMouseUp}} |
| 83 | + onfocus={{onFocus}} |
| 84 | + onclick={{onClick}} |
| 85 | + onmousedown={{onMouseDown}} /> |
| 86 | + `); |
| 87 | + |
| 88 | + tap('.target-element'); |
| 89 | +}); |
| 90 | + |
| 91 | + |
| 92 | +test('It the touchend event is defaultPrevented, the focus and mouse events are not fired', function(assert) { |
| 93 | + assert.expect(6); |
| 94 | + let index = 0; |
| 95 | + this.onTouchStart = (e) => { |
| 96 | + assert.equal(++index, 1, 'touchstart is fired first'); |
| 97 | + assert.ok(true, 'a touchstart event is fired'); |
| 98 | + assert.ok(e instanceof window.Event, 'It receives a native event'); |
| 99 | + } |
| 100 | + this.onTouchEnd = (e) => { |
| 101 | + assert.equal(++index, 2, 'touchend is fired second'); |
| 102 | + assert.ok(true, 'a touchend event is fired'); |
| 103 | + assert.ok(e instanceof window.Event, 'It receives a native event'); |
| 104 | + e.preventDefault(); |
| 105 | + } |
| 106 | + this.onMouseDown = () => assert.ok(false, 'mousedown should not be fired'); |
| 107 | + this.onFocus = () => assert.ok(false, 'focus should not be fired'); |
| 108 | + this.onMouseUp = () => assert.ok(false, 'mouseup should not be fired'); |
| 109 | + this.onClick = () => assert.ok(false, 'click should not be fired'); |
| 110 | + |
| 111 | + this.render(hbs` |
| 112 | + <input class="target-element" |
| 113 | + ontouchstart={{onTouchStart}} |
| 114 | + ontouchend={{onTouchEnd}} |
| 115 | + onmouseup={{onMouseUp}} |
| 116 | + onfocus={{onFocus}} |
| 117 | + onclick={{onClick}} |
| 118 | + onmousedown={{onMouseDown}} /> |
| 119 | + `); |
| 120 | + |
| 121 | + tap('.target-element'); |
| 122 | +}); |
0 commit comments