{expect, calls} = require 'racer/test/util' {DetachedModel: Model, ResMock} = require './mocks' View = require '../lib/View.server' describe 'View.render', -> it 'supports view.render with no defined views', -> view = new View res = new ResMock res.onEnd = (html) -> expect(html).to.match /^.*<\/title><script>.*<\/script><script.*><\/script>$/ view.render res describe 'View', -> view = model = null beforeEach (done) -> view = new View model = new Model view._init model, false, done it 'supports rendering a string literal view', -> view.make 'test', """ <style> body { margin: 0 } </style> """ # String views should have line breaks and leading whitespace removed expect(view.get 'test').to.eql '<style>body {margin: 0}</style>' it 'doctypes and conditional comments are maintained', -> view.make 'test', """ <!DOCTYPE html> <!-- This comment is removed --> <title> Not IE """ expect(view.get 'test').to.eql '' + '' + '' + 'Not IE' it 'supports substituting variables into text', -> view.make 'test', ''' {{connected}}{{canConnect}} {{nada}}

{{name}}

{{age}} - {{height}} - {{weight}}

''' ctx = connected: false weight: '165 lbs' nada: null model.set 'name', 'John' model.set 'age', 22 model.set 'height', '6 ft 2 in' model.set 'weight', '175 lbs' expected = 'falsetrue ' + '

John

' + '

22 - 6 ft 2 in - 165 lbs

' expect(view.get 'test', ctx).to.equal expected it 'supports path interpolation', -> view.make 'test', """ {{items[id]}} {{#each ids}} {{items[this]}} {{/}} {{#each others}} {{items[.id]}} {{/}} """ model.set 'id', 2 model.set 'ids', [2, 0] ctx = items: 0: 'red' 1: 'green' 2: 'blue' others: [ {id: 1} {id: 2} ] expect(view.get 'test', ctx).to.equal 'blue' + 'blue' + 'red' + 'green' + 'blue' it 'supports binding variables in text', -> view.make 'test', ''' {connected}{canConnect} {nada}

{name}

{age} - {height} - {weight}

''' ctx = connected: false weight: '165 lbs' nada: null model.set 'name', 'John' model.set 'age', 22 model.set 'height', '6 ft 2 in' model.set 'weight', '175 lbs' expect(view.get 'test', ctx).to.equal 'falsetrue ' + '

John

' + '

22 - 6 ft 2 in - 165 lbs

' it 'supports HTML escaping', -> # Attribute values are escaped regardless of placeholder type # Ampersands are escaped at the end of a replacement even when not # required, because it is sometimes needed depending on the following item template = ''' {html}x{unescaped html}''' value = '&Hi! & x& &' expected = ' ' + '<b id="hey">&Hi! & x& </b>&x' + '&Hi! & x& &' view.make 'test1', template expect(view.get 'test1', html: value).to.eql expected view._idCount = 0 model.set 'html', value expect(view.get 'test1').to.eql expected view.make 'test2', '

' expect(view.get 'test2', {a: '"', b: "'", c: '<', d: '>', e: '=', f: ' ', g: '', h: null} ).to.eql '

' it 'supports HTML entity unescaping in string partials', -> view.make 'title', '{{unescaped name}} - stuff' ctx = name: 'Crème Brûlée' expect(view.get 'title$s', ctx).to.eql 'Crème Brûlée - stuff' it 'supports conditional blocks in text', -> view.make 'literal', '{{#if show}}Yep{{else}}Nope{{/}}{{#if show}} Yes!{{/}} {{#unless show}}No{{/}}' view.make 'bound', '{#if show}Yep{else}Nope{/}{#if show} Yes!{/} {#unless show}No{/}' literalTruthy = 'Yep Yes! ' literalFalsey = 'Nope No' modelTruthy = 'Yep Yes! ' modelFalsey = 'Nope No' expect(view.get 'literal', show: true).to.eql literalTruthy expect(view.get 'literal', show: 1).to.eql literalTruthy expect(view.get 'literal', show: 'x').to.eql literalTruthy expect(view.get 'literal', show: {}).to.eql literalTruthy expect(view.get 'literal', show: [1]).to.eql literalTruthy expect(view.get 'literal', show: false).to.eql literalFalsey expect(view.get 'literal', show: undefined).to.eql literalFalsey expect(view.get 'literal', show: null).to.eql literalFalsey expect(view.get 'literal', show: 0).to.eql literalFalsey expect(view.get 'literal', show: '').to.eql literalFalsey expect(view.get 'literal', show: []).to.eql literalFalsey expect(view.get 'literal').to.eql literalFalsey # No parameter assumes it is a model path that is undefined view._idCount = 0 expect(view.get 'bound').to.eql modelFalsey view._idCount = 0 model.set 'show', true expect(view.get 'bound').to.eql modelTruthy view._idCount = 0 model.set 'show', 1 expect(view.get 'bound').to.eql modelTruthy view._idCount = 0 model.set 'show', 'x' expect(view.get 'bound').to.eql modelTruthy view._idCount = 0 model.set 'show', {} expect(view.get 'bound').to.eql modelTruthy view._idCount = 0 model.set 'show', false expect(view.get 'bound').to.eql modelFalsey view._idCount = 0 model.set 'show', undefined expect(view.get 'bound').to.eql modelFalsey view._idCount = 0 model.set 'show', null expect(view.get 'bound').to.eql modelFalsey view._idCount = 0 model.set 'show', 0 expect(view.get 'bound').to.eql modelFalsey view._idCount = 0 model.set 'show', '' expect(view.get 'bound').to.eql modelFalsey view._idCount = 0 model.set 'show', [] expect(view.get 'bound').to.eql modelFalsey it 'supports else if conditionals', -> view.make 'test', """ {{#if equal('red', value)}} 1 {{else if equal('red', value)}} 2 {{else if equal('green', value)}} 3 {{else}} 4 {{/}} """ expect(view.get 'test', value: 'red').to.equal '1' expect(view.get 'test', value: 'green').to.equal '3' expect(view.get 'test', value: 'blue').to.equal '4' expect(view.get 'test').to.equal '4' it 'supports unless then else conditionals', -> view.make 'test', """ {{#unless value}} 1 {{else}} 2 {{/}} """ expect(view.get 'test', value: true).to.equal '2' expect(view.get 'test', value: false).to.equal '1' it 'supports lists in text', -> template = """

""" view.make 'test', template expect(view.get 'test', arr: []) .to.eql '' view.make 'test', template expect(view.get 'test', arr: [{name: 'stuff'}, {name: 'more'}]) .to.eql '' it 'supports nesting lists with relative paths', -> template = """ {{#each table.rows}} {{#each .cells}} {{/}} {{/}}
""" model.set 'table.rows', [ {cells: [{text: 'A'}, {text: 'B'}, {text: 'C'}]} {cells: [{text: 1}, {text: 2}, {text: 3}]} ] view.make 'test', template expect(view.get 'test').to.equal [ '' '' '' '' '' '' '' '' '' '' '' '' '' '
' ].join('') it 'supports nesting aliases to flat lists', -> template = """ {{#each outer as :out}} {{#each inner as :in}} {{#each core as :core}} [{{:out.name}} {{:in.name}} {{:core.name}} {{equal(:out.color, :in.color)}}] {{/}} {{/}} {{/}} """ view.make 'test', template compiled = view.get 'test', outer: [{name: 'stuff', color: 'blue'}, {name: 'more', color: 'pink'}] inner: [{name: '0', color: 'pink'}, {name: '1', color: 'blue'}] core: [{name: '0'}, {name: '1'}] expect(compiled).to.equal [ '[stuff 0 0 false]' '[stuff 0 1 false]' '[stuff 1 0 true]' '[stuff 1 1 true]' '[more 0 0 true]' '[more 0 1 true]' '[more 1 0 false]' '[more 1 1 false]' ].join('') it 'supports aliases from relative paths on nested lists', -> template = """ """ model.set '_room.street', [ { type: 'home' people: [ {name: 'fred', editable: true} {name: 'wilma', editable: false} ] }, { type: 'neighbor' people: [ {name: 'barney', editable: false} {name: 'betty', editable: true} ] } ] view.make 'test', template expect(view.get 'test').to.equal [ '' ].join('') it 'supports aliases from aliases on nested lists', -> template = """ """ model.set '_room.street', [ { type: 'home' people: [ {name: 'fred', editable: true} {name: 'wilma', editable: false} ] }, { type: 'neighbor' people: [ {name: 'barney', editable: false} {name: 'betty', editable: true} ] } ] view.make 'test', template expect(view.get 'test').to.equal [ '' ].join('') it 'supports boolean attributes', -> view.make 'test', '' expect(view.get 'test').to.equal '' expect(view.get 'test', maybe: false).to.equal '' expect(view.get 'test', maybe: true).to.equal '' it 'supports paths containing dots for ctx object items', -> view.make 'test', '{{user.name}}' ctx = user: {name: 'John'} expect(view.get 'test', ctx).to.equal 'John' it 'supports relative paths for ctx object items', -> view.make 'test', '{{#if user}}{{.name}}{{/}}' ctx = user: {name: 'John'} expect(view.get 'test', ctx).to.equal 'John' it 'supports Arrays containing non-objects from ctx', -> view.make 'test1', '{{#each bools}}{{this}}{{/}}' view.make 'test2', '{{#each bools as :value}}{{:value}}{{/}}' ctx = bools: [true, false, true] expect(view.get 'test1', ctx).to.equal 'truefalsetrue' expect(view.get 'test2', ctx).to.equal 'truefalsetrue' it 'supports view helper functions', -> view.fn 'lower', (s) -> s.toLowerCase() view.make 'test', '''{{lower('HI')}} {lower( "HI" )}''' expect(view.get 'test').to.equal 'hi hi' view.fn 'sum', (a, b) -> a + b view.make 'test', '{{sum(4, 9)}}' expect(view.get 'test').to.equal '13' view._idCount = 0 view.make 'test', '{equal(1, sum(-5, 6))}' expect(view.get 'test').to.equal 'true' view._idCount = 0 view.make 'test', '{unescaped equal(sum(-5, 6), 1)}' expect(view.get 'test').to.equal 'true' view.make 'test', '{{sum(4, count)}}' expect(view.get 'test', {count: 7}).to.equal '11' model.set 'count', 13 expect(view.get 'test').to.equal '17' view._idCount = 0 view.make 'test', ''' ''' expect(view.get 'test', items: ['a', 'b', 'c'] current: 'c' ).to.equal '' view.fn 'positive', (num) -> num >= 0 view.make 'test', ''' {{#each nums as :num}} {{#if positive(:num)}} {{:num}}, {{/}} {{/}} ''' expect(view.get 'test', {nums: [-4, 8, 0, 2.3, -9]}).to.equal '8,0,2.3,' it 'supports x-no-minify', -> view.make 'test', ''' ''' expect(view.get 'test').to.equal ''' '''