Home Reference Source Test Repository

unit/index.js

  1. import chai from 'chai';
  2. import sinon from 'sinon';
  3. import sinonChai from 'sinon-chai';
  4. import NprOneSDK from './../../src/index';
  5. import Authorization from './../../src/controller/authorization';
  6. import Action from './../../src/model/action';
  7. import Logger from './../../src/util/logger';
  8.  
  9.  
  10. const should = chai.should();
  11. chai.use(sinonChai);
  12.  
  13.  
  14. /** @test {NprOneSDK} */
  15. describe('NprOneSDK', () => {
  16. /** @type {NprOneSDK} */
  17. let nprOne;
  18.  
  19. beforeEach(() => {
  20. nprOne = new NprOneSDK();
  21. });
  22.  
  23.  
  24. /** @test {NprOneSDK.config} */
  25. describe('config', () => {
  26. describe('getter', () => {
  27. it('should return an object with config related properties', () => {
  28. NprOneSDK.config.authProxyBaseUrl.should.be.ok;
  29. });
  30. });
  31.  
  32. describe('setter', () => {
  33. it('should update specific config values when set', () => {
  34. const previousUrl = NprOneSDK.config.authProxyBaseUrl;
  35. NprOneSDK.config = { 'authProxyBaseUrl': 'https://test' };
  36.  
  37. const newUrl = NprOneSDK.config.authProxyBaseUrl;
  38. NprOneSDK.config.authProxyBaseUrl = previousUrl;
  39. newUrl.should.equal('https://test');
  40. });
  41.  
  42. it('should setup defaults if they haven\'t been set yet', () => {
  43. NprOneSDK._config = undefined;
  44. NprOneSDK.config = { 'test': 'value' };
  45. NprOneSDK.config.test.should.equal('value');
  46. });
  47.  
  48. it('should log a warning when setting deprecated properties', () => {
  49. const stub = sinon.stub(Logger, 'warn');
  50.  
  51. const previousUrl = NprOneSDK.config.apiBaseUrl;
  52. NprOneSDK.config = {
  53. 'apiBaseUrl': 'https://test',
  54. 'apiVersion': 'v2',
  55. };
  56.  
  57. NprOneSDK.config.apiBaseUrl.should.equal('https://test');
  58. NprOneSDK.config.apiVersion.should.equal('v2');
  59. stub.should.have.been.calledTwice;
  60. });
  61. });
  62. });
  63.  
  64.  
  65. /** @test {NprOneSDK.accessToken} */
  66. describe('accessToken', () => {
  67. describe('getter', () => {
  68. it('should return the value of the accessToken string in the hidden config object', () => {
  69. NprOneSDK.accessToken.should.equal(NprOneSDK.config.accessToken);
  70. });
  71. });
  72.  
  73. describe('setter', () => {
  74. it('should throw a TypeError if the passed-in value is not a string', () => {
  75. chai.expect(() => {
  76. NprOneSDK.accessToken = 1234;
  77. }).to.throw('Value for accessToken must be a string');
  78. });
  79.  
  80. it('should set the internal access token to the supplied string', () => {
  81. NprOneSDK.accessToken = 'abcdefgh123456';
  82.  
  83. NprOneSDK.config.accessToken.should.equal('abcdefgh123456');
  84. });
  85.  
  86. it('should call the registered callback if one is set', (done) => {
  87. NprOneSDK.onAccessTokenChanged = () => {
  88. NprOneSDK.config.accessToken.should.equal('abcdefgh123456789abced');
  89. done();
  90. };
  91. NprOneSDK.accessToken = 'abcdefgh123456789abced';
  92. });
  93.  
  94. it('but only if the new token is different from the old', () => {
  95. NprOneSDK.onAccessTokenChanged = () => {
  96. throw new Error('access token did not actually change, should never get here');
  97. };
  98. NprOneSDK.accessToken = NprOneSDK.config.accessToken;
  99. });
  100. });
  101. });
  102.  
  103.  
  104. /** @test {NprOneSDK.onAccessTokenChanged} */
  105. describe('onAccessTokenChanged', () => {
  106. describe('setter', () => {
  107. it('should throw a TypeError if the passed-in value is not a function', () => {
  108. chai.expect(() => {
  109. NprOneSDK.onAccessTokenChanged = 'i_am_not_a_function';
  110. }).to.throw('Value for onAccessTokenChanged must be a function');
  111. });
  112. });
  113. });
  114.  
  115.  
  116. /** @test {NprOneSDK.Action} */
  117. describe('Action', () => {
  118. it('should return the Action class', () => {
  119. NprOneSDK.Action.should.equal(Action);
  120. });
  121. });
  122.  
  123.  
  124. /** @test {NprOneSDK.Logger} */
  125. describe('Logger', () => {
  126. it('should return the Logger class', () => {
  127. NprOneSDK.Logger.should.equal(Logger);
  128. });
  129. });
  130.  
  131.  
  132. /** @test {NprOneSDK.refreshExistingAccessToken} */
  133. describe('refreshExistingAccessToken', () => {
  134. afterEach(() => {
  135. Authorization.refreshExistingAccessToken.restore();
  136. });
  137.  
  138. it('should call the correct service controller method', () => {
  139. const stub = sinon.stub(Authorization, 'refreshExistingAccessToken');
  140. NprOneSDK.refreshExistingAccessToken();
  141. stub.should.have.been.calledWith();
  142. });
  143.  
  144. it('should call the correct service controller method with non-default param values', () => {
  145. const stub = sinon.stub(Authorization, 'refreshExistingAccessToken');
  146. NprOneSDK.refreshExistingAccessToken(3);
  147. stub.should.have.been.calledWith(3);
  148. });
  149. });
  150.  
  151.  
  152. /** @test {NprOneSDK#logout} */
  153. describe('logout', () => {
  154. it('should call the correct service controller method', () => {
  155. const stub = sinon.stub(nprOne._authorization, 'logout');
  156. nprOne.logout();
  157. stub.should.have.been.calledOnce;
  158. });
  159. });
  160.  
  161.  
  162. /** @test {NprOneSDK#getDeviceCode} */
  163. describe('getDeviceCode', () => {
  164. it('should call the correct service controller method', () => {
  165. const stub = sinon.stub(nprOne._authorization, 'getDeviceCode');
  166. nprOne.getDeviceCode();
  167. stub.should.have.been.calledWith([]);
  168. });
  169.  
  170. it('should call the correct service controller method with non-default param values', () => {
  171. const stub = sinon.stub(nprOne._authorization, 'getDeviceCode');
  172. nprOne.getDeviceCode(['listening.readonly']);
  173. stub.should.have.been.calledWith(['listening.readonly']);
  174. });
  175. });
  176.  
  177.  
  178. /** @test {NprOneSDK#pollDeviceCode} */
  179. describe('pollDeviceCode', () => {
  180. it('should call the correct service controller method', () => {
  181. const stub = sinon.stub(nprOne._authorization, 'pollDeviceCode');
  182. nprOne.pollDeviceCode();
  183. stub.should.have.been.calledOnce;
  184. });
  185. });
  186.  
  187.  
  188. /** @test {NprOneSDK#getRecommendation} */
  189. describe('getRecommendation', () => {
  190. it('should call the correct service controller method', () => {
  191. const stub = sinon.stub(nprOne._listening, 'getRecommendation');
  192. nprOne.getRecommendation();
  193. stub.should.have.been.calledWith('', 'npr');
  194. });
  195.  
  196. it('should call the correct service controller method with non-default param values', () => {
  197. const stub = sinon.stub(nprOne._listening, 'getRecommendation');
  198. nprOne.getRecommendation('string1');
  199. stub.should.have.been.calledWith('string1', 'npr');
  200. });
  201.  
  202. it('should call the correct service controller method with non-default param values', () => {
  203. const stub = sinon.stub(nprOne._listening, 'getRecommendation');
  204. nprOne.getRecommendation('', 'string2');
  205. stub.should.have.been.calledWith('', 'string2');
  206. });
  207. });
  208.  
  209.  
  210. /** @test {NprOneSDK#getUpcomingFlowRecommendations} */
  211. describe('getUpcomingFlowRecommendations', () => {
  212. it('should call the correct service controller method', () => {
  213. const stub = sinon.stub(nprOne._listening, 'getUpcomingFlowRecommendations');
  214. nprOne.getUpcomingFlowRecommendations();
  215. stub.should.have.been.calledWith('npr');
  216. });
  217.  
  218. it('should call the correct service controller method with non-default param values', () => {
  219. const stub = sinon.stub(nprOne._listening, 'getUpcomingFlowRecommendations');
  220. nprOne.getUpcomingFlowRecommendations('string1');
  221. stub.should.have.been.calledWith('string1');
  222. });
  223. });
  224.  
  225.  
  226. /** @test {NprOneSDK#getRecommendationsFromChannel} */
  227. describe('getRecommendationsFromChannel', () => {
  228. it('should call the correct service controller method', () => {
  229. const stub = sinon.stub(nprOne._listening, 'getRecommendationsFromChannel');
  230. nprOne.getRecommendationsFromChannel();
  231. stub.should.have.been.calledWith('recommended');
  232. });
  233.  
  234. it('should call the correct service controller method with non-default param values', () => {
  235. const stub = sinon.stub(nprOne._listening, 'getRecommendationsFromChannel');
  236. nprOne.getRecommendationsFromChannel('string1');
  237. stub.should.have.been.calledWith('string1');
  238. });
  239. });
  240.  
  241.  
  242. /** @test {NprOneSDK#queueRecommendationFromChannel} */
  243. describe('queueRecommendationFromChannel', () => {
  244. it('should call the correct service controller method', () => {
  245. const stub = sinon.stub(nprOne._listening, 'queueRecommendationFromChannel');
  246. nprOne.queueRecommendationFromChannel();
  247. stub.should.have.been.calledOnce;
  248. });
  249. });
  250.  
  251.  
  252. /** @test {NprOneSDK#getHistory} */
  253. describe('getHistory', () => {
  254. it('should call the correct service controller method', () => {
  255. const stub = sinon.stub(nprOne._listening, 'getHistory');
  256. nprOne.getHistory();
  257. stub.should.have.been.calledOnce;
  258. });
  259. });
  260.  
  261.  
  262. /** @test {NprOneSDK#resetFlow} */
  263. describe('resetFlow', () => {
  264. it('should call the correct service controller method', () => {
  265. const stub = sinon.stub(nprOne._listening, 'resetFlow');
  266. nprOne.resetFlow();
  267. stub.should.have.been.calledOnce;
  268. });
  269. });
  270.  
  271.  
  272. /** @test {NprOneSDK#resumeFlowFromRecommendation} */
  273. describe('resumeFlowFromRecommendation', () => {
  274. it('should call the correct service controller method', () => {
  275. const stub = sinon.stub(nprOne._listening, 'resumeFlowFromRecommendation');
  276. nprOne.resumeFlowFromRecommendation();
  277. stub.should.have.been.calledOnce;
  278. });
  279. });
  280.  
  281. /** @test {NprOneSDK#getUser} */
  282. describe('getUser', () => {
  283. it('should call the correct service controller method', () => {
  284. const stub = sinon.stub(nprOne._identity, 'getUser');
  285. nprOne.getUser();
  286. stub.should.have.been.calledOnce;
  287. });
  288. });
  289.  
  290.  
  291. /** @test {NprOneSDK#setUserStation} */
  292. describe('setUserStation', () => {
  293. it('should call the correct service controller method', () => {
  294. const stub = sinon.stub(nprOne._identity, 'setUserStation');
  295. nprOne.setUserStation('404');
  296. stub.should.have.been.calledWith('404');
  297. });
  298. });
  299.  
  300.  
  301. /** @test {NprOneSDK#followShow} */
  302. describe('followShow', () => {
  303. it('should call the correct service controller method', () => {
  304. const stub = sinon.stub(nprOne._identity, 'followShow');
  305. nprOne.followShow('123');
  306. stub.should.have.been.calledWith('123');
  307. });
  308. });
  309.  
  310.  
  311. /** @test {NprOneSDK#unfollowShow} */
  312. describe('unfollowShow', () => {
  313. it('should call the correct service controller method', () => {
  314. const stub = sinon.stub(nprOne._identity, 'unfollowShow');
  315. nprOne.unfollowShow('456');
  316. stub.should.have.been.calledWith('456');
  317. });
  318. });
  319.  
  320.  
  321. /** @test {NprOneSDK#createTemporaryUser} */
  322. describe('createTemporaryUser', () => {
  323. it('should call the correct service controller method', () => {
  324. const stub = sinon.stub(nprOne._identity, 'createTemporaryUser');
  325. nprOne.createTemporaryUser();
  326. stub.should.have.been.calledOnce;
  327. });
  328. });
  329.  
  330.  
  331. /** @test {NprOneSDK#searchStations} */
  332. describe('searchStations', () => {
  333. it('should call the correct service controller method', () => {
  334. const stub = sinon.stub(nprOne._stationfinder, 'searchStations');
  335. nprOne.searchStations();
  336. stub.should.have.been.calledWith();
  337. });
  338.  
  339. it('should call the correct service controller method with non-default param values', () => {
  340. const stub = sinon.stub(nprOne._stationfinder, 'searchStations');
  341. nprOne.searchStations('ohio');
  342. stub.should.have.been.calledWith('ohio');
  343. });
  344. });
  345.  
  346.  
  347. /** @test {NprOneSDK#searchStationsByLatLongCoordinates} */
  348. describe('searchStationsByLatLongCoordinates', () => {
  349. it('should call the correct service controller method', () => {
  350. const stub = sinon.stub(nprOne._stationfinder, 'searchStationsByLatLongCoordinates');
  351. nprOne.searchStationsByLatLongCoordinates('37.742', '-77.42');
  352. stub.should.have.been.calledWith('37.742', '-77.42');
  353. });
  354. });
  355.  
  356.  
  357. /** @test {NprOneSDK#searchStationsByCityAndState} */
  358. describe('searchStationsByCityAndState', () => {
  359. it('should call the correct service controller method', () => {
  360. const stub = sinon.stub(nprOne._stationfinder, 'searchStationsByCityAndState');
  361. nprOne.searchStationsByCityAndState('fremont', 'ohio');
  362. stub.should.have.been.calledWith('fremont', 'ohio');
  363. });
  364. });
  365.  
  366.  
  367. /** @test {NprOneSDK#searchStationsByCity} */
  368. describe('searchStationsByCity', () => {
  369. it('should call the correct service controller method', () => {
  370. const stub = sinon.stub(nprOne._stationfinder, 'searchStationsByCity');
  371. nprOne.searchStationsByCity('Toledo');
  372. stub.should.have.been.calledWith('Toledo');
  373. });
  374. });
  375.  
  376.  
  377. /** @test {NprOneSDK#searchStationsByState} */
  378. describe('searchStationsByState', () => {
  379. it('should call the correct service controller method', () => {
  380. const stub = sinon.stub(nprOne._stationfinder, 'searchStationsByState');
  381. nprOne.searchStationsByState('ohio');
  382. stub.should.have.been.calledWith('ohio');
  383. });
  384. });
  385.  
  386.  
  387. /** @test {NprOneSDK#getStationDetails} */
  388. describe('getStationDetails', () => {
  389. it('should call the correct service controller method', () => {
  390. const stub = sinon.stub(nprOne._stationfinder, 'getStationDetails');
  391. nprOne.getStationDetails(305);
  392. stub.should.have.been.calledWith(305);
  393. });
  394. });
  395.  
  396.  
  397. /** @test {NprOneSDK.getServiceUrl} */
  398. describe('getServiceUrl', () => {
  399. it('should return the correct API service url', () => {
  400. const url = NprOneSDK.getServiceUrl('listening');
  401. url.should.be.ok;
  402. });
  403.  
  404. it('should throw if an invalid service name is specified', () => {
  405. chai.expect(() => {
  406. NprOneSDK.getServiceUrl('invalid');
  407. }).to.throw('Must specify a valid service name');
  408. });
  409. });
  410. });