/w7d2/spec/javascripts/intro.spec.js

https://gitlab.com/mtdoss/AppAcademy · JavaScript · 165 lines · 113 code · 34 blank · 18 comment · 0 complexity · 36fa87062e4cef2703ddd12afc635a46 MD5 · raw file

  1. describe("Pokedex.Models.Pokemon", function() {
  2. it("has a urlRoot property", function() {
  3. var poke = new Pokedex.Models.Pokemon();
  4. expect(poke.urlRoot).toEqual('/pokemon');
  5. poke.set('id', 1);
  6. expect(poke.url()).toEqual('/pokemon/1');
  7. });
  8. });
  9. describe("Pokedex.Collections.Pokemon", function() {
  10. beforeEach(function() {
  11. this.pokeCollection = new Pokedex.Collections.Pokemon();
  12. });
  13. it("knows its model", function() {
  14. expect(this.pokeCollection.model).toEqual(Pokedex.Models.Pokemon);
  15. });
  16. it("knows its url", function() {
  17. expect(this.pokeCollection.url).toEqual('/pokemon');
  18. });
  19. });
  20. describe("listPokemon", function() {
  21. // AR: alternative way to check for 'fetch' call below
  22. //it("fetches the passed-in collection", function() {
  23. // var pokedex = new Pokedex();
  24. // var pokes = new Pokedex.Collections.Pokemon();
  25. // var eventSpy = sinon.spy();
  26. // pokes.on('request', eventSpy);
  27. // pokedex.listPokemon(pokes, function() {});
  28. // expect(eventSpy.calledOnce).toBeTruthy();
  29. //});
  30. it("fetches the passed-in collection", function() {
  31. var pokedex = new Pokedex($('<div>'));
  32. this.server = sinon.fakeServer.create();
  33. pokedex.listPokemon(function() {});
  34. expect(this.server.requests.length)
  35. .toEqual(1);
  36. expect(this.server.requests[0].method)
  37. .toEqual("GET");
  38. expect(this.server.requests[0].url)
  39. .toEqual("/pokemon");
  40. });
  41. it("fills the passed-in collection", function() {
  42. var pokedex = new Pokedex($('<div>'));
  43. this.fixture = this.fixtures.Pokemon.index.response;
  44. this.server = sinon.fakeServer.create();
  45. this.server.respondWith(
  46. "GET",
  47. "/pokemon",
  48. [
  49. 200,
  50. {"Content-Type": "application/json"},
  51. JSON.stringify(this.fixture)
  52. ]
  53. );
  54. pokedex.listPokemon(function() {});
  55. this.server.respond();
  56. // check number of pokemon on success - should be 3
  57. expect(pokedex.pokes.length).toEqual(this.fixture.length);
  58. });
  59. describe("calling the callback function", function() {
  60. beforeEach(function () {
  61. this.called = false;
  62. this.pokedex = new Pokedex($('<div>'));
  63. this.fixture = this.fixtures.Pokemon.index.response;
  64. this.server = sinon.fakeServer.create();
  65. this.server.respondWith(
  66. "GET",
  67. "/pokemon",
  68. [
  69. 200,
  70. {"Content-Type": "application/json"},
  71. JSON.stringify(this.fixture)
  72. ]
  73. );
  74. var that = this;
  75. this.pokedex.listPokemon(function() {
  76. that.called = true;
  77. });
  78. });
  79. afterEach(function() {
  80. this.server.restore();
  81. });
  82. it("calls the callback function", function() {
  83. this.server.respond();
  84. expect(this.called).toBeTruthy();
  85. });
  86. it("waits to call the callback until the collection has been filled",
  87. function() {
  88. expect(this.called).toBeFalsy();
  89. this.server.respond();
  90. expect(this.called).toBeTruthy();
  91. expect(this.pokedex.pokes.length).toEqual(this.fixture.length);
  92. });
  93. });
  94. });
  95. describe("createPokemon", function() {
  96. beforeEach(function() {
  97. this.server = sinon.fakeServer.create();
  98. this.pokedex = new Pokedex($('<div>'));
  99. });
  100. afterEach(function() {
  101. this.server.restore();
  102. });
  103. it("returns a Pokemon model", function() {
  104. var poke = this.pokedex.createPokemon();
  105. expect(poke.__proto__).toEqual(Pokedex.Models.Pokemon.prototype);
  106. });
  107. it("gives returned model the right attritbutes", function() {
  108. var poke = this.pokedex.createPokemon({name: "Pikachu", number: 25});
  109. expect(poke.get('name')).toEqual("Pikachu");
  110. expect(poke.get('number')).toEqual(25);
  111. });
  112. it("sends the correct HTTP request to the server", function() {
  113. this.pokedex.createPokemon();
  114. expect(this.server.requests.length)
  115. .toEqual(1);
  116. expect(this.server.requests[0].method)
  117. .toEqual("POST");
  118. expect(this.server.requests[0].url)
  119. .toEqual("/pokemon");
  120. });
  121. it("adds the model to Pokedex collection", function() {
  122. // do we want to have a global collection defined on Pokedex?
  123. // e.g., new Pokedex().pokes = new Pokedex.Collections.Pokemon();
  124. // I would like createPokemon to add the model to a collection,
  125. // so that students can do this with Collection#create, and maybe
  126. // experience using {wait: true}
  127. //this.pokedex.createPokemon({ name: "Pikachu", number: 25 });
  128. //this.server.respond();
  129. //expect(this.pokedex.pokes.length).toEqual(1);
  130. });
  131. });