| <!DOCTYPE html> |
| <!-- |
| Copyright 2018 The Chromium Authors. All rights reserved. |
| Use of this source code is governed by a BSD-style license that can be |
| found in the LICENSE file. |
| --> |
| <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes"> |
| <title>cache-object.js tests</title> |
| |
| <script src="../bower_components/webcomponentsjs/webcomponents-lite.js"></script> |
| <script src="../bower_components/web-component-tester/browser.js"></script> |
| |
| <script type="module"> |
| import {CacheObject} from '../src/main/resources/static/cache-object.js'; |
| |
| suite('CacheObject', () => { |
| let cacheObj; |
| |
| setup(() => { |
| cacheObj = new CacheObject('cacheObjectTest'); |
| }); |
| |
| teardown(() => { |
| cacheObj.clear(); |
| }); |
| |
| test('read with empty cache returns empty object', () => { |
| assert.deepEqual(cacheObj.read(), {}); |
| }); |
| |
| test('read with corrupted cache returns empty object', () => { |
| cacheObj.write({a: 1}); |
| localStorage.setItem(cacheObj.key, '1'); |
| assert.deepEqual(cacheObj.read(), {}); |
| }); |
| |
| test('write followed by read succeeds', () => { |
| cacheObj.write({a: 1}); |
| assert.deepEqual(cacheObj.read(), {a: 1}); |
| }); |
| |
| test('write then read by second CacheObject succeeds', () => { |
| cacheObj.write({a: 1}); |
| const obj2 = new CacheObject('cacheObjectTest'); |
| assert.deepEqual(obj2.read(), {a: 1}); |
| }); |
| |
| test('write then clear then read yields empty object', () => { |
| cacheObj.write({a: 1}); |
| cacheObj.clear(); |
| assert.deepEqual(cacheObj.read(), {}); |
| }); |
| |
| test('write with a non-object throws an error', () => { |
| assert.throws( |
| () => cacheObj.write([]), 'obj parameter value is not an Object'); |
| }); |
| }); |
| </script> |