-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: improve test coverage for DataProviderController (#7546)
- Loading branch information
Showing
3 changed files
with
163 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/component-base/test/data-provider-controller-placeholder.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { expect } from '@esm-bundle/chai'; | ||
import sinon from 'sinon'; | ||
import '@vaadin/testing-helpers'; | ||
import { DataProviderController } from '../src/data-provider-controller/data-provider-controller.js'; | ||
import { createDataProvider } from './data-provider-controller-helpers.js'; | ||
|
||
class Placeholder {} | ||
|
||
class CustomPlaceholder extends Placeholder {} | ||
|
||
describe('DataProviderController - placeholder', () => { | ||
let host, controller, placeholder, dataProviderSpy; | ||
|
||
describe('default', () => { | ||
beforeEach(() => { | ||
host = document.createElement('div'); | ||
|
||
placeholder = new Placeholder(); | ||
|
||
controller = new DataProviderController(host, { | ||
size: 10, | ||
pageSize: 2, | ||
placeholder, | ||
dataProvider: createDataProvider({ size: 10, async: true }), | ||
}); | ||
|
||
dataProviderSpy = sinon.spy(controller, 'dataProvider'); | ||
}); | ||
|
||
it('should have placeholder', () => { | ||
expect(controller.placeholder).to.equal(placeholder); | ||
}); | ||
|
||
it('should have rootCache items filled with placeholders', () => { | ||
expect(controller.rootCache.items).to.have.lengthOf(10); | ||
expect(controller.rootCache.items.every((item) => item === placeholder)).to.be.true; | ||
}); | ||
|
||
it('should request data when encountering the controller placeholder instance', () => { | ||
controller.ensureFlatIndexLoaded(0); | ||
expect(dataProviderSpy).to.have.callCount(1); | ||
|
||
controller.ensureFlatIndexLoaded(1); | ||
expect(dataProviderSpy).to.have.callCount(1); | ||
|
||
controller.ensureFlatIndexLoaded(2); | ||
expect(dataProviderSpy).to.have.callCount(2); | ||
|
||
controller.ensureFlatIndexLoaded(3); | ||
expect(dataProviderSpy).to.have.callCount(2); | ||
}); | ||
|
||
it('should not request data when encountering a different placeholder instance', () => { | ||
controller.rootCache.items[0] = new CustomPlaceholder(); | ||
controller.ensureFlatIndexLoaded(0); | ||
expect(dataProviderSpy).to.have.callCount(0); | ||
}); | ||
}); | ||
|
||
describe('custom equality function', () => { | ||
beforeEach(() => { | ||
host = document.createElement('div'); | ||
|
||
controller = new DataProviderController(host, { | ||
size: 10, | ||
pageSize: 2, | ||
placeholder: new Placeholder(), | ||
isPlaceholder: (item) => item instanceof Placeholder, | ||
dataProvider: createDataProvider({ size: 10, async: true }), | ||
}); | ||
|
||
dataProviderSpy = sinon.spy(controller, 'dataProvider'); | ||
}); | ||
|
||
it('should request data when encountering any placeholder instance', () => { | ||
controller.rootCache.items[0] = new CustomPlaceholder(); | ||
|
||
controller.ensureFlatIndexLoaded(0); | ||
expect(dataProviderSpy).to.have.callCount(1); | ||
|
||
controller.ensureFlatIndexLoaded(2); | ||
expect(dataProviderSpy).to.have.callCount(2); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters