mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-08 06:55:26 +02:00
Modified page's tests
This commit is contained in:
parent
82fa1da8ff
commit
dd2033f3c9
2 changed files with 222 additions and 4 deletions
|
@ -4,6 +4,7 @@ const path = require('path');
|
|||
const config = require('../../config');
|
||||
const Page = require('../../src/models/page');
|
||||
const {pages} = require('../../src/utils/database');
|
||||
const translateString = require('../../src/utils/translation');
|
||||
|
||||
describe('Page model', () => {
|
||||
after(() => {
|
||||
|
@ -23,6 +24,7 @@ describe('Page model', () => {
|
|||
|
||||
expect(data._id).to.be.undefined;
|
||||
expect(data.title).to.be.empty;
|
||||
expect(data.uri).to.be.empty;
|
||||
expect(data.body).to.be.undefined;
|
||||
expect(data.parent).to.be.undefined;
|
||||
|
||||
|
@ -32,6 +34,7 @@ describe('Page model', () => {
|
|||
|
||||
expect(data._id).to.be.undefined;
|
||||
expect(data.title).to.be.empty;
|
||||
expect(data.uri).to.be.empty;
|
||||
expect(data.body).to.be.undefined;
|
||||
expect(data.parent).to.be.undefined;
|
||||
|
||||
|
@ -57,11 +60,19 @@ describe('Page model', () => {
|
|||
|
||||
expect(data._id).to.equal(initialData._id);
|
||||
expect(data.title).to.equal(initialData.body.blocks[0].data.text);
|
||||
expect(data.uri).to.equal(translateString(initialData.body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
expect(data.body).to.deep.equal(initialData.body);
|
||||
expect(data.parent).to.be.undefined;
|
||||
|
||||
expect(json._id).to.equal(initialData._id);
|
||||
expect(json.title).to.equal(initialData.body.blocks[0].data.text);
|
||||
expect(json.title).to.equal(initialData.body.blocks[0].data.text);
|
||||
expect(json.body).to.deep.equal(initialData.body);
|
||||
expect(json.parent).to.be.undefined;
|
||||
|
||||
|
@ -85,6 +96,13 @@ describe('Page model', () => {
|
|||
|
||||
expect(data._id).to.equal(initialData._id);
|
||||
expect(data.title).to.equal(update.body.blocks[0].data.text);
|
||||
expect(data.uri).to.equal(translateString(update.body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
expect(data.body).to.equal(update.body);
|
||||
expect(data.parent).to.be.undefined;
|
||||
});
|
||||
|
@ -96,7 +114,7 @@ describe('Page model', () => {
|
|||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header'
|
||||
text: 'New page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
|
@ -108,6 +126,13 @@ describe('Page model', () => {
|
|||
|
||||
expect(savedPage._id).not.be.undefined;
|
||||
expect(savedPage.title).to.equal(initialData.body.blocks[0].data.text);
|
||||
expect(savedPage.uri).to.equal(translateString(initialData.body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
expect(savedPage.body).to.equal(initialData.body);
|
||||
expect(page._id).not.be.undefined;
|
||||
|
||||
|
@ -115,6 +140,7 @@ describe('Page model', () => {
|
|||
|
||||
expect(insertedPage._id).to.equal(page._id);
|
||||
expect(insertedPage.title).to.equal(page.title);
|
||||
expect(insertedPage.uri).to.equal(page.uri);
|
||||
expect(insertedPage.body).to.deep.equal(page.body);
|
||||
|
||||
const updateData = {
|
||||
|
@ -127,7 +153,8 @@ describe('Page model', () => {
|
|||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
uri: 'updated-uri'
|
||||
};
|
||||
|
||||
page.data = updateData;
|
||||
|
@ -139,6 +166,7 @@ describe('Page model', () => {
|
|||
|
||||
expect(updatedPage._id).to.equal(savedPage._id);
|
||||
expect(updatedPage.title).to.equal(updateData.body.blocks[0].data.text);
|
||||
expect(updatedPage.uri).to.equal(updateData.uri);
|
||||
expect(updatedPage.body).to.deep.equal(updateData.body);
|
||||
|
||||
await page.destroy();
|
||||
|
@ -150,6 +178,51 @@ describe('Page model', () => {
|
|||
expect(removedPage).to.be.null;
|
||||
});
|
||||
|
||||
it('Handle multiple page creation with the same uri', async () => {
|
||||
const initialData = {
|
||||
body: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'New page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
const firstPage = new Page(initialData);
|
||||
let firstSavedPage = await firstPage.save();
|
||||
const secondPage = new Page(initialData);
|
||||
let secondSavedPage = await secondPage.save();
|
||||
|
||||
expect(secondSavedPage.uri).to.equal(translateString(initialData.body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')) + '-1');
|
||||
|
||||
const newUri = 'new-uri';
|
||||
|
||||
firstPage.data = {...firstPage.data, uri: newUri};
|
||||
firstSavedPage = await firstPage.save();
|
||||
|
||||
expect(firstSavedPage.uri).to.equal(newUri);
|
||||
|
||||
const thirdPage = new Page(initialData);
|
||||
let thirdSavedPage = await thirdPage.save();
|
||||
|
||||
expect(thirdSavedPage.uri).to.equal(translateString(initialData.body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
});
|
||||
|
||||
it('Static get method', async () => {
|
||||
const initialData = {
|
||||
body: {
|
||||
|
@ -173,6 +246,13 @@ describe('Page model', () => {
|
|||
|
||||
expect(data._id).to.equal(savedPage._id);
|
||||
expect(data.title).to.equal(initialData.body.blocks[0].data.text);
|
||||
expect(data.uri).to.equal(translateString(initialData.body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
expect(data.body).to.deep.equal(initialData.body);
|
||||
|
||||
await page.destroy();
|
||||
|
@ -213,6 +293,13 @@ describe('Page model', () => {
|
|||
expect(foundPages.length).to.equal(2);
|
||||
foundPages.forEach((page, i) => {
|
||||
expect(page.title).to.equal(pagesToSave[i].body.blocks[0].data.text);
|
||||
expect(page.uri).to.equal(translateString(pagesToSave[i].body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
expect(page.body).to.deep.equal(pagesToSave[i].body);
|
||||
});
|
||||
});
|
||||
|
@ -257,6 +344,13 @@ describe('Page model', () => {
|
|||
|
||||
expect(testedParent._id).to.equal(parentId);
|
||||
expect(testedParent.title).to.equal(parent.body.blocks[0].data.text);
|
||||
expect(testedParent.uri).to.equal(translateString(parent.body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
expect(testedParent.body).to.deep.equal(parent.body);
|
||||
|
||||
const children = await parent.children;
|
||||
|
@ -267,6 +361,13 @@ describe('Page model', () => {
|
|||
|
||||
expect(testedChild._id).to.equal(childId);
|
||||
expect(testedChild.title).to.equal(child.body.blocks[0].data.text);
|
||||
expect(testedChild.uri).to.equal(translateString(child.body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
expect(testedChild.body).to.deep.equal(child.body);
|
||||
expect(testedChild._parent).to.equal(child._parent);
|
||||
expect(testedChild._parent).to.equal(parent._id);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const {app} = require('../../bin/www');
|
||||
const model = require('../../src/models/page');
|
||||
const translateString = require('../../src/utils/translation');
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
@ -49,6 +50,13 @@ describe('Pages REST: ', () => {
|
|||
expect(success).to.be.true;
|
||||
expect(result._id).to.be.a('string');
|
||||
expect(result.title).to.equal(body.blocks[0].data.text);
|
||||
expect(result.uri).to.equal(translateString(body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
expect(result.body).to.deep.equal(body);
|
||||
|
||||
const createdPage = await model.get(result._id);
|
||||
|
@ -56,6 +64,13 @@ describe('Pages REST: ', () => {
|
|||
expect(createdPage).not.be.null;
|
||||
expect(createdPage._id).to.equal(result._id);
|
||||
expect(createdPage.title).to.equal(body.blocks[0].data.text);
|
||||
expect(createdPage.uri).to.equal(translateString(body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
expect(createdPage.body).to.deep.equal(body);
|
||||
|
||||
createdPage.destroy();
|
||||
|
@ -109,6 +124,13 @@ describe('Pages REST: ', () => {
|
|||
|
||||
expect(foundPage._id).to.equal(_id);
|
||||
expect(foundPage.title).to.equal(body.blocks[0].data.text);
|
||||
expect(foundPage.uri).to.equal(translateString(body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
expect(foundPage.body).to.deep.equal(body);
|
||||
|
||||
foundPage.destroy();
|
||||
|
@ -157,10 +179,11 @@ describe('Pages REST: ', () => {
|
|||
}
|
||||
]
|
||||
};
|
||||
const updatedUri = 'updated-uri';
|
||||
|
||||
res = await agent
|
||||
.post(`/api/page/${_id}`)
|
||||
.send({body: updatedBody});
|
||||
.send({body: updatedBody, uri: updatedUri});
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
@ -171,6 +194,14 @@ describe('Pages REST: ', () => {
|
|||
expect(result._id).to.equal(_id);
|
||||
expect(result.title).not.equal(body.blocks[0].data.text);
|
||||
expect(result.title).to.equal(updatedBody.blocks[0].data.text);
|
||||
expect(result.uri).not.equal(translateString(body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
expect(result.uri).to.equal(updatedUri);
|
||||
expect(result.body).not.equal(body);
|
||||
expect(result.body).to.deep.equal(updatedBody);
|
||||
|
||||
|
@ -179,12 +210,91 @@ describe('Pages REST: ', () => {
|
|||
expect(updatedPage._id).to.equal(_id);
|
||||
expect(updatedPage.title).not.equal(body.blocks[0].data.text);
|
||||
expect(updatedPage.title).to.equal(updatedBody.blocks[0].data.text);
|
||||
expect(updatedPage.uri).not.equal(translateString(body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
expect(updatedPage.uri).to.equal(updatedUri);
|
||||
expect(updatedPage.body).not.equal(body);
|
||||
expect(updatedPage.body).to.deep.equal(updatedBody);
|
||||
|
||||
updatedPage.destroy();
|
||||
});
|
||||
|
||||
it('Handle multiple page creation with the same uri', async () => {
|
||||
const body = {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header'
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
let res = await agent
|
||||
.put('/api/page')
|
||||
.send({body});
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {result: {_id}} = res.body;
|
||||
|
||||
res = await agent
|
||||
.put('/api/page')
|
||||
.send({body: body});
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {success: secondPageSuccess, result: secondPageResult} = res.body;
|
||||
|
||||
expect(secondPageSuccess).to.be.true;
|
||||
expect(secondPageResult.title).to.equal(body.blocks[0].data.text);
|
||||
expect(secondPageResult.uri).to.equal(translateString(body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')) + '-1');
|
||||
expect(secondPageResult.body).to.deep.equal(body);
|
||||
|
||||
const newFirstPageUri = 'New uri';
|
||||
|
||||
res = await agent
|
||||
.post(`/api/page/${_id}`)
|
||||
.send({body: body, uri: newFirstPageUri});
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
res = await agent
|
||||
.put('/api/page')
|
||||
.send({body: body});
|
||||
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.be.json;
|
||||
|
||||
const {success: thirdPageSuccess, result: thirdPageResult} = res.body;
|
||||
|
||||
expect(thirdPageSuccess).to.be.true;
|
||||
expect(thirdPageResult.title).to.equal(body.blocks[0].data.text);
|
||||
expect(thirdPageResult.uri).to.equal(translateString(body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
expect(thirdPageResult.body).to.deep.equal(body);
|
||||
});
|
||||
|
||||
it('Updating page with not existing id', async () => {
|
||||
const res = await agent
|
||||
.post('/api/page/not-existing-id')
|
||||
|
@ -214,7 +324,7 @@ describe('Pages REST: ', () => {
|
|||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header'
|
||||
text: 'Page header to be deleted'
|
||||
}
|
||||
}
|
||||
]
|
||||
|
@ -240,6 +350,13 @@ describe('Pages REST: ', () => {
|
|||
expect(success).to.be.true;
|
||||
expect(result._id).to.be.undefined;
|
||||
expect(result.title).to.equal(body.blocks[0].data.text);
|
||||
expect(result.uri).to.equal(translateString(body.blocks[0].data.text
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/-/g, ' ')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
.join('-')));
|
||||
expect(result.body).to.deep.equal(body);
|
||||
|
||||
const deletedPage = await model.get(_id);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue