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

Test for view.prototype.render #6306

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion test/app.render.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,4 @@ function createApp() {
app.engine('.tmpl', tmpl);

return app;
}
}
99 changes: 99 additions & 0 deletions test/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
'use strict'

var path = require('node:path')
var assert = require('node:assert')
const View = require('../lib/view.js');


describe('View.prototype.render', function () {
it('should force callback to be async and pass correct arguments', function (done) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this test is correct. I would need to play around locally to be sure (which I didn't do), but your mockEngine is not asnyc and I am not sure this test even makes sense if it was. Can you describe what you are trying to prove with this isAsync part of the test?

var mockEngine = function (filePath, options, callback) {
callback(null, 'rendered content');
};

var view = new View('test', {
root: path.join(__dirname, 'fixtures'),
engines: { '.tmpl': mockEngine },
defaultEngine: '.tmpl'
});

var isAsync = false;

var originalCallback = function (err, html) {
assert(isAsync, 'Callback should be async');
assert.strictEqual(err, null, 'Error should be null');
assert.strictEqual(html, 'rendered content', 'Rendered content should match');
done();
};

view.render({}, function (err, html) {
isAsync = true;
originalCallback(err, html);
});
});

it('should handle errors correctly', function (done) {
var mockEngine = function (filePath, options, callback) {
callback(new Error('render error'));
};

var view = new View('test', {
root: path.join(__dirname, 'fixtures'),
engines: { '.tmpl': mockEngine },
defaultEngine: '.tmpl'
});

view.render({}, function (err, html) {
assert(err instanceof Error, 'Error should be an instance of Error');
assert.strictEqual(err.message, 'render error', 'Error message should match');
assert.strictEqual(html, undefined, 'HTML should be undefined when there is an error');
done();
});
});

it('should handle synchronous callbacks correctly', function (done) {
var mockEngine = function (filePath, options, callback) {
callback(null, 'sync rendered content');
};

var view = new View('test', {
root: path.join(__dirname, 'fixtures'),
engines: { '.tmpl': mockEngine },
defaultEngine: '.tmpl'
});

var isAsync = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with the one above, I dont think this isAsync stuff is doing anything meaningful. Can you describe what you are going for here to help me understand?


var originalCallback = function (err, html) {
assert(isAsync, 'Callback should be async');
assert.strictEqual(err, null, 'Error should be null');
assert.strictEqual(html, 'sync rendered content', 'Rendered content should match');
done();
};

view.render({}, function (err, html) {
isAsync = true;
originalCallback(err, html);
});
});

it('should pass correct arguments to the engine', function (done) {
var mockEngine = function (filePath, options, callback) {
assert.strictEqual(filePath, view.path, 'File path should match');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion is a self fulfilling test. If you want to test this properly you would need to hard code the expected path here, not use view.path since no matter what we changed view.path to this would pass. right?

assert.deepStrictEqual(options, { key: 'value' }, 'Options should match');
callback(null, 'rendered content');
};

var view = new View('test', {
root: path.join(__dirname, 'fixtures'),
engines: { '.tmpl': mockEngine },
defaultEngine: '.tmpl'
});

view.render({ key: 'value' }, function (err, html) {
assert.strictEqual(err, null, 'Error should be null');
assert.strictEqual(html, 'rendered content', 'Rendered content should match');
done();
});
});
});