Skip to content

Commit 493c135

Browse files
Made tests. missing two functions
1 parent 86ea167 commit 493c135

File tree

4 files changed

+164
-18
lines changed

4 files changed

+164
-18
lines changed

src/CachedResults.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,40 @@ export class CachedResults {
44
createdAt: Date;
55
results: any[];
66

7+
static toParseObjs(className: string, results: any[]): Parse.Object[] {
8+
return results.map((obj) => {
9+
const jsonReadyToBeConverted = {
10+
className,
11+
...obj,
12+
};
13+
14+
// return Parse objects
15+
return Parse.Object.fromJSON(jsonReadyToBeConverted, false);
16+
});
17+
}
18+
719
constructor(objs: Parse.Object[], createdAt?: Date) {
820
this.createdAt = createdAt || new Date();
921
this.results = objs.map(o => o.toJSON());
1022
}
1123

24+
toParseObjs(className: string) {
25+
return CachedResults.toParseObjs(className, this.results);
26+
}
27+
1228
toJSON(): any {
1329
return {
1430
createdAt: this.createdAt,
15-
resuts: this.results,
31+
results: this.results,
1632
};
1733
}
1834

19-
toParseObjs(className: string): Parse.Object[] {
20-
return this.results.map((obj) => {
21-
const jsonReadyToBeConverted = {
22-
className,
23-
...obj,
24-
};
25-
26-
// return Parse objects
27-
return Parse.Object.fromJSON(jsonReadyToBeConverted, false);
28-
});
29-
}
30-
3135
static fromJSON(
3236
json: { createdAt: Date, results: any[] },
3337
): CachedResults {
34-
return new CachedResults(json.results, json.createdAt);
38+
const { createdAt, results } = json;
39+
const c = new CachedResults([], createdAt);
40+
c.results = results;
41+
return c;
3542
}
3643
}

src/ParseOffline.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class ParseOffline {
3232
const results = await query.find(options);
3333

3434
// Save the items in the localStorage as JSON
35-
localStorage.setItem(
35+
window.localStorage.setItem(
3636
cacheKey,
3737
JSON.stringify(new CachedResults(results).toJSON()),
3838
);
@@ -51,13 +51,13 @@ export class ParseOffline {
5151
}): CachedResults {
5252
const { className, localStorageKey } = params;
5353

54-
const previousItems = localStorage.getItem(
54+
const previousItems = window.localStorage.getItem(
5555
localStorageKey || // from an arbitrary key
5656
ParseOffline.getLocalStorageKeyForClassName(className), // for an autogenerated key
5757
);
5858

5959
// return if there were no previous items
60-
if (!previousItems) return;
60+
if (!previousItems) return new CachedResults([]);
6161

6262
// parse the previous results
6363
const parsedJSON: any = JSON.parse(previousItems);
@@ -104,7 +104,7 @@ export class ParseOffline {
104104
const { query, localStorageKey, options, maxAge } = params;
105105

106106
// Get the previous items
107-
const previousItems = localStorage.getItem(
107+
const previousItems = window.localStorage.getItem(
108108
localStorageKey ||
109109
ParseOffline.getLocalStorageKeyForClassName(query.className),
110110
);

test/CachedResults.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as Parse from 'parse/node';
2+
import { CachedResults } from '../src/CachedResults';
3+
4+
const obj = new Parse.Object('Test');
5+
obj.set('testKey', 1);
6+
obj.set('test', true);
7+
8+
const obj2 = obj.clone();
9+
obj2.set('testKey', 2);
10+
11+
describe('#constructor', () => {
12+
test('should initialize with the right results', () => {
13+
const c = new CachedResults([obj, obj2]);
14+
expect(c.results).toEqual([obj.toJSON(), obj2.toJSON()]);
15+
});
16+
17+
test('should be able to initialize with the right date', () => {
18+
const d = new Date();
19+
const c = new CachedResults([obj, obj2], d);
20+
expect(c.results).toEqual([obj.toJSON(), obj2.toJSON()]);
21+
expect(c.createdAt).toEqual(d);
22+
});
23+
});
24+
25+
describe('#toJSON', () => {
26+
test('should return the right data', () => {
27+
const d = new Date();
28+
const c = new CachedResults([obj, obj2], d);
29+
expect(c.toJSON()).toEqual({
30+
createdAt: d,
31+
results: [obj.toJSON(), obj2.toJSON()],
32+
});
33+
});
34+
});
35+
36+
describe('#toParseObjs', () => {
37+
test('should convert back to Parse.Objects', () => {
38+
const d = new Date();
39+
const c = new CachedResults([obj, obj2], d);
40+
expect(CachedResults.toParseObjs('Test', [obj.toJSON(), obj2.toJSON()])).toEqual([obj, obj2]);
41+
});
42+
});
43+
44+
describe('#fromJSON', () => {
45+
test('should convert the results ok', () => {
46+
const results = [obj.toJSON(), obj2.toJSON()];
47+
const createdAt = new Date();
48+
const result = CachedResults.fromJSON({ createdAt, results });
49+
50+
expect(result instanceof CachedResults).toBe(true);
51+
expect(result.toParseObjs('Test').map(t => t.toJSON())).toEqual(results);
52+
});
53+
});

test/ParseOffline.test.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import * as Parse from 'parse/node';
2+
import { ParseOffline } from '../src/ParseOffline';
3+
import { CachedResults } from '../src/CachedResults';
4+
5+
const obj = new Parse.Object('Test');
6+
obj.set('testKey', 1);
7+
obj.set('test', true);
8+
9+
const obj2 = obj.clone();
10+
obj2.set('testKey', 2);
11+
12+
const store: any = {};
13+
14+
beforeAll(() => {
15+
Object.assign(window, {
16+
localStorage: {
17+
setItem: (key: string, value: string) => {
18+
store[key] = value;
19+
},
20+
getItem: key => store[key],
21+
},
22+
});
23+
});
24+
25+
describe('#saveResultsToLocalStorage', () => {
26+
test('should save the results in a way they can be retrieved back as Parse Objects', async () => {
27+
const query = new Parse.Query('Test');
28+
const spy = jest.spyOn(query, 'find');
29+
spy.mockImplementationOnce(async () => [obj, obj2]);
30+
31+
const spyStorage = jest.spyOn(localStorage, 'setItem');
32+
33+
const results = await ParseOffline.saveResultsToLocalStorage({
34+
query,
35+
options: { sessionToken: '123' },
36+
});
37+
38+
expect(spyStorage).toHaveBeenCalledTimes(1);
39+
expect(results).toEqual([obj, obj2]);
40+
41+
const str = localStorage.getItem('_cache_Test');
42+
const json = JSON.parse(str);
43+
44+
45+
const cachedResults = CachedResults
46+
.fromJSON({ ...json, className: 'Test' });
47+
48+
expect(cachedResults.toParseObjs('Test').map(o => o.toJSON()))
49+
.toEqual([obj.toJSON(), obj2.toJSON()]);
50+
});
51+
});
52+
53+
54+
describe('#getLocalStorageKeyForClassName', () => {
55+
test('should return the right key', () => {
56+
expect(ParseOffline.getLocalStorageKeyForClassName('Test'))
57+
.toEqual('_cache_Test');
58+
});
59+
});
60+
61+
describe('#getClassNameFromLocalStorageKey', () => {
62+
test('should return the right name', () => {
63+
expect(ParseOffline.getClassNameFromLocalStorageKey('_cache_Test'))
64+
.toEqual('Test');
65+
});
66+
});
67+
68+
describe('#getResultsFromTheLocalStorage', () => {
69+
test('should return the results from the localStorage', async () => {
70+
const results = await ParseOffline.getResultsFromTheLocalStorage({
71+
className: 'Test',
72+
});
73+
74+
expect(results.toParseObjs('Test').map(o => o.toJSON()))
75+
.toEqual([obj.toJSON(), obj2.toJSON()]);
76+
});
77+
78+
test('should return an empty CachedResults if there were no results in the localStorage',
79+
async () => {
80+
const results = await ParseOffline.getResultsFromTheLocalStorage({
81+
className: 'IDontExist',
82+
});
83+
84+
expect(results.results).toEqual([]);
85+
});
86+
});

0 commit comments

Comments
 (0)