UNPKG

6.15 kBJavaScriptView Raw
1var app, compound;
2before(function(done) {
3 app = getApp();
4 compound = app.compound;
5 compound.on('ready', function() {
6 done();
7 });
8});
9
10/*
11 * stylesheetLinkTag helper tests
12 */
13describe('stylesheetLinkTag', function() {
14 it('should generate a single tag', function (){
15 var match = /\<link media="screen" rel="stylesheet" type="text\/css" href="\/stylesheets\/style\.css" \/\>/;
16 var tag = compound.helpers.stylesheetLinkTag('style');
17
18 tag.should.match(match);
19 });
20
21 it('should generate multiple tags', function (){
22 var match = /<link.*?href="\/stylesheets\/.*?\.css/g;
23 var tag = compound.helpers.stylesheetLinkTag('reset', 'bootstrap');
24
25 tag.should.match(match);
26 tag.match(match).length.should.equal(2);
27 });
28
29 describe('assets timestamps', function() {
30 it('should generate a link with a timestamp if enabled');
31 it('should generate a link without a timestamp if disabled');
32 it('should never add a timestamp to external links');
33 });
34});
35
36/*
37 * javascriptIncludeTag helper tests
38 */
39describe('javascriptIncludeTag', function() {
40 it('should generate a single tag', function (){
41 var match = /<script type="text\/javascript" src="\/javascripts\/app\.js">/
42 var tag = compound.helpers.javascriptIncludeTag('app');
43
44 tag.should.match(match);
45 });
46
47 it('should generate multiple tags', function (){
48 var match = /<script.*?src="\/javascripts\/.*?\.js/g
49 var tag = compound.helpers.javascriptIncludeTag('rails', 'application');
50
51 tag.should.match(match);
52 tag.match(match).length.should.equal(2);
53 });
54
55 describe('assets timestamps', function() {
56 it('should generate a link with a timestamp if enabled');
57 it('should generate a link without a timestamp if disabled');
58 it('should never add a timestamp to external links');
59 });
60});
61
62/*
63 * formTag helper tests
64 */
65describe('formTag', function (){
66 before(function() {
67 compound.helpers.controller = {
68 app: app,
69 req: {
70 csrfParam: 'param_name',
71 csrfToken: 'token_value'
72 }
73 };
74 });
75
76 it('should generate an update form for resource with PUT method', function () {
77 var buf = arguments.callee.buf = [];
78 var res = {
79 constructor: {
80 modelName: 'Resource'
81 },
82 id: 7
83 };
84
85 compound.map.pathTo.resource = function (res) {
86 return '/resources/' + res.id;
87 }
88
89 var f = compound.helpers.formFor(res, {});
90 var res = f.begin();
91 var expectedFormString = '<form method="POST" action="/resources/7"><input type="hidden" name="param_name" value="token_value" /><input type="hidden" name="_method" value="PUT" />';
92 res.should.equal(expectedFormString);
93 });
94
95 it('should be able to create inputs without a block', function () {
96 var buf = [];
97 var res = {
98 constructor: {
99 modelName: 'Resource'
100 },
101 id: 7
102 };
103
104 compound.map.pathTo.resource = function (res) {
105 return '/resources/' + res.id;
106 }
107
108 var f = compound.helpers.formFor(res, {});
109 buf.push(f.begin());
110 buf.push(f.input('name'));
111 buf.push(f.input('sub[obj]'));
112 buf.push(f.end());
113
114 buf[0].should.equal('<form method="POST" action="/resources/7"><input type="hidden" name="param_name" value="token_value" /><input type="hidden" name="_method" value="PUT" />');
115 buf[1].should.equal('<input name="Resource[name]" id="Resource_name" type="text" value="" />');
116 buf[2].should.equal('<input name="Resource[sub][obj]" id="Resource_sub_obj" type="text" value="" />');
117 });
118
119 it('should allow to override "id" attribute of tag', function() {
120 var res = {
121 constructor: {
122 modelName: 'Resource'
123 },
124 id: 7
125 };
126 var f = compound.helpers.formFor(res, {});
127 f.textarea('name').should.equal('<textarea name="Resource[name]" id="Resource_name"></textarea>');
128 f.textarea('name', {id: 'over'}).should.equal('<textarea name="Resource[name]" id="over"></textarea>');
129 });
130
131 it('should work for nested resource', function() {
132 var res = {
133 constructor: {
134 modelName: 'User'
135 },
136 id: 7,
137 address: {
138 constructor: 'Address',
139 id: 9,
140 street: 'Liberty st'
141 }
142 };
143 var f = compound.helpers.formFor(res, {action: '/'});
144 var addr = f.fieldsFor('address');
145 addr.input('street').should.equal('<input name="User[address][street]" id="User_address_street" type="text" value="Liberty st" />');
146 });
147
148});
149
150/*
151 * errorMessagesFor helper tests
152 */
153describe('errorMessagesFor', function () {
154 var resource = {
155 errors: {
156 name: ['can\'t be blank', 'is invalid'],
157 email: ['is not unique']
158 }
159 };
160 it('should generate html errors', function () {
161 var html = compound.helpers.errorMessagesFor(resource);
162 var expectedErrorString = '<div class="alert alert-error"><p><strong>Validation failed. Fix the following errors to continue:</strong></p><ul><li class="error-message">Name can\'t be blank</li><li class="error-message">Name is invalid</li></ul><ul><li class="error-message">Email is not unique</li></ul></div>';
163 html.should.equal(expectedErrorString);
164 });
165});
166
167describe('metaTag', function() {
168
169 it('should generate metaTag(name, content)', function() {
170 var result = compound.helpers.metaTag('pageId', 77);
171 var expected = '<meta name="pageId" content="77" />';
172 result.should.equal(expected);
173 });
174
175 it('should generate metaTag(name, params)', function() {
176 var result = compound.helpers.metaTag('pageId', {foo: 'bar'});
177 var expected = '<meta name="pageId" foo="bar" />';
178 result.should.equal(expected);
179 });
180
181 it('should generate metaTag(params)', function() {
182 var result = compound.helpers.metaTag({name: 'foo', content: 'bar'});
183 var expected = '<meta name="foo" content="bar" />';
184 result.should.equal(expected);
185 });
186
187 it('should generate metaTag()', function() {
188 var result = compound.helpers.metaTag();
189 var expected = '<meta />';
190 result.should.equal(expected);
191 });
192
193});