LRUCache.test.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { LRUCache } from '../src/utilities/LRUCache.js';
  2. describe( 'LRUCache', () => {
  3. it( 'should not allow the same object to be added more than once.', () => {
  4. const cache = new LRUCache();
  5. const A = {};
  6. expect( cache.add( A, () => {} ) ).toEqual( true );
  7. expect( cache.add( A, () => {} ) ).toEqual( false );
  8. expect( cache.remove( A ) ).toEqual( true );
  9. expect( cache.remove( A ) ).toEqual( false );
  10. } );
  11. it( 'should not allow adding if the cache is full.', () => {
  12. const cache = new LRUCache();
  13. cache.minSize = cache.maxSize = 1;
  14. expect( cache.isFull() ).toEqual( false );
  15. cache.add( {}, () => {} );
  16. expect( cache.add( {}, () => {} ) ).toEqual( false );
  17. expect( cache.isFull() ).toEqual( true );
  18. } );
  19. it( 'should fire the callback when removing an item.', () => {
  20. const cache = new LRUCache();
  21. const A = {};
  22. let called = false;
  23. cache.add( A, () => called = true );
  24. expect( called ).toEqual( false );
  25. cache.remove( A );
  26. expect( called ).toEqual( true );
  27. } );
  28. it( 'should mark an item as used when adding.', () => {
  29. const cache = new LRUCache();
  30. cache.minSize = 0;
  31. cache.maxSize = 1;
  32. cache.add( {}, () => {} );
  33. expect( cache.isFull() ).toEqual( true );
  34. cache.unloadUnusedContent( 0.25, null );
  35. expect( cache.isFull() ).toEqual( true );
  36. cache.markAllUnused();
  37. cache.unloadUnusedContent( 0.25, null );
  38. expect( cache.isFull() ).toEqual( false );
  39. } );
  40. } );