<commit_before>describe('Backbone.fetchCache', function() {
  var originalPriorityFn = Backbone.fetchCache.priorityFn;
  beforeEach(function() {
    this.model = new Backbone.Model();
    this.model.url = '/model-cache-test';
    this.collection = new Backbone.Collection();
    this.collection.url = '/collection-cache-test';

    // Mock xhr resposes
    this.server = sinon.fakeServer.create();
    this.modelResponse = { sausages: 'bacon' };
    this.collectionResponse = [{ sausages: 'bacon' }, { rice: 'peas' }];
    this.server.respondWith('GET', this.model.url, [
      200,
      { "Content-Type": "application/json" },
      JSON.stringify(this.modelResponse)
    ]);
    this.server.respondWith('GET', this.collection.url, [
      200,
      { "Content-Type": "application/json" },
      JSON.stringify(this.collectionResponse)
    ]);
  });

  afterEach(function() {
    Backbone.fetchCache._cache = {};
    localStorage.clear('backboneCache');
    Backbone.fetchCache.priorityFn = originalPriorityFn;
    this.server.restore();
  });

  describe('AMD', function() {
    it('defines an AMD module if supported', function() {
      var s = document.createElement('script');
      s.src = '/backbone.fetch-cache.js';

      window.define = jasmine.createSpy('AMD define');
      window.define.amd = true;

      s.onload = function(){
        expect(window.define).toHaveBeenCalled();
      };

      document.body.appendChild(s);
    });
  });

  describe('.setCache', function() {
    beforeEach(function() {
      this.opts = { cache: true };
    });

    it('noops if the instance does not have a url', function() {
      this.model.url = null;
      Backbone.fetchCache.setCache(this.model, this.opts, this.modelResponse);
      expect(Backbone.fetchCache._cache[this.model.url]).toBeUndefined();
    });

    it('noops unless cache: true is passed', function() {
      Backbone.fetchCache.setCache(this.model, null, this.modelResponse);
      expect(Backbone.fetchCache._cache[this.model.url]).toBeUndefined();
    });

    it('keys cache items by URL',function() {
      Backbone.fetchCache.setCache(this.model, this.opts, this.modelResponse);
      expect(Backbone.fetchCache._cache[this.model.url].value)
        .toEqual(this.modelResponse);
    });

    it('calls setLocalStorage', function() {
      spyOn(Backbone.fetchCache, 'setLocalStorage');
      Backbone.fetchCache.setCache(this.model, this.opts);
      expect(Backbone.fetchCache.setLocalStorage).toHaveBeenCalled();
    });

    describe('with prefill: true option', function() {
      beforeEach(function() {
        this.opts = { prefill: true };
      });

      it('caches even if cache: true is not passed', function() {
        Backbone.fetchCache.setCache(this.model, this.opts, this.modelResponse);
        expect(Backbone.fetchCache._cache[this.model.url].value)
          .toEqual(this.modelResponse);
      });

      it('does not cache if cache: false is passed', function() {
        this.opts.cache = false;
        Backbone.fetchCache.setCache(this.model, this.opts, this.modelResponse);
        expect(Backbone.fetchCache._cache[this.model.url]).toBeUndefined();
      });

    });

    describe('cache expiry', function() {
      beforeEach(function() {
        this.clock = sinon.useFakeTimers();
      });

      afterEach(function() {
        this.clock.restore();
      });

      it('sets default expiry times for cache keys', function() {
        Backbone.fetchCache.setCache(this.model, { cache: true }, this.modelResponse);
        expect(Backbone.fetchCache._cache[this.model.url].expires)
          .toEqual((new Date()).getTime() + (5* 60 * 1000));
      });

      it('sets expiry times for cache keys', function() {
        var opts = { cache: true, expires: 1000 };
        Backbone.fetchCache.setCache(this.model, opts, this.modelResponse);
        expect(Backbone.fetchCache._cache[this.model.url].expires)
          .toEqual((new Date()).getTime() + (opts.expires * 1000));
      });

      it('is not set if expires: false is set', function() {
        var opts = { cache: true, expires: false };
        Backbone.fetchCache.setCache(this.model, opts, this.modelResponse);
        expect(Backbone.fetchCache._cache[this.model.url].expires)
          .toEqual(false);
      });
    });
  });

  describe('.clearItem', function() {
    beforeEach(function() {
      Backbone.fetchCache._cache = {
        '/item/1': { foo: 'bar' },
        '/item/2': { beep: 'boop' }
      };
    });

    it('deletes a single item from the cache', function() {
      Backbone.fetchCache.clearItem('/item/1');
      expect(Backbone.fetchCache._cache['/item/1']).toBeUndefined();
      expect(Backbone.fetchCache._cache['/item/2']).toEqual({ beep: 'boop' });
    });
  });

  describe('.setLocalStorage', function() {
    it('puts the cache into localStorage', function() {
      var cache = Backbone.fetchCache._cache = {
        '/url1': { expires: false, value: { bacon: 'sandwich' } },
        '/url2': { expires: false, value: { egg: 'roll' } }
      };
      Backbone.fetchCache.setLocalStorage();
      expect(localStorage.getItem('backboneCache')).toEqual(JSON.stringify(cache));
    });

    it('does not put the cache into localStorage if localStorage is false', function() {
      var cache = Backbone.fetchCache._cache = {
        '/url1': { expires: false, value: { bacon: 'sandwich' } },<commit_msg>Add a test for clearItem to set localStorage<commit_after>describe('Backbone.fetchCache', function() {
  var originalPriorityFn = Backbone.fetchCache.priorityFn;
  beforeEach(function() {
    this.model = new Backbone.Model();
    this.model.url = '/model-cache-test';
    this.collection = new Backbone.Collection();
    this.collection.url = '/collection-cache-test';

    // Mock xhr resposes
    this.server = sinon.fakeServer.create();
    this.modelResponse = { sausages: 'bacon' };
    this.collectionResponse = [{ sausages: 'bacon' }, { rice: 'peas' }];
    this.server.respondWith('GET', this.model.url, [
      200,
      { "Content-Type": "application/json" },
      JSON.stringify(this.modelResponse)
    ]);
    this.server.respondWith('GET', this.collection.url, [
      200,
      { "Content-Type": "application/json" },
      JSON.stringify(this.collectionResponse)
    ]);
  });

  afterEach(function() {
    Backbone.fetchCache._cache = {};
    localStorage.clear('backboneCache');
    Backbone.fetchCache.priorityFn = originalPriorityFn;
    this.server.restore();
  });

  describe('AMD', function() {
    it('defines an AMD module if supported', function() {
      var s = document.createElement('script');
      s.src = '/backbone.fetch-cache.js';

      window.define = jasmine.createSpy('AMD define');
      window.define.amd = true;

      s.onload = function(){
        expect(window.define).toHaveBeenCalled();
      };

      document.body.appendChild(s);
    });
  });

  describe('.setCache', function() {
    beforeEach(function() {
      this.opts = { cache: true };
    });

    it('noops if the instance does not have a url', function() {
      this.model.url = null;
      Backbone.fetchCache.setCache(this.model, this.opts, this.modelResponse);
      expect(Backbone.fetchCache._cache[this.model.url]).toBeUndefined();
    });

    it('noops unless cache: true is passed', function() {
      Backbone.fetchCache.setCache(this.model, null, this.modelResponse);
      expect(Backbone.fetchCache._cache[this.model.url]).toBeUndefined();
    });

    it('keys cache items by URL',function() {
      Backbone.fetchCache.setCache(this.model, this.opts, this.modelResponse);
      expect(Backbone.fetchCache._cache[this.model.url].value)
        .toEqual(this.modelResponse);
    });

    it('calls setLocalStorage', function() {
      spyOn(Backbone.fetchCache, 'setLocalStorage');
      Backbone.fetchCache.setCache(this.model, this.opts);
      expect(Backbone.fetchCache.setLocalStorage).toHaveBeenCalled();
    });

    describe('with prefill: true option', function() {
      beforeEach(function() {
        this.opts = { prefill: true };
      });

      it('caches even if cache: true is not passed', function() {
        Backbone.fetchCache.setCache(this.model, this.opts, this.modelResponse);
        expect(Backbone.fetchCache._cache[this.model.url].value)
          .toEqual(this.modelResponse);
      });

      it('does not cache if cache: false is passed', function() {
        this.opts.cache = false;
        Backbone.fetchCache.setCache(this.model, this.opts, this.modelResponse);
        expect(Backbone.fetchCache._cache[this.model.url]).toBeUndefined();
      });

    });

    describe('cache expiry', function() {
      beforeEach(function() {
        this.clock = sinon.useFakeTimers();
      });

      afterEach(function() {
        this.clock.restore();
      });

      it('sets default expiry times for cache keys', function() {
        Backbone.fetchCache.setCache(this.model, { cache: true }, this.modelResponse);
        expect(Backbone.fetchCache._cache[this.model.url].expires)
          .toEqual((new Date()).getTime() + (5* 60 * 1000));
      });

      it('sets expiry times for cache keys', function() {
        var opts = { cache: true, expires: 1000 };
        Backbone.fetchCache.setCache(this.model, opts, this.modelResponse);
        expect(Backbone.fetchCache._cache[this.model.url].expires)
          .toEqual((new Date()).getTime() + (opts.expires * 1000));
      });

      it('is not set if expires: false is set', function() {
        var opts = { cache: true, expires: false };
        Backbone.fetchCache.setCache(this.model, opts, this.modelResponse);
        expect(Backbone.fetchCache._cache[this.model.url].expires)
          .toEqual(false);
      });
    });
  });

  describe('.clearItem', function() {
    beforeEach(function() {
      Backbone.fetchCache._cache = {
        '/item/1': { foo: 'bar' },
        '/item/2': { beep: 'boop' }
      };
    });

    it('deletes a single item from the cache', function() {
      Backbone.fetchCache.clearItem('/item/1');
      expect(Backbone.fetchCache._cache['/item/1']).toBeUndefined();
      expect(Backbone.fetchCache._cache['/item/2']).toEqual({ beep: 'boop' });
    });

    it('updates localStorage', function() {
      spyOn(Backbone.fetchCache, 'setLocalStorage');
      Backbone.fetchCache.clearItem('/item/1');
      expect(Backbone.fetchCache.setLocalStorage).toHaveBeenCalled();
    });
  });

  describe('.setLocalStorage', function() {
    it('puts the cache into localStorage', function() {
      var cache = Backbone.fetchCache._cache = {
        '/url1': { expires: false, value: { bacon: 'sandwich' } },
        '/url2': { expires: false, value: { egg: 'roll' } }
      };
      Backbone.fetchCache.setLocalStorage();
      expect(localStorage.getItem('backboneCache')).toEqual(JSON.stringify(cache));
    });

    it('does not put the cache into localStorage if localStorage is false', function() {
      var cache = Backbone.fetchCache._cache = {
        '/url1': { expires: false, value: { bacon: 'sandwich' } },
{'fraction_non_alphanumeric': 0.1585266768980771, 'fraction_numerical': 0.005326351900334025, 'mean_word_length': 3.013768115942029, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6939509', 'n_tokens_mistral': 3388, 'n_tokens_neox': 3294, 'n_words': 604}