Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for loadController #259

Merged
merged 1 commit into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions core/loadController.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,19 @@ module.exports = async function (next) {
/* Load templating engine */
this.templating = new Templating(this);

/* Location of the controller file */
const controllerPath = path.join(this.dir, this.config.routes);

this.controller = {};

/* Generate a controller from the available views */
if (this.config.autoRouting === 'on' || this.config.autoRouting === true) {
if ((this.config.autoRouting === 'on' || this.config.autoRouting === true) && this.config.viewsDir !== null) {
const viewsPath = path.join(this.dir, this.config.viewsDir);

if (fs.existsSync(viewsPath)) {
if (fs.existsSync(viewsPath) && fs.lstatSync(viewsPath).isDirectory()) {
/* Load all views in the views directory */
const views = this.utils.getFiles(viewsPath);

/* Go through each view */
for (const view_ of views) {
const segments = view_.split('/');
const segments = path.relative(this.dir, view_).split('/');

/* Filter out the views where any segment begins with _ */
const protectedSegments = segments.filter(item => {
Expand All @@ -50,7 +47,7 @@ module.exports = async function (next) {
}

/* Filter out any files that do not use the correct file extension */
if (view_.split('.').slice(-1)[0] !== this.config.extension) {
if (this.config.extension !== null && view_.split('.').slice(-1)[0] !== this.config.extension) {
continue;
}

Expand All @@ -69,8 +66,11 @@ module.exports = async function (next) {
}
}

/* Location of the controller file */
const controllerPath = path.join(this.dir, this.config.routes || '');

/* Load the controller file */
if (fs.existsSync(controllerPath)) {
if (fs.existsSync(controllerPath) && fs.lstatSync(controllerPath).isFile()) {
/* If we have a controller file, let's load it */
const file = fs.readFileSync(controllerPath);

Expand Down
4 changes: 4 additions & 0 deletions test/_data/controller/controller.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"/": "index.html",
"/my-account": "my-account.html"
}
2 changes: 2 additions & 0 deletions test/_data/controller/mangled.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
"/": "index.ht
1 change: 1 addition & 0 deletions test/_data/controller/plain/bar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<strong>This is a template.</strong>
Empty file.
1 change: 1 addition & 0 deletions test/_data/controller/plain/sub/foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<strong>This is a template.</strong>
1 change: 1 addition & 0 deletions test/_data/controller/plain/sub/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<strong>This is a template.</strong>
1 change: 1 addition & 0 deletions test/_data/controller/plain/unrelated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is not a view.
1 change: 1 addition & 0 deletions test/_data/controller/protectedFiles/_protected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<strong>This is a template.</strong>
1 change: 1 addition & 0 deletions test/_data/controller/protectedFiles/sub/_protected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<strong>This is a template.</strong>
1 change: 1 addition & 0 deletions test/_data/controller/protectedFiles/sub/foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<strong>This is a template.</strong>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<strong>This is a template.</strong>
1 change: 1 addition & 0 deletions test/_data/controller/protectedFolders/_protected/foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<strong>This is a template.</strong>
1 change: 1 addition & 0 deletions test/_data/controller/protectedFolders/bar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<strong>This is a template.</strong>
170 changes: 169 additions & 1 deletion test/core/loadController.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,173 @@
const test = require('ava');
const path = require('path');
const _ = require('underscore');

const loadController = require('../../core/loadController');

test.todo('tests loadController');

test.beforeEach(t => {
t.context.app = _.defaults({
dir: path.join(__dirname, '../_data')
}, require('../_utils/app')());
});


test('generates a controller from a plain view directory', async t => {
t.plan(1);

t.context.app.config.autoRouting = true;
t.context.app.config.extension = 'html';
t.context.app.config.viewsDir = 'controller/plain';

await loadController.call(t.context.app, () => {
t.deepEqual(
t.context.app.controller,
{ '/': 'index', '/bar': 'bar', '/sub/foo': 'sub/foo', '/sub': 'sub/index' }
);
});
});

test('generates a controller from a view directory with protected files', async t => {
t.plan(1);

t.context.app.config.autoRouting = true;
t.context.app.config.extension = 'html';
t.context.app.config.viewsDir = 'controller/protectedFiles';

await loadController.call(t.context.app, () => {
t.deepEqual(
t.context.app.controller,
{ '/sub/foo': 'sub/foo' }
);
});
});

test('generates a controller from a view directory with protected folders', async t => {
t.plan(1);

t.context.app.config.autoRouting = true;
t.context.app.config.extension = 'html';
t.context.app.config.viewsDir = 'controller/protectedFolders';

await loadController.call(t.context.app, () => {
t.deepEqual(
t.context.app.controller,
{ '/bar': 'bar' }
);
});
});

test('does not generate a controller from an improper directory', async t => {
t.plan(1);

t.context.app.config.autoRouting = true;
t.context.app.config.extension = 'html';
t.context.app.config.viewsDir = 'controller/controller.json';

await loadController.call(t.context.app, () => {
t.deepEqual(
t.context.app.controller,
{}
);
});
});


test('loads a controller file correctly', t => {
t.plan(1);

t.context.app.config.autoRouting = false;
t.context.app.config.routes = 'controller/controller.json';

loadController.call(t.context.app, () => {
t.deepEqual(
t.context.app.controller,
{ '/': 'index', '/my-account': 'my-account' }
);
});
});

test('logs an error if controller file is mangled', async t => {
t.plan(2);

process.env.NODE_ENV = 'production';
console.log = () => true;

t.context.app.config.autoRouting = false;
t.context.app.config.routes = 'controller/mangled.json';

console.error = (context, message) => {
const controllerPath = path.join(t.context.app.dir, t.context.app.config.routes);
t.is(message, `Controller at path: \`${controllerPath}\` could not be loaded.`);
};

await t.notThrowsAsync(async () => {
return await loadController.call(t.context.app);
});
});

test('fails silently if controller file does not exist', async t => {
t.plan(1);

t.context.app.config.autoRouting = false;
t.context.app.config.routes = 'controller/nonexistent.json';

console.error = () => {
t.fail();
};

await t.notThrowsAsync(async () => {
return await loadController.call(t.context.app);
});
});

test('fails silently if controller file is a directory', async t => {
t.plan(1);

t.context.app.config.autoRouting = false;
t.context.app.config.routes = 'controller/plain/';

console.error = () => {
t.fail();
};

await t.notThrowsAsync(async () => {
return await loadController.call(t.context.app);
});
});

test('fails silently if controller file is undefined', async t => {
t.plan(1);

t.context.app.config.autoRouting = false;

console.error = () => {
t.fail();
};

await t.notThrowsAsync(async () => {
return await loadController.call(t.context.app);
});
});


test('merges autorouted and explicit controllers correctly', async t => {
t.plan(1);

t.context.app.config.autoRouting = true;
t.context.app.config.extension = 'html';
t.context.app.config.viewsDir = 'controller/plain';
t.context.app.config.routes = 'controller/controller.json';

await loadController.call(t.context.app, () => {
t.deepEqual(
t.context.app.controller,
{ '/': 'index', '/my-account': 'my-account', '/bar': 'bar', '/sub/foo': 'sub/foo', '/sub': 'sub/index' }
);
});
});


test.after.always(t => {
process.env.NODE_ENV = 'test';
});