Skip to content

Commit 44c56be

Browse files
author
Marcin Wojciechowski
committed
ListItemAttachments - support new items unit tests
1 parent 0a3df78 commit 44c56be

File tree

8 files changed

+399
-62
lines changed

8 files changed

+399
-62
lines changed

package.json

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,28 +127,8 @@
127127
"@microsoft/sp-webpart-base": "identity-obj-proxy",
128128
"@microsoft/sp-core-library": "identity-obj-proxy",
129129
"@microsoft/sp-application-base": "identity-obj-proxy",
130-
"office-ui-fabric-react/lib/FocusZone": "identity-obj-proxy",
131-
"office-ui-fabric-react/lib/List": "office-ui-fabric-react/lib-commonjs/List",
132-
"office-ui-fabric-react/lib/Spinner": "office-ui-fabric-react/lib-commonjs/Spinner",
133-
"office-ui-fabric-react/lib/Image": "identity-obj-proxy",
134-
"office-ui-fabric-react/lib/Button": "identity-obj-proxy",
135-
"office-ui-fabric-react/lib/components/Button": "identity-obj-proxy",
136-
"office-ui-fabric-react/lib/Selection": "office-ui-fabric-react/lib-commonjs/Selection",
137-
"office-ui-fabric-react/lib/Icon": "identity-obj-proxy",
138-
"office-ui-fabric-react/lib/Styling": "identity-obj-proxy",
139-
"office-ui-fabric-react/lib/Check": "identity-obj-proxy",
140-
"office-ui-fabric-react/lib/DetailsList": "office-ui-fabric-react/lib-commonjs/DetailsList",
141-
"office-ui-fabric-react/lib/CommandBar": "identity-obj-proxy",
142-
"office-ui-fabric-react/lib/ContextualMenu": "identity-obj-proxy",
143-
"office-ui-fabric-react/lib/ScrollablePane": "identity-obj-proxy",
144-
"office-ui-fabric-react/lib/Breadcrumb": "identity-obj-proxy",
145-
"office-ui-fabric-react/lib/Link": "identity-obj-proxy",
146-
"office-ui-fabric-react/lib/Dialog": "identity-obj-proxy",
147-
"office-ui-fabric-react/lib/common/DirectionalHint": "identity-obj-proxy",
148-
"office-ui-fabric-react/lib/Persona": "identity-obj-proxy",
149-
"office-ui-fabric-react/lib/HoverCard": "identity-obj-proxy",
150-
"office-ui-fabric-react/lib/components/Icon": "identity-obj-proxy",
151-
"office-ui-fabric-react/lib/Utilities": "office-ui-fabric-react/lib-commonjs/Utilities",
130+
"office-ui-fabric-react/lib/(.*)$": "office-ui-fabric-react/lib-commonjs/$1",
131+
"src/common/telemetry/(.*)$":"identity-obj-proxy",
152132
"@pnp/sp": "identity-obj-proxy",
153133
"'@pnp/sp/fields": "identity-obj-proxy",
154134
"ControlStrings": "identity-obj-proxy",

src/controls/listItemAttachments/IListItemAttachmentsProps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { BaseComponentContext } from '@microsoft/sp-component-base';
22

33
export interface IListItemAttachmentsProps {
44
listId: string;
5-
itemId: number;
5+
itemId?: number;
66
className?: string;
77
webUrl?:string;
88
disabled?: boolean;

src/controls/listItemAttachments/ListItemAttachments.tsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
5757
/**
5858
* componentDidMount lifecycle hook
5959
*/
60-
public componentDidMount() {
61-
this.loadAttachments();
60+
public async componentDidMount() {
61+
await this.loadAttachments();
6262
}
6363

6464
private async loadAttachmentPreview(file: IListItemAttachmentFile): Promise<IDocumentCardPreviewImage> {
@@ -83,10 +83,12 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
8383
file,
8484
this.props.webUrl)));
8585
}
86-
this.setState({
86+
return new Promise<void>((resolve,error)=>{
87+
this.setState({
8788
filesToUpload: [],
8889
itemId: itemId
89-
},this.loadAttachments);
90+
},()=>this.loadAttachments().then(resolve));
91+
});
9092
}
9193
protected loadAttachmentsPreview(files: IListItemAttachmentFile[]){
9294
const filePreviewImages = files.map(file => this.loadAttachmentPreview(file));
@@ -109,15 +111,15 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
109111
*/
110112
private async loadAttachments() {
111113
if(this.state.itemId){
112-
this._spservice.getListItemAttachments(this.props.listId, this.state.itemId).then(async (files: IListItemAttachmentFile[]) => {
113-
await this.loadAttachmentsPreview(files);
114-
}).catch((error: Error) => {
115-
this.setState({
116-
fireUpload: false,
117-
hideDialog: false,
118-
dialogMessage: strings.ListItemAttachmentserrorLoadAttachments.replace('{0}', error.message)
114+
await this._spservice.getListItemAttachments(this.props.listId, this.state.itemId).then(async (files: IListItemAttachmentFile[]) => {
115+
await this.loadAttachmentsPreview(files);
116+
}).catch((error: Error) => {
117+
this.setState({
118+
fireUpload: false,
119+
hideDialog: false,
120+
dialogMessage: strings.ListItemAttachmentserrorLoadAttachments.replace('{0}', error.message)
121+
});
119122
});
120-
});
121123
}
122124
else if(this.state.filesToUpload && this.state.filesToUpload.length > 0){
123125
let files = this.state.filesToUpload.map(file=>({
@@ -154,7 +156,7 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
154156
/**
155157
* Attachment uploaded event handler
156158
*/
157-
private _onAttachmentUpload = (file: File) => {
159+
private _onAttachmentUpload = async (file: File) => {
158160
// load Attachments
159161
if(!this.state.itemId){
160162
let files = this.state.filesToUpload || [];
@@ -163,7 +165,7 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
163165
filesToUpload: [...files]
164166
});
165167
}
166-
this.loadAttachments();
168+
await this.loadAttachments();
167169
}
168170

169171
/**

src/controls/listItemAttachments/UploadAttachment.tsx

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,34 @@ export class UploadAttachment extends React.Component<IUploadAttachmentProps, IU
6060

6161
const reader = new FileReader();
6262
const file = e.target.files[0];
63-
64-
reader.onloadend = async () => {
65-
this.setState({
66-
file: file
67-
});
68-
69-
try {
70-
if(this.props.itemId && this.props.itemId > 0){
71-
await this._spservice.addAttachment(this.props.listId, this.props.itemId, file.name, file, this.props.webUrl);
72-
}
73-
74-
this.setState({
75-
isLoading: false
76-
});
77-
this.props.onAttachmentUpload(file);
78-
} catch (error) {
63+
return new Promise<void>((resolve,errorCallback)=>{
64+
reader.onloadend = async () => {
7965
this.setState({
80-
hideDialog: false,
81-
isLoading: false,
82-
dialogMessage: strings.ListItemAttachmentsuploadAttachmentErrorMsg.replace('{0}', file.name).replace('{1}', error.message)
66+
file: file
8367
});
84-
}
85-
};
86-
reader.readAsDataURL(file);
68+
69+
try {
70+
if(this.props.itemId && this.props.itemId > 0){
71+
await this._spservice.addAttachment(this.props.listId, this.props.itemId, file.name, file, this.props.webUrl);
72+
}
73+
74+
this.setState({
75+
isLoading: false
76+
});
77+
this.props.onAttachmentUpload(file);
78+
resolve();
79+
} catch (error) {
80+
this.setState({
81+
hideDialog: false,
82+
isLoading: false,
83+
dialogMessage: strings.ListItemAttachmentsuploadAttachmentErrorMsg.replace('{0}', file.name).replace('{1}', error.message)
84+
});
85+
errorCallback(error);
86+
}
87+
};
88+
reader.readAsDataURL(file);
89+
});
90+
8791
}
8892

8993
/**

tests/controls/documentLibraryBrowser/DocumentLibraryBrowser.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ describe("<DocumentLibraryBrowser />", ()=>{
5353
let libraryTitle = documentLibraryBrowser.instance()._onRenderLibraryTile(browserService.getSiteMediaLibrariesResult[0],0);
5454
let iconControl = libraryTitle.props.children.props.children.props.children[0];
5555
let buttonControl = libraryTitle.props.children.props.children.props.children[1];
56-
assert.equal(iconControl.type,"Image");
57-
assert.equal(buttonControl.type,"DefaultButton");
56+
assert.equal(iconControl.type.displayName,"StyledImageBase");
57+
assert.equal(buttonControl.type.displayName,"CustomizedDefaultButton");
5858
});
5959
test("should call onOpenLibrary", async ()=>{
6060
let asserted = false;

0 commit comments

Comments
 (0)