Skip to content

Commit 5193663

Browse files
authored
Patch only (#422)
* Send patched fields only in patch payload * Add tests
1 parent c935c45 commit 5193663

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

scilog/src/app/logbook/core/add-content/add-content.component.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,13 @@ describe('AddContentComponent', () => {
136136
component.editor = jasmine.createSpyObj("component.editor", ["getData"])
137137
component.notification.snippetType = 'edit';
138138
component.contentChanged = true;
139+
const sendEditMessageSpy = spyOn<any>(component, 'sendEditMessage');
139140
const setFromLocalStorageSpy = spyOn<any>(component, 'setFromLocalStorage');
140141
component.addContent("");
141142
expect(component.editor.getData).toHaveBeenCalled();
142143
expect(component.notification.snippetType).toEqual('paragraph');
143144
expect(setFromLocalStorageSpy).toHaveBeenCalledTimes(0);
145+
expect(sendEditMessageSpy).toHaveBeenCalledTimes(0);
144146
});
145147

146148
it('should prepare quote', () => {
@@ -381,6 +383,19 @@ describe('AddContentComponent', () => {
381383
expect(localStorageSpy).toHaveBeenCalledOnceWith('123_message', 'edit');
382384
});
383385

386+
it('should test sendEditMessage', () => {
387+
component.notification = notificationMock;
388+
component.data = 'abc';
389+
const sendMessageSpy = spyOn(component, 'sendMessage');
390+
component['sendEditMessage']();
391+
expect(sendMessageSpy).toHaveBeenCalledOnceWith({
392+
textcontent: 'abc',
393+
id: '6061d9a13587f37b851694d5',
394+
linkType: 'paragraph',
395+
files: [],
396+
});
397+
});
398+
384399
function combineHtmlFigureHash(figureMock: string[], hash: string) {
385400
return (figureMock[0] + ' title="' + hash + '"' + figureMock[1]);
386401
}

scilog/src/app/logbook/core/add-content/add-content.component.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,18 @@ export class AddContentComponent implements OnInit {
166166
this.notification.snippetType = 'paragraph';
167167
localStorage.removeItem(`${this.message?.id}_message`);
168168
localStorage.removeItem(`${this.message?.id}_tags`);
169+
if (this.dialogTitle === 'Modify data snippet')
170+
return this.sendEditMessage();
169171
this.prepareMessage(this.data);
170172
this.sendMessage();
171173
};
172174

175+
private sendEditMessage() {
176+
const notification = extractNotificationMessage(this.data, this.message?.files ? this.message.files : []);
177+
notification.id = this.notification.id;
178+
this.sendMessage(notification);
179+
}
180+
173181
onChange({ editor }: ChangeEvent) {
174182
localStorage.setItem(`${this.message.id}_message`, editor.getData());
175183
this.contentChanged = true;
@@ -224,9 +232,9 @@ export class AddContentComponent implements OnInit {
224232
}
225233
}
226234

227-
sendMessage() {
235+
sendMessage(message: ChangeStreamNotification = undefined) {
228236
console.log(this.notification);
229-
this.dataService.changeMessage(this.notification);
237+
this.dataService.changeMessage(message ?? this.notification);
230238
}
231239

232240
adjustContentForEditor() {

scilog/src/app/logbook/widgets/logbook-item/logbook-item.component.spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,6 @@ describe('LogbookItemComponent', () => {
471471

472472
let res = component._preparePatchPayload(referenceEntry, msg);
473473
// I guess it should only take these variables if they are not defined in the msg...
474-
expect(res.accessGroups).toEqual(referenceEntry.accessGroups);
475-
expect(res.ownerGroup).toEqual(referenceEntry.ownerGroup);
476474
expect(res.isPrivate).toEqual(referenceEntry.isPrivate);
477475
expect(res.tags).toEqual(msg.tags);
478476
expect(res.files).toEqual([]);

scilog/src/app/logbook/widgets/logbook-item/logbook-item.component.ts

-6
Original file line numberDiff line numberDiff line change
@@ -474,13 +474,9 @@ export class LogbookItemComponent implements OnInit {
474474
// in the future, this should be extended to support inserting snippets below/above other snippets
475475
// for now, I just take the last array entry
476476
let referenceEntry: ChangeStreamNotification = {};
477-
// if (this.childSnippets.toArray().length == 0) {
478477
referenceEntry.ownerGroup = this.logbookInfo.logbookInfo.ownerGroup;
479478
referenceEntry.accessGroups = this.logbookInfo.logbookInfo.accessGroups;
480479
referenceEntry.isPrivate = this.logbookInfo.logbookInfo.isPrivate;
481-
// } else {
482-
// referenceEntry = this.childSnippets.toArray()[this.childSnippets.toArray().length - 1].snippet;
483-
// }
484480
if (msg.snippetType === "edit") {
485481
// POST -- EDIT SNIPPET
486482
let payload: ChangeStreamNotification = this._prepareEditPostPayload(referenceEntry, msg);
@@ -513,8 +509,6 @@ export class LogbookItemComponent implements OnInit {
513509
// I guess it should only take these variables if they are not defined in the msg...
514510
console.log("referenceEntry: ", referenceEntry)
515511
let payload: ChangeStreamNotification = {
516-
ownerGroup: referenceEntry.ownerGroup,
517-
accessGroups: referenceEntry.accessGroups,
518512
isPrivate: referenceEntry.isPrivate,
519513
tags: msg.tags,
520514
snippetType: "paragraph",

0 commit comments

Comments
 (0)