mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-07-19 21:29:41 +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
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);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue