|
29 | 29 | var eq = strictEqual; |
30 | 30 |
|
31 | 31 | test('Requirements.', function () { |
32 | | - expect(6); |
| 32 | + expect(7); |
33 | 33 |
|
34 | 34 | // Detect native JSON parser support. |
35 | 35 | var jsonSupported = function () { |
|
53 | 53 | eq(typeof JSONCache, 'object', 'JSONCache is required'); |
54 | 54 | eq(typeof JSONCache.getCachedJSON, 'function', 'JSONCache is required'); |
55 | 55 | eq(JSONCache.settings.browserOk, true, 'JSONCache.browserOk'); |
| 56 | + eq(JSONCache.settings.prefix, 'JSONCache', 'JSONCache prefix must match the one used in tests.'); |
56 | 57 | }); |
57 | 58 |
|
58 | 59 | test('Basic localStorage functionality', function () { |
|
97 | 98 | }); |
98 | 99 | }); |
99 | 100 |
|
| 101 | + // JSONCache.clear |
| 102 | + test('Cache item clearing with empty cache.', function () { |
| 103 | + expect(2); |
| 104 | + eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.'); |
| 105 | + JSONCache.clear('http://example.org/key?param1=a%20b+c#hash-123'); |
| 106 | + eq(window.localStorage.length, 0, 'localStorage should be empty in the end.'); |
| 107 | + }); |
| 108 | + test('Whole cache clearing with empty cache.', function () { |
| 109 | + expect(2); |
| 110 | + eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.'); |
| 111 | + JSONCache.clear(); |
| 112 | + eq(window.localStorage.length, 0, 'localStorage should be empty in the end.'); |
| 113 | + }); |
| 114 | + test('Cache item clearing with items in the cache.', function () { |
| 115 | + expect(3); |
| 116 | + eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.'); |
| 117 | + |
| 118 | + var dataKey = 'JSONCache data http://example.org/key?param1=a%20b+c#hash-123'; |
| 119 | + var timeKey = 'JSONCache time http://example.org/key?param1=a%20b+c#hash-123'; |
| 120 | + window.localStorage[dataKey] = 'my dätä'; |
| 121 | + window.localStorage[timeKey] = '123'; |
| 122 | + |
| 123 | + window.localStorage['öther kéy'] = 'öther dätä'; |
| 124 | + |
| 125 | + // Remove a certain item from the cache. |
| 126 | + JSONCache.clear('http://example.org/key?param1=a%20b+c#hash-123'); |
| 127 | + |
| 128 | + eq(window.localStorage.length, 1, 'There should be one item in the localStorage.'); |
| 129 | + eq(window.localStorage['öther kéy'], 'öther dätä', 'Correct data should be in the localStorage.'); |
| 130 | + |
| 131 | + }); |
| 132 | + test('Whole cache clearing with several items in the cache.', function () { |
| 133 | + expect(3); |
| 134 | + eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.'); |
| 135 | + |
| 136 | + var dataKey = 'JSONCache data http://example.org/key?param1=a%20b+c#hash-123'; |
| 137 | + var timeKey = 'JSONCache time http://example.org/key?param1=a%20b+c#hash-123'; |
| 138 | + window.localStorage[dataKey] = 'my dätä'; |
| 139 | + window.localStorage[timeKey] = '123'; |
| 140 | + |
| 141 | + window.localStorage['JSONCache data http://example.org/data'] = 'my dätä 2'; |
| 142 | + window.localStorage['JSONCache time http://example.org/time'] = '1234'; |
| 143 | + |
| 144 | + window.localStorage['öther kéy'] = 'öther dätä'; |
| 145 | + |
| 146 | + // Clear the whole cache. |
| 147 | + JSONCache.clear(); |
| 148 | + |
| 149 | + eq(window.localStorage.length, 1, 'There should be one item in the localStorage.'); |
| 150 | + eq(window.localStorage['öther kéy'], 'öther dätä', 'Correct data should be in the localStorage.'); |
| 151 | + }); |
| 152 | + |
100 | 153 | }(jQuery)); |
0 commit comments