mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-07-24 15:49:42 +02:00
Move testing to 3001 port (#8)
Create separate database for testing Add runtime configuration file support
This commit is contained in:
parent
073772c047
commit
452d0ae816
17 changed files with 547 additions and 36 deletions
|
@ -1,11 +1,12 @@
|
|||
const fs = require('fs');
|
||||
const config = require('../config');
|
||||
const {expect} = require('chai');
|
||||
|
||||
const {class: Database} = require('../src/utils/database');
|
||||
const Datastore = require('nedb');
|
||||
|
||||
describe('Database', () => {
|
||||
const pathToDB = './.db/test.db';
|
||||
const pathToDB = `./${config.database}/test.db`;
|
||||
let nedbInstance;
|
||||
let db;
|
||||
|
||||
|
|
|
@ -1,8 +1,19 @@
|
|||
const {expect} = require('chai');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const config = require('../../config');
|
||||
const Page = require('../../src/models/page');
|
||||
const {pages} = require('../../src/utils/database');
|
||||
|
||||
describe('Page model', () => {
|
||||
after(() => {
|
||||
const pathToDB = path.resolve(__dirname, '../../', config.database, './pages.db');
|
||||
|
||||
if (fs.existsSync(pathToDB)) {
|
||||
fs.unlinkSync(pathToDB);
|
||||
}
|
||||
});
|
||||
|
||||
it('Working with empty model', async () => {
|
||||
let page = new Page();
|
||||
|
||||
|
|
262
test/rcparser.js
Normal file
262
test/rcparser.js
Normal file
|
@ -0,0 +1,262 @@
|
|||
const {expect} = require('chai');
|
||||
|
||||
require('mocha-sinon');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const config = require('../config');
|
||||
const rcParser = require('../src/utils/rcparser');
|
||||
|
||||
const rcPath = path.resolve(process.cwd(), config.rcFile);
|
||||
|
||||
describe('RC file parser test', () => {
|
||||
beforeEach(function () {
|
||||
this.sinon.stub(console, 'log');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (fs.existsSync(rcPath)) {
|
||||
fs.unlinkSync(rcPath);
|
||||
}
|
||||
});
|
||||
|
||||
it('Default config', async () => {
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig).to.be.deep.equal(rcParser.DEFAULTS);
|
||||
});
|
||||
|
||||
it('Invalid JSON formatted config', () => {
|
||||
const invalidJson = '{title: "Codex Docs"}';
|
||||
|
||||
fs.writeFileSync(rcPath, invalidJson, 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(console.log.calledOnce).to.be.true;
|
||||
expect(console.log.calledWith('CodeX Docs rc file should be in JSON format.')).to.be.true;
|
||||
|
||||
expect(parsedConfig).to.be.deep.equal(rcParser.DEFAULTS);
|
||||
});
|
||||
|
||||
it('Normal config', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{title: 'Option 1', uri: '/option1'},
|
||||
{title: 'Option 2', uri: '/option2'},
|
||||
{title: 'Option 3', uri: '/option3'}
|
||||
]
|
||||
};
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig).to.be.deep.equal(normalConfig);
|
||||
});
|
||||
|
||||
it('Missed title', () => {
|
||||
const normalConfig = {
|
||||
menu: [
|
||||
{title: 'Option 1', uri: '/option1'},
|
||||
{title: 'Option 2', uri: '/option2'},
|
||||
{title: 'Option 3', uri: '/option3'}
|
||||
]
|
||||
};
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig.menu).to.be.deep.equal(normalConfig.menu);
|
||||
expect(parsedConfig.title).to.be.equal(rcParser.DEFAULTS.title);
|
||||
});
|
||||
|
||||
it('Missed menu', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation'
|
||||
};
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(rcParser.DEFAULTS.menu);
|
||||
});
|
||||
|
||||
it('Menu is not an array', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: {
|
||||
0: {title: 'Option 1', uri: '/option1'},
|
||||
1: {title: 'Option 2', uri: '/option2'},
|
||||
2: {title: 'Option 3', uri: '/option3'}
|
||||
}
|
||||
};
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(console.log.calledOnce).to.be.true;
|
||||
expect(console.log.calledWith('Menu section in the rc file must be an array.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(rcParser.DEFAULTS.menu);
|
||||
});
|
||||
|
||||
it('Menu option is a string', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
'Option 1',
|
||||
{title: 'Option 2', uri: '/option2'},
|
||||
{title: 'Option 3', uri: '/option3'}
|
||||
]
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{title: 'Option 1', uri: '/option-1'},
|
||||
{title: 'Option 2', uri: '/option2'},
|
||||
{title: 'Option 3', uri: '/option3'}
|
||||
];
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
});
|
||||
|
||||
it('Menu option is not a string or an object', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
[ {title: 'Option 1', uri: '/option1'} ],
|
||||
{title: 'Option 2', uri: '/option2'},
|
||||
{title: 'Option 3', uri: '/option3'}
|
||||
]
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{title: 'Option 2', uri: '/option2'},
|
||||
{title: 'Option 3', uri: '/option3'}
|
||||
];
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(console.log.calledOnce).to.be.true;
|
||||
expect(console.log.calledWith('Menu option #1 in rc file must be a string or an object')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
});
|
||||
|
||||
it('Menu option title is undefined', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{uri: '/option1'},
|
||||
{title: 'Option 2', uri: '/option2'},
|
||||
{title: 'Option 3', uri: '/option3'}
|
||||
]
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{title: 'Option 2', uri: '/option2'},
|
||||
{title: 'Option 3', uri: '/option3'}
|
||||
];
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(console.log.calledOnce).to.be.true;
|
||||
expect(console.log.calledWith('Menu option #1 title must be a string.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
});
|
||||
|
||||
it('Menu option title is not a string', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{title: [], uri: '/option1'},
|
||||
{title: 'Option 2', uri: '/option2'},
|
||||
{title: 'Option 3', uri: '/option3'}
|
||||
]
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{title: 'Option 2', uri: '/option2'},
|
||||
{title: 'Option 3', uri: '/option3'}
|
||||
];
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(console.log.calledOnce).to.be.true;
|
||||
expect(console.log.calledWith('Menu option #1 title must be a string.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
});
|
||||
|
||||
it('Menu option uri is undefined', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{title: 'Option 1'},
|
||||
{title: 'Option 2', uri: '/option2'},
|
||||
{title: 'Option 3', uri: '/option3'}
|
||||
]
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{title: 'Option 2', uri: '/option2'},
|
||||
{title: 'Option 3', uri: '/option3'}
|
||||
];
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(console.log.calledOnce).to.be.true;
|
||||
expect(console.log.calledWith('Menu option #1 uri must be a string.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
});
|
||||
|
||||
it('Menu option title is not a string', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{title: 'Option 1', uri: []},
|
||||
{title: 'Option 2', uri: '/option2'},
|
||||
{title: 'Option 3', uri: '/option3'}
|
||||
]
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{title: 'Option 2', uri: '/option2'},
|
||||
{title: 'Option 3', uri: '/option3'}
|
||||
];
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(console.log.calledOnce).to.be.true;
|
||||
expect(console.log.calledWith('Menu option #1 uri must be a string.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
});
|
||||
});
|
|
@ -1,6 +1,9 @@
|
|||
const {app} = require('../../bin/www');
|
||||
const model = require('../../src/models/page');
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const config = require('../../config');
|
||||
const chai = require('chai');
|
||||
const chaiHTTP = require('chai-http');
|
||||
const {expect} = chai;
|
||||
|
@ -14,6 +17,14 @@ describe('Pages REST: ', () => {
|
|||
agent = chai.request.agent(app);
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
const pathToDB = path.resolve(__dirname, '../../', config.database, './pages.db');
|
||||
|
||||
if (fs.existsSync(pathToDB)) {
|
||||
fs.unlinkSync(pathToDB);
|
||||
}
|
||||
});
|
||||
|
||||
it('Creating page', async () => {
|
||||
const body = {
|
||||
blocks: [
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue