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