Skip to content

Commit 0b61fc9

Browse files
committed
chore: test cases added for outside click and on focus out
1 parent 9b62b5b commit 0b61fc9

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

elements/pf-search-input/test/pf-search-input.spec.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,5 +988,133 @@ describe('<pf-search-input>', function() {
988988
});
989989
});
990990
});
991+
992+
describe('Outside click behavior', function() {
993+
let element: PfSearchInput;
994+
const updateComplete = () => element.updateComplete;
995+
const focus = () => element.focus();
996+
let outsideElement: HTMLElement;
997+
998+
beforeEach(async function() {
999+
element = await createFixture<PfSearchInput>(html`
1000+
<pf-search-input>
1001+
<pf-option value="11">11</pf-option>
1002+
<pf-option value="12">12</pf-option>
1003+
<pf-option value="13">13</pf-option>
1004+
<pf-option value="21">21</pf-option>
1005+
<pf-option value="22">22</pf-option>
1006+
<pf-option value="23">23</pf-option>
1007+
</pf-search-input>
1008+
`);
1009+
1010+
// Create a dummy div to represent an "outside" area
1011+
outsideElement = document.createElement('div');
1012+
outsideElement.style.width = '200px';
1013+
outsideElement.style.height = '200px';
1014+
document.body.appendChild(outsideElement);
1015+
});
1016+
1017+
describe('focus()', function() {
1018+
beforeEach(focus);
1019+
beforeEach(updateComplete);
1020+
1021+
describe('"1"', function() {
1022+
beforeEach(press('1'));
1023+
beforeEach(() => aTimeout(300));
1024+
beforeEach(updateComplete);
1025+
1026+
it('should open the listbox when focused', function() {
1027+
expect(element.expanded).to.be.true;
1028+
});
1029+
1030+
it('only shows options that start with "1"', async function() {
1031+
expect(getVisibleOptionValues(element)).to.deep.equal([
1032+
'11',
1033+
'12',
1034+
'13',
1035+
]);
1036+
});
1037+
1038+
describe('click outside the element', function() {
1039+
// Click outside element
1040+
beforeEach(() => clickElementAtCenter(outsideElement));
1041+
beforeEach(() => aTimeout(300));
1042+
beforeEach(updateComplete);
1043+
1044+
it('should close the listbox when clicking outside', async function() {
1045+
expect(element.expanded).to.be.false;
1046+
});
1047+
1048+
it('should keep the entered value after outside click', async function() {
1049+
const input = element.shadowRoot?.querySelector('input');
1050+
expect(input?.value).to.equal('1');
1051+
});
1052+
});
1053+
});
1054+
});
1055+
});
1056+
1057+
describe('on focus-out behavior', function() {
1058+
let element: PfSearchInput;
1059+
const updateComplete = () => element.updateComplete;
1060+
const focus = () => element.focus();
1061+
1062+
beforeEach(async function() {
1063+
element = await createFixture<PfSearchInput>(html`
1064+
<pf-search-input>
1065+
<pf-option value="11">11</pf-option>
1066+
<pf-option value="12">12</pf-option>
1067+
<pf-option value="13">13</pf-option>
1068+
<pf-option value="21">21</pf-option>
1069+
<pf-option value="22">22</pf-option>
1070+
<pf-option value="23">23</pf-option>
1071+
</pf-search-input>
1072+
`);
1073+
1074+
beforeEach(updateComplete);
1075+
});
1076+
1077+
describe('focus()', function() {
1078+
beforeEach(focus);
1079+
beforeEach(() => aTimeout(300));
1080+
beforeEach(updateComplete);
1081+
1082+
describe('"1"', function() {
1083+
beforeEach(press('1'));
1084+
beforeEach(() => aTimeout(300));
1085+
beforeEach(updateComplete);
1086+
1087+
it('should open the listbox when focused', function() {
1088+
expect(element.expanded).to.be.true;
1089+
});
1090+
1091+
it('only shows options that start with "1"', async function() {
1092+
expect(getVisibleOptionValues(element)).to.deep.equal([
1093+
'11',
1094+
'12',
1095+
'13',
1096+
]);
1097+
});
1098+
1099+
describe('move the focus out of the element', function() {
1100+
beforeEach(press('Tab'));
1101+
beforeEach(() => aTimeout(300));
1102+
beforeEach(updateComplete);
1103+
beforeEach(press('Tab'));
1104+
beforeEach(() => aTimeout(300));
1105+
beforeEach(updateComplete);
1106+
1107+
it('should close the listbox when focused out', async function() {
1108+
expect(element.expanded).to.be.false;
1109+
});
1110+
1111+
it('should keep the entered value after focus out', async function() {
1112+
const input = element.shadowRoot?.querySelector('input');
1113+
expect(input?.value).to.equal('1');
1114+
});
1115+
});
1116+
});
1117+
});
1118+
});
9911119
});
9921120
});

0 commit comments

Comments
 (0)