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 1 commit into
base: master
Choose a base branch
from
Open
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
94 changes: 94 additions & 0 deletions test/app.render.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var assert = require('node:assert')
var express = require('..');
var path = require('node:path')
var tmpl = require('./support/tmpl');
const View = require('../lib/view.js');

describe('app', function(){
describe('.render(name, fn)', function(){
Expand Down Expand Up @@ -365,6 +366,99 @@ describe('app', function(){
})
})

describe('View.prototype.render', function () {
it('should force callback to be async and pass correct arguments', function (done) {
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;

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');
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();
});
});
});

function createApp() {
var app = express();

Expand Down