1 |
|
2 | describe('When Custom Drivers are used', function() {
|
3 | 'use strict';
|
4 | var errorMessage = 'Custom driver not compliant; see ' +
|
5 | 'https://mozilla.github.io/localForage/#definedriver';
|
6 |
|
7 | it('fails to define a no-name custom driver', function(done) {
|
8 | localforage.defineDriver({
|
9 | _initStorage: function() {},
|
10 | iterate: function() {},
|
11 | getItem: function() {},
|
12 | setItem: function() {},
|
13 | removeItem: function() {},
|
14 | clear: function() {},
|
15 | length: function() {},
|
16 | key: function() {},
|
17 | keys: function() {}
|
18 | }, null, function(err) {
|
19 | expect(err).to.be.an(Error);
|
20 | expect(err.message).to.be(errorMessage);
|
21 | done();
|
22 | });
|
23 | });
|
24 |
|
25 | it('fails to define a no-name custom driver [promise]', function(done) {
|
26 | localforage.defineDriver({
|
27 | _initStorage: function() {},
|
28 | iterate: function() {},
|
29 | getItem: function() {},
|
30 | setItem: function() {},
|
31 | removeItem: function() {},
|
32 | clear: function() {},
|
33 | length: function() {},
|
34 | key: function() {},
|
35 | keys: function() {}
|
36 | }).then(null, function(err) {
|
37 | expect(err).to.be.an(Error);
|
38 | expect(err.message).to.be(errorMessage);
|
39 | done();
|
40 | });
|
41 | });
|
42 |
|
43 | it('fails to define a custom driver with missing methods', function(done) {
|
44 | localforage.defineDriver({
|
45 | _driver: 'missingMethodsDriver',
|
46 | _initStorage: function() {},
|
47 | iterate: function() {},
|
48 | getItem: function() {},
|
49 | setItem: function() {},
|
50 | removeItem: function() {},
|
51 | clear: function() {}
|
52 | }, null, function(err) {
|
53 | expect(err).to.be.an(Error);
|
54 | expect(err.message).to.be(errorMessage);
|
55 | done();
|
56 | });
|
57 | });
|
58 |
|
59 | it('fails to define a custom driver with missing methods [promise]', function(done) {
|
60 | localforage.defineDriver({
|
61 | _driver: 'missingMethodsDriver',
|
62 | _initStorage: function() {},
|
63 | iterate: function() {},
|
64 | getItem: function() {},
|
65 | setItem: function() {},
|
66 | removeItem: function() {},
|
67 | clear: function() {}
|
68 | }).then(null, function(err) {
|
69 | expect(err).to.be.an(Error);
|
70 | expect(err.message).to.be(errorMessage);
|
71 | done();
|
72 | });
|
73 | });
|
74 |
|
75 | it('defines a compliant custom driver', function(done) {
|
76 | localforage.defineDriver(dummyStorageDriver, function() {
|
77 | done();
|
78 | });
|
79 | });
|
80 |
|
81 | it('defines a compliant custom driver [promise]', function(done) {
|
82 | localforage.defineDriver(dummyStorageDriver).then(function() {
|
83 | done();
|
84 | });
|
85 | });
|
86 |
|
87 | it('sets a custom driver', function(done) {
|
88 | localforage.defineDriver(dummyStorageDriver, function() {
|
89 | localforage.setDriver(dummyStorageDriver._driver, function() {
|
90 | expect(localforage.driver()).to.be(dummyStorageDriver._driver);
|
91 | done();
|
92 | });
|
93 | });
|
94 | });
|
95 |
|
96 | it('sets a custom driver [promise]', function(done) {
|
97 | localforage.defineDriver(dummyStorageDriver).then(function() {
|
98 | return localforage.setDriver(dummyStorageDriver._driver);
|
99 | }).then(function() {
|
100 | expect(localforage.driver()).to.be(dummyStorageDriver._driver);
|
101 | done();
|
102 | });
|
103 | });
|
104 |
|
105 | it('defines a driver synchronously when it doesn\'t have _supports()', function(done) {
|
106 | var customDriver = {
|
107 | _driver: 'dummyStorageDriver' + (+new Date()),
|
108 | _initStorage: function() { },
|
109 |
|
110 | iterate: function() { },
|
111 | getItem: function() { },
|
112 | setItem: function() { },
|
113 | removeItem: function() { },
|
114 | clear: function() { },
|
115 | length: function() { },
|
116 | key: function() { },
|
117 | keys: function() { }
|
118 | };
|
119 |
|
120 | localforage.defineDriver(customDriver);
|
121 | localforage.setDriver(customDriver._driver).then(function() {
|
122 | expect(localforage.driver()).to.be(customDriver._driver);
|
123 | done();
|
124 | });
|
125 | });
|
126 |
|
127 | it('defines a driver synchronously when it has boolean _supports()', function(done) {
|
128 | var customDriver = {
|
129 | _driver: 'dummyStorageDriver' + (+new Date()),
|
130 | _initStorage: function() { },
|
131 | _support: true,
|
132 | iterate: function() { },
|
133 | getItem: function() { },
|
134 | setItem: function() { },
|
135 | removeItem: function() { },
|
136 | clear: function() { },
|
137 | length: function() { },
|
138 | key: function() { },
|
139 | keys: function() { }
|
140 | };
|
141 |
|
142 | localforage.defineDriver(customDriver);
|
143 | localforage.setDriver(customDriver._driver).then(function() {
|
144 | expect(localforage.driver()).to.be(customDriver._driver);
|
145 | done();
|
146 | });
|
147 | });
|
148 |
|
149 | it('defines a driver asynchronously when _supports() returns a Promise<boolean>', function(done) {
|
150 | var customDriver = {
|
151 | _driver: 'dummyStorageDriver' + (+new Date()),
|
152 | _initStorage: function() { },
|
153 | _support: function() {
|
154 | return Promise.resolve(true);
|
155 | },
|
156 | iterate: function() { },
|
157 | getItem: function() { },
|
158 | setItem: function() { },
|
159 | removeItem: function() { },
|
160 | clear: function() { },
|
161 | length: function() { },
|
162 | key: function() { },
|
163 | keys: function() { }
|
164 | };
|
165 |
|
166 | localforage.defineDriver(customDriver).then(function() {
|
167 | return localforage.setDriver(customDriver._driver);
|
168 | }).then(function() {
|
169 | expect(localforage.driver()).to.be(customDriver._driver);
|
170 | done();
|
171 | });
|
172 | });
|
173 |
|
174 | it('sets and uses a custom driver', function(done) {
|
175 | localforage.defineDriver(dummyStorageDriver, function() {
|
176 | localforage.setDriver(dummyStorageDriver._driver, function(err) {
|
177 | expect(err).to.be(undefined);
|
178 | localforage.setItem('testCallbackKey', 'testCallbackValue', function(err) {
|
179 | expect(err).to.be(null);
|
180 | localforage.getItem('testCallbackKey', function(err, value) {
|
181 | expect(err).to.be(null);
|
182 | expect(value).to.be('testCallbackValue');
|
183 | done();
|
184 | });
|
185 | });
|
186 | });
|
187 | });
|
188 | });
|
189 |
|
190 | it('sets and uses a custom driver [promise]', function(done) {
|
191 | localforage.defineDriver(dummyStorageDriver).then(function() {
|
192 | return localforage.setDriver(dummyStorageDriver._driver);
|
193 | }).then(function() {
|
194 | return localforage.setItem('testPromiseKey', 'testPromiseValue');
|
195 | }).then(function() {
|
196 | return localforage.getItem('testPromiseKey');
|
197 | }).then(function(value) {
|
198 | expect(value).to.be('testPromiseValue');
|
199 | done();
|
200 | });
|
201 | });
|
202 |
|
203 | describe('when dropInstance is not defined', function() {
|
204 | it('rejects when it is used', function(done) {
|
205 | var customDriver = {
|
206 | _driver: 'dummyStorageDriver' + (+new Date()),
|
207 | _initStorage: function() { },
|
208 | _support: function() {
|
209 | return Promise.resolve(true);
|
210 | },
|
211 | iterate: function() { },
|
212 | getItem: function() { },
|
213 | setItem: function() { },
|
214 | removeItem: function() { },
|
215 | clear: function() { },
|
216 | length: function() { },
|
217 | key: function() { },
|
218 | keys: function() { }
|
219 | };
|
220 |
|
221 | localforage.defineDriver(customDriver).then(function() {
|
222 | return localforage.setDriver(customDriver._driver);
|
223 | }).then(function() {
|
224 | return localforage.dropInstance();
|
225 | }).catch(function(err) {
|
226 | expect(err.message).to.be('Method dropInstance is not implemented by the current driver');
|
227 | done();
|
228 | });
|
229 | });
|
230 | });
|
231 |
|
232 | describe('when dropInstance is defined', function() {
|
233 | it('is does not reject', function(done) {
|
234 | var customDriver = {
|
235 | _driver: 'dummyStorageDriver' + (+new Date()),
|
236 | _initStorage: function() { },
|
237 | _support: function() {
|
238 | return Promise.resolve(true);
|
239 | },
|
240 | iterate: function() { },
|
241 | getItem: function() { },
|
242 | setItem: function() { },
|
243 | removeItem: function() { },
|
244 | clear: function() { },
|
245 | length: function() { },
|
246 | key: function() { },
|
247 | keys: function() { },
|
248 | dropInstance: function() { }
|
249 | };
|
250 |
|
251 | localforage.defineDriver(customDriver).then(function() {
|
252 | return localforage.setDriver(customDriver._driver);
|
253 | }).then(function() {
|
254 | return localforage.dropInstance();
|
255 | }).then(function() {
|
256 | done();
|
257 | });
|
258 | });
|
259 | });
|
260 |
|
261 | });
|