@@ -3,8 +3,8 @@ import { spy } from 'sinon';
33
44import { defineComponents } from '../common/definitions/defineComponents.js' ;
55import {
6- FormAssociatedTestBed ,
76 type ValidationContainerTestsParams ,
7+ createFormAssociatedTestBed ,
88 runValidationContainerTests ,
99} from '../common/utils.spec.js' ;
1010import type IgcInputComponent from '../input/input.js' ;
@@ -1240,7 +1240,7 @@ describe('Combo', () => {
12401240 } ) ;
12411241
12421242 describe ( 'Form integration' , ( ) => {
1243- const spec = new FormAssociatedTestBed < IgcComboComponent < City > > (
1243+ const spec = createFormAssociatedTestBed < IgcComboComponent < City > > (
12441244 html `< igc-combo
12451245 name ="combo "
12461246 .data =${ cities }
@@ -1254,63 +1254,55 @@ describe('Combo', () => {
12541254 await spec . setup ( IgcComboComponent . tagName ) ;
12551255 } ) ;
12561256
1257- it ( 'is form associated' , async ( ) => {
1257+ it ( 'is form associated' , ( ) => {
12581258 expect ( spec . element . form ) . to . equal ( spec . form ) ;
12591259 } ) ;
12601260
12611261 it ( 'is not associated on submit if no value' , async ( ) => {
1262- spec . element . value = [ ] ;
1263- await elementUpdated ( spec . element ) ;
1264-
1265- expect ( spec . submit ( ) ?. get ( spec . element . name ) ) . to . be . null ;
1262+ // FIXME: The combo value setter does not update the form state in a synchronous manner
1263+ // FIXME: delegating to a @watch callback.
1264+ await spec . setProperties ( { value : [ ] } ) ;
1265+ spec . assertSubmitHasValue ( null ) ;
12661266 } ) ;
12671267
12681268 it ( 'is associated on submit with value-key (single)' , async ( ) => {
1269- spec . element . singleSelect = true ;
1270- await elementUpdated ( spec . element ) ;
1271-
1272- spec . element . value = [ 'BG01' , 'BG02' ] ;
1273- await elementUpdated ( spec . element ) ;
1274-
1275- expect ( spec . submit ( ) ?. get ( spec . element . name ) ) . to . equal ( 'BG01' ) ;
1269+ await spec . setProperties ( { singleSelect : true } ) ;
1270+ await spec . setProperties ( { value : [ 'BG01' , 'BG02' ] } ) ;
1271+ spec . assertSubmitHasValue ( 'BG01' ) ;
12761272 } ) ;
12771273
1278- it ( 'is associated on submit with value-key (multiple)' , async ( ) => {
1279- expect ( spec . submit ( ) ?. get ( spec . element . name ) ) . to . equal ( 'BG01' ) ;
1280- expect ( spec . submit ( ) ?. getAll ( spec . element . name ) ) . to . eql ( [ 'BG01' , 'BG02' ] ) ;
1274+ it ( 'is associated on submit with value-key (multiple)' , ( ) => {
1275+ spec . assertSubmitHasValue ( 'BG01' ) ;
1276+ spec . assertSubmitHasValues ( [ 'BG01' , 'BG02' ] ) ;
12811277 } ) ;
12821278
12831279 it ( 'is associated on submit without value-key (single)' , async ( ) => {
12841280 const [ first , second , _ ] = cities ;
12851281
1286- spec . element . valueKey = undefined ;
1287- spec . element . singleSelect = true ;
1288- await elementUpdated ( spec . element ) ;
1282+ await spec . setProperties ( { valueKey : undefined , singleSelect : true } ) ;
12891283 spec . element . select ( [ first , second ] ) ;
12901284 await elementUpdated ( spec . element ) ;
12911285
1292- expect ( spec . submit ( ) ?. get ( spec . element . name ) ) . not . to . be . null ;
1286+ expect ( spec . formData . getAll ( spec . element . name ) ) . lengthOf ( 1 ) ;
1287+ spec . assertSubmitPasses ( ) ;
12931288 } ) ;
12941289
12951290 it ( 'is associated on submit without value-key (multiple)' , async ( ) => {
12961291 const [ first , second , _ ] = cities ;
12971292
1298- spec . element . valueKey = undefined ;
1293+ await spec . setProperties ( { valueKey : undefined } ) ;
12991294 spec . element . select ( [ first , second ] ) ;
13001295 await elementUpdated ( spec . element ) ;
13011296
1302- expect ( spec . submit ( ) ?. get ( spec . element . name ) ) . not . to . be . null ;
1303- expect ( spec . submit ( ) ?. getAll ( spec . element . name ) . length ) . to . eql ( 2 ) ;
1297+ expect ( spec . formData . getAll ( spec . element . name ) ) . lengthOf ( 2 ) ;
1298+ spec . assertSubmitPasses ( ) ;
13041299 } ) ;
13051300
13061301 it ( 'is correctly reset on form reset (multiple)' , async ( ) => {
13071302 const initial = spec . element . value ;
13081303
1309- spec . element . setAttribute ( 'value' , '["BG01", "BG02"]' ) ;
1310- await elementUpdated ( spec . element ) ;
1311-
1312- spec . element . value = [ ] ;
1313- await elementUpdated ( spec . element ) ;
1304+ await spec . setAttributes ( { value : JSON . stringify ( [ 'BG01' , 'BG02' ] ) } ) ;
1305+ await spec . setProperties ( { value : [ ] } ) ;
13141306
13151307 spec . reset ( ) ;
13161308 expect ( spec . element . value ) . to . eql ( initial ) ;
@@ -1320,11 +1312,8 @@ describe('Combo', () => {
13201312 // Initial value is a multiple array. The combo defaults to the first item
13211313 const initial = [ spec . element . value [ 0 ] ] ;
13221314
1323- spec . element . singleSelect = true ;
1324- await elementUpdated ( spec . element ) ;
1325-
1326- spec . element . value = [ 'US01' ] ;
1327- await elementUpdated ( spec . element ) ;
1315+ await spec . setProperties ( { singleSelect : true } ) ;
1316+ await spec . setProperties ( { value : [ 'US01' ] } ) ;
13281317
13291318 expect ( spec . element . value ) . to . eql ( [ 'US01' ] ) ;
13301319
@@ -1333,31 +1322,26 @@ describe('Combo', () => {
13331322 } ) ;
13341323
13351324 it ( 'should reset to the new default value after setAttribute() call (multiple)' , async ( ) => {
1336- spec . element . setAttribute ( ' value' , JSON . stringify ( [ 'US01' , 'US02' ] ) ) ;
1337- spec . element . value = [ ] ;
1325+ await spec . setAttributes ( { value : JSON . stringify ( [ 'US01' , 'US02' ] ) } ) ;
1326+ await spec . setProperties ( { value : [ ] } ) ;
13381327
13391328 spec . reset ( ) ;
13401329 expect ( spec . element . value ) . to . eql ( [ 'US01' , 'US02' ] ) ;
1341- expect ( spec . submit ( ) ?. getAll ( spec . element . name ) ) . to . eql (
1342- spec . element . value
1343- ) ;
1330+ spec . assertSubmitHasValues ( spec . element . value ) ;
13441331 } ) ;
13451332
13461333 it ( 'should reset to the new default value after setAttribute() call (single)' , async ( ) => {
1347- spec . element . singleSelect = true ;
1348- await elementUpdated ( spec . element ) ;
1349-
1350- spec . element . setAttribute ( 'value' , JSON . stringify ( [ 'US01' ] ) ) ;
1351- spec . element . value = [ ] ;
1334+ await spec . setProperties ( { singleSelect : true } ) ;
1335+ await spec . setAttributes ( { value : JSON . stringify ( [ 'US01' ] ) } ) ;
1336+ await spec . setProperties ( { value : [ ] } ) ;
13521337
13531338 spec . reset ( ) ;
1339+
13541340 expect ( spec . element . value ) . to . eql ( [ 'US01' ] ) ;
1355- expect ( spec . submit ( ) ?. getAll ( spec . element . name ) ) . to . eql (
1356- spec . element . value
1357- ) ;
1341+ spec . assertSubmitHasValue ( 'US01' ) ;
13581342 } ) ;
13591343
1360- it ( 'reflects disabled ancestor state' , async ( ) => {
1344+ it ( 'reflects disabled ancestor state' , ( ) => {
13611345 spec . setAncestorDisabledState ( true ) ;
13621346 expect ( spec . element . disabled ) . to . be . true ;
13631347
@@ -1366,17 +1350,14 @@ describe('Combo', () => {
13661350 } ) ;
13671351
13681352 it ( 'fulfils required constraint' , async ( ) => {
1369- spec . element . value = [ ] ;
1370- spec . element . required = true ;
1371- await elementUpdated ( spec . element ) ;
1372- spec . submitFails ( ) ;
1353+ await spec . setProperties ( { value : [ ] , required : true } ) ;
1354+ spec . assertSubmitFails ( ) ;
13731355
1374- spec . element . value = [ 'BG01' , 'BG02' ] ;
1375- await elementUpdated ( spec . element ) ;
1376- spec . submitValidates ( ) ;
1356+ await spec . setProperties ( { value : [ 'BG01' , 'BG02' ] } ) ;
1357+ spec . assertSubmitPasses ( ) ;
13771358 } ) ;
13781359
1379- it ( 'fulfils custom constraint' , async ( ) => {
1360+ it ( 'fulfils custom constraint' , ( ) => {
13801361 spec . element . setCustomValidity ( 'invalid' ) ;
13811362 spec . submitFails ( ) ;
13821363
@@ -1385,6 +1366,70 @@ describe('Combo', () => {
13851366 } ) ;
13861367 } ) ;
13871368
1369+ describe ( 'defaultValue' , ( ) => {
1370+ const value = [ 'BG01' , 'BG02' ] ;
1371+
1372+ describe ( 'Form integration' , ( ) => {
1373+ const spec = createFormAssociatedTestBed < IgcComboComponent < City > > ( html `
1374+ < igc-combo
1375+ name ="combo "
1376+ .data =${ cities }
1377+ .defaultValue =${ value }
1378+ value-key="id"
1379+ display-key="name"
1380+ > </ igc-combo >
1381+ ` ) ;
1382+
1383+ beforeEach ( async ( ) => {
1384+ await spec . setup ( IgcComboComponent . tagName ) ;
1385+ } ) ;
1386+
1387+ it ( 'correct initial state' , ( ) => {
1388+ spec . assertIsPristine ( ) ;
1389+ expect ( spec . element . value ) . to . eql ( value ) ;
1390+ } ) ;
1391+
1392+ it ( 'is correctly submitted' , ( ) => {
1393+ spec . assertSubmitHasValues ( value ) ;
1394+ } ) ;
1395+
1396+ it ( 'is correctly reset' , async ( ) => {
1397+ await spec . setProperties ( { value : [ ] } ) ;
1398+ spec . reset ( ) ;
1399+
1400+ expect ( spec . element . value ) . to . eql ( value ) ;
1401+ } ) ;
1402+ } ) ;
1403+
1404+ describe ( 'Validation' , ( ) => {
1405+ const spec = createFormAssociatedTestBed < IgcComboComponent < City > > ( html `
1406+ < igc-combo
1407+ name ="combo "
1408+ .data =${ cities }
1409+ .defaultValue =${ [ ] }
1410+ value-key="id"
1411+ display-key="name"
1412+ > </ igc-combo >
1413+ ` ) ;
1414+
1415+ beforeEach ( async ( ) => {
1416+ await spec . setup ( IgcComboComponent . tagName ) ;
1417+ } ) ;
1418+
1419+ it ( 'fails required validation' , ( ) => {
1420+ spec . assertIsPristine ( ) ;
1421+ spec . assertSubmitFails ( ) ;
1422+ } ) ;
1423+
1424+ it ( 'passes required validation' , async ( ) => {
1425+ await spec . setProperties ( { defaultValue : value } ) ;
1426+ spec . assertIsPristine ( ) ;
1427+
1428+ spec . assertSubmitHasValues ( value ) ;
1429+ } ) ;
1430+ } ) ;
1431+ } ) ;
1432+
13881433 describe ( 'Validation message slots' , ( ) => {
13891434 it ( '' , async ( ) => {
13901435 const testParameters : ValidationContainerTestsParams < IgcComboComponent > [ ] =
0 commit comments