|
| 1 | +import { module, test } from 'qunit'; |
| 2 | +import { setupRenderingTest } from 'ember-qunit'; |
| 3 | +import { render } from '@ember/test-helpers'; |
| 4 | +import hbs from 'htmlbars-inline-precompile'; |
| 5 | +import sinon from 'sinon'; |
| 6 | +import mockDidIntersect from 'ember-scroll-modifiers/test-support/did-intersect-mock'; |
| 7 | + |
| 8 | +module( |
| 9 | + 'Integration | Modifier | addon-test-support/did-intersect-mock', |
| 10 | + function (hooks) { |
| 11 | + setupRenderingTest(hooks); |
| 12 | + |
| 13 | + hooks.beforeEach(function () { |
| 14 | + this.didIntersectMock = mockDidIntersect(sinon); |
| 15 | + this.enterStub = sinon.stub(); |
| 16 | + this.exitStub = sinon.stub(); |
| 17 | + this.maxEnter = 1; |
| 18 | + this.maxExit = 1; |
| 19 | + }); |
| 20 | + |
| 21 | + test('Did intersect mock triggers onEnter correctly', async function (assert) { |
| 22 | + assert.expect(2); |
| 23 | + |
| 24 | + await render( |
| 25 | + hbs`<div data-test-did-intersect {{did-intersect onEnter=this.enterStub onExit=this.exitStub}}></div>` |
| 26 | + ); |
| 27 | + await this.didIntersectMock.enter('[data-test-did-intersect]'); |
| 28 | + |
| 29 | + assert.ok(this.enterStub.calledOnce); |
| 30 | + assert.notOk(this.exitStub.calledOnce); |
| 31 | + }); |
| 32 | + |
| 33 | + test('Did intersect mock triggers onExit correctly', async function (assert) { |
| 34 | + assert.expect(2); |
| 35 | + |
| 36 | + await render( |
| 37 | + hbs`<div data-test-did-intersect {{did-intersect onEnter=this.enterStub onExit=this.exitStub}}></div>` |
| 38 | + ); |
| 39 | + await this.didIntersectMock.exit('[data-test-did-intersect]'); |
| 40 | + |
| 41 | + assert.notOk(this.enterStub.calledOnce); |
| 42 | + assert.ok(this.exitStub.calledOnce); |
| 43 | + }); |
| 44 | + |
| 45 | + test('Did intersect mock triggers onExit never exceeds maxEnter if maxEnter is provided', async function (assert) { |
| 46 | + assert.expect(1); |
| 47 | + |
| 48 | + await render( |
| 49 | + hbs`<div data-test-did-intersect {{did-intersect onEnter=this.enterStub onExit=this.exitStub maxEnter=this.maxEnter}}></div>` |
| 50 | + ); |
| 51 | + |
| 52 | + for (let i = 0; i < this.maxEnter + 1; i++) { |
| 53 | + await this.didIntersectMock.enter('[data-test-did-intersect]'); |
| 54 | + } |
| 55 | + |
| 56 | + assert.equal(this.enterStub.callCount, this.maxEnter); |
| 57 | + }); |
| 58 | + |
| 59 | + test('Did intersect mock triggers onExit never exceeds maxExit if maxExit is provided', async function (assert) { |
| 60 | + assert.expect(1); |
| 61 | + |
| 62 | + await render( |
| 63 | + hbs`<div data-test-did-intersect {{did-intersect onEnter=this.enterStub onExit=this.exitStub maxExit=this.maxExit}}></div>` |
| 64 | + ); |
| 65 | + |
| 66 | + for (let i = 0; i < this.maxExit + 1; i++) { |
| 67 | + await this.didIntersectMock.exit('[data-test-did-intersect]'); |
| 68 | + } |
| 69 | + |
| 70 | + assert.equal(this.exitStub.callCount, this.maxExit); |
| 71 | + }); |
| 72 | + |
| 73 | + test('Did intersect mock fire without limit if maxEnter and maxExit is not provided', async function (assert) { |
| 74 | + assert.expect(2); |
| 75 | + |
| 76 | + await render( |
| 77 | + hbs`<div data-test-did-intersect {{did-intersect onEnter=this.enterStub onExit=this.exitStub}}></div>` |
| 78 | + ); |
| 79 | + |
| 80 | + const numOfFiredCallback = this.maxEnter + this.maxExit; |
| 81 | + |
| 82 | + for (let i = 0; i < numOfFiredCallback; i++) { |
| 83 | + this.didIntersectMock.enter('[data-test-did-intersect]'); |
| 84 | + this.didIntersectMock.exit('[data-test-did-intersect]'); |
| 85 | + } |
| 86 | + |
| 87 | + assert.equal( |
| 88 | + this.enterStub.callCount, |
| 89 | + numOfFiredCallback, |
| 90 | + 'Enter callback has fired more than maxEnter times' |
| 91 | + ); |
| 92 | + assert.equal( |
| 93 | + this.exitStub.callCount, |
| 94 | + numOfFiredCallback, |
| 95 | + 'Exit callback has fired more than maxExit times' |
| 96 | + ); |
| 97 | + }); |
| 98 | + } |
| 99 | +); |
0 commit comments