PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/public/js/specs/views/bank_account/BankAccountFormView.spec.js

https://github.com/bozz/cashflow
JavaScript | 107 lines | 80 code | 25 blank | 2 comment | 0 complexity | 82742407aa8f615fc14dfcec49a4fb70 MD5 | raw file
  1. describe('BankAccountFormView', function() {
  2. var BankAccountFormView;
  3. beforeEach(function() {
  4. this.bankAccountStub = sinon.spy(Backbone.Model.extend({
  5. urlRoot: '/api/banks/'
  6. }));
  7. BankAccountFormView = testr('views/bank_account/form', {
  8. 'models/BankAccount': this.bankAccountStub
  9. });
  10. });
  11. describe("When instantiated with existing model", function() {
  12. beforeEach(function() {
  13. this.bankAccount = Factory.createModel('BankAccount');
  14. this.view = new BankAccountFormView({
  15. model: this.bankAccount
  16. });
  17. this.view.render();
  18. });
  19. it("should create a default DIV element", function() {
  20. expect(this.view.el.nodeName).toEqual("DIV");
  21. });
  22. it("should contain a FORM element with class 'form-horizontal'", function() {
  23. expect(this.view.$el).toContain('form.form-horizontal');
  24. });
  25. it("should have 'name' input field initialized with model name", function() {
  26. expect($('input#ba-name', this.view.$el)).toHaveValue(this.bankAccount.get('name'));
  27. });
  28. it("should not create new model", function() {
  29. expect(this.bankAccountStub).not.toHaveBeenCalled();
  30. });
  31. });
  32. describe("When instantiated with model id", function() {
  33. describe("without collection", function() {
  34. beforeEach(function() {
  35. this.server = sinon.fakeServer.create();
  36. this.factoryResponse = Factory.createResponse('BankAccount', 'valid');
  37. this.server.respondWith(
  38. "GET",
  39. "/api/banks/1",
  40. this.factoryResponse.wrapped
  41. );
  42. this.view = new BankAccountFormView({
  43. id: 1
  44. });
  45. // this.renderSpy = sinon.spy(this.view, 'render');
  46. });
  47. afterEach(function() {
  48. this.server.restore();
  49. })
  50. it("should make correct request to fetch model", function(){
  51. expect(this.server.requests.length).toEqual(1);
  52. expect(this.server.requests[0].method).toEqual("GET");
  53. expect(this.server.requests[0].url).toEqual("/api/banks/1");
  54. });
  55. it("should fetch and initialize model", function() {
  56. this.server.respond();
  57. var raw = this.factoryResponse.raw;
  58. expect(this.view.model.get('id')).toEqual(raw.response.id);
  59. expect(this.view.model.get('name')).toEqual(raw.response.name);
  60. expect(this.view.model.isNew()).not.toBeTruthy();
  61. });
  62. it("should render view after fetching model", function() {
  63. this.server.respond();
  64. // expect(this.renderSpy).toHaveBeenCalledOnce();
  65. expect($('input#ba-bank', this.view.$el)).toHaveValue(this.view.model.get('bank'));
  66. });
  67. });
  68. });
  69. describe("When instantiated with no model or model id", function() {
  70. beforeEach(function() {
  71. this.view = new BankAccountFormView();
  72. this.view.render();
  73. });
  74. it("should create new model", function() {
  75. expect(this.bankAccountStub).toHaveBeenCalledOnce();
  76. });
  77. it("should have model without id and be flagged as new", function() {
  78. expect(this.view.model.id).toBeUndefined();
  79. expect(this.view.model.isNew()).toBeTruthy();
  80. });
  81. });
  82. });