1 | var Code = require('code'),
|
2 | Lab = require('lab'),
|
3 | Paper = require('../index'),
|
4 | lab = exports.lab = Lab.script(),
|
5 | describe = lab.experiment,
|
6 | expect = Code.expect,
|
7 | it = lab.it;
|
8 |
|
9 | describe('loadTheme()', function() {
|
10 | var assembler = {
|
11 | getTemplates: function(path, processor, callback) {
|
12 | process.nextTick(function() {
|
13 | var templates = {
|
14 | 'pages/product': '<html></html>',
|
15 | 'pages/partial': '<p></p>',
|
16 | 'pages/localeName': '{{locale_name}}',
|
17 | };
|
18 |
|
19 | callback(null, processor(templates));
|
20 | });
|
21 | },
|
22 | getTranslations: function (callback) {
|
23 | process.nextTick(function() {
|
24 | var translations = {
|
25 | 'en': {
|
26 | hello: 'Hello {name}',
|
27 | level1: {
|
28 | level2: 'we are in the second level'
|
29 | }
|
30 | },
|
31 | 'fr': {
|
32 | hello: 'Bonjour {name}',
|
33 | level1: {
|
34 | level2: 'nous sommes dans le deuxième niveau'
|
35 | }
|
36 | },
|
37 | 'fr-CA': {
|
38 | hello: 'Salut {name}'
|
39 | }
|
40 | };
|
41 |
|
42 | callback(null, translations);
|
43 | });
|
44 | }
|
45 | };
|
46 |
|
47 | it('should use the assembler interface to load templates and translations', done => {
|
48 | const paper = new Paper(null, null, assembler);
|
49 |
|
50 | paper.loadTheme('pages/product', 'fr-CA;q=0.8, fr, en', () => {
|
51 | expect(paper.handlebars.templates['pages/product']).to.be.a.function();
|
52 | expect(paper.handlebars.templates['pages/partial']).to.be.a.function();
|
53 | expect(paper.translator.translate('hello', {name: 'Mario'})).to.be.equal('Bonjour Mario');
|
54 | expect(paper.translator.translate('hello', {name: 'Already Compiled'})).to.be.equal('Bonjour Already Compiled');
|
55 | expect(paper.translator.translate('does_not_exist')).to.be.equal('does_not_exist');
|
56 |
|
57 | done();
|
58 | });
|
59 | });
|
60 |
|
61 | it('should get the localeName from the acceptLanguage header', done => {
|
62 | const paper = new Paper(null, null, assembler);
|
63 |
|
64 | paper.loadTheme('pages/localeName', 'fr-CA;q=0.8, fr, en', () => {
|
65 | expect(paper.translator.getLocale()).to.be.equal('fr');
|
66 |
|
67 | done();
|
68 | });
|
69 | });
|
70 |
|
71 | it('should default to english if the locale is not supported', done => {
|
72 | const paper = new Paper(null, null, assembler);
|
73 |
|
74 | paper.loadTheme('pages/localeName', 'es-VE, en', () => {
|
75 | expect(paper.translator.getLocale()).to.be.equal('en');
|
76 |
|
77 | done();
|
78 | });
|
79 | });
|
80 |
|
81 | it('should include the langName in the template context', done => {
|
82 | const paper = new Paper(null, null, assembler);
|
83 |
|
84 | paper.loadTheme('pages/localeName', 'fr-CA, en', () => {
|
85 | expect(paper.render('pages/localeName', {})).to.be.equal('fr-CA');
|
86 |
|
87 | done();
|
88 | });
|
89 | });
|
90 | });
|
91 |
|
92 | describe('cdnify()', function () {
|
93 | it('should not include session id', function (done) {
|
94 | var paper = new Paper({
|
95 | cdn_url: 'http://cdn.example.com/foo',
|
96 | theme_version_id: '123',
|
97 | });
|
98 |
|
99 | expect(paper.cdnify('/assets/image.jpg'))
|
100 | .to.be.equal('http://cdn.example.com/foo/stencil/123/image.jpg');
|
101 |
|
102 | done();
|
103 | });
|
104 |
|
105 | it('should use sessionId if available', function (done) {
|
106 | var paper = new Paper({
|
107 | cdn_url: 'http://cdn.example.com/foo',
|
108 | theme_version_id: '123',
|
109 | theme_session_id: '345',
|
110 | });
|
111 |
|
112 | expect(paper.cdnify('/assets/image.jpg'))
|
113 | .to.be.equal('http://cdn.example.com/foo/stencil/123/e/345/image.jpg');
|
114 |
|
115 | done();
|
116 | });
|
117 |
|
118 | it('should avoid double slash in path', function (done) {
|
119 | var paper = new Paper({
|
120 | cdn_url: 'http://cdn.example.com/foo',
|
121 | theme_version_id: '123',
|
122 | });
|
123 |
|
124 | expect(paper.cdnify('img/icon-sprite.svg'))
|
125 | .to.be.equal('http://cdn.example.com/foo/stencil/123/img/icon-sprite.svg');
|
126 |
|
127 | done();
|
128 | });
|
129 | });
|
130 |
|
131 | describe('render()', function() {
|
132 | var templates = {
|
133 | 'pages/product': '<html>{{> pages/partial}}</html>',
|
134 | 'pages/partial': '<p>{{variable}}</p>',
|
135 | 'pages/greet': '<h1>{{lang \'good\'}} {{lang \'morning\'}}</h1>',
|
136 | 'pages/pre': '{{{pre object}}}',
|
137 | },
|
138 | context = {
|
139 | variable: 'hello world',
|
140 | object: {}
|
141 | };
|
142 |
|
143 | it('should render pages/product', function(done) {
|
144 | var compiled = new Paper().loadTemplatesSync(templates).render('pages/product', context);
|
145 | expect(compiled).to.be.equal('<html><p>hello world</p></html>');
|
146 | done();
|
147 | });
|
148 |
|
149 | it('should render pages/partial', function(done) {
|
150 | var compiled = new Paper().loadTemplatesSync(templates).render('pages/partial', context);
|
151 | expect(compiled).to.be.equal('<p>hello world</p>');
|
152 | done();
|
153 | });
|
154 |
|
155 | it('should render with errors', function(done) {
|
156 | var templates = {
|
157 | 'errorPage': '{{'
|
158 | };
|
159 |
|
160 | try {
|
161 | var compiled = new Paper().loadTemplatesSync(templates).render('errorPage', context);
|
162 | expect(compiled).not.to.exist();
|
163 | } catch (ex) {
|
164 | expect(ex).to.exist();
|
165 | }
|
166 |
|
167 | done();
|
168 | });
|
169 | });
|