1+ /// <reference types="sinon" />
2+
3+ import * as React from 'react' ;
4+ import { assert , expect } from 'chai' ;
5+ import { mount , ReactWrapper } from 'enzyme' ;
6+ import { ComboBoxListItemPicker } from './ComboBoxListItemPicker' ;
7+ import { RequestClientMock } from '../../common/mocks/RequestClientMock' ;
8+
9+ declare const sinon ;
10+
11+ let mockHttpClient : RequestClientMock = new RequestClientMock ( ) ;
12+ mockHttpClient . Requests . push ( {
13+ url : "/sites/test-site/_api/web/lists('TestId')/items?$select=Id,Title&$filter=Id gt 0" ,
14+ method : "GET" ,
15+ resultString : JSON . stringify ( { "odata.metadata" : "" , "value" : [ { "odata.type" : "SP.Data.Test_x0020_listListItem" , "odata.id" : "9624204a-c049-49a1-8a18-a417872f8883" , "odata.etag" : "\"2\"" , "odata.editLink" : "Web/Lists(guid'490fae3c-f8cb-4b50-9737-f2c94f6b6727')/Items(1)" , "Id" : 1 , "ID" : 1 , "Title" : "Test 1" } , { "odata.type" : "SP.Data.Test_x0020_listListItem" , "odata.id" : "8b0e4ce8-f1e4-4b17-8f95-1ea7d63fefd6" , "odata.etag" : "\"1\"" , "odata.editLink" : "Web/Lists(guid'490fae3c-f8cb-4b50-9737-f2c94f6b6727')/Items(2)" , "Id" : 2 , "ID" : 2 , "Title" : "Test 2" } , { "odata.type" : "SP.Data.Test_x0020_listListItem" , "odata.id" : "62aea125-e442-44cf-ab08-c555ffdd2798" , "odata.etag" : "\"1\"" , "odata.editLink" : "Web/Lists(guid'490fae3c-f8cb-4b50-9737-f2c94f6b6727')/Items(3)" , "Id" : 3 , "ID" : 3 , "Title" : "Test 3" } , { "odata.type" : "SP.Data.Test_x0020_listListItem" , "odata.id" : "a25c55b2-9a4f-4976-8a12-f77393abe437" , "odata.etag" : "\"1\"" , "odata.editLink" : "Web/Lists(guid'490fae3c-f8cb-4b50-9737-f2c94f6b6727')/Items(4)" , "Id" : 4 , "ID" : 4 , "Title" : "Test 4" } , { "odata.type" : "SP.Data.Test_x0020_listListItem" , "odata.id" : "8c57be67-a9a8-46c8-8954-6e51d3464be2" , "odata.etag" : "\"1\"" , "odata.editLink" : "Web/Lists(guid'490fae3c-f8cb-4b50-9737-f2c94f6b6727')/Items(5)" , "Id" : 5 , "ID" : 5 , "Title" : "Test 5" } , { "odata.type" : "SP.Data.Test_x0020_listListItem" , "odata.id" : "d130664b-4e28-4ae5-b567-a3189f887922" , "odata.etag" : "\"1\"" , "odata.editLink" : "Web/Lists(guid'490fae3c-f8cb-4b50-9737-f2c94f6b6727')/Items(6)" , "Id" : 6 , "ID" : 6 , "Title" : "Test 6" } , { "odata.type" : "SP.Data.Test_x0020_listListItem" , "odata.id" : "6e333b93-3f19-40ee-943c-19817193a3de" , "odata.etag" : "\"1\"" , "odata.editLink" : "Web/Lists(guid'490fae3c-f8cb-4b50-9737-f2c94f6b6727')/Items(7)" , "Id" : 7 , "ID" : 7 , "Title" : "Test 7" } , { "odata.type" : "SP.Data.Test_x0020_listListItem" , "odata.id" : "a0063d1f-66ab-461b-8032-7dd38d7b8749" , "odata.etag" : "\"1\"" , "odata.editLink" : "Web/Lists(guid'490fae3c-f8cb-4b50-9737-f2c94f6b6727')/Items(8)" , "Id" : 8 , "ID" : 8 , "Title" : "Test 8" } , { "odata.type" : "SP.Data.Test_x0020_listListItem" , "odata.id" : "8aca4c7c-4922-45e9-9f03-7d4a4bb6f926" , "odata.etag" : "\"1\"" , "odata.editLink" : "Web/Lists(guid'490fae3c-f8cb-4b50-9737-f2c94f6b6727')/Items(9)" , "Id" : 9 , "ID" : 9 , "Title" : "Test 9" } , { "odata.type" : "SP.Data.Test_x0020_listListItem" , "odata.id" : "f352a0c8-711b-47e4-9990-2a7121f961c3" , "odata.etag" : "\"1\"" , "odata.editLink" : "Web/Lists(guid'490fae3c-f8cb-4b50-9737-f2c94f6b6727')/Items(10)" , "Id" : 10 , "ID" : 10 , "Title" : "Test 10" } ] } )
16+ } ) ;
17+ describe ( '<ComboBoxListItemPicker />' , ( ) => {
18+ it ( "Should render initial data" , ( ) => {
19+ return new Promise ( ( resolve , error ) => {
20+ let comboBox = mount ( < ComboBoxListItemPicker
21+ columnInternalName = "Title"
22+ spHttpClient = { mockHttpClient }
23+ webUrl = "/sites/test-site"
24+ filter = "Id gt 0"
25+ listId = "TestId"
26+ itemLimit = { 20 }
27+ onInitialized = { ( ) => {
28+
29+ expect ( comboBox . state ( 'availableOptions' ) ) . to . have . length ( 10 ) ;
30+ resolve ( ) ;
31+ } }
32+ onSelectedItem = { ( item ) => { } } /> ) ;
33+ } ) ;
34+ } ) ;
35+ it ( "Should call onSelectedItem" , ( ) => {
36+ return new Promise ( ( resolve , error ) => {
37+ let comboBox = mount ( < ComboBoxListItemPicker
38+ columnInternalName = "Title"
39+ spHttpClient = { mockHttpClient }
40+ webUrl = "/sites/test-site"
41+ filter = "Id gt 0"
42+ listId = "TestId"
43+ itemLimit = { 20 }
44+ onInitialized = { ( ) => {
45+
46+ let ddBtn = comboBox . find ( '.ms-Button-flexContainer' ) . first ( ) ;
47+ ddBtn . simulate ( 'click' ) ;
48+ //actual list is not part of the component
49+ let checkBoxBtn = document . querySelectorAll ( '.ms-ComboBox-option' ) [ 3 ] ;
50+ ( checkBoxBtn as HTMLButtonElement ) . click ( ) ;
51+
52+ //ddBtn.simulate('click');
53+ } }
54+ onSelectedItem = { ( item ) => {
55+ expect ( item . Id ) . to . equal ( 4 ) ;
56+
57+ resolve ( ) ;
58+ } } /> ) ;
59+ } ) ;
60+ } ) ;
61+ it ( "Should initialize with default selection (id)" , ( ) => {
62+ return new Promise ( ( resolve , error ) => {
63+ let comboBox = mount ( < ComboBoxListItemPicker
64+ columnInternalName = "Title"
65+ spHttpClient = { mockHttpClient }
66+ webUrl = "/sites/test-site"
67+ defaultSelectedItems = { [ 1 ] }
68+ filter = "Id gt 0"
69+ listId = "TestId"
70+ itemLimit = { 20 }
71+ onInitialized = { ( ) => {
72+ let ddInput = comboBox . find ( '.ms-ComboBox-Input' ) . first ( ) ;
73+ expect ( ( ddInput . getNode ( ) as any ) . value ) . to . be . equal ( "Test 1" ) ;
74+
75+ resolve ( ) ;
76+ } }
77+ onSelectedItem = { ( item ) => {
78+ } } /> ) ;
79+ } ) ;
80+ } ) ;
81+ it ( "Should initialize with default selection (object)" , ( ) => {
82+ return new Promise ( ( resolve , error ) => {
83+ let comboBox = mount ( < ComboBoxListItemPicker
84+ columnInternalName = "Title"
85+ spHttpClient = { mockHttpClient }
86+ webUrl = "/sites/test-site"
87+ defaultSelectedItems = { [ { Id :1 , Title : "Test 1" } ] }
88+ filter = "Id gt 0"
89+ listId = "TestId"
90+ itemLimit = { 20 }
91+ onInitialized = { ( ) => {
92+
93+ let ddInput = comboBox . find ( '.ms-ComboBox-Input' ) . first ( ) ;
94+ expect ( ( ddInput . getNode ( ) as any ) . value ) . to . be . equal ( "Test 1" ) ;
95+
96+ resolve ( ) ;
97+ } }
98+ onSelectedItem = { ( item ) => {
99+ } } /> ) ;
100+ } ) ;
101+ } ) ;
102+ it ( "Should call onSelectedItem (multi)" , ( ) => {
103+ return new Promise ( ( resolve , error ) => {
104+ let comboBox = mount ( < ComboBoxListItemPicker
105+ columnInternalName = "Title"
106+ spHttpClient = { mockHttpClient }
107+ webUrl = "/sites/test-site"
108+ filter = "Id gt 0"
109+ listId = "TestId"
110+ itemLimit = { 20 }
111+ multiSelect = { true }
112+ onInitialized = { ( ) => {
113+
114+ let ddBtn = comboBox . find ( '.ms-Button-flexContainer' ) . first ( ) ;
115+ ddBtn . simulate ( 'click' ) ;
116+ //actual list is not part of the component
117+ let checkBoxBtn = document . querySelectorAll ( '.ms-ComboBox-option' ) [ 3 ] ;
118+ ( checkBoxBtn as HTMLButtonElement ) . click ( ) ;
119+ //ddBtn.simulate('click');
120+ } }
121+ onSelectedItem = { ( item ) => {
122+ expect ( item . Id ) . to . equal ( 4 ) ;
123+ expect ( item . selected ) . to . be . equal ( true ) ;
124+
125+ let ddBtn = comboBox . find ( '.ms-Button-flexContainer' ) . first ( ) ;
126+ ddBtn . simulate ( 'click' ) ;
127+ resolve ( ) ;
128+ } } /> ) ;
129+ } ) ;
130+ } ) ;
131+ it ( "Should initialize with default selection (multi) (object)" , ( ) => {
132+ return new Promise ( ( resolve , error ) => {
133+ let comboBox = mount ( < ComboBoxListItemPicker
134+ columnInternalName = "Title"
135+ spHttpClient = { mockHttpClient }
136+ webUrl = "/sites/test-site"
137+ defaultSelectedItems = { [ { Id :1 , Title : "Test 1" } , { Id :2 , Title : "Test 2" } ] }
138+ filter = "Id gt 0"
139+ listId = "TestId"
140+ multiSelect = { true }
141+ itemLimit = { 20 }
142+ onInitialized = { ( ) => {
143+
144+ let ddBtn = comboBox . find ( '.ms-Button-flexContainer' ) . first ( ) ;
145+ ddBtn . simulate ( 'click' ) ;
146+ let checkBoxBtn = document . querySelectorAll ( '.ms-ComboBox-option' ) [ 0 ] ;
147+ expect ( checkBoxBtn . classList . contains ( "is-checked" ) ) . to . be . equal ( true ) ;
148+ let ddInput = comboBox . find ( '.ms-ComboBox-Input' ) . first ( ) ;
149+ expect ( ( ddInput . getNode ( ) as any ) . value ) . to . be . equal ( "Test 1, Test 2" ) ;
150+ ddBtn . simulate ( 'click' ) ;
151+ resolve ( ) ;
152+ } }
153+ onSelectedItem = { ( item ) => {
154+ } } /> ) ;
155+ } ) ;
156+ } ) ;
157+ it ( "Should initialize with default selection (multi) (id)" , ( ) => {
158+ return new Promise ( ( resolve , error ) => {
159+ let comboBox = mount ( < ComboBoxListItemPicker
160+ columnInternalName = "Title"
161+ spHttpClient = { mockHttpClient }
162+ webUrl = "/sites/test-site"
163+ defaultSelectedItems = { [ 1 , 2 ] }
164+ filter = "Id gt 0"
165+ listId = "TestId"
166+ itemLimit = { 20 }
167+ multiSelect = { true }
168+ onInitialized = { ( ) => {
169+
170+ let ddBtn = comboBox . find ( '.ms-Button-flexContainer' ) . first ( ) ;
171+ ddBtn . simulate ( 'click' ) ;
172+ let checkBoxBtn = document . querySelectorAll ( '.ms-ComboBox-option' ) [ 0 ] ;
173+ expect ( checkBoxBtn . classList . contains ( "is-checked" ) ) . to . be . equal ( true ) ;
174+
175+ let ddInput = comboBox . find ( '.ms-ComboBox-Input' ) . first ( ) ;
176+ expect ( ( ddInput . getNode ( ) as any ) . value ) . to . be . equal ( "Test 1, Test 2" ) ;
177+ ddBtn . simulate ( 'click' ) ;
178+ resolve ( ) ;
179+ } }
180+ onSelectedItem = { ( item ) => {
181+ } } /> ) ;
182+ } ) ;
183+ } ) ;
184+ } ) ;
0 commit comments