Skip to content

Commit 4ceabc2

Browse files
committed
chore: added test cases for form submission
1 parent 3e2d8ed commit 4ceabc2

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,5 +1116,110 @@ describe('<pf-search-input>', function() {
11161116
});
11171117
});
11181118
});
1119+
1120+
describe('form submission with pf-search-input', function() {
1121+
let form: HTMLFormElement;
1122+
let searchInput: PfSearchInput;
1123+
const updateComplete = () => searchInput.updateComplete;
1124+
const focus = () => searchInput.focus();
1125+
let submittedData: FormData | null;
1126+
1127+
beforeEach(async function() {
1128+
form = await createFixture<HTMLFormElement>(html`
1129+
<form>
1130+
<pf-search-input name="search">
1131+
<pf-option value="Alabama">Alabama</pf-option>
1132+
<pf-option value="New York">New York</pf-option>
1133+
<pf-option value="New Jersey">New Jersey</pf-option>
1134+
</pf-search-input>
1135+
<button type="submit">Submit</button>
1136+
</form>
1137+
`);
1138+
1139+
searchInput = form.querySelector('pf-search-input')!;
1140+
beforeEach(updateComplete);
1141+
1142+
form.addEventListener('submit', e => {
1143+
e.preventDefault();
1144+
submittedData = new FormData(form);
1145+
});
1146+
});
1147+
1148+
describe('when a user types and selects an option', function() {
1149+
describe('focus', function() {
1150+
beforeEach(focus);
1151+
beforeEach(() => aTimeout(300));
1152+
beforeEach(updateComplete);
1153+
describe('press `New`', function() {
1154+
beforeEach(updateComplete);
1155+
beforeEach(() => aTimeout(300));
1156+
beforeEach(press('N'));
1157+
beforeEach(() => aTimeout(300));
1158+
beforeEach(press('e'));
1159+
beforeEach(() => aTimeout(300));
1160+
beforeEach(press('w'));
1161+
1162+
describe('select the first option starting with `New` and submit the form', function() {
1163+
beforeEach(updateComplete);
1164+
beforeEach(() => aTimeout(300));
1165+
beforeEach(press('ArrowDown'));
1166+
beforeEach(() => aTimeout(300));
1167+
beforeEach(press('Enter'));
1168+
beforeEach(updateComplete);
1169+
1170+
it('should select the option', function() {
1171+
expect(getSelectedOptionValue(searchInput)).to.deep.equal([
1172+
'New York',
1173+
]);
1174+
});
1175+
1176+
it('should close the dropdown after selection', function() {
1177+
expect(searchInput.expanded).to.be.false;
1178+
});
1179+
1180+
it('should keep selected value in input after submission', function() {
1181+
const input = searchInput.shadowRoot?.querySelector('input');
1182+
expect(input?.value).to.equal('New York');
1183+
});
1184+
1185+
describe('on form submit event', function() {
1186+
it('should submit the selected value from pf-search-input', async function() {
1187+
const event = new SubmitEvent('submit', {
1188+
bubbles: true,
1189+
cancelable: true,
1190+
});
1191+
1192+
form.dispatchEvent(event);
1193+
expect(event.defaultPrevented).to.be.true;
1194+
1195+
expect(submittedData).to.not.be.null;
1196+
expect(submittedData?.get('search')).to.equal('New York');
1197+
});
1198+
});
1199+
1200+
describe('on submit button click', function() {
1201+
it('should submit the selected value', async function() {
1202+
form.querySelector('button')?.click();
1203+
beforeEach(() => aTimeout(300));
1204+
1205+
expect(submittedData).to.not.be.null;
1206+
expect(submittedData?.get('search')).to.equal('New York');
1207+
});
1208+
});
1209+
1210+
describe('form submit on clicking `enter` key', function() {
1211+
beforeEach(press('Enter'));
1212+
beforeEach(updateComplete);
1213+
1214+
it('should submit the selected value', function() {
1215+
expect(submittedData).to.not.be.null;
1216+
expect(submittedData?.get('search')).to.equal('New York');
1217+
});
1218+
});
1219+
});
1220+
});
1221+
});
1222+
});
1223+
});
11191224
});
11201225
});

0 commit comments

Comments
 (0)