test.webutil.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* jshint expr: true */
  2. const expect = chai.expect;
  3. import * as WebUtil from '../app/webutil.js';
  4. describe('WebUtil', function () {
  5. "use strict";
  6. describe('config variables', function () {
  7. it('should parse query string variables', function () {
  8. // history.pushState() will not cause the browser to attempt loading
  9. // the URL, this is exactly what we want here for the tests.
  10. history.pushState({}, '', "test?myvar=myval");
  11. expect(WebUtil.getConfigVar("myvar")).to.be.equal("myval");
  12. });
  13. it('should return default value when no query match', function () {
  14. history.pushState({}, '', "test?myvar=myval");
  15. expect(WebUtil.getConfigVar("other", "def")).to.be.equal("def");
  16. });
  17. it('should handle no query match and no default value', function () {
  18. history.pushState({}, '', "test?myvar=myval");
  19. expect(WebUtil.getConfigVar("other")).to.be.equal(null);
  20. });
  21. it('should parse fragment variables', function () {
  22. history.pushState({}, '', "test#myvar=myval");
  23. expect(WebUtil.getConfigVar("myvar")).to.be.equal("myval");
  24. });
  25. it('should return default value when no fragment match', function () {
  26. history.pushState({}, '', "test#myvar=myval");
  27. expect(WebUtil.getConfigVar("other", "def")).to.be.equal("def");
  28. });
  29. it('should handle no fragment match and no default value', function () {
  30. history.pushState({}, '', "test#myvar=myval");
  31. expect(WebUtil.getConfigVar("other")).to.be.equal(null);
  32. });
  33. it('should handle both query and fragment', function () {
  34. history.pushState({}, '', "test?myquery=1#myhash=2");
  35. expect(WebUtil.getConfigVar("myquery")).to.be.equal("1");
  36. expect(WebUtil.getConfigVar("myhash")).to.be.equal("2");
  37. });
  38. it('should prioritize fragment if both provide same var', function () {
  39. history.pushState({}, '', "test?myvar=1#myvar=2");
  40. expect(WebUtil.getConfigVar("myvar")).to.be.equal("2");
  41. });
  42. });
  43. describe('cookies', function () {
  44. // TODO
  45. });
  46. describe('settings', function () {
  47. describe('localStorage', function () {
  48. let chrome = window.chrome;
  49. before(function () {
  50. chrome = window.chrome;
  51. window.chrome = null;
  52. });
  53. after(function () {
  54. window.chrome = chrome;
  55. });
  56. let origLocalStorage;
  57. beforeEach(function () {
  58. origLocalStorage = Object.getOwnPropertyDescriptor(window, "localStorage");
  59. Object.defineProperty(window, "localStorage", {value: {}});
  60. if (window.localStorage.setItem !== undefined) {
  61. // Object.defineProperty() doesn't work properly in old
  62. // versions of Chrome
  63. this.skip();
  64. }
  65. window.localStorage.setItem = sinon.stub();
  66. window.localStorage.getItem = sinon.stub();
  67. window.localStorage.removeItem = sinon.stub();
  68. return WebUtil.initSettings();
  69. });
  70. afterEach(function () {
  71. if (origLocalStorage !== undefined) {
  72. Object.defineProperty(window, "localStorage", origLocalStorage);
  73. }
  74. });
  75. describe('writeSetting', function () {
  76. it('should save the setting value to local storage', function () {
  77. WebUtil.writeSetting('test', 'value');
  78. expect(window.localStorage.setItem).to.have.been.calledWithExactly('test', 'value');
  79. expect(WebUtil.readSetting('test')).to.equal('value');
  80. });
  81. });
  82. describe('setSetting', function () {
  83. it('should update the setting but not save to local storage', function () {
  84. WebUtil.setSetting('test', 'value');
  85. expect(window.localStorage.setItem).to.not.have.been.called;
  86. expect(WebUtil.readSetting('test')).to.equal('value');
  87. });
  88. });
  89. describe('readSetting', function () {
  90. it('should read the setting value from local storage', function () {
  91. localStorage.getItem.returns('value');
  92. expect(WebUtil.readSetting('test')).to.equal('value');
  93. });
  94. it('should return the default value when not in local storage', function () {
  95. expect(WebUtil.readSetting('test', 'default')).to.equal('default');
  96. });
  97. it('should return the cached value even if local storage changed', function () {
  98. localStorage.getItem.returns('value');
  99. expect(WebUtil.readSetting('test')).to.equal('value');
  100. localStorage.getItem.returns('something else');
  101. expect(WebUtil.readSetting('test')).to.equal('value');
  102. });
  103. it('should cache the value even if it is not initially in local storage', function () {
  104. expect(WebUtil.readSetting('test')).to.be.null;
  105. localStorage.getItem.returns('value');
  106. expect(WebUtil.readSetting('test')).to.be.null;
  107. });
  108. it('should return the default value always if the first read was not in local storage', function () {
  109. expect(WebUtil.readSetting('test', 'default')).to.equal('default');
  110. localStorage.getItem.returns('value');
  111. expect(WebUtil.readSetting('test', 'another default')).to.equal('another default');
  112. });
  113. it('should return the last local written value', function () {
  114. localStorage.getItem.returns('value');
  115. expect(WebUtil.readSetting('test')).to.equal('value');
  116. WebUtil.writeSetting('test', 'something else');
  117. expect(WebUtil.readSetting('test')).to.equal('something else');
  118. });
  119. });
  120. // this doesn't appear to be used anywhere
  121. describe('eraseSetting', function () {
  122. it('should remove the setting from local storage', function () {
  123. WebUtil.eraseSetting('test');
  124. expect(window.localStorage.removeItem).to.have.been.calledWithExactly('test');
  125. });
  126. });
  127. });
  128. describe('chrome.storage', function () {
  129. let chrome = window.chrome;
  130. let settings = {};
  131. before(function () {
  132. chrome = window.chrome;
  133. window.chrome = {
  134. storage: {
  135. sync: {
  136. get(cb) { cb(settings); },
  137. set() {},
  138. remove() {}
  139. }
  140. }
  141. };
  142. });
  143. after(function () {
  144. window.chrome = chrome;
  145. });
  146. const csSandbox = sinon.createSandbox();
  147. beforeEach(function () {
  148. settings = {};
  149. csSandbox.spy(window.chrome.storage.sync, 'set');
  150. csSandbox.spy(window.chrome.storage.sync, 'remove');
  151. return WebUtil.initSettings();
  152. });
  153. afterEach(function () {
  154. csSandbox.restore();
  155. });
  156. describe('writeSetting', function () {
  157. it('should save the setting value to chrome storage', function () {
  158. WebUtil.writeSetting('test', 'value');
  159. expect(window.chrome.storage.sync.set).to.have.been.calledWithExactly(sinon.match({ test: 'value' }));
  160. expect(WebUtil.readSetting('test')).to.equal('value');
  161. });
  162. });
  163. describe('setSetting', function () {
  164. it('should update the setting but not save to chrome storage', function () {
  165. WebUtil.setSetting('test', 'value');
  166. expect(window.chrome.storage.sync.set).to.not.have.been.called;
  167. expect(WebUtil.readSetting('test')).to.equal('value');
  168. });
  169. });
  170. describe('readSetting', function () {
  171. it('should read the setting value from chrome storage', function () {
  172. settings.test = 'value';
  173. expect(WebUtil.readSetting('test')).to.equal('value');
  174. });
  175. it('should return the default value when not in chrome storage', function () {
  176. expect(WebUtil.readSetting('test', 'default')).to.equal('default');
  177. });
  178. it('should return the last local written value', function () {
  179. settings.test = 'value';
  180. expect(WebUtil.readSetting('test')).to.equal('value');
  181. WebUtil.writeSetting('test', 'something else');
  182. expect(WebUtil.readSetting('test')).to.equal('something else');
  183. });
  184. });
  185. // this doesn't appear to be used anywhere
  186. describe('eraseSetting', function () {
  187. it('should remove the setting from chrome storage', function () {
  188. WebUtil.eraseSetting('test');
  189. expect(window.chrome.storage.sync.remove).to.have.been.calledWithExactly('test');
  190. });
  191. });
  192. });
  193. });
  194. });