github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/ui-v2/tests/unit/utils/storage/local-storage-test.js (about)

     1  import localStorage from 'consul-ui/utils/storage/local-storage';
     2  import { module, test } from 'qunit';
     3  
     4  module('Unit | Utility | storage/local-storage');
     5  
     6  // Replace this with your real tests.
     7  const mockStorage = function(obj, encode = val => val, decode = val => val) {
     8    return localStorage('test', obj, encode, decode);
     9  };
    10  test('getValue returns an empty string if the value is null', function(assert) {
    11    const expected = '""';
    12    const storage = mockStorage({
    13      getItem: function(path) {
    14        return null;
    15      },
    16    });
    17    const actual = storage.getValue('test');
    18    assert.equal(actual, expected);
    19  });
    20  test('getValue uses the scheme in the path', function(assert) {
    21    assert.expect(1);
    22    const expected = 'test:test';
    23    const storage = mockStorage({
    24      getItem: function(actual) {
    25        assert.equal(actual, expected);
    26        return '';
    27      },
    28    });
    29    storage.getValue('test');
    30  });
    31  test('setValue uses the scheme in the path', function(assert) {
    32    assert.expect(1);
    33    const expected = 'test:test';
    34    const storage = mockStorage({
    35      setItem: function(actual, value) {
    36        assert.equal(actual, expected);
    37        return '';
    38      },
    39    });
    40    storage.setValue('test');
    41  });
    42  test('setValue calls removeItem if the value is null', function(assert) {
    43    assert.expect(1);
    44    const expected = 'test:test';
    45    const storage = mockStorage({
    46      removeItem: function(actual) {
    47        assert.equal(actual, expected);
    48      },
    49    });
    50    storage.setValue('test', null);
    51  });
    52  test('all returns an object of kvs under the correct prefix/scheme', function(assert) {
    53    const storage = mockStorage({
    54      'tester:a': 'a',
    55      b: 'b',
    56      'test:a': 'a',
    57      'test:b': 'b',
    58      getItem: function(path) {
    59        return this[path];
    60      },
    61    });
    62    const expected = {
    63      a: 'a',
    64      b: 'b',
    65    };
    66    const actual = storage.all();
    67    assert.deepEqual(actual, expected);
    68  });