1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-08-08 06:55:26 +02:00

move methods

This commit is contained in:
Murod Khaydarov 2019-01-19 00:50:01 +03:00
parent 4299123855
commit 022deaf74b
No known key found for this signature in database
GPG key ID: C480BA53A8D274C5
3 changed files with 38 additions and 9 deletions

View file

@ -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();
}
}

View file

@ -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
*

View file

@ -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();
});
});