|
| 1 | +import { module, test } from 'qunit'; |
| 2 | +import { setupRenderingTest } from 'ember-qunit'; |
| 3 | +import { render } from '@ember/test-helpers'; |
| 4 | +import { hbs } from 'ember-cli-htmlbars'; |
| 5 | +import sinon from 'sinon'; |
| 6 | + |
| 7 | +module( |
| 8 | + 'Integration | Modifier | scroll-to-element-with-offset', |
| 9 | + function (hooks) { |
| 10 | + setupRenderingTest(hooks); |
| 11 | + const sandbox = sinon.createSandbox(); |
| 12 | + |
| 13 | + hooks.beforeEach(function () { |
| 14 | + this.scrollToSpy = sandbox.spy(window, 'scrollTo'); |
| 15 | + this.getBoundingClientRectStub = sandbox.stub( |
| 16 | + Element.prototype, |
| 17 | + 'getBoundingClientRect' |
| 18 | + ); |
| 19 | + this.getBoundingClientRectStub.onCall(0).returns({ top: 100 }); |
| 20 | + this.getBoundingClientRectStub.onCall(1).returns({ top: 25 }); |
| 21 | + }); |
| 22 | + |
| 23 | + hooks.afterEach(function () { |
| 24 | + this.scrollToSpy = null; |
| 25 | + sandbox.restore(); |
| 26 | + }); |
| 27 | + |
| 28 | + test('it renders and calls scrollTo when shouldScroll is true', async function (assert) { |
| 29 | + await render( |
| 30 | + hbs`<div {{scroll-to-element-with-offset shouldScroll=true}}></div>` |
| 31 | + ); |
| 32 | + |
| 33 | + assert.ok(this.scrollToSpy.called, 'scrollTo was called'); |
| 34 | + }); |
| 35 | + |
| 36 | + test('it renders and passes default options to scrollTo', async function (assert) { |
| 37 | + await render( |
| 38 | + hbs`<div {{scroll-to-element-with-offset shouldScroll=true}}></div>` |
| 39 | + ); |
| 40 | + |
| 41 | + assert.deepEqual( |
| 42 | + this.scrollToSpy.args[0][0].behavior, |
| 43 | + 'smooth', |
| 44 | + 'scrollTo was called with correct params' |
| 45 | + ); |
| 46 | + |
| 47 | + assert.deepEqual( |
| 48 | + this.scrollToSpy.args[0][0].left, |
| 49 | + 0, |
| 50 | + 'scrollTo was called with correct params' |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + test('it renders and calculates correct default top offset for scrollTo', async function (assert) { |
| 55 | + await render( |
| 56 | + hbs`<div id="test" {{scroll-to-element-with-offset shouldScroll=true}}></div>` |
| 57 | + ); |
| 58 | + |
| 59 | + assert.deepEqual( |
| 60 | + this.scrollToSpy.args[0][0], |
| 61 | + { |
| 62 | + behavior: 'smooth', |
| 63 | + left: 0, |
| 64 | + top: 75, |
| 65 | + }, |
| 66 | + 'scrollTo was called with correct params' |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + test('it renders and calculates correct top offset for scrollTo when offset is passed in', async function (assert) { |
| 71 | + this.options = { |
| 72 | + offset: 50, |
| 73 | + }; |
| 74 | + |
| 75 | + await render( |
| 76 | + hbs`<div id="test" {{scroll-to-element-with-offset shouldScroll=true options=this.options}}></div>` |
| 77 | + ); |
| 78 | + |
| 79 | + assert.deepEqual( |
| 80 | + this.scrollToSpy.args[0][0], |
| 81 | + { |
| 82 | + behavior: 'smooth', |
| 83 | + left: 0, |
| 84 | + top: 25, |
| 85 | + }, |
| 86 | + 'scrollTo was called with correct params' |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + test('it does not call scrollTo when shouldScroll is false', async function (assert) { |
| 91 | + await render( |
| 92 | + hbs`<div {{scroll-to-element-with-offset shouldScroll=false}}></div>` |
| 93 | + ); |
| 94 | + |
| 95 | + assert.notOk(this.scrollToSpy.called, 'scrollTo was not called'); |
| 96 | + }); |
| 97 | + |
| 98 | + test('it renders when shouldScroll resolves to true', async function (assert) { |
| 99 | + this.options = { test: true }; |
| 100 | + let resolvePromise; |
| 101 | + this.shouldScroll = new Promise((resolve) => (resolvePromise = resolve)); |
| 102 | + |
| 103 | + await render( |
| 104 | + hbs`<div {{scroll-to-element-with-offset shouldScroll=this.shouldScroll}}></div>` |
| 105 | + ); |
| 106 | + |
| 107 | + assert.ok(this.scrollToSpy.notCalled, 'scrollTo was not called'); |
| 108 | + |
| 109 | + await resolvePromise(true); |
| 110 | + |
| 111 | + assert.ok(this.scrollToSpy.called, 'scrollTo was called'); |
| 112 | + }); |
| 113 | + |
| 114 | + test('it does not render when shouldScroll resolves to false', async function (assert) { |
| 115 | + this.options = { test: true }; |
| 116 | + let resolvePromise; |
| 117 | + this.shouldScroll = new Promise((resolve) => (resolvePromise = resolve)); |
| 118 | + |
| 119 | + await render( |
| 120 | + hbs`<div {{scroll-to-element-with-offset shouldScroll=this.shouldScroll}}></div>` |
| 121 | + ); |
| 122 | + |
| 123 | + await resolvePromise(false); |
| 124 | + |
| 125 | + assert.ok(this.scrollToSpy.notCalled, 'scrollTo was not called'); |
| 126 | + }); |
| 127 | + } |
| 128 | +); |
0 commit comments