Skip to content

Commit

Permalink
Test for view.prototype.render
Browse files Browse the repository at this point in the history
  • Loading branch information
KovacevicAleksa committed Jan 31, 2025
1 parent 6233671 commit 3081901
Showing 1 changed file with 94 additions and 0 deletions.
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

0 comments on commit 3081901

Please sign in to comment.