/
test_sammy_application.js
1085 lines (1017 loc) · 36.9 KB
/
test_sammy_application.js
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function($) {
// $(function() {
with(QUnit) {
context('Sammy','apps')
.should('return a new application if no arguments passed', function() {
var app = Sammy();
defined(app, 'route');
})
.should('save the application to Sammy.apps', function() {
var app = Sammy();
ok(Sammy.apps['body']);
equal(Sammy.apps['body'], app);
})
.should('create a new app and set the element selector', function() {
var app = Sammy('#main');
equal(app.element_selector, '#main');
ok(Sammy.apps['#main']);
equal(Sammy.apps['#main'], app);
})
.should('return the app at selector', function() {
Sammy('#main');
var app = Sammy('#main')
equal(app.element_selector, '#main');
ok(Sammy.apps['#main']);
equal(Sammy.apps['#main'], app);
})
.should('extend the app at selector', function() {
var app = Sammy('#main', function() {
this.extended = true;
});
equal(app.element_selector, '#main');
ok(Sammy.apps['#main']);
equal(Sammy.apps['#main'], app);
ok(app.extended);
});
context('Sammy.Application', 'init', {
before: function() {
var context = this;
this.app = new Sammy.Application(function(app) {
this.random_setting = 1;
context.yielded_app = app;
});
}
})
.should('create a sammy object', function() {
defined(this.app, 'route');
})
.should('set arbitrary settings in the app', function() {
equal(this.app.random_setting, 1);
})
.should('set namespace as random UUID', function() {
matches(/^(\d+)-(\d{1,3})$/, this.app.namespace);
})
.should('initialize empty routes object', function() {
isType(this.app.routes, 'Object');
})
.should('yield the app as a argument', function() {
equal(this.yielded_app, this.app)
})
.should('set the location proxy to the default hash location proxy', function() {
ok(this.app._location_proxy);
defined(this.app._location_proxy, 'getLocation');
});
context('Sammy.Application', 'route', {
before: function() {
var context = this;
this.app = new Sammy.Application(function() {
context.returned = this.route('get', /testing/, function() {
$('#main').trigger('click');
});
this.mycallback = function() { this.redirect('#/'); };
this.route('get', '/blah', function() {
$('#testarea').show();
});
this.route('get', '/boosh/:boosh1/:boosh2', function() {
$('#testarea').show();
});
this.get(/blurgh/, function() {
alert('blurgh');
});
this.get('#/', function() {
alert('home');
});
this.post('#/post', 'mycallback');
this.route('#/verbless', function() {});
this.route('any', '/any', function() {});
});
}
})
.should('return the sammy application instance', function() {
equal(this.returned, this.app);
})
.should('turn a string path into a regular expression', function() {
var app = this.app;
ok(app.routes['get']);
var route = app.routes['get'][1];
isType(route.path, 'RegExp');
})
.should('turn a string path with a named param into a regex and save to param_names', function() {
var app = this.app;
ok(app.routes['get']);
var route = app.routes['get'][2];
isType(route.path, 'RegExp');
// deepEqual(route.path.toString(), /^\/boosh\/([^\/]+)\/([^\/]+)$/.toString());
deepEqual(route.param_names, ['boosh1', 'boosh2']);
})
.should('append route to application.routes object', function() {
var app = this.app;
ok(app.routes['get']);
var route = app.routes['get'][1]
isType(route.path, 'RegExp');
equal(route.verb, 'get');
defined(route, 'callback');
})
.should('allow shortcuts for defining routes', function() {
var app = this.app;
ok(app.routes['get']);
var route = app.routes['get'][3];
isType(route.path, 'RegExp');
equal(route.verb, 'get');
defined(route, 'callback');
})
.should('append late and short route to application.routes object', function() {
var app = this.app;
ok(app.routes['get']);
equal(app.routes['get'].length, 7)
var route = app.routes['get'][4];
isType(route.path, 'RegExp');
equal(route.verb, 'get');
defined(route, 'callback');
equal(route.path.toString(), new RegExp("#/$").toString());
})
.should('lookup callback as a string', function() {
var app = this.app;
ok(app.routes['post'], "post routes defined");
var route = app.routes['post'][0];
ok(route, "route exists");
equal(route.callback, app.mycallback);
})
.should('add an "any" route to every route verb', function() {
var app = this.app;
$.each(app.ROUTE_VERBS, function(i, verb) {
ok(app.routes[verb], verb + "is set on routes");
var route = app.routes[verb].pop();
ok(route, "route exists on " + verb);
equal(route.path.toString(), new RegExp("/any$").toString());
});
});
context('Sammy.Application', 'mapRoutes', {
before: function() {
var context = this;
context.empty_callback = function() {};
context.routes = [
['get', '#/get', context.empty_callback],
['post', '#/post', context.empty_callback],
['any', '#/any', context.empty_callback],
['get', '#/string', 'empty']
]
context.app = new Sammy.Application(function() {
this.empty = context.empty_callback;
context.returned = this.mapRoutes(context.routes);
});
}
})
.should('return the app', function() {
equal(this.returned, this.app);
})
.should('add routes to the app', function() {
var app = this.app;
ok(app.routes, "should have routes");
ok(app.routes['get'], "should have get routes");
equal(app.routes['get'][0].path.toString(), new RegExp("#/get$").toString());
})
.should('lookup callbacks as strings', function() {
var app = this.app, route = app.routes['get'].pop();
ok(route);
equal(route.path, new RegExp("#/string$").toString());
equal(route.callback, this.empty_callback);
});
context('Sammy.Application', 'bind', {
before: function() {
var context = this;
context.triggered = false;
this.app = new Sammy.Application(function() {
context.returned = this.bind('boosh', function() {
context.triggered = 'boosh';
context.inner_context = this;
});
this.bind('blurgh', function() {
context.triggered = 'blurgh';
context.inner_context = this;
});
});
}
})
.should('return the sammy application instance', function() {
equal(this.returned, this.app);
})
.should('add callback to the listeners collection', function() {
equal(this.app.listeners['boosh'].length, 1);
})
.should('not be able to trigger before run', function() {
var app = this.app;
var context = this;
app.trigger('boosh');
soon(function() {
equal(context.triggered, false);
});
})
.should('actually bind/be able to trigger to element after run', function() {
var app = this.app;
var context = this;
app.run();
app.trigger('blurgh');
soon(function() {
equal(context.triggered, 'blurgh');
app.unload();
});
})
.should('catch events on the bound element', function() {
var app = this.app;
var context = this;
app.run();
app.$element().trigger('boosh');
soon(function() {
equal(context.triggered, 'boosh');
equal(context.inner_context.verb, 'bind');
app.unload();
}, this, 2, 2);
})
.should('set the context of the bound events to an EventContext', function() {
var app = this.app;
var event_context = null;
var yielded_context = null;
this.app.bind('serious-boosh', function() {
event_context = this;
});
app.run();
app.trigger('serious-boosh');
soon(function() {
deepEqual(event_context.app, app);
equal(event_context.verb, 'bind');
equal(event_context.path, 'serious-boosh');
app.unload();
}, this, 1, 3);
});
context('Sammy.Application','run', {
before: function () {
window.location.hash = ''
var context = this;
context.yielded_context = "";
$('.get_area').text('');
this.app = new Sammy.Application(function() {
this.element_selector = '#main';
this.debug = true;
this.form_params = {};
this.route('get', '#/', function() {
$('.get_area').text('');
});
this.route('get', '#/test', function() {
$('.get_area').text('test success');
});
this.route('post', '#/test', function() {
this.app.form_was_run = 'YES';
this.app.form_params = this.params;
this.app.form_target = this.target;
return false;
});
this.route('post', '#/live', function() {
this.app.form_was_run = 'LIVE';
this.app.form_params = this.params;
this.app.form_target = this.target;
return false;
});
this.route('put', '#/puttest', function() {
this.app.form_was_run = 'PUT';
this.app.form_params = this.params;
this.app.form_target = this.target;
return false;
});
this.route('get', '#/yield', function(c) {
context.yielded_context = c;
});
this.route('get', '#/postpost', function(c) {
this.log('get', '#/postpost');
this.app.get_after_form = 'YES';
});
this.route('post', '#/postpost', function(c) {
this.app.form_was_run = 'POSTPOST';
this.log('post', '#/postpost');
this.redirect('#/postpost');
});
this.bind('blurgh', function () {
$('.get_area').text('event fired');
});
});
}
})
.should('set the location to the start url', function() {
var app = this.app;
app.run('#/');
soon(function() {
equal(window.location.hash, '#/');
app.unload();
});
})
.should('bind events to all forms', function() {
var app = this.app;
app.run('#/');
$('#main form').submit();
soon(function() {
equal(app.form_was_run, 'YES');
ok(app.form_params);
equal(app.form_target, $('#main form').get(0));
app.unload();
}, this, 1, 3);
})
.should('bind events to all future forms', function () {
var app = this.app;
app.run('#/');
// add a new form to the page
$('#main').append('<form id="live_form" action="#/live" method="post">' +
'<input name="live_test" type="text" />' +
'<input type="submit" class="submit" />' +
'</form>'
);
$('#live_form .submit').submit();
soon(function() {
equal(app.form_was_run, 'LIVE');
equal(app.form_target, $('#live_form').get(0));
app.unload();
}, this, 1, 2);
})
.should('get form verb from _method input', function() {
var app = this.app;
// add a new form to the page
$('#main').append('<form id="put_form" action="#/puttest" method="post">' +
'<input name="_method" type="hidden" value="put" />' +
'<input type="submit" class="submit" />' +
'</form>'
);
app.run('#/');
$('#put_form .submit').submit();
soon(function() {
equal(app.form_was_run, 'PUT');
equal(app.form_target, $('#put_form').get(0));
app.unload();
}, this, 1, 2);
})
.should('get form verb even if the browser has no support for the method', function() {
var app = this.app;
// add a new form to the page
$('#main').append('<form id="put_form2" action="#/puttest" method="put">' +
'<input type="submit" class="submit" />' +
'</form>'
);
app.run('#/');
$('#put_form2 .submit').submit();
soon(function() {
equal(app.form_was_run, 'PUT');
equal(app.form_target, $('#put_form2').get(0));
app.unload();
}, this, 1, 2);
})
.should('change the hash when submitting get forms', function() {
var app = this.app;
app.route('get', '#/live', function() {});
app.run('#/');
$('#main').append('<form id="live_form" action="#/live" method="get">' +
'<input name="live_test" type="text" value="live_value"/>' +
'<input type="submit" class="submit"/>' +
'</form>'
);
$('#live_form .submit').submit();
soon(function() {
equal(window.location.hash, '#/live?live_test=live_value');
app.unload();
});
})
.should('URL encode form values', function() {
var app = this.app;
app.route('get', '#/live', function() {});
app.run('#/');
$('#main').append('<form id="live_form" action="#/live" method="get">' +
'<input name="spaces" type="text" value="value with spaces"/>' +
'<input name="pluses" type="text" value="+++"/>' +
'<input type="submit" class="submit"/>' +
'</form>'
);
$('#live_form .submit').submit();
soon(function() {
equal(window.location.hash, '#/live?spaces=value%20with%20spaces&pluses=%2B%2B%2B');
app.unload();
});
})
.should('trigger routes on URL change', function() {
var app = this.app;
app.run();
window.location.hash = '#/test';
soon(function() {
equal($('.get_area').text(), 'test success');
app.unload();
});
})
.should('yield the event context to the route', function() {
var app = this.app;
window.location.hash = '#';
app.run('#/yield');
soon(function() {
equal(this.yielded_context.path, '/#/yield');
app.unload();
}, this);
})
.should('trigger events using the apps trigger method', function() {
var app = this.app;
app.run();
app.trigger('blurgh');
soon(function() {
equal($('.get_area').text(), 'event fired');
app.unload();
});
})
.should('die silently if route is not found and 404s are off', function() {
var app = this.app;
app.raise_errors = false;
app.run();
notRaised(function() {
window.location.hash = '#/no-route-for-me'
soon(function() { app.unload(); });
});
})
.should('redirect to same path after form submit', function() {
var app = this.app;
app.run('#/');
// add a new form to the page
$('#main').append('<form id="live_form" action="#/postpost" method="post">' +
'<input type="submit" class="submit" />' +
'</form>'
);
$('#live_form .submit').submit();
soon(function() {
equal(app.form_was_run, 'POSTPOST');
equal(app.get_after_form, 'YES');
app.unload();
}, this, 2, 2);
});
context('Sammy.Application','lookupRoute', {
before: function() {
this.app = new Sammy.Application(function() {
this.route('get', /\/blah\/(.+)/, function() {
$('#main').trigger('click');
});
this.route('get', '#/boo', function() {
$('#main').trigger('click');
});
this.route('post', '/blah', function() {
$('#testarea').show();
});
});
}
})
.should('find a route by verb and route', function() {
var app = this.app;
var route = app.lookupRoute('post','/blah');
isType(route, 'Object')
equal(route.verb, 'post');
defined(route, 'callback');
})
.should('find a route by verb and partial route', function() {
var app = this.app;
var route = app.lookupRoute('get','/blah/mess');
isType(route, 'Object')
equal(route.verb, 'get');
defined(route, 'callback');
})
.should('ignore any hash query string when looking up a route', function() {
var app = this.app;
var route = app.lookupRoute('get', '#/boo?ohdontmindeme');
isType(route, 'Object');
equal(route.verb, 'get');
defined(route, 'callback');
});
context('Sammy.Application','runRoute', {
before: function() {
var context = this;
this.app = new Sammy.Application(function() {
this.route('get', /\/blah\/(.+)/, function() {
context.params = this.params;
});
this.route('get', /\/forward\/([^\/]+)\/([^\/]+)/, function(c, part1, part2) {
context.inner_context = this;
context.context_arg = c;
context.part1 = part1;
context.part2 = part2;
});
this.route('get', '#/boosh/:test/:test2', function() {
context.params = this.params;
});
this.route('get', '#/message/:message', function() {
context.params = this.params;
});
});
}
})
.should('set named params from a string route', function() {
this.app.runRoute('get', '#/boosh/blurgh/kapow');
equal(this.params['test'], 'blurgh');
equal(this.params['test2'], 'kapow');
})
.should('set unnamed params from a regex route in "splat"', function() {
this.app.runRoute('get', '#/blah/could/be/anything');
equal(this.params['splat'], 'could/be/anything');
})
.should('forward unnamed params to the callback as arguments', function() {
this.app.runRoute('get', '#/forward/to/route');
deepEqual(this.context_arg, this.inner_context);
equal(this.part1, 'to');
equal(this.part2, 'route');
})
.should('set additional params from a query string after the hash', function() {
this.app.runRoute('get', '#/boosh/farg/wow?with=some&nifty=params');
equal(this.params['with'], 'some');
equal(this.params['nifty'], 'params');
})
.should('set value-less params to an empty string', function() {
this.app.runRoute('get', '#/boosh/farg/wow?empty&again=¬-empty=blah');
equal(this.params['empty'], '');
equal(this.params['again'], '');
equal(this.params['not-empty'], 'blah');
})
.should('exclude the query string from named param values', function() {
this.app.runRoute('get', '#/boosh/farg/wow?with=some&nifty=params');
equal(this.params['test'], 'farg');
equal(this.params['test2'], 'wow');
})
.should('exclude the query string from unnamed param values', function() {
this.app.runRoute('get', '#/blah/could/be/anything?except=aquerystring');
equal(this.params['splat'], 'could/be/anything');
})
.should('decode the query string values', function() {
this.app.runRoute('get', '#/boosh/farg/wow?encoded=this%20should%20be%20decoded%24%25%5E');
equal(this.params['encoded'], "this should be decoded$%^")
})
.should('decode param values', function() {
this.app.runRoute('get', '#/message/hello%20there');
equal(this.params['message'], 'hello there');
this.app.runRoute('get', '#/message/hello there');
equal(this.params['message'], 'hello there');
})
.should('raise error when route can not be found', function() {
var app = this.app;
app.raise_errors = true;
raised(/404/, function() {
app.runRoute('get','/blurgh');
});
});
context('Sammy.Application','before', {
before: function() {
window.location.hash = '';
var context = this;
context.before = {};
context.route = {};
this.app = new Sammy.Application(function() {
this.before(function() {
this.params['belch'] = 'burp';
context.before = this;
});
this.get('#/', function() {
context.route = this;
});
this.get('#/boosh', function() {
context.route = this;
});
});
}
})
.should('run before route', function() {
var context = this;
window.location.hash = '#';
this.app.run('#/');
soon(function() {
equal(context.route.params['belch'], 'burp');
context.app.unload();
});
})
.should('set context to event context', function() {
var context = this;
context.app.run('#/');
soon(function() {
deepEqual(context.route, context.before);
context.app.unload();
});
})
.should('not run route if before returns false', function() {
var context = this;
context.app.before(function() {
return false;
});
context.app.run('#/');
soon(function() {
deepEqual(context.before.app, context.app);
deepEqual(context.route, {});
context.app.unload();
}, this, 1, 2);
})
.should('only run if before matches options', function() {
var context = this;
context.before_run = [];
context.app.before({only: '#/boosh'}, function() {
context.before_run.push('/boosh');
});
context.app.before({only: '#/'}, function() {
context.before_run.push('/')
});
window.location.hash = '';
context.app.run();
context.app.setLocation('#/');
expect(4);
stop();
setTimeout(function() {
ok(context.route);
equal(context.route.path, '/#/');
deepEqual(context.before_run, ['/'], 'should match /')
window.location = '#/boosh';
setTimeout(function() {
deepEqual(context.before_run, ['/', '/boosh'], "should match ['/', 'boosh']");
context.app.unload();
start();
}, 100);
}, 200);
})
context('Sammy.Application','after', {
before: function() {
var context = this;
context.after = {};
context.route = {};
this.app = new Sammy.Application(function() {
this.after(function() {
this.params['belch'] = 'burp';
context.after = this;
});
this.get('#/', function() {
this.params['belch'] = 'boosh';
context.route = this;
});
});
}
})
.should('run after route', function() {
var context = this;
this.app.run('#/');
window.location.hash = '#/';
soon(function() {
equal(context.after.params['belch'], 'burp');
context.app.unload();
}, this, 5, 1);
})
.should('set context to event context', function() {
var context = this;
context.app.run('#/');
soon(function() {
deepEqual(context.route, context.after);
context.app.unload();
});
});
context('Sammy.Application', 'around', {
before: function() {
window.location.hash = '';
var context = this;
context.path = [];
context.run_route = true;
this.app = new Sammy.Application(function() {
this.around(function(callback) {
context.path.push('around1 in');
if (context.run_route) {
callback();
}
context.path.push('around1 out');
});
this.get('#/', function() {
context.path.push('route #/');
});
});
}
})
.should('run route with callback()', function() {
var context = this;
context.app.run('#/');
soon(function() {
deepEqual(context.path, ['around1 in', 'route #/', 'around1 out']);
context.app.unload();
});
})
.should('not run route if callback is never called', function() {
var context = this;
context.run_route = false;
context.app.run('#/');
soon(function() {
deepEqual(context.path, ['around1 in', 'around1 out']);
context.app.unload();
});
})
.should('run multiple around filters', function() {
var context = this;
context.app.around(function(callback) {
context.path.push('around2 in');
callback();
context.path.push('around2 out');
});
context.app.run('#/');
soon(function() {
deepEqual(context.path, ['around1 in', 'around2 in', 'route #/', 'around2 out', 'around1 out']);
context.app.unload();
});
})
.should('run before filters after around filter', function() {
var context = this;
context.app.before(function() {
context.path.push('before');
});
context.app.run('#/');
soon(function() {
deepEqual(context.path, ['around1 in', 'before', 'route #/', 'around1 out']);
context.app.unload();
});
});
context('Sammy.Application','helpers', {
before: function() {
var context = this;
context.event_context = null;
this.app = new Sammy.Application(function() {
this.helpers({
helpme: function() {
return "halp!";
}
});
this.get('#/', function() {
this.params['belch'] = 'boosh';
context.event_context = this;
});
this.bind('blurgh', function() {
context.event_context = this;
});
});
}
})
.should('extend event context for routes', function() {
var context = this;
this.app.run('#/');
soon(function() {
ok(context['event_context']);
isType(context.event_context.helpme, 'Function');
this.app.unload();
}, this, 2, 2);
})
.should('extend event context for bind', function() {
var context = this;
this.app.run('#/');
this.app.trigger('blurgh');
soon(function() {
ok(context['event_context']);
isType(context.event_context.helpme, 'Function');
this.app.unload();
}, this, 2, 2);
});
context('Sammy.Application','helper', {
before: function() {
var context = this;
context.event_context = null;
this.app = new Sammy.Application(function() {
this.helper(
"helpme", function() {
return "halp!";
}
);
this.get('#/', function() {
this.params['belch'] = 'boosh';
context.event_context = this;
});
this.bind('blurgh', function() {
context.event_context = this;
});
});
}
})
.should('extend event context for routes', function() {
var context = this;
this.app.run('#/');
soon(function() {
ok(context['event_context']);
isType(context.event_context.helpme, 'Function');
this.app.unload();
}, this, 2, 2);
})
.should('extend event context for bind', function() {
var context = this;
this.app.run('#/');
this.app.trigger('blurgh');
soon(function() {
ok(context['event_context']);
isType(context.event_context.helpme, 'Function');
this.app.unload();
}, this, 2, 2);
});
context('Sammy.Application', 'getLocation', {
before: function() {
this.app = new Sammy.Application;
}
})
.should('return the browsers path and hash by default', function() {
window.location.hash = '#/boosh';
soon(function() {
equal(this.app.getLocation(), "/#/boosh");
}, this);
});
context('Sammy.Application', 'setLocation', {
before: function() {
this.app = new Sammy.Application;
}
})
.should('set the browsers hash by default', function() {
this.app.setLocation('#/blurgh');
soon(function() {
equal(window.location.hash, '#/blurgh');
})
});
context('Sammy.Application', 'post routes', {
before: function() {
var context = this;
context.visited = [];
context.location = "";
context.posted = false;
this.app = new Sammy.Application(function() {
this.get('#/blah', function() {
context.location = "blah";
context.visited.push('blah');
this.redirect('#/boosh');
});
this.get('#/boosh', function() {
context.location = "boosh";
context.visited.push('boosh');
});
this.post(/test/, function() {
context.location = "post";
context.posted = true;
context.visited.push('post');
this.redirect('#/boosh');
});
});
}
})
.should('redirect after a get', function() {
window.location.hash = '';
var context = this;
context.app.run();
window.location.hash = '/blah';
expect(3)
stop();
setTimeout(function() {
$('#main form').submit();
setTimeout(function() {
ok(context.posted);
deepEqual(context.visited, ['blah', 'boosh', 'post', 'boosh'], "was: " + context.visited);
equal(context.location, 'boosh');
context.app.unload();
start();
}, 1000);
}, 1000);
});
context('Sammy.Application', 'contextMatchesOptions', {
before: function() {
this.app = $.sammy();
this.route = {
verb: 'get',
path: '#/boosh',
params: {
'blurgh': 'boosh'
}
};
}
})
.should('match against empty options', function() {
ok(this.app.contextMatchesOptions(this.route, {}));
})
.should('match against only with path', function() {
ok(this.app.contextMatchesOptions(this.route, {only: {path: '#/boosh'}}));
ok(this.app.contextMatchesOptions(this.route, {only: '#/boosh'}));
ok(!this.app.contextMatchesOptions(this.route, {only: {path: '#/'}}));
ok(!this.app.contextMatchesOptions(this.route, {only: '#/'}));
})
.should('match against only with path and verb', function() {
ok(this.app.contextMatchesOptions(this.route, {only: {path: '#/boosh', verb: 'get'}}));
ok(!this.app.contextMatchesOptions(this.route, {only: {path: '#/boosh', verb: 'put'}}));
ok(!this.app.contextMatchesOptions(this.route, {only: {path: '#/', verb: 'get'}}));
})
.should('match against only with verb', function() {
ok(this.app.contextMatchesOptions(this.route, {only: {verb: 'get'}}));
ok(!this.app.contextMatchesOptions(this.route, {only: {verb: 'put'}}));
})
.should('match against only with verb array', function() {
ok(this.app.contextMatchesOptions(this.route, {only: {verb: ['get', 'post']}}));
ok(!this.app.contextMatchesOptions(this.route, {only: {verb: ['put', 'post']}}));
})
.should('match against except with path and verb', function() {
ok(this.app.contextMatchesOptions(this.route, {except: {path: '#/', verb: 'get'}}));
ok(!this.app.contextMatchesOptions(this.route, {except: {path: '#/boosh', verb: 'get'}}));
ok(this.app.contextMatchesOptions(this.route, {except: {path: '#/boosh', verb: 'put'}}));
})
.should('match against except with path', function() {
ok(this.app.contextMatchesOptions(this.route, {except: {path: '#/'}}));
ok(this.app.contextMatchesOptions(this.route, {except: '#/'}));
ok(!this.app.contextMatchesOptions(this.route, {except: {path: '#/boosh'}}));
ok(!this.app.contextMatchesOptions(this.route, {except: '#/boosh'}));
})
.should('match against except with verb', function() {
ok(!this.app.contextMatchesOptions(this.route, {except: {verb: 'get'}}));
ok(this.app.contextMatchesOptions(this.route, {except: {verb: 'put'}}));
})
.should('match against just path', function() {
ok(this.app.contextMatchesOptions(this.route, '#/boosh'), 'should match exact string path');
ok(!this.app.contextMatchesOptions(this.route, '#/boo'), 'should not match partial string path');
ok(this.app.contextMatchesOptions(this.route, /^\#\/boosh/), 'should match regex');
ok(!this.app.contextMatchesOptions(this.route, /^\#\/$/), 'should not match regex');
})
.should('match empty options', function() {
ok(this.app.contextMatchesOptions(this.route, {}));
});
context('Sammy.Application', 'use', {
before: function() {
var context = this;
var TrivialLogin = function(app, a, b, c) {
context.plugin_this = this;
this.a = a;
app.b = b;
this.c = c;
this.isAuthenticated = function(username) {
return true;
}