|
| 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(null); |
| 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 | + onInitialized={() => { |
| 27 | + |
| 28 | + expect(comboBox.state('availableOptions')).to.have.length(10); |
| 29 | + resolve(); |
| 30 | + }} |
| 31 | + onSelectedItem={(item) => { }} />); |
| 32 | + }); |
| 33 | + }); |
| 34 | + it("Should call onSelectedItem", () => { |
| 35 | + return new Promise((resolve, error) => { |
| 36 | + let comboBox = mount(<ComboBoxListItemPicker |
| 37 | + columnInternalName="Title" |
| 38 | + spHttpClient={mockHttpClient} |
| 39 | + webUrl="/sites/test-site" |
| 40 | + filter="Id gt 0" |
| 41 | + listId="TestId" |
| 42 | + onInitialized={() => { |
| 43 | + |
| 44 | + let ddBtn = comboBox.find('.ms-Button-flexContainer').first(); |
| 45 | + ddBtn.simulate('click'); |
| 46 | + //actual list is not part of the component |
| 47 | + let checkBoxBtn = document.querySelectorAll('.ms-ComboBox-option')[3]; |
| 48 | + (checkBoxBtn as HTMLButtonElement).click(); |
| 49 | + |
| 50 | + //ddBtn.simulate('click'); |
| 51 | + }} |
| 52 | + onSelectedItem={(item) => { |
| 53 | + expect(item.Id).to.equal(4); |
| 54 | + |
| 55 | + resolve(); |
| 56 | + }} />); |
| 57 | + }); |
| 58 | + }); |
| 59 | + it("Should initialize with default selection (id)", () => { |
| 60 | + return new Promise((resolve, error) => { |
| 61 | + let comboBox = mount(<ComboBoxListItemPicker |
| 62 | + columnInternalName="Title" |
| 63 | + spHttpClient={mockHttpClient} |
| 64 | + webUrl="/sites/test-site" |
| 65 | + defaultSelectedItems={[1]} |
| 66 | + filter="Id gt 0" |
| 67 | + listId="TestId" |
| 68 | + onInitialized={() => { |
| 69 | + let ddInput = comboBox.find('.ms-ComboBox-Input').first(); |
| 70 | + expect((ddInput.getNode() as any).value).to.be.equal("Test 1"); |
| 71 | + |
| 72 | + resolve(); |
| 73 | + }} |
| 74 | + onSelectedItem={(item) => { |
| 75 | + }} />); |
| 76 | + }); |
| 77 | + }); |
| 78 | + it("Should initialize with default selection (object)", () => { |
| 79 | + return new Promise((resolve, error) => { |
| 80 | + let comboBox = mount(<ComboBoxListItemPicker |
| 81 | + columnInternalName="Title" |
| 82 | + spHttpClient={mockHttpClient} |
| 83 | + webUrl="/sites/test-site" |
| 84 | + defaultSelectedItems={[{Id:1, Title: "Test 1"}]} |
| 85 | + filter="Id gt 0" |
| 86 | + listId="TestId" |
| 87 | + onInitialized={() => { |
| 88 | + |
| 89 | + let ddInput = comboBox.find('.ms-ComboBox-Input').first(); |
| 90 | + expect((ddInput.getNode() as any).value).to.be.equal("Test 1"); |
| 91 | + |
| 92 | + resolve(); |
| 93 | + }} |
| 94 | + onSelectedItem={(item) => { |
| 95 | + }} />); |
| 96 | + }); |
| 97 | + }); |
| 98 | + it("Should call onSelectedItem (multi)", () => { |
| 99 | + return new Promise((resolve, error) => { |
| 100 | + let comboBox = mount(<ComboBoxListItemPicker |
| 101 | + columnInternalName="Title" |
| 102 | + spHttpClient={mockHttpClient} |
| 103 | + webUrl="/sites/test-site" |
| 104 | + filter="Id gt 0" |
| 105 | + listId="TestId" |
| 106 | + multiSelect={true} |
| 107 | + onInitialized={() => { |
| 108 | + |
| 109 | + let ddBtn = comboBox.find('.ms-Button-flexContainer').first(); |
| 110 | + ddBtn.simulate('click'); |
| 111 | + //actual list is not part of the component |
| 112 | + let checkBoxBtn = document.querySelectorAll('.ms-ComboBox-option')[3]; |
| 113 | + (checkBoxBtn as HTMLButtonElement).click(); |
| 114 | + //ddBtn.simulate('click'); |
| 115 | + }} |
| 116 | + onSelectedItem={(item) => { |
| 117 | + expect(item.Id).to.equal(4); |
| 118 | + expect(item.selected).to.be.equal(true); |
| 119 | + |
| 120 | + let ddBtn = comboBox.find('.ms-Button-flexContainer').first(); |
| 121 | + ddBtn.simulate('click'); |
| 122 | + resolve(); |
| 123 | + }} />); |
| 124 | + }); |
| 125 | + }); |
| 126 | + it("Should initialize with default selection (multi) (object)", () => { |
| 127 | + return new Promise((resolve, error) => { |
| 128 | + let comboBox = mount(<ComboBoxListItemPicker |
| 129 | + columnInternalName="Title" |
| 130 | + spHttpClient={mockHttpClient} |
| 131 | + webUrl="/sites/test-site" |
| 132 | + defaultSelectedItems={[{Id:1, Title: "Test 1"},{ Id:2, Title: "Test 2"}]} |
| 133 | + filter="Id gt 0" |
| 134 | + listId="TestId" |
| 135 | + multiSelect={true} |
| 136 | + onInitialized={() => { |
| 137 | + |
| 138 | + let ddBtn = comboBox.find('.ms-Button-flexContainer').first(); |
| 139 | + ddBtn.simulate('click'); |
| 140 | + let checkBoxBtn = document.querySelectorAll('.ms-ComboBox-option')[0]; |
| 141 | + expect(checkBoxBtn.classList.contains("is-checked")).to.be.equal(true); |
| 142 | + let ddInput = comboBox.find('.ms-ComboBox-Input').first(); |
| 143 | + expect((ddInput.getNode() as any).value).to.be.equal("Test 1, Test 2"); |
| 144 | + ddBtn.simulate('click'); |
| 145 | + resolve(); |
| 146 | + }} |
| 147 | + onSelectedItem={(item) => { |
| 148 | + }} />); |
| 149 | + }); |
| 150 | + }); |
| 151 | + it("Should initialize with default selection (multi) (id)", () => { |
| 152 | + return new Promise((resolve, error) => { |
| 153 | + let comboBox = mount(<ComboBoxListItemPicker |
| 154 | + columnInternalName="Title" |
| 155 | + spHttpClient={mockHttpClient} |
| 156 | + webUrl="/sites/test-site" |
| 157 | + defaultSelectedItems={[1,2]} |
| 158 | + filter="Id gt 0" |
| 159 | + listId="TestId" |
| 160 | + multiSelect={true} |
| 161 | + onInitialized={() => { |
| 162 | + |
| 163 | + let ddBtn = comboBox.find('.ms-Button-flexContainer').first(); |
| 164 | + ddBtn.simulate('click'); |
| 165 | + let checkBoxBtn = document.querySelectorAll('.ms-ComboBox-option')[0]; |
| 166 | + expect(checkBoxBtn.classList.contains("is-checked")).to.be.equal(true); |
| 167 | + |
| 168 | + let ddInput = comboBox.find('.ms-ComboBox-Input').first(); |
| 169 | + expect((ddInput.getNode() as any).value).to.be.equal("Test 1, Test 2"); |
| 170 | + ddBtn.simulate('click'); |
| 171 | + resolve(); |
| 172 | + }} |
| 173 | + onSelectedItem={(item) => { |
| 174 | + }} />); |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments