From 022deaf74b3c81cc03ecdd6c056d41a71ebf135e Mon Sep 17 00:00:00 2001 From: Murod Khaydarov Date: Sat, 19 Jan 2019 00:50:01 +0300 Subject: [PATCH] move methods --- src/controllers/pagesOrder.js | 12 ++++-------- src/models/pageOrder.js | 21 ++++++++++++++++++++- test/models/pageOrder.js | 14 ++++++++++++++ 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/controllers/pagesOrder.js b/src/controllers/pagesOrder.js index 02b3911..d1e6362 100644 --- a/src/controllers/pagesOrder.js +++ b/src/controllers/pagesOrder.js @@ -17,7 +17,7 @@ class PagesOrder { const order = await Model.get(parentId); if (!order._id) { - throw new Error('Page with given id does not exist'); + throw new Error('Page with given id does not contain order'); } return order; @@ -85,14 +85,10 @@ class PagesOrder { * @param {string} putAbovePageId - page's id above which we put the target page */ static async update(currentPageId, parentPageId, putAbovePageId) { - const children = await Model.get(parentPageId); - const found1 = children.order.indexOf(putAbovePageId); - const found2 = children.order.indexOf(currentPageId); - const margin = found1 < found2 ? 1 : 0; + const pageOrder = await Model.get(parentPageId); - children.order.splice(found1, 0, currentPageId); - children.order.splice(found2 + margin, 1); - children.save(); + pageOrder.shift(currentPageId, putAbovePageId); + await pageOrder.save(); } } diff --git a/src/models/pageOrder.js b/src/models/pageOrder.js index 5a0fc11..a47694c 100644 --- a/src/models/pageOrder.js +++ b/src/models/pageOrder.js @@ -77,7 +77,11 @@ class PageOrder { * @param {string} pageId - page's id */ push(pageId) { - this._order.push(pageId); + if (typeof pageId === 'string') { + this._order.push(pageId); + } else { + throw new Error('given id is not string'); + } } /** @@ -93,6 +97,21 @@ class PageOrder { } } + /** + * @param {string} currentPageId - page's id that changes the order + * @param {string} putAbovePageId - page's id above which we put the target page + * + * @returns void + */ + shift(currentPageId, putAbovePageId) { + const found1 = this.order.indexOf(putAbovePageId); + const found2 = this.order.indexOf(currentPageId); + const margin = found1 < found2 ? 1 : 0; + + this.order.splice(found1, 0, currentPageId); + this.order.splice(found2 + margin, 1); + } + /** * Returns ordered list * diff --git a/test/models/pageOrder.js b/test/models/pageOrder.js index 19d5bc6..e492123 100644 --- a/test/models/pageOrder.js +++ b/test/models/pageOrder.js @@ -96,11 +96,21 @@ describe('PageOrder model', () => { const pageOrder = new PageOrder(testData); await pageOrder.save(); pageOrder.push('3'); + expect(pageOrder.data.order).to.be.an('array').that.is.not.empty; + pageOrder.data.order.forEach((el) => { + expect(el).to.be.an('string') + }); + expect(pageOrder.data.order).to.deep.equals(['1', '2', '3']); pageOrder.remove('2'); expect(pageOrder.data.order).to.deep.equals(['1', '3']); + expect(() => { + pageOrder.push(3); + }).to.throw('given id is not string'); + + await pageOrder.destroy(); }); @@ -116,6 +126,10 @@ describe('PageOrder model', () => { expect(insertedPageOrder).to.instanceOf(PageOrder); expect(insertedPageOrder.data._id).to.be.equal(insertedData.data._id); + const emptyInstance = await PageOrder.get(null); + expect(emptyInstance.data.page).to.be.equal('0'); + expect(emptyInstance.data.order).to.be.an('array').that.is.empty; + await pageOrder.destroy(); }); });