Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f1c1228

Browse files
committedMay 18, 2014
Lots of documentation improvements.
Added DSLocalStorageAdapter.
1 parent 87a9652 commit f1c1228

File tree

18 files changed

+715
-73
lines changed

18 files changed

+715
-73
lines changed
 

‎dist/angular-data.js

Lines changed: 216 additions & 35 deletions
Large diffs are not rendered by default.

‎dist/angular-data.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎guide/angular-data-mocks/overview.doc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
@name Overview
44
@description
55

6-
Angular-data is a fake angular-data implementation suitable for unit testing angular applications that use the `angular-data.DS` module.
6+
Angular-data-mocks is a fake angular-data implementation suitable for unit testing angular applications that use the `angular-data.DS` module.
77

8-
__Version:__ 0.3.0
8+
__Version:__ 0.3.2
99

1010
__angular-data-mocks requires [sinon](http://sinonjs.org/) to be loaded in order to work.__
1111

‎guide/angular-data-mocks/setup.doc

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,37 @@
33
@name Setting Up
44
@description
55

6-
TODO: Explain how to set up angular tests to use angular-data-mocks.
6+
Angular-data-mocks works just like [angular-mocks](https://docs.angularjs.org/api/ngMock). Angular-data-mocks simply needs
7+
to be loaded after angular, angular-mocks, and angular-data in your tests. After you declare your test module, and before
8+
you create the injector, activate angular-data-mocks by calling `module('angular-data.mocks')`.
9+
10+
## Example (Mocha)
11+
`karma.start.js`
12+
13+
```js
14+
var DS, DSHttpAdapter;
15+
16+
angular.module('myApp', ['angular-data.DS']);
17+
18+
beforeEach(function () {
19+
angular.mocks.module('myApp');
20+
});
21+
22+
beforeEach(function () {
23+
angular.mocks.module('angular-data.mocks');
24+
});
25+
26+
beforeEach(function (done) {
27+
inject(function (_DS_, _DSHttpAdapter_) {
28+
DS = _DS_;
29+
DSHttpAdapter = _DSHttpAdapter_;
30+
31+
done();
32+
});
33+
});
34+
```
35+
36+
If you're using [KarmaJS](http://karma-runner.github.io/0.12/index.html) to test your app, look through the tests in the
37+
[angular-data-mocks repo](https://github.com/jmdobry/angular-data-mocks) for an example Karma setup.
738

839
Refer to the [angular-data-mocks API](/documentation/api/angular-data-mocks/angular-data-mocks) for more detailed information.

‎guide/angular-data-mocks/testing.doc

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,61 @@
33
@name Testing
44
@description
55

6-
TODO: Explain how to use angular-data-mocks in angular tests.
6+
Once you're setup to test using angular-data-mocks you can use it much like `$httpBackend`. For the asynchronous methods
7+
you declare expectations and then flush pending requests.
8+
9+
## Example:
10+
`app.js`:
11+
12+
```js
13+
angular.module('testApp', ['angular-data.DS'])
14+
.controller('MyCtrl', function ($scope, DS) {
15+
'use strict';
16+
DS.find('post', 45);
17+
18+
$scope.update = function (attrs) {
19+
return DS.update('post', 45, attrs);
20+
}
21+
});
22+
```
23+
24+
`test.js`:
25+
26+
```js
27+
describe('test', function () {
28+
var MyCtrl, $scope;
29+
30+
beforeEach(function (done) {
31+
$scope = $rootScope.$new();
32+
33+
DS.expectFind('post', 45).respond({
34+
author: 'John Anderson',
35+
id: 5
36+
});
37+
38+
inject(function ($controller) {
39+
MyCtrl = $controller('MyCtrl', {
40+
$scope: $scope,
41+
DS: DS
42+
});
43+
44+
DS.flush();
45+
46+
done();
47+
});
48+
});
49+
50+
it('should update the post', function () {
51+
DS.expectUpdate('post', 45, { author: 'Sally' })
52+
.respond({ id: 5, author: 'Sally' });
53+
54+
$scope.update().then(function (post) {
55+
assert.deepEqual(post, { id: 5, author: 'Sally' });
56+
});
57+
58+
DS.flush();
59+
});
60+
});
61+
```
762

863
Refer to the [angular-data-mocks API](/documentation/api/angular-data-mocks/angular-data-mocks) for more detailed information.

‎guide/angular-data/adapters/adapters.doc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,37 @@
1111
@id overview
1212
@name Overview of adapters
1313
@description
14+
15+
Angular-data ships with a `DSHttpAdapter` and a `DSLocalStorageAdapter`. This is default adapter
16+
used for `DS.findAll`, `DS.update`, etc.
17+
18+
Register a custom adapter:
19+
```js
20+
DS.adapters.myCustomAdapter = { ... };
21+
```
22+
23+
Other available adapters:
24+
25+
- [DSLocalStorageAdapter](https://github.com/jmdobry/angular-data/blob/master/src/adapters/localStorage.js)
26+
- [DSLocalForageAdapter](https://github.com/jmdobry/angular-data-localForage)
27+
28+
The default adapter can be set globally:
29+
30+
```js
31+
DSProvider.defaults.defaultAdapter = 'DSHttpAdapter';
32+
```
33+
34+
per resource:
35+
36+
```js
37+
DS.defineResource({
38+
name: 'user',
39+
defaultAdapter: 'DSLocalForageAdapter'
40+
});
41+
``
42+
43+
per method
44+
45+
```js
46+
DS.update('post', 45, { author: 'Sally' }, { adapter: 'DSLocalForageAdapter' });
47+
```

‎guide/angular-data/asynchronous.doc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ The asynchronous methods return Promises produced by Angular's `$q` service.
1111
Example:
1212

1313
```js
14+
// synchronous
1415
DS.get('document', 45); // undefined
1516

17+
// asynchronous
1618
DS.find('document', 45).then(function (document) {
1719
document; // { title: 'How to Cook', id: 45 }
1820

@@ -40,4 +42,4 @@ DS.save('document', 45).then(function (document) {
4042
DS.get('document', 45); // { title: 'How to Cook', id: 45 }
4143
```
4244

43-
See the [API](/documentation/api/api/index) for more information.
45+
See the [DS API](/documentation/api/angular-data/DS) for more information.

‎guide/angular-data/how.doc

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
11
@doc overview
22
@id how
3-
@name How do I...?
3+
@name How do I?
44
@description
55

66
#### How do I serialize data before it's saved?
7+
Before the data store sends date to an adapter, you may need to transform it to a custom request object of yours.
8+
9+
Define a global serialization method:
710
```js
8-
DSHttpAdapter.defaults.serialize = function (data) {
11+
DSProvider.defaults.serialize = function (resourceName, data) {
912
// custom payload format
1013
return {
1114
payload: data
1215
};
1316
};
1417
```
1518

19+
Define a serialization method for a specific resource:
20+
```js
21+
DS.defineResource({
22+
name: 'user',
23+
serialize: function (resourceName, user) {
24+
return {
25+
payload: user
26+
};
27+
}
28+
});
29+
```
30+
1631
#### How do I deserialize data?
32+
When an adapter returns data to the data store from the server, for example, you may need to extract data from a custom response object of yours.
33+
34+
Define a global deserialization method:
1735
```js
18-
DSHttpAdapter.defaults.deserialize = function (data) {
36+
DSProvider.defaults.deserialize = function (resourceName, data) {
1937
// extract data from custom payload format
2038
return data ? data.payload : data;
2139
};
2240
```
41+
42+
Define a deserialization method for a specific resource:
43+
```js
44+
DS.defineResource({
45+
name: 'user',
46+
deserialize: function (resourceName, data) {
47+
return data.data.embedded;
48+
}
49+
});
50+
```

‎guide/angular-data/overview.doc

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
@description
55

66
Angular-data is an in-browser data store for [Angular.js](http://angularjs.org).
7-
[Synchronous methods](/documentation/guide/synchronous/index) work only with data already in the data store, and
8-
[asynchronous methods](/documentation/guide/asynchronous/index) work with a persistence layer.
7+
[Synchronous methods](/documentation/guide/angular-data/synchronous) work only with data already in the data store, and
8+
[Asynchronous methods](/documentation/guide/angular-data/asynchronous) work with a persistence layer.
99

1010
<p>
1111
<img src="/resources/img/chart.png" width="940">
@@ -15,7 +15,7 @@ Angular-data is an in-browser data store for [Angular.js](http://angularjs.org).
1515
```js
1616
angular.module('myApp', ['angular-data.DS'])
1717
.config(function (DSProvider) {
18-
DSProvider.defaults.baseUrl = 'https://example.com/api';
18+
DSProvider.defaults.baseUrl = '/api';
1919
})
2020
.run(function (DS) {
2121
DS.defineResource({
@@ -25,16 +25,21 @@ angular.module('myApp', ['angular-data.DS'])
2525
});
2626
})
2727
.controller('PostCtrl', function ($scope, DS) {
28-
var query = {
29-
query: {
30-
where: {
31-
author: 'John Anderson'
32-
}
33-
}
34-
};
28+
var params = {
29+
query: {
30+
where: {
31+
author: {
32+
'==': 'John Anderson'
33+
}
34+
}
35+
}
36+
};
3537

36-
DS.findAll('post', query);
38+
DS.findAll('post', params);
3739

40+
DS.bindAll($scope', 'posts', 'post', params);
41+
42+
// Verbose way of doing the bindAll() above, but gives more control
3843
$scope.$watch(function () {
3944
return DS.lastModified('post');
4045
}, function () {
@@ -54,7 +59,7 @@ $scope.posts; // [{ id: 1, author: 'John Anderson', title: 'How to cook' },
5459

5560
You define _resources_ and register them with the data store. A _resource definition_ tells angular-data
5661
about a particular resource, like what its root endpoint is and which attribute refers to the primary key of the
57-
resource. A _resource definition_ can also specify validation functions to be executed during model lifecycle operations.
62+
resource. A _resource definition_ can also specify functions to be executed during model lifecycle operations.
5863

5964
```js
6065
DS.defineResource({
@@ -71,7 +76,7 @@ DS.defineResource({
7176
});
7277
```
7378

74-
`validate` will be executed at the beginning of the lifecycle initiated by a call to `DS.create` or `DS.save`.
79+
`validate` will be executed at the beginning of the lifecycle initiated by a calls to `DS.create`, `DS.save`, etc.
7580
```js
7681
DS.create('post', { author: 'Sally', title: 'Angular gotchas' })
7782
.then(function (post) {

‎guide/angular-data/synchronous.doc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $scope.$watch(function () {
2121
// Here we are watching the "lastModified" timestamp maintained by the data store for this particular document
2222

2323
return DS.lastModified('document', 45);
24-
}, function (lastModifiedTimestamp) {
24+
}, function () {
2525
// When this callback is executed, it means that the data store thinks the item changed
2626

2727
// Retrieve the updated item from the data store's cache
@@ -35,7 +35,7 @@ To make things simpler, angular-data has some bind methods to help with this:
3535
DS.bindOne($scope, 'myDoc', 'document', 45');
3636
```
3737

38-
The above example shows how to bind an item in the data store to the stop. Whenever that item changes it will be updated
38+
The above example shows how to bind an item in the data store to the $scope. Whenever that item changes it will be updated
3939
on the $scope.
4040

4141
When the app starts up, the calls to `lastModified()` and `get()` will both returned undefined, because the item isn't in
@@ -66,6 +66,6 @@ DS.eject('document', 45); // synchronously eject document from the store
6666
```
6767

6868
User #2 doesn't need to destroy document 45, because it's already been deleted on the server by user #1. User #2 just
69-
needs to kick document 45 out of the data store and be done with it.
69+
needs to kick document #45 out of the data store and be done with it.
7070

71-
See the [API](/documentation/api/api/index) for more information.
71+
See the [DS API](/documentation/api/angular-data/DS) for more information.

‎karma.start.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Setup global test variables
2-
var $rootScope, $q, $log, DSProvider, DSHttpAdapterProvider, DS, DSHttpAdapter, app, $httpBackend, p1, p2, p3, p4, p5;
2+
var $rootScope, $q, $log, DSHttpAdapterProvider, DSProvider, DSLocalStorageAdapter, DS, DSUtils, DSHttpAdapter, app, $httpBackend, p1, p2, p3, p4, p5;
33

44
var lifecycle = {};
55

@@ -86,8 +86,6 @@ beforeEach(function (done) {
8686
return query;
8787
};
8888
module('app', function (_DSProvider_, _DSHttpAdapterProvider_) {
89-
DSHttpAdapterProvider = _DSHttpAdapterProvider_;
90-
DSHttpAdapterProvider.defaults.queryTransform = lifecycle.queryTransform;
9189
DSProvider = _DSProvider_;
9290
DSProvider.defaults.baseUrl = 'http://test.angular-cache.com';
9391
DSProvider.defaults.beforeValidate = lifecycle.beforeValidate;
@@ -103,13 +101,19 @@ beforeEach(function (done) {
103101
DSProvider.defaults.afterInject = lifecycle.afterInject;
104102
DSProvider.defaults.serialize = lifecycle.serialize;
105103
DSProvider.defaults.deserialize = lifecycle.deserialize;
104+
DSHttpAdapterProvider = _DSHttpAdapterProvider_;
105+
DSHttpAdapterProvider.defaults.queryTransform = lifecycle.queryTransform;
106+
106107
});
107-
inject(function (_$rootScope_, _$q_, _$httpBackend_, _DS_, _$log_, _DSHttpAdapter_) {
108+
inject(function (_$rootScope_, _$q_, _$httpBackend_, _DS_, _$log_, _DSUtils_, _DSHttpAdapter_, _DSLocalStorageAdapter_) {
108109
// Setup global mocks
110+
localStorage.clear();
109111
$q = _$q_;
110112
$rootScope = _$rootScope_;
111113
DS = _DS_;
114+
DSUtils = _DSUtils_;
112115
DSHttpAdapter = _DSHttpAdapter_;
116+
DSLocalStorageAdapter = _DSLocalStorageAdapter_;
113117
$httpBackend = _$httpBackend_;
114118
DS.defineResource({
115119
name: 'post',

‎src/adapters/http.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,6 @@ function DSHttpAdapterProvider() {
229229
*/
230230
create: create,
231231

232-
createMany: function () {
233-
throw new Error('Not yet implemented!');
234-
},
235-
236232
/**
237233
* @doc method
238234
* @id DSHttpAdapter.methods:update

‎src/adapters/localStorage.js

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/**
2+
* @doc function
3+
* @id DSLocalStorageProvider
4+
* @name DSLocalStorageProvider
5+
*/
6+
function DSLocalStorageProvider() {
7+
8+
this.$get = ['$q', 'DSUtils', function ($q, DSUtils) {
9+
10+
/**
11+
* @doc interface
12+
* @id DSLocalStorage
13+
* @name DSLocalStorage
14+
* @description
15+
* Default adapter used by angular-data. This adapter uses AJAX and JSON to send/retrieve data to/from a server.
16+
* Developers may provide custom adapters that implement the adapter interface.
17+
*/
18+
return {
19+
/**
20+
* @doc method
21+
* @id DSLocalStorage.methods:find
22+
* @name find
23+
* @description
24+
* Retrieve a single entity from localStorage.
25+
*
26+
* Calls `localStorage.getItem(key)`.
27+
*
28+
* @param {object} resourceConfig Properties:
29+
* - `{string}` - `baseUrl` - Base url.
30+
* - `{string=}` - `namespace` - Namespace path for the resource.
31+
* @param {string|number} id The primary key of the entity to retrieve.
32+
* @returns {Promise} Promise.
33+
*/
34+
find: find,
35+
36+
/**
37+
* @doc method
38+
* @id DSLocalStorage.methods:findAll
39+
* @name findAll
40+
* @description
41+
* Not supported.
42+
*/
43+
findAll: function () {
44+
throw new Error('Not supported!');
45+
},
46+
47+
/**
48+
* @doc method
49+
* @id DSLocalStorage.methods:findAll
50+
* @name find
51+
* @description
52+
* Not supported.
53+
*/
54+
create: function () {
55+
throw new Error('Not supported!');
56+
},
57+
58+
/**
59+
* @doc method
60+
* @id DSLocalStorage.methods:update
61+
* @name update
62+
* @description
63+
* Update an entity in localStorage.
64+
*
65+
* Calls `localStorage.setItem(key, value)`.
66+
*
67+
* @param {object} resourceConfig Properties:
68+
* - `{string}` - `baseUrl` - Base url.
69+
* - `{string=}` - `namespace` - Namespace path for the resource.
70+
* @param {string|number} id The primary key of the entity to update.
71+
* @param {object} attrs The attributes with which to update the entity.
72+
* @returns {Promise} Promise.
73+
*/
74+
update: update,
75+
76+
/**
77+
* @doc method
78+
* @id DSLocalStorage.methods:updateAll
79+
* @name updateAll
80+
* @description
81+
* Not supported.
82+
*/
83+
updateAll: function () {
84+
throw new Error('Not supported!');
85+
},
86+
87+
/**
88+
* @doc method
89+
* @id DSLocalStorage.methods:destroy
90+
* @name destroy
91+
* @description
92+
* Destroy an entity from localStorage.
93+
*
94+
* Calls `localStorage.removeItem(key)`.
95+
*
96+
* @param {object} resourceConfig Properties:
97+
* - `{string}` - `baseUrl` - Base url.
98+
* - `{string=}` - `endpoint` - Endpoint path for the resource.
99+
* @param {string|number} id The primary key of the entity to destroy.
100+
* @returns {Promise} Promise.
101+
*/
102+
destroy: destroy,
103+
104+
/**
105+
* @doc method
106+
* @id DSLocalStorage.methods:destroyAll
107+
* @name destroyAll
108+
* @description
109+
* Not supported.
110+
*/
111+
destroyAll: function () {
112+
throw new Error('Not supported!');
113+
}
114+
};
115+
116+
function GET(key) {
117+
var deferred = $q.defer();
118+
try {
119+
var item = localStorage.getItem(key);
120+
deferred.resolve(item ? angular.fromJson(item) : undefined);
121+
} catch (err) {
122+
deferred.reject(err);
123+
}
124+
return deferred.promise;
125+
}
126+
127+
function PUT(key, value) {
128+
var deferred = $q.defer();
129+
try {
130+
var item = localStorage.getItem(key);
131+
if (item) {
132+
item = angular.fromJson(item);
133+
DSUtils.deepMixIn(item, value);
134+
deferred.resolve(localStorage.setItem(key, angular.toJson(item)));
135+
} else {
136+
deferred.resolve(localStorage.setItem(key, angular.toJson(value)));
137+
}
138+
} catch (err) {
139+
deferred.reject(err);
140+
}
141+
return deferred.promise;
142+
}
143+
144+
function DEL(key) {
145+
var deferred = $q.defer();
146+
try {
147+
deferred.resolve(localStorage.removeItem(key));
148+
} catch (err) {
149+
deferred.reject(err);
150+
}
151+
return deferred.promise;
152+
}
153+
154+
function destroy(resourceConfig, id, options) {
155+
options = options || {};
156+
return DEL(
157+
DSUtils.makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.endpoint, id),
158+
options
159+
);
160+
}
161+
162+
function find(resourceConfig, id, options) {
163+
options = options || {};
164+
return GET(
165+
DSUtils.makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.endpoint, id),
166+
options
167+
);
168+
}
169+
170+
function update(resourceConfig, id, attrs, options) {
171+
options = options || {};
172+
return PUT(
173+
DSUtils.makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.endpoint, id),
174+
attrs,
175+
options
176+
);
177+
}
178+
}];
179+
}
180+
181+
module.exports = DSLocalStorageProvider;

‎src/datastore/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,8 @@ function DSProvider() {
461461
var defaults = this.defaults = new BaseConfig();
462462

463463
this.$get = [
464-
'$rootScope', '$log', '$q', 'DSHttpAdapter', 'DSUtils', 'DSErrors', 'DSCacheFactory',
465-
function ($rootScope, $log, $q, DSHttpAdapter, DSUtils, DSErrors, DSCacheFactory) {
464+
'$rootScope', '$log', '$q', 'DSHttpAdapter', 'DSLocalStorageAdapter', 'DSUtils', 'DSErrors', 'DSCacheFactory',
465+
function ($rootScope, $log, $q, DSHttpAdapter, DSLocalStorageAdapter, DSUtils, DSErrors, DSCacheFactory) {
466466

467467
var syncMethods = require('./sync_methods'),
468468
asyncMethods = require('./async_methods'),
@@ -528,7 +528,8 @@ function DSProvider() {
528528
* the name of the adapter and the value is the adapter itself.
529529
*/
530530
adapters: {
531-
DSHttpAdapter: DSHttpAdapter
531+
DSHttpAdapter: DSHttpAdapter,
532+
DSLocalStorageAdapter: DSLocalStorageAdapter
532533
},
533534

534535
/**

‎src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
.factory('DSUtils', require('./utils'))
4747
.factory('DSErrors', require('./errors'))
4848
.provider('DSHttpAdapter', require('./adapters/http'))
49+
.provider('DSLocalStorageAdapter', require('./adapters/localStorage'))
4950
.provider('DS', require('./datastore'))
5051
.config(['$provide', function ($provide) {
5152
$provide.decorator('$q', ['$delegate', function ($delegate) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
describe('DSLocalStorageAdapter.destroy(resourceConfig, id, options)', function () {
2+
it('should destroy an item from localStorage', function (done) {
3+
var path = DSUtils.makePath('api', 'posts', 1);
4+
5+
localStorage.setItem(path, angular.toJson(p1));
6+
7+
assert.deepEqual(angular.fromJson(localStorage.getItem(path)), p1, 'p1 should be in localStorage');
8+
9+
DSLocalStorageAdapter.destroy({
10+
baseUrl: 'api',
11+
endpoint: 'posts'
12+
}, 1).then(function () {
13+
assert.isNull(localStorage.getItem(path), 'the item should be gone from localStorage');
14+
15+
path = DSUtils.makePath('api2', 'posts', 1);
16+
localStorage.setItem(path, angular.toJson(p2));
17+
assert.deepEqual(angular.fromJson(localStorage.getItem(path)), p2);
18+
19+
DSLocalStorageAdapter.destroy({
20+
baseUrl: 'api',
21+
endpoint: 'posts'
22+
}, 1, { baseUrl: 'api2' }).then(function () {
23+
assert.isNull(localStorage.getItem(path), 'the item should be gone from localStorage');
24+
done();
25+
}, function (err) {
26+
console.error(err.stack);
27+
fail('should not have rejected');
28+
});
29+
30+
$rootScope.$apply();
31+
}, function (err) {
32+
console.error(err.stack);
33+
fail('should not have rejected');
34+
});
35+
36+
$rootScope.$apply();
37+
});
38+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
describe('DSLocalStorageAdapter.find(resourceConfig, id, options)', function () {
2+
it('should retrieve an item from localStorage', function (done) {
3+
var path = DSUtils.makePath('api', 'posts', 1);
4+
5+
localStorage.setItem(path, angular.toJson(p1));
6+
7+
assert.deepEqual(angular.fromJson(localStorage.getItem(path)), p1, 'p1 should be in localStorage');
8+
9+
DSLocalStorageAdapter.find({
10+
baseUrl: 'api',
11+
endpoint: 'posts'
12+
}, 1).then(function (data) {
13+
assert.deepEqual(data, p1, 'post should have been found');
14+
15+
path = DSUtils.makePath('api2', 'posts', 2);
16+
17+
localStorage.setItem(path, angular.toJson(p2));
18+
19+
assert.deepEqual(angular.fromJson(localStorage.getItem(path)), p2, 'p2 should be in localStorage');
20+
21+
DSLocalStorageAdapter.find({
22+
baseUrl: 'api',
23+
endpoint: 'posts'
24+
}, 2, { baseUrl: 'api2' }).then(function (data) {
25+
assert.deepEqual(data, p2, 'post should have been found');
26+
27+
done();
28+
}, function (err) {
29+
console.error(err.stack);
30+
fail('should not have rejected');
31+
});
32+
33+
$rootScope.$apply();
34+
}, function (err) {
35+
console.error(err.stack);
36+
fail('should not have rejected');
37+
});
38+
39+
$rootScope.$apply();
40+
});
41+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
describe('DSLocalStorageAdapter.update(resourceConfig, id, attrs, options)', function () {
2+
it('should make a PUT request', function (done) {
3+
var path = DSUtils.makePath('api', 'posts', 1);
4+
5+
localStorage.setItem(path, angular.toJson(p1));
6+
7+
assert.deepEqual(angular.fromJson(localStorage.getItem(path)), p1, 'p1 should be in localStorage');
8+
9+
DSLocalStorageAdapter.update({
10+
baseUrl: 'api',
11+
endpoint: 'posts'
12+
}, 1, { author: 'Sally' }).then(function (data) {
13+
assert.isUndefined(data, 'data should be undefined');
14+
assert.deepEqual(angular.fromJson(localStorage.getItem(path)), { author: 'Sally', age: 30, id: 5 }, 'p1 should be in localStorage');
15+
16+
path = DSUtils.makePath('api2', 'posts', 2);
17+
18+
localStorage.setItem(path, angular.toJson(p2));
19+
20+
assert.deepEqual(angular.fromJson(localStorage.getItem(path)), p2, 'p2 should be in localStorage');
21+
22+
DSLocalStorageAdapter.update({
23+
baseUrl: 'api',
24+
endpoint: 'posts'
25+
}, 2, { age: 44 }, { baseUrl: 'api2' }).then(function (data) {
26+
assert.isUndefined(data, 'data should be undefined');
27+
28+
assert.deepEqual(angular.fromJson(localStorage.getItem(path)), { author: 'Sally', age: 44, id: 6 }, 'p1 should be in localStorage');
29+
30+
done();
31+
}, function (err) {
32+
console.error(err.stack);
33+
fail('should not have rejected');
34+
});
35+
36+
$rootScope.$apply();
37+
}, function (err) {
38+
console.error(err.stack);
39+
fail('should not have rejected');
40+
});
41+
42+
$rootScope.$apply();
43+
});
44+
});

1 commit comments

Comments
 (1)

jmdobry commented on May 18, 2014

@jmdobry
MemberAuthor
Please sign in to comment.