1 | var app = getApp();
|
2 | var compound = app.compound;
|
3 |
|
4 | function initApp() {
|
5 | var s = compound.structure;
|
6 | s.controllers.ctl = function Ctl(){};
|
7 | s.views['ctl/view'] = 'exists';
|
8 | s.views['layouts/application_layout'] = 'layout';
|
9 |
|
10 | };
|
11 |
|
12 | describe('controller-extensions', function() {
|
13 |
|
14 | before(initApp);
|
15 |
|
16 | var controller, rendered, params = {}, req = {
|
17 | app: app,
|
18 | param: function(what) {
|
19 | return params[what];
|
20 | }
|
21 | }, res = {
|
22 | render: function (file, params, callback) {
|
23 | rendered.push(file);
|
24 | if (callback) callback();
|
25 | }
|
26 | };
|
27 |
|
28 | function declareAndRun(action, test) {
|
29 | controller.action(action.name, action);
|
30 | controller.perform(action.name, req, res, test);
|
31 | }
|
32 |
|
33 | describe('rendering', function() {
|
34 |
|
35 | beforeEach(function() {
|
36 | controller = compound.controllerBridge.getInstance('ctl');
|
37 | rendered = [];
|
38 | });
|
39 |
|
40 | it('should render existing view', function(done) {
|
41 | declareAndRun(function renderExisting(c) {
|
42 | c.render('view');
|
43 | }, function() {
|
44 | rendered.should.eql(['exists', 'layout']);
|
45 | done();
|
46 | });
|
47 | });
|
48 |
|
49 | it('should render file without layout', function(done) {
|
50 | declareAndRun(function renderNoLayout(c) {
|
51 | c.render('view', {layout: false});
|
52 | }, function() {
|
53 | rendered.should.eql(['exists']);
|
54 | done();
|
55 | });
|
56 | });
|
57 | });
|
58 |
|
59 | describe('safe', function() {
|
60 | beforeEach(function() {
|
61 | controller = compound.controllerBridge.getInstance('ctl');
|
62 | rendered = [];
|
63 | });
|
64 |
|
65 | it('should call callback when no error', function(done) {
|
66 | var args;
|
67 | declareAndRun(function renderDifferentFormat(c) {
|
68 | doAsyncStuff(c.safe(function() {
|
69 | args = Array.prototype.slice.call(arguments);
|
70 | c.next();
|
71 | }));
|
72 | }, function() {
|
73 | args.should.have.lengthOf(1);
|
74 | args[0].should.equal('he');
|
75 | done();
|
76 | });
|
77 | function doAsyncStuff(cb) {process.nextTick(function(){cb(null, 'he')})}
|
78 | });
|
79 |
|
80 | it('should call c.next(err) when error', function(done) {
|
81 | var called = false;
|
82 | declareAndRun(function renderDifferentFormat(c) {
|
83 | doAsyncStuff(c.safe(function() {
|
84 | called = true;
|
85 | }));
|
86 | }, function() {
|
87 | called.should.be.false;
|
88 | done();
|
89 | });
|
90 | function doAsyncStuff(cb) {process.nextTick(function(){cb(new Error)})}
|
91 | });
|
92 | });
|
93 |
|
94 | describe('format', function() {
|
95 | beforeEach(function() {
|
96 | controller = compound.controllerBridge.getInstance('ctl');
|
97 | rendered = [];
|
98 | delete params.format;
|
99 | });
|
100 |
|
101 | it('should render desired format', function(done) {
|
102 | var args;
|
103 | params.format = 'mobile';
|
104 | declareAndRun(function renderDesiredFormat(c) {
|
105 | c.format({
|
106 | mobile: function() {
|
107 | c.next();
|
108 | }
|
109 | });
|
110 | }, done)
|
111 | });
|
112 |
|
113 | it('should render default format', function(done) {
|
114 | var args;
|
115 | params.format = 'unknown';
|
116 | declareAndRun(function renderDefaultFormat(c) {
|
117 | c.format({
|
118 | html: function() {
|
119 | c.next();
|
120 | }
|
121 | });
|
122 | }, done)
|
123 | });
|
124 |
|
125 | it('should render customized default format', function(done) {
|
126 | var args;
|
127 | app.set('default format', 'json');
|
128 | params.format = 'unknown';
|
129 | declareAndRun(function renderDefaultFormat(c) {
|
130 | c.format({
|
131 | json: function() {
|
132 | c.next();
|
133 | delete app.settings['default format'];
|
134 | }
|
135 | });
|
136 | }, done)
|
137 | });
|
138 | });
|
139 |
|
140 | });
|