Skip to content

Commit 36cb4ee

Browse files
author
Marcin Wojciechowski
committed
Few more tests with a little better desrciption
1 parent b9fd582 commit 36cb4ee

File tree

5 files changed

+108
-17
lines changed

5 files changed

+108
-17
lines changed

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,16 @@
127127
"@microsoft/sp-core-library": "identity-obj-proxy",
128128
"@microsoft/sp-application-base": "identity-obj-proxy",
129129
"office-ui-fabric-react/lib/FocusZone": "identity-obj-proxy",
130-
"office-ui-fabric-react/lib/List": "identity-obj-proxy",
131-
"office-ui-fabric-react/lib/Spinner": "identity-obj-proxy",
130+
"office-ui-fabric-react/lib/List": "office-ui-fabric-react/lib-commonjs/List",
131+
"office-ui-fabric-react/lib/Spinner": "office-ui-fabric-react/lib-commonjs/Spinner",
132132
"office-ui-fabric-react/lib/Image": "identity-obj-proxy",
133133
"office-ui-fabric-react/lib/Button": "identity-obj-proxy",
134134
"office-ui-fabric-react/lib/components/Button": "identity-obj-proxy",
135-
"office-ui-fabric-react/lib/Selection": "identity-obj-proxy",
135+
"office-ui-fabric-react/lib/Selection": "office-ui-fabric-react/lib-commonjs/Selection",
136136
"office-ui-fabric-react/lib/Icon": "identity-obj-proxy",
137137
"office-ui-fabric-react/lib/Styling": "identity-obj-proxy",
138138
"office-ui-fabric-react/lib/Check": "identity-obj-proxy",
139-
"office-ui-fabric-react/lib/DetailsList": "identity-obj-proxy",
139+
"office-ui-fabric-react/lib/DetailsList": "office-ui-fabric-react/lib-commonjs/DetailsList",
140140
"office-ui-fabric-react/lib/CommandBar": "identity-obj-proxy",
141141
"office-ui-fabric-react/lib/ContextualMenu": "identity-obj-proxy",
142142
"office-ui-fabric-react/lib/ScrollablePane": "identity-obj-proxy",
@@ -147,6 +147,7 @@
147147
"office-ui-fabric-react/lib/Persona": "identity-obj-proxy",
148148
"office-ui-fabric-react/lib/HoverCard": "identity-obj-proxy",
149149
"office-ui-fabric-react/lib/components/Icon": "identity-obj-proxy",
150+
"office-ui-fabric-react/lib/Utilities":"office-ui-fabric-react/lib-commonjs/Utilities",
150151
"@pnp/sp": "identity-obj-proxy",
151152
"'@pnp/sp/fields": "identity-obj-proxy",
152153
"ControlStrings": "identity-obj-proxy",

tests/controls/documentLibraryBrowser/DocumentLibraryBrowser.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe("<DocumentLibraryBrowser />", ()=>{
2626

2727
}}
2828
/>);
29-
assert.equal(documentLibraryBrowser.getDOMNode().tagName, "SPINNER");
29+
assert.equal(documentLibraryBrowser.getDOMNode().tagName, "DIV");
3030

3131
await documentLibraryBrowser.instance().componentDidMount();
3232
documentLibraryBrowser.update();
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
///<reference types="jest" />
2+
import * as React from "react";
3+
import { mount, configure } from "enzyme";
4+
import * as Adapter from 'enzyme-adapter-react-16';
5+
import { FileBrowser } from "../../../src/controls/filePicker/controls/FileBrowser/FileBrowser";
6+
import { MockFileBrowserService } from "../../mock/services/MockFileBrowserService";
7+
import { IFile } from "../../../src/services/FileBrowserService.types";
8+
import { assert } from "chai";
9+
configure({ adapter: new Adapter() });
10+
11+
describe("<FileBrowser />", ()=>{
12+
test("should render loading animation", async ()=>{
13+
//The purpose of this test is to make sure we will see the loading animation before anything else.
14+
let mockFileBrowserService: MockFileBrowserService = new MockFileBrowserService();
15+
let component = mount(<FileBrowser
16+
fileBrowserService={mockFileBrowserService as any}
17+
libraryUrl=""
18+
folderPath="string"
19+
accepts={[]}
20+
onChange={(filePickerResult) => {}}
21+
onOpenFolder={(folder: IFile) => {}}
22+
/>);
23+
//as in package.json we map spinner to lib-common spinner mount will actually render the whole thing
24+
//so let's try to find our spinner by class name
25+
let spinner = component.getDOMNode().querySelector(".ms-Spinner-circle");
26+
assert.isOk(spinner);
27+
});
28+
test("should load initial data", async ()=>{
29+
//In this test we will call componentDidMount manually
30+
//As mounting in test context will not trigger full component lifecycle we have to do it on our own
31+
//(It won't be a case if we were to use @testing-library/react but this is a topic for another time:) )
32+
let mockFileBrowserService: MockFileBrowserService = new MockFileBrowserService();
33+
//FileBrowser uses getListItems method on fileBrowserService.
34+
//Our mock already provides that method, so let's assign our mock data
35+
mockFileBrowserService.getListItemsResult ={
36+
nextHref: undefined,
37+
items: [{
38+
name: "Test file",
39+
absoluteUrl: "https://test.sharepoint.com/sites/tea-point/Shared Documents/TestFile.docx",
40+
serverRelativeUrl: "/sites/tea-point/Shared Documents/TestFile.docx",
41+
isFolder: false,
42+
modified: "",
43+
fileIcon: "",
44+
fileType: "docx",
45+
// URL required to generate thumbnail preview
46+
spItemUrl: "",
47+
supportsThumbnail: false
48+
},{
49+
name: "Another test file",
50+
absoluteUrl: "https://test.sharepoint.com/sites/tea-point/Shared Documents/AnotherTestFile.docx",
51+
serverRelativeUrl: "/sites/tea-point/Shared Documents/AnotherTestFile.docx",
52+
isFolder: false,
53+
modified: "",
54+
fileIcon: "",
55+
fileType: "docx",
56+
// URL required to generate thumbnail preview
57+
spItemUrl: "",
58+
supportsThumbnail: false
59+
}]
60+
}
61+
//As we also want to validate correct data is passed to getListItem method, let's add some assertion in our mock event
62+
//Don't hesitate to peek the mock definition to check out how it's done.
63+
//To avoid false positives I declare asserted variable.
64+
//It's value will be set to true in event handler and asserted at the end of this test
65+
let asserted = false;
66+
mockFileBrowserService.onGetListItems = (listUrl,folderPath,acceptedExtensions,nextPageParams)=>{
67+
assert.equal(listUrl, "Shared Documents");
68+
assert.equal(folderPath,"/");
69+
assert.deepEqual(acceptedExtensions,["docx","xlsx"]);
70+
//nextPageParams should be falsy in this case
71+
assert.isNotOk(nextPageParams);
72+
asserted = true;
73+
}
74+
let component = mount(<FileBrowser
75+
fileBrowserService={mockFileBrowserService as any}
76+
libraryUrl="Shared Documents"
77+
folderPath="/"
78+
accepts={["docx","xlsx"]}
79+
onChange={(filePickerResult) => {}}
80+
onOpenFolder={(folder: IFile) => {}}
81+
/>);
82+
//You could wonder - why not to test loading animation here instead of the previous test?
83+
//I want to avoid situation in which broken loading animation would affect test for loading data.
84+
//Those are two different functionalities I wish to test separately
85+
//as promised - here we are manually call lifecycle method. Note await keyword.
86+
await component.instance().componentDidMount();
87+
88+
//Now, let's make sure our component is rerendered
89+
component.update();
90+
//And now let's find our rows. Note we are using lib-commonjs to mock details list which allows us to fully render the component
91+
let dataRows = component.getDOMNode().querySelectorAll('[data-automationid="DetailsRowFields"]')
92+
//We expect to have to rows. Content of each should be just the name of the document.
93+
assert.equal(dataRows[0].textContent,"Test file");
94+
assert.equal(dataRows[1].textContent,"Another test file");
95+
//And finally let's make sure we asserted the call to getListItems
96+
assert.isTrue(asserted);
97+
});
98+
});

tests/controls/filePicker/SiteFilePickerTab.test.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ import { assert } from "chai";
88

99
configure({ adapter: new Adapter() });
1010

11-
jest.mock("office-ui-fabric-react/lib/Utilities",()=>({
12-
IRenderFunction:{
13-
14-
},
15-
IRectangle:{
16-
17-
},
18-
css: ()=>{
19-
20-
}
21-
}))
22-
2311
describe("<SiteFilePickerTab />", ()=>{
2412
test("should load initial data", async ()=>{
2513
let browserService = new MockFileBrowserService();

tests/mock/services/MockFileBrowserService.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import { FilesQueryResult, IFile, ILibrary } from "../../../src/services/FileBro
33
export class MockFileBrowserService {
44
public getListItemsResult: FilesQueryResult;
55
public getFileThumbnailUrlResultMap: Map<string, string> = new Map<string, string>();
6+
public onGetListItems: (listUrl: string, folderPath: string, acceptedFilesExtensions?: string[], nextPageQueryStringParams?: string) => void;
67
public getSiteMediaLibrariesResult: ILibrary[] = [];
78
constructor() {
89

910
}
1011
public getListItems = (listUrl: string, folderPath: string, acceptedFilesExtensions?: string[], nextPageQueryStringParams?: string): Promise<FilesQueryResult> => {
12+
if (this.onGetListItems) {
13+
this.onGetListItems(listUrl, folderPath, acceptedFilesExtensions, nextPageQueryStringParams);
14+
}
1115
return Promise.resolve(this.getListItemsResult);
1216
}
1317
public getFileThumbnailUrl = (file: IFile, thumbnailWidth: number, thumbnailHeight: number): string => {

0 commit comments

Comments
 (0)