Alternating uuid logic and tests

This commit is contained in:
Mo Bitar
2018-07-02 11:11:56 -05:00
parent 7625e037fe
commit 82b1586fd7
4 changed files with 73 additions and 16 deletions

View File

@@ -154,7 +154,7 @@ class AccountMenu {
}
else {
modelManager.resetLocalMemory();
storageManager.clearAllModels().them(() => {
storageManager.clearAllModels().then(() => {
block();
})
}

View File

@@ -83,29 +83,27 @@ class ModelManager extends SFModelManager {
// remove from relevant array, but don't remove from all items.
// This way, it's removed from the display, but still synced via get dirty items
if(item.content_type == "Tag") {
_.pull(this.tags, item);
} else if(item.content_type == "Note") {
_.pull(this.notes, item);
} else if(item.content_type == "Extension") {
_.pull(this._extensions, item);
}
this.removeItemFromRespectiveArray(item);
}
removeItemLocally(item, callback) {
super.removeItemLocally(item, callback);
if(item.content_type == "Tag") {
_.pull(this.tags, item);
} else if(item.content_type == "Note") {
_.pull(this.notes, item);
} else if(item.content_type == "Extension") {
_.pull(this._extensions, item);
}
this.removeItemFromRespectiveArray(item);
this.storageManager.deleteModel(item).then(callback);
}
removeItemFromRespectiveArray(item) {
if(item.content_type == "Tag") {
_.remove(this.tags, {uuid: item.uuid});
} else if(item.content_type == "Note") {
_.remove(this.notes, {uuid: item.uuid});
} else if(item.content_type == "Extension") {
_.remove(this._extensions, {uuid: item.uuid});
}
}
/*
Misc
*/

View File

@@ -53,7 +53,7 @@ export default class LocalStorageManager extends SFStorageManager {
// clear only models
for(var key in localStorage) {
if(key.startsWith("item-")) {
this.removeItem(`item-${item.uuid}`);
this.removeItem(key);
}
}
}

View File

@@ -290,6 +290,65 @@ describe("syncing", () => {
}
}).timeout(10000);
it("handles signing in and merging data", async () => {
let syncManager = new SFSyncManager(modelManager, Factory.globalStorageManager(), Factory.globalHttpManager());
syncManager.setEventHandler(() => {})
// be offline
syncManager.setKeyRequestHandler(async () => {
return {
offline: true
};
})
modelManager.resetLocalMemory();
let pair = createRelatedNoteTagPair();
let noteParams = pair[0];
let tagParams = pair[1];
modelManager.mapResponseItemsToLocalModels([noteParams, tagParams]);
let originalNote = modelManager.allItemsMatchingTypes(["Note"])[0];
let originalTag = modelManager.allItemsMatchingTypes(["Tag"])[0];
originalNote.setDirty(true);
originalTag.setDirty(true);
await syncManager.sync();
expect(originalTag.content.references.length).to.equal(1);
expect(originalTag.notes.length).to.equal(1);
expect(originalNote.tags.length).to.equal(1);
// go online
syncManager.setKeyRequestHandler(async () => {
return {
keys: await authManager.keys(),
offline: false
};
})
// when signing in, all local items are cleared from storage (but kept in memory; to clear desktop logs),
// then resaved with alternated uuids.
await Factory.globalStorageManager().clearAllModels();
return expect(syncManager.markAllItemsDirtyAndSaveOffline(true)).to.be.fulfilled.then(() => {
let note = modelManager.allItemsMatchingTypes(["Note"])[0];
let tag = modelManager.allItemsMatchingTypes(["Tag"])[0];
expect(modelManager.allItems.length).to.equal(2);
expect(note.uuid).to.not.equal(originalNote.uuid);
expect(tag.uuid).to.not.equal(originalTag.uuid);
expect(tag.content.references.length).to.equal(1);
expect(note.content.references.length).to.equal(0);
expect(note.referencingObjects.length).to.equal(1);
expect(tag.notes.length).to.equal(1);
expect(note.tags.length).to.equal(1);
});
})
it('duplicating a tag should maintian its relationships', async () => {
modelManager.resetLocalMemory();
let pair = createRelatedNoteTagPair();