-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: extract data provider filtering tests (#7464)
- Loading branch information
Showing
4 changed files
with
142 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import './not-animated-styles.js'; | ||
import '@vaadin/text-field/vaadin-lit-text-field.js'; | ||
import '../src/vaadin-lit-combo-box-light.js'; | ||
import '../src/vaadin-lit-combo-box.js'; | ||
import './data-provider-filtering.common.js'; |
5 changes: 5 additions & 0 deletions
5
packages/combo-box/test/data-provider-filtering-polymer.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,5 @@ | ||
import './not-animated-styles.js'; | ||
import '@vaadin/text-field/vaadin-text-field.js'; | ||
import '../vaadin-combo-box-light.js'; | ||
import '../vaadin-combo-box.js'; | ||
import './data-provider-filtering.common.js'; |
132 changes: 132 additions & 0 deletions
132
packages/combo-box/test/data-provider-filtering.common.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,132 @@ | ||
import { expect } from '@esm-bundle/chai'; | ||
import { aTimeout, fixtureSync, nextRender } from '@vaadin/testing-helpers'; | ||
import sinon from 'sinon'; | ||
import { setInputValue } from './helpers.js'; | ||
|
||
const TEMPLATES = { | ||
'vaadin-combo-box': ` | ||
<vaadin-combo-box></vaadin-combo-box> | ||
`, | ||
'vaadin-combo-box-light': ` | ||
<vaadin-combo-box-light> | ||
<vaadin-text-field></vaadin-text-field> | ||
</vaadin-combo-box-light> | ||
`, | ||
}; | ||
|
||
['vaadin-combo-box', 'vaadin-combo-box-light'].forEach((tag) => { | ||
describe(`data provider filtering - ${tag}`, () => { | ||
let comboBox; | ||
|
||
describe('basic', () => { | ||
beforeEach(async () => { | ||
comboBox = fixtureSync(TEMPLATES[tag]); | ||
await nextRender(); | ||
|
||
comboBox.dataProvider = sinon.spy(async (params, callback) => { | ||
const items = ['Item 1', 'Item 2', 'Item 3']; | ||
const filteredItems = items.filter((item) => item.includes(params.filter)); | ||
await aTimeout(0); | ||
callback(filteredItems, filteredItems.length); | ||
}); | ||
comboBox.opened = true; | ||
await aTimeout(0); | ||
}); | ||
|
||
it('should make request with empty filter when opened', () => { | ||
const params = comboBox.dataProvider.lastCall.args[0]; | ||
expect(params.filter).to.equal(''); | ||
}); | ||
|
||
it('should make request with entered filter on filter change', () => { | ||
comboBox.dataProvider.resetHistory(); | ||
setInputValue(comboBox, 'Item'); | ||
expect(comboBox.dataProvider).to.be.called; | ||
const params = comboBox.dataProvider.lastCall.args[0]; | ||
expect(params.filter).to.equal('Item'); | ||
}); | ||
|
||
it('should make request with empty filter on value change', async () => { | ||
setInputValue(comboBox, 'Item'); | ||
await aTimeout(0); | ||
comboBox.dataProvider.resetHistory(); | ||
comboBox.value = 'Item 2'; | ||
const params = comboBox.dataProvider.lastCall.args[0]; | ||
expect(params.filter).to.equal(''); | ||
}); | ||
|
||
it('should make request with empty filter after partial filter & cancel & reopen', async () => { | ||
setInputValue(comboBox, 'It'); | ||
await aTimeout(0); | ||
comboBox.dataProvider.resetHistory(); | ||
comboBox.cancel(); | ||
comboBox.opened = true; | ||
await aTimeout(0); | ||
const params = comboBox.dataProvider.lastCall.args[0]; | ||
expect(params.filter).to.equal(''); | ||
}); | ||
|
||
it('should clear filter on value clear', async () => { | ||
setInputValue(comboBox, 'Item'); | ||
await aTimeout(0); | ||
comboBox.value = 'Item 1'; | ||
comboBox.value = ''; | ||
expect(comboBox.filter).to.equal(''); | ||
}); | ||
|
||
it('should clear filter on close', async () => { | ||
setInputValue(comboBox, 'Item'); | ||
await aTimeout(0); | ||
comboBox.opened = false; | ||
expect(comboBox.filter).to.equal(''); | ||
}); | ||
}); | ||
|
||
describe('dropdown behaviour', () => { | ||
let openedSpy; | ||
|
||
beforeEach(async () => { | ||
comboBox = fixtureSync(TEMPLATES[tag]); | ||
await nextRender(); | ||
|
||
comboBox.dataProvider = sinon.spy((_params, callback) => { | ||
const items = ['Item 1', 'Item 2', 'Item 3']; | ||
const filteredItems = items.filter((item) => item.includes(comboBox.filter)); | ||
callback(filteredItems, filteredItems.length); | ||
}); | ||
comboBox.opened = true; | ||
comboBox.dataProvider.resetHistory(); | ||
|
||
openedSpy = sinon.spy(); | ||
comboBox.addEventListener('vaadin-combo-box-dropdown-opened', openedSpy); | ||
}); | ||
|
||
it('should not toggle between opened and closed when filtering', () => { | ||
// Filter for something that should return results | ||
comboBox.filter = 'Item'; | ||
// Verify data provider has been called | ||
expect(comboBox.dataProvider).to.be.calledOnce; | ||
// Dropdown should not have been closed and re-opened | ||
expect(openedSpy).to.be.not.called; | ||
}); | ||
|
||
it('should not toggle between opened and closed when setting a value', () => { | ||
// Filter for something that should return results | ||
comboBox.filter = 'Item'; | ||
// Set a value | ||
comboBox.value = 'Item 1'; | ||
// Dropdown should not have been closed and re-opened | ||
expect(openedSpy).to.be.not.called; | ||
}); | ||
|
||
it('should close when there are no items after filtering', () => { | ||
// Filter for something that doesn't exist | ||
comboBox.filter = 'doesnotexist'; | ||
// Verify data provider has been called | ||
expect(comboBox.dataProvider).to.be.calledOnce; | ||
// Dropdown should close | ||
expect(comboBox.$.overlay.opened).to.be.false; | ||
}); | ||
}); | ||
}); | ||
}); |
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