Skip to content

Commit 45906b5

Browse files
author
Marcin Wojciechowski
committed
Few more tests
1 parent 36cb4ee commit 45906b5

File tree

1 file changed

+102
-1
lines changed

1 file changed

+102
-1
lines changed

tests/controls/filePicker/FileBrowser.test.tsx

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe("<FileBrowser />", ()=>{
3232
let mockFileBrowserService: MockFileBrowserService = new MockFileBrowserService();
3333
//FileBrowser uses getListItems method on fileBrowserService.
3434
//Our mock already provides that method, so let's assign our mock data
35-
mockFileBrowserService.getListItemsResult ={
35+
mockFileBrowserService.getListItemsResult = {
3636
nextHref: undefined,
3737
items: [{
3838
name: "Test file",
@@ -95,4 +95,105 @@ describe("<FileBrowser />", ()=>{
9595
//And finally let's make sure we asserted the call to getListItems
9696
assert.isTrue(asserted);
9797
});
98+
test("should handle folder change", async ()=>{
99+
//In this test we want to assert if changing the folder will trigger proper event
100+
//Same as previously we will have to call lifecycle events and click handlers on our own
101+
let mockFileBrowserService: MockFileBrowserService = new MockFileBrowserService();
102+
//First let define first Mock Data
103+
let mockData = {
104+
nextHref: undefined,
105+
items: [{
106+
name: "Test Folder",
107+
absoluteUrl: "https://test.sharepoint.com/sites/tea-point/Shared Documents/Test Folder",
108+
serverRelativeUrl: "/sites/tea-point/Shared Documents/Test Folder",
109+
isFolder: true,
110+
modified: "",
111+
fileIcon: "",
112+
fileType: "folder",
113+
// URL required to generate thumbnail preview
114+
spItemUrl: "",
115+
supportsThumbnail: false
116+
}]
117+
};
118+
mockFileBrowserService.getListItemsResult = mockData;
119+
120+
//Let's mount our component...
121+
//The key to this test is to pass onOpenFolder method that will assert validity of the event
122+
let asserted = false;
123+
let component = mount(<FileBrowser
124+
fileBrowserService={mockFileBrowserService as any}
125+
libraryUrl="Shared Documents"
126+
folderPath="/"
127+
accepts={["docx","xlsx"]}
128+
onChange={(filePickerResult) => {}}
129+
onOpenFolder={(folder: IFile) => {
130+
assert.deepEqual(folder,mockData.items[0]);
131+
asserted = true;
132+
}}
133+
/>);
134+
//...and await relevant event
135+
await component.instance().componentDidMount();
136+
component.update();
137+
138+
//Now we want to mock click event. There are two ways around it. One possibility is to send click event on some element.
139+
//The other one is to call private method of our component with specific argument. In our case, that would be our folder.
140+
//In this case I would lean toward the second option. The first one could fail if exception occur in DetailsList and we don't have to worry about it.
141+
//However I do plan to include test sample with mocking external components (Will be more useful for functional components)
142+
//@ts-ignore
143+
component.instance()._handleItemInvoked(mockData.items[0]);
144+
145+
assert.isTrue(asserted);
146+
});
147+
test("should handle item change", async ()=>{
148+
//In this test we want to assert if selecting a file will trigger proper event
149+
//Same as previously we will have to call lifecycle events and click handlers on our own
150+
let mockFileBrowserService: MockFileBrowserService = new MockFileBrowserService();
151+
//First let define first Mock Data
152+
let mockData = {
153+
nextHref: undefined,
154+
items: [{
155+
name: "Test File",
156+
absoluteUrl: "https://test.sharepoint.com/sites/tea-point/Shared Documents/Test File.docx",
157+
serverRelativeUrl: "/sites/tea-point/Shared Documents/Test File.docx",
158+
isFolder: false,
159+
modified: "",
160+
fileIcon: "",
161+
fileType: "docx",
162+
// URL required to generate thumbnail preview
163+
spItemUrl: "",
164+
supportsThumbnail: false
165+
}]
166+
};
167+
mockFileBrowserService.getListItemsResult = mockData;
168+
//Also let's define our expected file
169+
const expectedFilePicked = {
170+
fileName: "Test File",
171+
fileNameWithoutExtension: "Test File",
172+
fileAbsoluteUrl: "https://test.sharepoint.com/sites/tea-point/Shared Documents/Test File.docx",
173+
spItemUrl: "",
174+
downloadFileContent: null
175+
}
176+
//Let's mount our component...
177+
//The key to this test is to pass onChange method that will assert validity of the event
178+
let asserted = false;
179+
let component = mount(<FileBrowser
180+
fileBrowserService={mockFileBrowserService as any}
181+
libraryUrl="Shared Documents"
182+
folderPath="/"
183+
accepts={["docx","xlsx"]}
184+
onChange={(filePickerResult) => {
185+
assert.deepEqual(filePickerResult,expectedFilePicked);
186+
asserted = true;}}
187+
onOpenFolder={(folder: IFile) => {}}
188+
/>);
189+
//...and await relevant event
190+
await component.instance().componentDidMount();
191+
component.update();
192+
193+
//We can use same approach as in previous test
194+
//@ts-ignore
195+
component.instance()._handleItemInvoked(mockData.items[0]);
196+
197+
assert.isTrue(asserted);
198+
});
98199
});

0 commit comments

Comments
 (0)