running-tools

A collection of tools for runners and their coaches
git clone https://git.ashermorgan.net/running-tools/
Log | Files | Refs | README

utils.spec.js (4951B)


      1 import { beforeEach, describe, test, expect } from 'vitest';
      2 import * as utils from '@/core/utils';
      3 
      4 beforeEach(() => {
      5   localStorage.clear();
      6 });
      7 
      8 describe('deepCopy method', () => {
      9   test('should deeply clone an object', () => {
     10     let input = {
     11       foo: 123,
     12       bar: ['a', 'b', 'c'],
     13       baz: {
     14         baz: {
     15           baz: {
     16             baz: 'baz'
     17           }
     18         }
     19       }
     20     };
     21     let output = utils.deepCopy(input);
     22 
     23     // Output should equal input
     24     expect(output).to.deep.equal(input);
     25 
     26     // Modifying input should not modify output
     27     input.foo = 1234;
     28     input.baz.baz.baz.baz = 'baz2';
     29     expect(output).to.deep.equal({
     30       foo: 123,
     31       bar: ['a', 'b', 'c'],
     32       baz: {
     33         baz: {
     34           baz: {
     35             baz: 'baz'
     36           }
     37         }
     38       }
     39     });
     40 
     41     // Modifying output should not modify input
     42     output.foo = 12345;
     43     output.baz.baz.baz.baz = 'baz3';
     44     expect(input).to.deep.equal({
     45       foo: 1234,
     46       bar: ['a', 'b', 'c'],
     47       baz: {
     48         baz: {
     49           baz: {
     50             baz: 'baz2'
     51           }
     52         }
     53       }
     54     });
     55   });
     56 });
     57 
     58 describe('deepEqual method', () => {
     59   test('should correctly compare an object with itself', () => {
     60     let obj1 = {
     61       foo: 123,
     62       bar: ['a', 'b', 'c'],
     63       baz: {
     64         baz: {
     65           baz: {
     66             baz: 'baz'
     67           }
     68         }
     69       }
     70     };
     71 
     72     // obj1 should equal obj1
     73     expect(utils.deepEqual(obj1, obj1)).to.equal(true);
     74   });
     75 
     76   test('should correctly compare identical objects', () => {
     77     let obj1 = {
     78       foo: 123,
     79       bar: ['a', 'b', 'c'],
     80       baz: {
     81         baz: {
     82           baz: {
     83             baz: 'baz'
     84           }
     85         }
     86       }
     87     };
     88     let obj2 = {
     89       foo: 123,
     90       bar: ['a', 'b', 'c'],
     91       baz: {
     92         baz: {
     93           baz: {
     94             baz: 'baz'
     95           }
     96         }
     97       }
     98     };
     99 
    100     // obj1 should equal obj2
    101     expect(utils.deepEqual(obj1, obj2)).to.equal(true);
    102   });
    103 
    104   test('should correctly compare unequal objects', () => {
    105     let obj1 = {
    106       foo: 123,
    107       bar: ['a', 'b', 'c'],
    108       baz: {
    109         baz: {
    110           baz: {
    111             baz: 'baz'
    112           }
    113         }
    114       }
    115     };
    116     let obj2 = {
    117       foo: 123,
    118       bar: ['a', 'b', 'c'],
    119       baz: {
    120         baz: {
    121           baz: {
    122             baz: 'baz2'
    123           }
    124         }
    125       }
    126     };
    127 
    128     // obj1 should not equal obj2
    129     expect(utils.deepEqual(obj1, obj2)).to.equal(false);
    130   });
    131 });
    132 
    133 describe('getLocalStorage method', () => {
    134   test('should correctly parse correct localStorage item', async () => {
    135     // Initialize localStorage
    136     localStorage.setItem('running-tools.foo', '{"bar":123}');
    137 
    138     // Assert result is correct
    139     expect(utils.getLocalStorage('foo')).to.deep.equal({ bar: 123 });
    140   });
    141 
    142   test('should return null for corrupt localStorage item', async () => {
    143     // Initialize localStorage
    144     localStorage.setItem('running-tools.foo', 'invalid json');
    145 
    146     // Assert result is correct
    147     expect(utils.getLocalStorage('foo')).to.equal(null);
    148   });
    149 
    150   test('should return null for missing localStorage item', async () => {
    151     // Initialize localStorage
    152     localStorage.setItem('running-tools.foo', '{"bar":123}');
    153 
    154     // Assert result is correct
    155     expect(utils.getLocalStorage('baz')).to.equal(null);
    156   });
    157 });
    158 
    159 describe('setLocalStorage method', () => {
    160   test('should correctly set new localStorage item', async () => {
    161     // Set localStorage item
    162     utils.setLocalStorage('foo', { baz: 456 });
    163 
    164     // Assert result is correct
    165     expect(localStorage.getItem('running-tools.foo')).to.equal('{"baz":456}');
    166   });
    167 
    168   test('should correctly override existing localStorage item', async () => {
    169     // Initialize localStorage
    170     localStorage.setItem('running-tools.foo', '{"bar":123}');
    171 
    172     // Set localStorage item
    173     utils.setLocalStorage('foo', { baz: 456 });
    174 
    175     // Assert result is correct
    176     expect(localStorage.getItem('running-tools.foo')).to.equal('{"baz":456}');
    177   });
    178 });
    179 
    180 describe('unsetLocalStorage method', () => {
    181   test('should correctly remove existing localStorage item', async () => {
    182     // Set localStorage item
    183     localStorage.setItem('running-tools.foo', '1');
    184     localStorage.setItem('running-tools.bar', '2');
    185 
    186     // Remove localStorage item
    187     utils.unsetLocalStorage('bar');
    188 
    189     // Assert localStorage updated correctly
    190     expect(localStorage.getItem('running-tools.foo')).to.equal('1');
    191     expect(localStorage.getItem('running-tools.bar')).to.equal(null);
    192     expect(localStorage.length).to.equal(1);
    193   });
    194 
    195   test('should remove non-existant localStorage item without error', async () => {
    196     // Set localStorage item
    197     localStorage.setItem('running-tools.foo', '1');
    198 
    199     // Remove localStorage item
    200     utils.unsetLocalStorage('missing');
    201 
    202     // Assert localStorage updated correctly
    203     expect(localStorage.length).to.equal(1);
    204     expect(localStorage.getItem('running-tools.foo')).to.equal('1');
    205   });
    206 });