1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Fix to update tag list when note is deleted. (#2554)

* Ensure tag list updated when note is deleted.

Add integration tests.

* Revert whitespace changes.
This commit is contained in:
mic704b 2020-02-27 10:42:14 +11:00 committed by GitHub
parent ef8af13500
commit 3830f5ee46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 140 additions and 1 deletions

View File

@ -0,0 +1,138 @@
/* eslint-disable no-unused-vars */
require('app-module-path').addPath(__dirname);
const { setupDatabaseAndSynchronizer, switchClient, asyncTest, TestApp } = require('test-utils.js');
const Setting = require('lib/models/Setting.js');
const Folder = require('lib/models/Folder.js');
const Note = require('lib/models/Note.js');
const Tag = require('lib/models/Tag.js');
const { time } = require('lib/time-utils.js');
async function createNTestFolders(n) {
let folders = [];
for (let i = 0; i < n; i++) {
let folder = await Folder.save({ title: 'folder' });
folders.push(folder);
}
return folders;
}
async function createNTestNotes(n, folder) {
let notes = [];
for (let i = 0; i < n; i++) {
let note = await Note.save({ title: 'note', parent_id: folder.id, is_conflict: 0 });
notes.push(note);
}
return notes;
}
async function createNTestTags(n) {
let tags = [];
for (let i = 0; i < n; i++) {
let tag = await Tag.save({ title: 'tag' });
tags.push(tag);
}
return tags;
}
// use this until Javascript arr.flat() function works in Travis
function flatten(arr) {
return (arr.reduce((acc, val) => acc.concat(val), []));
}
let testApp = null;
describe('integration_TagList', function() {
beforeEach(async (done) => {
testApp = new TestApp();
await testApp.start(['--no-welcome']);
done();
});
afterEach(async (done) => {
if (testApp !== null) await testApp.destroy();
testApp = null;
done();
});
// the tag list should be cleared if the next note has no tags
it('should clear tag list when a note is deleted', asyncTest(async () => {
// setup and select the note
let folders = await createNTestFolders(1);
let notes = await createNTestNotes(5, folders[0]);
let tags = await createNTestTags(3);
await Tag.addNote(tags[2].id, notes[2].id);
testApp.dispatch({
type: 'FOLDER_SELECT',
id: folders[0].id,
});
await time.msleep(100);
testApp.dispatch({
type: 'NOTE_SELECT',
id: notes[2].id,
});
await time.msleep(100);
// check the tag list is correct
let state = testApp.store().getState();
expect(state.selectedNoteTags.length).toEqual(1);
expect(state.selectedNoteTags[0].id).toEqual(tags[2].id);
// delete the note
testApp.dispatch({
type: 'NOTE_DELETE',
id: notes[2].id,
});
await time.msleep(100);
// check the tag list is updated
state = testApp.store().getState();
expect(state.selectedNoteTags.length).toEqual(0);
}));
// the tag list should be updated if the next note has tags
it('should update tag list when a note is deleted', asyncTest(async () => {
// set up and select the note
let folders = await createNTestFolders(1);
let notes = await createNTestNotes(5, folders[0]);
let tags = await createNTestTags(3);
await Tag.addNote(tags[1].id, notes[1].id);
await Tag.addNote(tags[0].id, notes[0].id);
await Tag.addNote(tags[2].id, notes[0].id);
testApp.dispatch({
type: 'FOLDER_SELECT',
id: folders[0].id,
});
await time.msleep(100);
testApp.dispatch({
type: 'NOTE_SELECT',
id: notes[1].id,
});
await time.msleep(100);
// check the tag list is correct
let state = testApp.store().getState();
expect(state.selectedNoteTags.length).toEqual(1);
expect(state.selectedNoteTags[0].id).toEqual(tags[1].id);
// delete the note
testApp.dispatch({
type: 'NOTE_DELETE',
id: notes[1].id,
});
await time.msleep(100);
// check the tag list is updated
state = testApp.store().getState();
let tagIds = state.selectedNoteTags.map(n => n.id).sort();
let expectedTagIds = [tags[0].id, tags[2].id].sort();
expect(state.selectedNoteTags.length).toEqual(2);
expect(tagIds).toEqual(expectedTagIds);
}));
});

View File

@ -28,7 +28,8 @@ const reduxSharedMiddleware = async function(store, next, action) {
refreshTags = true; refreshTags = true;
} }
if (action.type === 'NOTE_SELECT' || if (action.type === 'NOTE_DELETE' ||
action.type === 'NOTE_SELECT' ||
action.type === 'NOTE_SELECT_TOGGLE' || action.type === 'NOTE_SELECT_TOGGLE' ||
action.type === 'NOTE_SET_NEW_ONE') { action.type === 'NOTE_SET_NEW_ONE') {
let noteTags = []; let noteTags = [];