-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRenderServiceSpec.cfc
More file actions
500 lines (387 loc) · 23 KB
/
RenderServiceSpec.cfc
File metadata and controls
500 lines (387 loc) · 23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
component extends="coldbox.system.testing.BaseTestCase" {
function beforeAll() {
variables.mockController = createStub();
variables.mockUtility = createStub();
variables.mockChecksumService = createStub();
variables.mockValidationService = createStub();
variables.mockRequestService = createStub();
variables.renderService = prepareMock( new cbwire.models.services.RenderService() );
renderService.setCBWIREController( mockController );
renderService.setUtilityService( mockUtility );
renderService.setChecksumService( mockChecksumService );
renderService.setValidationService( mockValidationService );
renderService.setRequestService( mockRequestService );
}
function run() {
describe( "RenderService", function() {
describe( "render()", function() {
it( "should render with initial attributes on initial load", function() {
var mockWire = createStub();
mockWire.$( "get_initialLoad", true );
mockWire.$( "get_id", "abc123" );
mockWire.$( "get_listeners", {} );
mockWire.$( "get_scripts", {} );
mockWire.$( "_getSnapshot", { foo: "bar" } );
mockChecksumService.$( "calculateChecksum", serializeJson( { checksum: "abc" } ) );
var baseHtml = "<div>content</div>";
var output = renderService.render( mockWire, baseHtml );
expect( output ).toInclude( "abc123" );
expect( output ).toInclude( "abc" );
expect( output ).toInclude( "<div" );
expect( output ).toInclude( "</div>" );
});
it( "should render trimmed HTML with no extra attributes on subsequent request", function() {
var mockWire = createStub();
mockWire.$( "get_initialLoad", false );
mockWire.$( "get_id", "xyz789" );
var baseHtml = " <div>updated</div> ";
var output = renderService.render( mockWire, baseHtml );
expect( output ).toInclude( "xyz789" );
expect( output ).toInclude( "<div" );
expect( output ).toInclude( "</div>" );
expect( output ).notToInclude( " " ); // trimmed
});
xit( "should throw if HTML does not have a single outer element", function() {
var mockWire = createStub();
mockWire.$( "get_initialLoad", false );
mockWire.$( "get_id", "test" );
var invalidHtml = "<div></div><div></div>";
expect( function() {
renderService.render( mockWire, invalidHtml );
} ).toThrow();
});
it( "should insert wire:snapshot and wire:id attributes on initial load", function() {
var mockWire = createStub();
mockWire.$( "get_initialLoad", true );
mockWire.$( "get_id", "abc123" );
mockWire.$( "get_listeners", { someEvent: "someAction" } );
mockWire.$( "get_scripts", { scriptA: "console.log('a')" } );
mockWire.$( "_getSnapshot", { foo: "bar" } );
mockChecksumService.$( "calculateChecksum", serializeJson( { checksum: "xyz" } ) );
var html = "<div>hello</div>";
var result = renderService.render( mockWire, html );
expect( result ).toInclude( 'wire:snapshot="' );
expect( result ).toInclude( 'wire:id="abc123"' );
expect( result ).toInclude( 'wire:effects=' );
});
it( "should insert wire:id attribute only on subsequent render", function() {
var mockWire = createStub();
mockWire.$( "get_initialLoad", false );
mockWire.$( "get_id", "def456" );
var html = "<div>later</div>";
var result = renderService.render( mockWire, html );
expect( result ).toInclude( 'wire:id="def456"' );
expect( result ).notToInclude( "wire:snapshot" );
expect( result ).notToInclude( "wire:effects" );
});
});
describe( "renderViewContent()", function() {
it( "should render with test encapsulator", function() {
var wire = prepareMock( createStub() );
wire.$( "get_renderedContent", "" );
wire.$( "set_renderedContent" );
wire.$( "get_id", "abc123" );
var testTemplate = "/tests/resources/RendererStub.cfm"; // You control this
var result = renderService.renderViewContent( wire, "fake.path", {}, testTemplate );
debug(result);
expect( result ).toInclude( "Rendered abc123" );
});
});
describe( "getOuterElement()", function() {
it( "should return the outer element tag name for a simple div", function() {
var html = "<div></div>";
var result = renderService.getOuterElement( html );
expect( result ).toBe( "div" );
});
it( "should return the outer tag name when attributes are present", function() {
var html = '<section class="wrapper" id="main">';
var result = renderService.getOuterElement( html );
expect( result ).toBe( "section" );
});
it( "should ignore leading whitespace before tag", function() {
var html = ' <article data-id="1">';
var result = renderService.getOuterElement( html );
expect( result ).toBe( "article" );
});
it( "should handle uppercase tag names", function() {
var html = "<DIV class='test'>";
var result = renderService.getOuterElement( html );
expect( result ).toBe( "DIV" );
});
it( "should throw CBWIREException when no HTML elements are present", function() {
var html = "no tags here";
expect( function() {
renderService.getOuterElement( html );
}).toThrow( "CBWIREException" );
var html = "<div>tags here</div>";
expect( function() {
renderService.getOuterElement( html );
}).notToThrow( "CBWIREException" );
});
it( "should throw CBWIREException with descriptive message for empty HTML", function() {
var html = "";
expect( function() {
renderService.getOuterElement( html );
}).toThrow( "CBWIREException" );
});
it( "should throw CBWIREException for HTML with only text content", function() {
var html = "Just some text content without any tags";
expect( function() {
renderService.getOuterElement( html );
}).toThrow( "CBWIREException" );
});
it( "should return the first tag if multiple are present", function() {
var html = "<div><span></span></div>";
var result = renderService.getOuterElement( html );
expect( result ).toBe( "div" );
});
});
describe( "getComponentTag()", function() {
it( "should return 'div' for simple div rendering", function() {
var result = renderService.getComponentTag( "<div>content</div>" );
expect( result ).toBe( "div" );
});
it( "should return tag name with attributes present", function() {
var result = renderService.getComponentTag( '<section id="main" class="wrapper">' );
expect( result ).toBe( "section" );
});
it( "should handle leading whitespace", function() {
var result = renderService.getComponentTag( " <article>" );
expect( result ).toBe( "article" );
});
it( "should handle uppercase tags", function() {
var result = renderService.getComponentTag( "<SPAN class='highlight'>" );
expect( result ).toBe( "SPAN" );
});
it( "should throw CBWIREException if no tag is found", function() {
expect( function() {
renderService.getComponentTag( "no valid html" );
} ).toThrow( "CBWIREException" );
});
it( "should throw CBWIREException on empty string", function() {
expect( function() {
renderService.getComponentTag( "" );
} ).toThrow( "CBWIREException" );
});
});
describe( "validateSingleOuterElement()", function() {
it( "should allow single outer div", function() {
var html = "<div><span>ok</span></div>";
expect( function() {
renderService.validateSingleOuterElement( html );
} ).notToThrow();
});
it( "should allow void elements inside a single outer div", function() {
var html = '<div><br><img src="x.png" /></div>';
expect( function() {
renderService.validateSingleOuterElement( html );
} ).notToThrow();
});
xit( "should throw when there are multiple outer elements", function() {
var html = "<div>One</div><div>Two</div>";
expect( function() {
renderService.validateSingleOuterElement( html );
} ).toThrow( "CBWIRETemplateException" );
});
xit( "should throw when HTML is empty", function() {
var html = "";
expect( function() {
renderService.validateSingleOuterElement( html );
} ).toThrow( "ApplicationException" );
});
xit( "should throw when outer tags do not match", function() {
var html = "<section><p>Mismatch</p></div>";
expect( function() {
renderService.validateSingleOuterElement( html );
} ).toThrow( "CBWIRETemplateException" );
});
xit( "should throw when closing tag is missing", function() {
var html = "<div><span>Missing</span>";
expect( function() {
renderService.validateSingleOuterElement( html );
} ).toThrow( "CBWIRETemplateException" );
});
});
describe( "normalizeViewPath()", function() {
it( "should return .bxm path when .bxm file exists", function() {
var input = "my.view.template";
// simulate the components variables._path which is what is passed to the wire() method
var componentPath = "my.view.template";
var bxmAbsolutePath = expandPath( "/my/view/template.bxm" );
var cfmAbsolutePath = expandPath( "/my/view/template.cfm" );
var expectedPath = "/wires/my/view/template.bxm";
mockUtility.$( "fileExists" ).$args( bxmAbsolutePath ).$results( true );
mockUtility.$( "fileExists" ).$args( cfmAbsolutePath ).$results( false );
var result = renderService.normalizeViewPath( input, componentPath );
expect( result ).toBe( expectedPath );
});
it( "should return .cfm path when only .cfm file exists", function() {
var input = "my.view.template";
// simulate the components variables._path which is what is passed to the wire() method
var component_path = "my.view.template";
var bxmAbsolutePath = expandPath( "/my/view/template.bxm" );
var cfmAbsolutePath = expandPath( "/my/view/template.cfm" );
var expectedPath = "/wires/my/view/template.cfm";
mockUtility.$( "fileExists" ).$args( bxmAbsolutePath ).$results( false );
mockUtility.$( "fileExists" ).$args( cfmAbsolutePath ).$results( true );
var result = renderService.normalizeViewPath( input, component_path );
expect( result ).toBe( expectedPath );
});
it( "should return .cfm module path when wire is located in module wires directory", function() {
var input = "modules_app.testingmodule.wires.twoFileModuleComponent";
// simulate the components variables._path which is what is passed to the wire() method
var component_path = "twoFileModuleComponent@testingmodule";
// use full path to module wire
var bxmAbsolutePath = expandPath( "../modules_app/testingmodule/wires/twoFileModuleComponent.bxm" );
var cfmAbsolutePath = expandPath( "../modules_app/testingmodule/wires/twoFileModuleComponent.cfm" );
var expectedPath = "/modules_app/testingmodule/wires/twoFileModuleComponent.cfm";
// mock the file exists calls for both .bxm and .cfm paths
mockUtility.$( "fileExists" ).$args( bxmAbsolutePath ).$results( false );
mockUtility.$( "fileExists" ).$args( cfmAbsolutePath ).$results( true );
var result = renderService.normalizeViewPath( input, component_path );
expect( result ).toBe( expectedPath );
});
it( "should throw when neither .bxm nor .cfm exists", function() {
var input = "my.view.template";
// simulate the components variables._path which is what is passed to the wire() method
var component_path = "my.view.template";
var bxmAbsolutePath = expandPath( "/nonexistent/view.bxm" );
var cfmAbsolutePath = expandPath( "/nonexistent/view.cfm" );
var expectedPath = "/wires/my/view/template.cfm";
mockUtility.$( "fileExists" ).$args( bxmAbsolutePath ).$results( false );
mockUtility.$( "fileExists" ).$args( cfmAbsolutePath ).$results( false );
expect( function() {
renderService.normalizeViewPath( "nonexistent.view", component_path );
}).toThrow( "CBWIREException" );
});
it( "should return path with .bxm for tmp path when .bxm exists", function() {
var input = "cbwire.models.tmp.componentName";
// simulate the components variables._path which is what is passed to the wire() method
var component_path = "cbwire.models.tmp.componentName";
mockUtility.$( "fileExists" ).$args( expandPath( "/cbwire/models/tmp/componentName.bxm" ) ).$results( true );
mockUtility.$( "fileExists" ).$args( expandPath( "/cbwire/models/tmp/componentName.cfm" ) ).$results( false );
var result = renderService.normalizeViewPath( input, component_path );
expect( result ).toBe( "/cbwire/models/tmp/componentName.bxm" );
});
it( "should return path with .cfm for tmp path when .cfm exists", function() {
var input = "cbwire.models.tmp.componentName";
// simulate the components variables._path which is what is passed to the wire() method
var component_path = "cbwire.models.tmp.componentName";
mockUtility.$( "fileExists" ).$args( expandPath( "/cbwire/models/tmp/componentName.bxm" ) ).$results( false );
mockUtility.$( "fileExists" ).$args( expandPath( "/cbwire/models/tmp/componentName.cfm" ) ).$results( true );
var result = renderService.normalizeViewPath( input, component_path );
expect( result ).toBe( "/cbwire/models/tmp/componentName.cfm" );
});
});
describe( "getTemplatePath()", function() {
it( "should return full module path when module path is used", function() {
var mockWire = createStub();
mockWire.$( "_getModuleName", "exampleModule" );
mockWire.$( "_getComponentName", "myComponent@cbwire" );
mockController.$( "getModuleRootPath", "modules_app/exampleModule" );
var result = renderService.getTemplatePath( mockWire, "my@view" );
expect( result ).toBe( "modules_app/exampleModule.wires.myComponent" );
});
it( "should return default path when not a module path", function() {
var mockWire = createStub();
var result = renderService.getTemplatePath( mockWire, "some.view" );
expect( result ).toBe( "wires.some.view" );
});
});
describe( "isModulePath()", function() {
it( "should return true for paths with @", function() {
expect( renderService.isModulePath( "some@path" ) ).toBeTrue();
});
it( "should return false for paths without @", function() {
expect( renderService.isModulePath( "some/path" ) ).toBeFalse();
});
});
describe( "captureTemplateReturnValues()", function() {
it( "should track script and asset tags", function() {
var mockWire = createStub();
mockWire.$( "_getCompileTimeKey", "abc123" );
mockWire.$( "_trackScript" );
mockWire.$( "_trackAsset" );
mockController.$( "getRequestAssets", {} );
var returnValues = {
"script1": "<script>...</script>",
"assets1": "<link rel='stylesheet'>"
};
renderService.captureTemplateReturnValues( mockWire, returnValues );
mockWire.$once( "_trackScript" );
mockWire.$once( "_trackAsset" );
});
});
describe( "generateWireEffectsAttribute()", function() {
it( "should return encoded JSON with listeners only", function() {
var listeners = { "someEvent": "someAction", "anotherEvent": "anotherAction" };
var scripts = {};
var result = renderService.generateWireEffectsAttribute( listeners, scripts );
var decoded = deserializeJSON( decodeForHTML( result ) );
expect( decoded ).toHaveKey( "listeners" );
expect( decoded.listeners ).toInclude( "someEvent" );
expect( decoded.listeners ).toInclude( "anotherEvent" );
expect( decoded ).notToHaveKey( "scripts" );
});
it( "should return encoded JSON with scripts only", function() {
var listeners = {};
var scripts = { init: "console.log('init')" };
var result = renderService.generateWireEffectsAttribute( listeners, scripts );
var decoded = deserializeJSON( decodeForHTML( result ) );
expect( decoded ).toHaveKey( "scripts" );
expect( decoded.scripts ).toHaveKey( "init" );
expect( decoded ).notToHaveKey( "listeners" );
});
it( "should return encoded JSON with both listeners and scripts", function() {
var listeners = { "someEvent": "someAction" };
var scripts = { foo: "bar" };
var result = renderService.generateWireEffectsAttribute( listeners, scripts );
var decoded = deserializeJSON( decodeForHTML( result ) );
expect( decoded ).toHaveKey( "listeners" );
expect( decoded ).toHaveKey( "scripts" );
expect( decoded.listeners ).toInclude( "someEvent" );
});
it( "should return encoded empty array when both are empty", function() {
var listeners = {};
var scripts = {};
var result = renderService.generateWireEffectsAttribute( listeners, scripts );
expect( result ).toBe( "[]" );
});
it( "should encode attribute safely", function() {
var listeners = { "someEvent": "someAction" };
var scripts = {};
var result = renderService.generateWireEffectsAttribute( listeners, scripts );
expect( isJson( decodeForHTML( result ) ) ).toBeTrue();
});
});
describe( "encodeAttribute()", function() {
it( "should encode double quotes", function() {
var input = 'some "quoted" value';
var result = renderService.encodeAttribute( input );
expect( result ).toInclude( """ );
});
it( "should encode angle brackets", function() {
var input = "<script>alert('xss')</script>";
var result = renderService.encodeAttribute( input );
expect( result ).notToInclude( "<" );
expect( result ).notToInclude( ">" );
});
it( "should encode ampersands", function() {
var input = "name=foo&value=bar";
var result = renderService.encodeAttribute( input );
expect( result ).toInclude( "&" );
});
it( "should return same value if no special characters", function() {
var input = "safeValue123";
var result = renderService.encodeAttribute( input );
expect( result ).toBe( "safeValue123" );
});
it( "should encode JSON string safely", function() {
var input = serializeJSON( { foo: "<bar>" } );
var result = renderService.encodeAttribute( input );
expect( isJson( decodeForHTML( result ) ) ).toBeTrue();
});
});
});
}
}