PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/chiropractor-spec.js

https://github.com/pjmurray/chiropractor
JavaScript | 324 lines | 247 code | 65 blank | 12 comment | 1 complexity | cf12c9388564429990a42798d531d225 MD5 | raw file
  1. describe("Chiropractor", function () {
  2. function getParams(subject) {
  3. var ajaxCall = spyOn(jQuery, 'ajax');
  4. subject();
  5. return ajaxCall.mostRecentCall.args[0];
  6. }
  7. function serverCall(subject, status) {
  8. var params = getParams(subject);
  9. params.error({status: status.toString()});
  10. }
  11. function sharedBehaviourForMethod(subject, modelModification) {
  12. var aGeneric = 404;
  13. var theDefault = 500;
  14. var specific = 420;
  15. var notInValids = 501;
  16. describe("models", function () {
  17. var model, defaultHandler, genericHandler, opts;
  18. describe("save", function () {
  19. var method;
  20. beforeEach(function () {
  21. defaultHandler = jasmine.createSpy('generic 500 error');
  22. genericHandler = jasmine.createSpy('generic 404 error');
  23. var generics = {};
  24. generics[theDefault] = defaultHandler;
  25. generics[aGeneric] = genericHandler;
  26. subject(
  27. theDefault, //default status code
  28. [aGeneric, specific, theDefault], //all accepted codes
  29. generics
  30. );
  31. var Model = Backbone.Model.extend({
  32. url: function () {
  33. return "test"
  34. }
  35. });
  36. model = new Model();
  37. modelModification(model);
  38. method = model.save;
  39. opts = {};
  40. });
  41. function runMethodForErrorCode(code) {
  42. serverCall(function () {
  43. method.call(model, {}, opts)
  44. }, code);
  45. }
  46. describe("default handler", function () {
  47. describe('when the status is not in validCodes', function () {
  48. it("should be called", function () {
  49. runMethodForErrorCode(notInValids);
  50. expect(defaultHandler).toHaveBeenCalled();
  51. });
  52. });
  53. describe('when there is no specific or generic handler for the status', function () {
  54. it("should be called", function () {
  55. runMethodForErrorCode(specific);
  56. expect(defaultHandler).toHaveBeenCalled();
  57. });
  58. });
  59. });
  60. describe("generic handlers", function () {
  61. it("should perform the provided behaviour", function () {
  62. runMethodForErrorCode(theDefault);
  63. expect(defaultHandler).toHaveBeenCalled();
  64. });
  65. it("should work with any status code", function () {
  66. runMethodForErrorCode(aGeneric);
  67. expect(genericHandler).toHaveBeenCalled();
  68. });
  69. });
  70. describe("specific handlers", function () {
  71. var specificHandler;
  72. beforeEach(function () {
  73. specificHandler = jasmine.createSpy('specific error');
  74. });
  75. it("should override a generic handler", function () {
  76. opts[aGeneric] = specificHandler;
  77. runMethodForErrorCode(aGeneric);
  78. expect(specificHandler).toHaveBeenCalled();
  79. });
  80. it("should override the default handler", function () {
  81. opts[theDefault] = specificHandler;
  82. runMethodForErrorCode(notInValids);
  83. expect(specificHandler).toHaveBeenCalled();
  84. });
  85. });
  86. describe("hooks", function () {
  87. describe("post hook", function () {
  88. var postHook;
  89. beforeEach(function () {
  90. postHook = jasmine.createSpy('generic post error function');
  91. opts['post_error'] = postHook;
  92. });
  93. it("should get called", function () {
  94. runMethodForErrorCode(theDefault);
  95. expect(postHook).toHaveBeenCalled();
  96. });
  97. it("should get called before the generic handler", function () {
  98. var someVariable;
  99. var generics = {};
  100. generics[theDefault] = function () {
  101. someVariable = 2;
  102. }
  103. subject(theDefault, [theDefault], generics);
  104. opts['post_error'] = function () {
  105. someVariable = someVariable + 1;
  106. };
  107. runMethodForErrorCode(theDefault);
  108. expect(someVariable).toEqual(3);
  109. });
  110. it("should not get called when a specific handler is called", function () {
  111. opts[specific] = function () {};
  112. runMethodForErrorCode(specific);
  113. expect(postHook).not.toHaveBeenCalled();
  114. });
  115. });
  116. describe("pre hook", function () {
  117. var preHook;
  118. beforeEach(function () {
  119. preHook = jasmine.createSpy('generic pre error function');
  120. opts['pre_error'] = preHook;
  121. });
  122. it("should get called when defined as pre_error", function () {
  123. runMethodForErrorCode(theDefault);
  124. expect(preHook).toHaveBeenCalled();
  125. });
  126. it("should get called after the generic handler", function () {
  127. var someVariable;
  128. var generics = {};
  129. generics[theDefault] = function () {
  130. someVariable = someVariable + 2;
  131. };
  132. subject(theDefault, [theDefault], generics);
  133. opts['pre_error'] = function () {
  134. someVariable = 1;
  135. };
  136. runMethodForErrorCode(theDefault);
  137. expect(someVariable).toEqual(3);
  138. });
  139. it("should not get called when a specific handler is called", function () {
  140. opts[specific] = function () {};
  141. runMethodForErrorCode(specific);
  142. expect(preHook).not.toHaveBeenCalled();
  143. });
  144. });
  145. });
  146. describe('error', function () {
  147. var errorHandler;
  148. beforeEach(function () {
  149. errorHandler = jasmine.createSpy('error handler');
  150. opts['error'] = errorHandler;
  151. });
  152. it('should not run the preHook', function () {
  153. });
  154. it('should not run the postHook', function () {
  155. });
  156. it("should be run instead of the default", function () {
  157. runMethodForErrorCode(theDefault);
  158. expect(errorHandler).toHaveBeenCalled();
  159. expect(defaultHandler).not.toHaveBeenCalled();
  160. });
  161. it("should be run instead of a generic", function () {
  162. runMethodForErrorCode(aGeneric);
  163. expect(errorHandler).toHaveBeenCalled();
  164. expect(genericHandler).not.toHaveBeenCalled();
  165. });
  166. it("should be run instead of a specifc", function () {
  167. var specificHandler = jasmine.createSpy('specifc error function');
  168. opts[specific] = specificHandler;
  169. runMethodForErrorCode(specific);
  170. expect(errorHandler).toHaveBeenCalled();
  171. expect(specificHandler).not.toHaveBeenCalled();
  172. });
  173. });
  174. });
  175. //
  176. describe("when the init happens after the use call", function () {
  177. it("should still work", function () {
  178. defaultHandler = jasmine.createSpy('error');
  179. var Model = Backbone.Model.extend({
  180. url: function () {
  181. return "test"
  182. }
  183. });
  184. model = new Model();
  185. modelModification(model);
  186. chiropractor.each(500, [500], {500: defaultHandler});
  187. serverCall(function () {
  188. model.save();
  189. }, 500);
  190. expect(defaultHandler).toHaveBeenCalled();
  191. });
  192. });
  193. });
  194. describe("collections", function () {
  195. var collection;
  196. beforeEach(function () {
  197. errorCallback = jasmine.createSpy('500 error');
  198. chiropractor.each(500, [500], {500: errorCallback});
  199. var Collection = Backbone.Collection.extend({
  200. url: function () {
  201. return "test"
  202. }
  203. });
  204. collection = new Collection();
  205. modelModification(collection);
  206. });
  207. it("should default to 500", function () {
  208. serverCall(function () {
  209. collection.fetch()
  210. }, notInValids);
  211. expect(errorCallback).toHaveBeenCalled();
  212. });
  213. it("should work with create when not passed an existing backbone model", function () {
  214. serverCall(function () {
  215. collection.create({});
  216. }, notInValids);
  217. expect(errorCallback).toHaveBeenCalled();
  218. });
  219. });
  220. }
  221. describe("#all", function () {
  222. sharedBehaviourForMethod(chiropractor.all, function () {
  223. });
  224. });
  225. describe("#each", function () {
  226. describe('with #use', function () { sharedBehaviourForMethod(chiropractor.each, function (object) {
  227. chiropractor.use(object);
  228. });
  229. });
  230. describe('without anything', function () {
  231. describe('model#save', function () {
  232. it("should be unchanged", function () {
  233. chiropractor.each(500, [], {});
  234. var model = new Backbone.Model({});
  235. expect(model.save).toEqual(Backbone.Model.prototype.save);
  236. });
  237. });
  238. })
  239. });
  240. describe('oneOff', function () {
  241. describe('with #each', function () {
  242. it("should return the error handler", function () {
  243. })
  244. });
  245. });
  246. }
  247. )
  248. ;
  249. //
  250. //function test (fn) {
  251. // return function () {
  252. // it("should work", function () {
  253. // expect(fn()).toEqual(7)
  254. // });
  255. // }
  256. //}
  257. //describe("testing", test(function () {
  258. // return 7
  259. //}))