UNPKG

7.2 kBJavaScriptView Raw
1/* global beforeEach:true, describe:true, expect:true, it:true */
2describe('Driver API', function() {
3 'use strict';
4
5 beforeEach(function(done) {
6 if (localforage.supports(localforage.INDEXEDDB)) {
7 localforage.setDriver(localforage.INDEXEDDB, function() {
8 done();
9 });
10 } else if (localforage.supports(localforage.WEBSQL)) {
11 localforage.setDriver(localforage.WEBSQL, function() {
12 done();
13 });
14 } else {
15 done();
16 }
17 });
18
19 if ((localforage.supports(localforage.INDEXEDDB) &&
20 localforage.driver() === localforage.INDEXEDDB) ||
21 (localforage.supports(localforage.WEBSQL) &&
22 localforage.driver() === localforage.WEBSQL)) {
23 it('can change to localStorage from ' + localforage.driver() +
24 ' [callback]', function(done) {
25 var previousDriver = localforage.driver();
26
27 localforage.setDriver(localforage.LOCALSTORAGE, function() {
28 expect(localforage.driver()).to.be(localforage.LOCALSTORAGE);
29 expect(localforage.driver()).to.not.be(previousDriver);
30 done();
31 });
32 });
33 it('can change to localStorage from ' + localforage.driver() +
34 ' [promise]', function(done) {
35 var previousDriver = localforage.driver();
36
37 localforage.setDriver(localforage.LOCALSTORAGE).then(function() {
38 expect(localforage.driver()).to.be(localforage.LOCALSTORAGE);
39 expect(localforage.driver()).to.not.be(previousDriver);
40 done();
41 });
42 });
43 }
44
45 if (!localforage.supports(localforage.INDEXEDDB)) {
46 it("can't use unsupported IndexedDB [callback]", function(done) {
47 var previousDriver = localforage.driver();
48 expect(previousDriver).to.not.be(localforage.INDEXEDDB);
49
50 // These should be rejected in component builds but aren't.
51 // TODO: Look into why.
52 localforage.setDriver(localforage.INDEXEDDB, null, function() {
53 expect(localforage.driver()).to.be(previousDriver);
54 done();
55 });
56 });
57 it("can't use unsupported IndexedDB [promise]", function(done) {
58 var previousDriver = localforage.driver();
59 expect(previousDriver).to.not.be(localforage.INDEXEDDB);
60
61 // These should be rejected in component builds but aren't.
62 // TODO: Look into why.
63 localforage.setDriver(localforage.INDEXEDDB)
64 .then(null, function() {
65 expect(localforage.driver()).to.be(previousDriver);
66 done();
67 });
68 });
69 } else {
70 it('can set already active IndexedDB [callback]', function(done) {
71 var previousDriver = localforage.driver();
72 expect(previousDriver).to.be(localforage.INDEXEDDB);
73
74 localforage.setDriver(localforage.INDEXEDDB, function() {
75 expect(localforage.driver()).to.be(previousDriver);
76 done();
77 });
78 });
79 it('can set already active IndexedDB [promise]', function(done) {
80 var previousDriver = localforage.driver();
81 expect(previousDriver).to.be(localforage.INDEXEDDB);
82
83 localforage.setDriver(localforage.INDEXEDDB).then(function() {
84 expect(localforage.driver()).to.be(previousDriver);
85 done();
86 });
87 });
88 }
89
90 if (!localforage.supports(localforage.LOCALSTORAGE)) {
91 it("can't use unsupported localStorage [callback]", function(done) {
92 var previousDriver = localforage.driver();
93 expect(previousDriver).to.not.be(localforage.LOCALSTORAGE);
94
95 localforage.setDriver(localforage.LOCALSTORAGE, null, function() {
96 expect(localforage.driver()).to.be(previousDriver);
97 done();
98 });
99 });
100 it("can't use unsupported localStorage [promise]", function(done) {
101 var previousDriver = localforage.driver();
102 expect(previousDriver).to.not.be(localforage.LOCALSTORAGE);
103
104 localforage.setDriver(localforage.LOCALSTORAGE)
105 .then(null, function() {
106 expect(localforage.driver()).to.be(previousDriver);
107 done();
108 });
109 });
110 } else if (!localforage.supports(localforage.INDEXEDDB) &&
111 !localforage.supports(localforage.WEBSQL)) {
112 it('can set already active localStorage [callback]', function(done) {
113 var previousDriver = localforage.driver();
114 expect(previousDriver).to.be(localforage.LOCALSTORAGE);
115
116 localforage.setDriver(localforage.LOCALSTORAGE, function() {
117 expect(localforage.driver()).to.be(previousDriver);
118 done();
119 });
120 });
121 it('can set already active localStorage [promise]', function(done) {
122 var previousDriver = localforage.driver();
123 expect(previousDriver).to.be(localforage.LOCALSTORAGE);
124
125 localforage.setDriver(localforage.LOCALSTORAGE).then(function() {
126 expect(localforage.driver()).to.be(previousDriver);
127 done();
128 });
129 });
130 }
131
132 if (!localforage.supports(localforage.WEBSQL)) {
133 it("can't use unsupported WebSQL [callback]", function(done) {
134 var previousDriver = localforage.driver();
135 expect(previousDriver).to.not.be(localforage.WEBSQL);
136
137 localforage.setDriver(localforage.WEBSQL, null, function() {
138 expect(localforage.driver()).to.be(previousDriver);
139 done();
140 });
141 });
142 it("can't use unsupported WebSQL [promise]", function(done) {
143 var previousDriver = localforage.driver();
144 expect(previousDriver).to.not.be(localforage.WEBSQL);
145
146 localforage.setDriver(localforage.WEBSQL)
147 .then(null, function() {
148 expect(localforage.driver()).to.be(previousDriver);
149 done();
150 });
151 });
152 } else {
153 it('can set already active WebSQL [callback]', function(done) {
154 localforage.setDriver(localforage.WEBSQL, function() {
155 var previousDriver = localforage.driver();
156 expect(previousDriver).to.be(localforage.WEBSQL);
157
158 localforage.setDriver(localforage.WEBSQL, function() {
159 expect(localforage.driver()).to.be(previousDriver);
160 done();
161 });
162 });
163 });
164 it('can set already active WebSQL [promise]', function(done) {
165 localforage.setDriver(localforage.WEBSQL).then(function() {
166 var previousDriver = localforage.driver();
167 expect(previousDriver).to.be(localforage.WEBSQL);
168
169 localforage.setDriver(localforage.WEBSQL).then(function() {
170 expect(localforage.driver()).to.be(previousDriver);
171 done();
172 });
173 });
174 });
175 }
176});