feat(views): built-in template engine + content-type map
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+77
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { render, escapeHtml, contentTypeFor } from '../src/views';
|
||||
|
||||
describe('escapeHtml', () => {
|
||||
it('escapes the five HTML-significant characters', () => {
|
||||
expect(escapeHtml(`<a href="x">&'`)).toBe('<a href="x">&'');
|
||||
});
|
||||
});
|
||||
|
||||
describe('render', () => {
|
||||
it('interpolates and HTML-escapes {{ x }} by default', () => {
|
||||
expect(render('<h1>{{ name }}</h1>', { name: '<b>hi</b>' })).toBe('<h1><b>hi</b></h1>');
|
||||
});
|
||||
it('leaves {{{ x }}} raw (unescaped)', () => {
|
||||
expect(render('{{{ html }}}', { html: '<b>hi</b>' })).toBe('<b>hi</b>');
|
||||
});
|
||||
it('resolves nested keys and renders missing keys as empty', () => {
|
||||
expect(render('{{ user.name }}|{{ nope }}', { user: { name: 'Diego' } })).toBe('Diego|');
|
||||
});
|
||||
});
|
||||
|
||||
describe('contentTypeFor', () => {
|
||||
it('maps known extensions and defaults unknown to octet-stream', () => {
|
||||
expect(contentTypeFor('.css')).toBe('text/css; charset=utf-8');
|
||||
expect(contentTypeFor('.PNG')).toBe('image/png');
|
||||
expect(contentTypeFor('.xyz')).toBe('application/octet-stream');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user