Home Reference Source Test Repository

unit/controller/station-finder.js

  1. import chai from 'chai';
  2. import { STATION_FINDER_RESPONSE } from '../../test-data';
  3. import mockery from 'mockery';
  4. import fetchMock from 'fetch-mock';
  5. import StationFinder from './../../../src/controller/station-finder';
  6. import NprOne from './../../../src/index';
  7. import { testConfig } from '../../test';
  8.  
  9. const should = chai.should();
  10.  
  11.  
  12. /** @test {StationFinder} */
  13. describe('StationFinder', () => {
  14. let stationFinder;
  15. let stationResponseClone;
  16. const searchStationsUrl = `^${NprOne.getServiceUrl('stationfinder')}/stations`;
  17.  
  18. beforeEach(() => {
  19. stationFinder = new StationFinder();
  20. NprOne.config = testConfig;
  21.  
  22. stationResponseClone = JSON.parse(JSON.stringify(STATION_FINDER_RESPONSE));
  23.  
  24. mockery.registerMock('fetch', fetchMock
  25. .mock(searchStationsUrl, 'GET', stationResponseClone)
  26. .getMock());
  27. });
  28.  
  29. afterEach(() => {
  30. fetchMock.restore();
  31. mockery.deregisterMock('fetch');
  32. });
  33.  
  34.  
  35. /** @test {StationFinder#searchStations} */
  36. describe('searchStations', () => {
  37. it('should make a request to /stationfinder/stations and find two stations', (done) => {
  38. stationFinder.searchStations()
  39. .then((stations) => {
  40. fetchMock.called(searchStationsUrl).should.be.true;
  41. fetchMock.calls().unmatched.length.should.equal(0);
  42. stations.length.should.equal(2);
  43. done();
  44. })
  45. .catch(done);
  46. });
  47.  
  48. it('should include the query string in the search if one is given', (done) => {
  49. stationFinder.searchStations('colorado')
  50. .then((stations) => {
  51. fetchMock.called(searchStationsUrl).should.be.true;
  52. /q=colorado/.test(fetchMock.lastUrl()).should.be.true;
  53. stations.length.should.equal(2);
  54. done();
  55. })
  56. .catch(done);
  57. });
  58.  
  59. it('should throw an error when given an invalid query string', () => {
  60. chai.expect(() => {
  61. stationFinder.searchStations(124);
  62. }).to.throw('Station search query must be a string');
  63. });
  64.  
  65. it('should return an empty array if no stations results were found', (done) => {
  66. fetchMock.restore();
  67. mockery.deregisterMock('fetch');
  68.  
  69. mockery.registerMock('fetch', fetchMock
  70. .mock(searchStationsUrl, 'GET', {})
  71. .getMock());
  72.  
  73. stationFinder.searchStations()
  74. .then((stations) => {
  75. fetchMock.called(searchStationsUrl).should.be.true;
  76. fetchMock.calls().unmatched.length.should.equal(0);
  77. stations.length.should.equal(0);
  78. done();
  79. })
  80. .catch(done);
  81. });
  82. });
  83.  
  84.  
  85. /** @test {StationFinder#searchStationsByLatLongCoordinates} */
  86. describe('searchStationsByLatLongCoordinates', () => {
  87. it('should make a request to /stationfinder/stations and find two stations', (done) => {
  88. stationFinder.searchStationsByLatLongCoordinates(37.241, -77.1352)
  89. .then((stations) => {
  90. fetchMock.called(searchStationsUrl).should.be.true;
  91. fetchMock.calls().unmatched.length.should.equal(0);
  92. stations.length.should.equal(2);
  93. done();
  94. })
  95. .catch(done);
  96. });
  97.  
  98. it('should throw an error when given an invalid values', () => {
  99. chai.expect(() => {
  100. stationFinder.searchStationsByLatLongCoordinates('bad', 'values');
  101. }).to.throw('Latitude and longitude must both be valid numbers (floats)');
  102. });
  103. });
  104.  
  105.  
  106. /** @test {StationFinder#searchStationsByCity} */
  107. describe('searchStationsByCity', () => {
  108. it('should make a request to /stationfinder/stations and find two stations', (done) => {
  109. stationFinder.searchStationsByCity('toledo')
  110. .then((stations) => {
  111. fetchMock.called(searchStationsUrl).should.be.true;
  112. fetchMock.calls().unmatched.length.should.equal(0);
  113. stations.length.should.equal(2);
  114. done();
  115. })
  116. .catch(done);
  117. });
  118.  
  119. it('should throw an error when given an invalid values', () => {
  120. chai.expect(() => {
  121. stationFinder.searchStationsByCity(4134);
  122. }).to.throw('Station search city name must be a string');
  123. });
  124. });
  125.  
  126.  
  127. /** @test {StationFinder#searchStationsByState} */
  128. describe('searchStationsByState', () => {
  129. it('should make a request to /stationfinder/stations and find two stations', (done) => {
  130. stationFinder.searchStationsByState('ohio')
  131. .then((stations) => {
  132. fetchMock.called(searchStationsUrl).should.be.true;
  133. fetchMock.calls().unmatched.length.should.equal(0);
  134. stations.length.should.equal(2);
  135. done();
  136. })
  137. .catch(done);
  138. });
  139.  
  140. it('should throw an error when given an invalid values', () => {
  141. chai.expect(() => {
  142. stationFinder.searchStationsByState(4134);
  143. }).to.throw('Station search state name must be a string');
  144. });
  145. });
  146.  
  147.  
  148. /** @test {StationFinder#searchStationsByCityAndState} */
  149. describe('searchStationsByCityAndState', () => {
  150. it('should make a request to /stationfinder/stations and find two stations', (done) => {
  151. stationFinder.searchStationsByCityAndState('toledo', 'oh')
  152. .then((stations) => {
  153. fetchMock.called(searchStationsUrl).should.be.true;
  154. fetchMock.calls().unmatched.length.should.equal(0);
  155. stations.length.should.equal(2);
  156. done();
  157. })
  158. .catch(done);
  159. });
  160. });
  161.  
  162.  
  163. /** @test {StationFinder#getStationDetails} */
  164. describe('getStationDetails', () => {
  165. it('should make a request to /stationfinder/stations and find the requested station', (done) => {
  166. fetchMock.restore();
  167. mockery.deregisterMock('fetch');
  168. mockery.registerMock('fetch', fetchMock
  169. .mock(searchStationsUrl, 'GET', JSON.parse(JSON.stringify(STATION_FINDER_RESPONSE.items[0])))
  170. .getMock());
  171.  
  172. const stationId = '330';
  173. stationFinder.getStationDetails(stationId)
  174. .then((station) => {
  175. fetchMock.called(searchStationsUrl).should.be.true;
  176. fetchMock.calls().unmatched.length.should.equal(0);
  177. station.id.should.equal(stationId);
  178. done();
  179. })
  180. .catch(done);
  181. });
  182.  
  183. it('should make a request to /stationfinder/stations and return a promise that rejects if the requested station is a non-NPR One station', (done) => {
  184. fetchMock.restore();
  185. mockery.deregisterMock('fetch');
  186. mockery.registerMock('fetch', fetchMock
  187. .mock(searchStationsUrl, 'GET', JSON.parse(JSON.stringify(STATION_FINDER_RESPONSE.items[2])))
  188. .getMock());
  189.  
  190. const stationId = '1209';
  191. const error = `The station ${stationId} is not eligible for NPR One.`;
  192. stationFinder.getStationDetails(stationId).should.be.rejectedWith(error).notify(done);
  193. });
  194.  
  195. it('should throw an error when given an invalid values', (done) => {
  196. const error = 'Error: Station ID must be an integer greater than 0';
  197. stationFinder.getStationDetails('bad_value').should.be.rejectedWith(error).notify(done);
  198. });
  199. });
  200. });