Skip to content

Commit

Permalink
Desktop: Fix show-all-notes update bugs. (#2642)
Browse files Browse the repository at this point in the history
  • Loading branch information
miciasto committed Mar 15, 2020
1 parent 91d864b commit 7fb061e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
77 changes: 77 additions & 0 deletions CliClient/tests/integration_ShowAllNotes.js
Expand Up @@ -90,4 +90,81 @@ describe('integration_ShowAllNotes', function() {
expect(sortedIds(state.notes)).toEqual(sortedIds(notes0.concat(notes1)));
expect(state.selectedNoteIds).toEqual(ids([notes1[1]]));
}));

it('should support note duplication', asyncTest(async () => {
// setup
let folder1 = await Folder.save({ title: 'folder1' });
let folder2 = await Folder.save({ title: 'folder2' });
let note1 = await Note.save({ title: 'note1', parent_id: folder1.id });
let note2 = await Note.save({ title: 'note2', parent_id: folder2.id });
testApp.dispatch({ type: 'FOLDER_SELECT', id: folder1.id }); // active folder
await time.msleep(100);
testApp.dispatch({ type: 'NOTE_SELECT', id: note1.id });
await time.msleep(100);
testApp.dispatch({ type: 'SMART_FILTER_SELECT', id: ALL_NOTES_FILTER_ID });
await time.msleep(100);

// check the state is set up as expected
let state = testApp.store().getState();
expect(state.notesParentType).toEqual('SmartFilter');
expect(sortedIds(state.notes)).toEqual(sortedIds([note1, note2]));

// TEST ACTION: duplicate a note from the active folder
const newNote1 = await Note.duplicate(note1.id);
await time.msleep(100);

// check the note is duplicated and the view updated
state = testApp.store().getState();
expect(state.notes.length).toEqual(3);
expect(sortedIds(state.notes)).toEqual(sortedIds([note1, note2, newNote1]));

// TEST ACTION: duplicate a note from a non-active folder
const newNote2 = await Note.duplicate(note2.id);
await time.msleep(100);

// check the note is duplicated and the view updated
state = testApp.store().getState();
expect(state.notes.length).toEqual(4);
expect(sortedIds(state.notes)).toEqual(sortedIds([note1, note2, newNote1, newNote2]));
}));

it('should support changing the note parent', asyncTest(async () => {
// setup
let folder1 = await Folder.save({ title: 'folder1' });
let folder2 = await Folder.save({ title: 'folder2' });
let note1 = await Note.save({ title: 'note1', parent_id: folder1.id });
let note2 = await Note.save({ title: 'note1', parent_id: folder2.id });
testApp.dispatch({ type: 'FOLDER_SELECT', id: folder1.id }); // active folder
await time.msleep(100);
testApp.dispatch({ type: 'NOTE_SELECT', id: note1.id });
await time.msleep(100);
testApp.dispatch({ type: 'SMART_FILTER_SELECT', id: ALL_NOTES_FILTER_ID });
await time.msleep(100);

// check the state is set up as expected
let state = testApp.store().getState();
expect(state.notesParentType).toEqual('SmartFilter');
expect(sortedIds(state.notes)).toEqual(sortedIds([note1, note2]));
expect(note1.parent_id).toEqual(folder1.id);

// TEST ACTION: change the notes parent
await Note.moveToFolder(note1.id, folder2.id);
await time.msleep(100);

// check the note is duplicated and the view updated
state = testApp.store().getState();
expect(state.notes.length).toEqual(2);
let n1 = await Note.load(note1.id);
expect(n1.parent_id).toEqual(folder2.id);

// TEST ACTION: change the notes parent
await Note.moveToFolder(note1.id, folder1.id);
await time.msleep(100);

// check the note is duplicated and the view updated
state = testApp.store().getState();
expect(state.notes.length).toEqual(2);
n1 = await Note.load(note1.id);
expect(n1.parent_id).toEqual(folder1.id);
}));
});
6 changes: 4 additions & 2 deletions ReactNativeClient/lib/reducer.js
@@ -1,6 +1,7 @@
const Note = require('lib/models/Note.js');
const Folder = require('lib/models/Folder.js');
const ArrayUtils = require('lib/ArrayUtils.js');
const { ALL_NOTES_FILTER_ID } = require('lib/reserved-ids');

const defaultState = {
notes: [],
Expand Down Expand Up @@ -524,6 +525,7 @@ const reducer = (state = defaultState, action) => {
case 'NOTE_UPDATE_ONE':
{
const modNote = action.note;
const isViewingAllNotes = (state.notesParentType === 'SmartFilter' && state.selectedSmartFilterId === ALL_NOTES_FILTER_ID);

const noteIsInFolder = function(note, folderId) {
if (note.is_conflict) return folderId === Folder.conflictFolderId();
Expand All @@ -539,7 +541,7 @@ const reducer = (state = defaultState, action) => {
const n = newNotes[i];
if (n.id == modNote.id) {
// Note is still in the same folder
if (noteIsInFolder(modNote, n.parent_id)) {
if (isViewingAllNotes || noteIsInFolder(modNote, n.parent_id)) {
// Merge the properties that have changed (in modNote) into
// the object we already have.
newNotes[i] = Object.assign({}, newNotes[i]);
Expand All @@ -562,7 +564,7 @@ const reducer = (state = defaultState, action) => {
// Note was not found - if the current folder is the same as the note folder,
// add it to it.
if (!found) {
if (noteIsInFolder(modNote, state.selectedFolderId)) {
if (isViewingAllNotes || noteIsInFolder(modNote, state.selectedFolderId)) {
newNotes.push(modNote);
}
}
Expand Down

0 comments on commit 7fb061e

Please sign in to comment.