UI for Zipcoin Blue

test.config.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* global before:true, beforeEach:true, describe:true, expect:true, it:true */
  2. describe('Config API', function() {
  3. 'use strict';
  4. var DRIVERS = [
  5. localforage.INDEXEDDB,
  6. localforage.LOCALSTORAGE,
  7. localforage.WEBSQL
  8. ];
  9. var supportedDrivers = [];
  10. before(function() {
  11. this.defaultConfig = localforage.config();
  12. supportedDrivers = [];
  13. for (var i = 0; i <= DRIVERS.length; i++) {
  14. if (localforage.supports(DRIVERS[i])) {
  15. supportedDrivers.push(DRIVERS[i]);
  16. }
  17. }
  18. });
  19. // Reset localForage before each test so we can call `config()` without
  20. // errors.
  21. beforeEach(function() {
  22. localforage._ready = null;
  23. localforage.config(this.defaultConfig);
  24. });
  25. it('returns the default values', function(done) {
  26. expect(localforage.config('description')).to.be('');
  27. expect(localforage.config('name')).to.be('localforage');
  28. expect(localforage.config('size')).to.be(4980736);
  29. expect(localforage.config('storeName')).to.be('keyvaluepairs');
  30. expect(localforage.config('version')).to.be(1.0);
  31. localforage.ready(function() {
  32. expect(localforage.config('driver')).to.be(localforage.driver());
  33. done();
  34. });
  35. });
  36. it('returns error if API call was already made', function(done) {
  37. localforage.length(function() {
  38. var configResult = localforage.config({
  39. description: '123',
  40. driver: 'I a not set driver',
  41. name: 'My Cool App',
  42. storeName: 'myStoreName',
  43. version: 2.0
  44. });
  45. var error = "Error: Can't call config() after localforage " +
  46. 'has been used.';
  47. expect(configResult).to.not.be(true);
  48. expect(configResult.toString()).to.be(error);
  49. // Expect the config values to be as they were before.
  50. expect(localforage.config('description')).to.not.be('123');
  51. expect(localforage.config('description')).to.be('');
  52. expect(localforage.config('driver')).to.be(localforage.driver());
  53. expect(localforage.config('driver')).to.not
  54. .be('I a not set driver');
  55. expect(localforage.config('name')).to.be('localforage');
  56. expect(localforage.config('name')).to.not.be('My Cool App');
  57. expect(localforage.config('size')).to.be(4980736);
  58. expect(localforage.config('storeName')).to.be('keyvaluepairs');
  59. expect(localforage.config('version')).to.be(1.0);
  60. done();
  61. });
  62. });
  63. it('sets new values and returns them properly', function(done) {
  64. var secondSupportedDriver = supportedDrivers.length >= 2 ? supportedDrivers[1] : null;
  65. localforage.config({
  66. description: 'The offline datastore for my cool app',
  67. driver: secondSupportedDriver,
  68. name: 'My Cool App',
  69. storeName: 'myStoreName',
  70. version: 2.0
  71. });
  72. expect(localforage.config('description')).to.not.be('');
  73. expect(localforage.config('description')).to
  74. .be('The offline datastore for my cool app');
  75. expect(localforage.config('driver')).to
  76. .be(secondSupportedDriver);
  77. expect(localforage.config('name')).to.be('My Cool App');
  78. expect(localforage.config('size')).to.be(4980736);
  79. expect(localforage.config('storeName')).to.be('myStoreName');
  80. expect(localforage.config('version')).to.be(2.0);
  81. localforage.ready(function() {
  82. if (supportedDrivers.length >= 2) {
  83. expect(localforage.config('driver')).to
  84. .be(secondSupportedDriver);
  85. } else {
  86. expect(localforage.config('driver')).to
  87. .be(supportedDrivers[0]);
  88. }
  89. done();
  90. });
  91. });
  92. if (supportedDrivers.length >= 2) {
  93. it('sets new driver using preference order', function(done) {
  94. var otherSupportedDrivers = supportedDrivers.slice(1);
  95. localforage.config({
  96. driver: otherSupportedDrivers
  97. });
  98. localforage.ready(function() {
  99. expect(localforage.config('driver')).to
  100. .be(otherSupportedDrivers[0]);
  101. done();
  102. });
  103. });
  104. }
  105. it('it does not set an unsupported driver', function(done) {
  106. var oldDriver = localforage.driver();
  107. localforage.config({
  108. driver: 'I am a not supported driver'
  109. });
  110. localforage.ready(function() {
  111. expect(localforage.config('driver')).to
  112. .be(oldDriver);
  113. done();
  114. });
  115. });
  116. it('it does not set an unsupported driver using preference order', function(done) {
  117. var oldDriver = localforage.driver();
  118. localforage.config({
  119. driver: [
  120. 'I am a not supported driver',
  121. 'I am a an other not supported driver'
  122. ]
  123. });
  124. localforage.ready(function() {
  125. expect(localforage.config('driver')).to
  126. .be(oldDriver);
  127. done();
  128. });
  129. });
  130. it('converts bad config values across drivers', function() {
  131. localforage.config({
  132. name: 'My Cool App',
  133. // https://github.com/mozilla/localForage/issues/247
  134. storeName: 'my store&name-v1',
  135. version: 2.0
  136. });
  137. expect(localforage.config('name')).to.be('My Cool App');
  138. expect(localforage.config('storeName')).to.be('my_store_name_v1');
  139. expect(localforage.config('version')).to.be(2.0);
  140. });
  141. it('uses the config values in ' + localforage.driver(), function(done) {
  142. localforage.config({
  143. description: 'The offline datastore for my cool app',
  144. driver: localforage.driver(),
  145. name: 'My Cool App',
  146. storeName: 'myStoreName',
  147. version: 2.0
  148. });
  149. localforage.setItem('some key', 'some value').then(function(value) {
  150. if (localforage.driver() === localforage.INDEXEDDB) {
  151. var indexedDB = (indexedDB || window.indexedDB ||
  152. window.webkitIndexedDB ||
  153. window.mozIndexedDB || window.OIndexedDB ||
  154. window.msIndexedDB);
  155. var req = indexedDB.open('My Cool App', 2.0);
  156. req.onsuccess = function() {
  157. var dbValue = req.result
  158. .transaction('myStoreName', 'readonly')
  159. .objectStore('myStoreName')
  160. .get('some key');
  161. expect(dbValue).to.be(value);
  162. done();
  163. };
  164. } else if (localforage.driver() === localforage.WEBSQL) {
  165. window.openDatabase('My Cool App', String(2.0), '',
  166. 4980736).transaction(function(t) {
  167. t.executeSql('SELECT * FROM myStoreName WHERE key = ? ' +
  168. 'LIMIT 1', ['some key'],
  169. function(t, results) {
  170. var dbValue = JSON.parse(results.rows.item(0).value);
  171. expect(dbValue).to.be(value);
  172. done();
  173. });
  174. });
  175. } else if (localforage.driver() === localforage.LOCALSTORAGE) {
  176. var dbValue = JSON.parse(
  177. localStorage['My Cool App/myStoreName/some key']);
  178. expect(dbValue).to.be(value);
  179. done();
  180. }
  181. });
  182. });
  183. it("returns all values when config isn't passed arguments", function() {
  184. expect(localforage.config()).to.be.an('object');
  185. expect(Object.keys(localforage.config()).length).to.be(6);
  186. });
  187. // This may go away when https://github.com/mozilla/localForage/issues/168
  188. // is fixed.
  189. it('maintains config values across setDriver calls', function(done) {
  190. localforage.config({
  191. name: 'Mega Mozilla Dino'
  192. });
  193. localforage.length().then(function() {
  194. return localforage.setDriver(localforage.LOCALSTORAGE);
  195. }).then(function() {
  196. expect(localforage.config('name')).to.be('Mega Mozilla Dino');
  197. done();
  198. });
  199. });
  200. });