<commit_before>'use strict';

describe('MwListCollectionFilter', function() {
  var MwListCollectionFilter,
    filterInLocalStorage,
    currentUserUuid,
    FilterHolderProviderSpy,
    FilterHolderSpy,
    AuthenticatedUserSpy,
    $rootScope;

  beforeEach(module('mwCollection'));

  beforeEach(module(function($provide) {
    //create stubs to mimic external input-methods from the real context
    var LocalForageStub = {
      getItem: function(dummyParameter) {
        return {
          then: function() {
            return filterInLocalStorage;
          }
        };
      }
    };

    var FilterHolderStub = function (dummy, type) {
      this.type = type;
    };

    //create spies to mimic output-methods to external real context
    FilterHolderProviderSpy = { createFilterHolder: function() {}};

    FilterHolderSpy = {
      get: function() {},
      set: function(value) {}
    };

    AuthenticatedUserSpy = {
      get: function() {},
      set: function(attr, val){
        if(attr === 'uuid'){
          currentUserUuid = val;
          if(this.callback){
            this.callback();
          }
        }
      },
      once: function(evName, callback){
        this.callback = callback
      }
    };

    //redirect calls-to-this-external-methods to the stubs / spies
    $provide.value('LocalForage', LocalForageStub);
    $provide.value('FilterHoldersCollection', FilterHolderStub);
    $provide.value('FilterHolderProvider', FilterHolderProviderSpy);
    $provide.value('AuthenticatedUser', AuthenticatedUserSpy);

    //spy on these methods to see if they get called
    spyOn(FilterHolderProviderSpy, 'createFilterHolder').and.returnValue(FilterHolderSpy);
    spyOn(FilterHolderSpy, 'set');
    spyOn(AuthenticatedUserSpy, 'get').and.callFake(function(attr){
      if(attr === 'authenticated'){
        return !!currentUserUuid;
      }
      return currentUserUuid;
    });

    currentUserUuid = null;
  }));


  beforeEach(inject(function(_$rootScope_, _MwListCollectionFilter_) {
    $rootScope = _$rootScope_;
    MwListCollectionFilter = _MwListCollectionFilter_;
  }));



  describe('fetching applied filters', function() {
    var listCollectionFilter;

    beforeEach(function() {
      listCollectionFilter = new MwListCollectionFilter('IRRELEVANT');
    });

    it('returns null if no applied filter is in localstorage', function() {
      filterInLocalStorage = null;

      var appliedFilter = listCollectionFilter.fetchAppliedFilter();

      expect(appliedFilter).toBeNull();
    });

    it('returns filter in localstorage if present', function() {
      filterInLocalStorage = 'IRRELEVANT';

      var appliedFilter = listCollectionFilter.fetchAppliedFilter();

      expect(appliedFilter).not.toBeNull();
    });

    it('waits until the user is authenticated before setting appliedfilter', function(done) {
      var filterInLocalStorage = {
        aclEntries: ['USER1:rw']
      };
      listCollectionFilter._setAppliedFilter(filterInLocalStorage).then(function(){
        done();
      });

      AuthenticatedUserSpy.set('uuid', 'USER1');
      $rootScope.$digest();
    });

    it('sets applied filter when current user is the filter owner', function(done) {
      var filterInLocalStorage = {
        aclEntries: ['USER1:rw']
      };

      listCollectionFilter._setAppliedFilter(filterInLocalStorage).then(function(appliedFilter){
        expect(appliedFilter).not.toBeNull();
        expect(FilterHolderSpy.set).toHaveBeenCalledWith(filterInLocalStorage);
        done();
      });

      AuthenticatedUserSpy.set('uuid', 'USER1');
      $rootScope.$digest();
    });

    it('does not set applied filter when current user is not the filter owner', function(done) {
      var filterInLocalStorage = {
        aclEntries: ['USER1:rw']
      };
      listCollectionFilter._setAppliedFilter(filterInLocalStorage).then(function(appliedFilter){
        expect(appliedFilter).not.toBeNull();
        expect(FilterHolderSpy.set).not.toHaveBeenCalledWith(filterInLocalStorage);
        done();
      });

      AuthenticatedUserSpy.set('uuid', 'USER2');
      $rootScope.$digest();
    });
  });

});<commit_msg>Adjust test case<commit_after>'use strict';

describe('MwListCollectionFilter', function() {
  var $q,
    MwListCollectionFilter,
    filterInLocalStorage,
    currentUserUuid,
    FilterHolderProviderSpy,
    FilterHolderSpy,
    $rootScope;

  beforeEach(module('mwCollection'));

  beforeEach(module(function($provide) {
    //create stubs to mimic external input-methods from the real context
    var LocalForageStub = {
      getItem: function(dummyParameter) {
        return {
          then: function() {
            return filterInLocalStorage;
          }
        };
      }
    };

    var FilterHolderStub = function (dummy, type) {
      this.type = type;
      this.fetch = function(){
        var dfd = $q.defer();
        dfd.resolve([]);
        return dfd.promise;
      };
      this.get = function(filter){
        if(filter.id === 'BELONGS_TO_USER'){
          return true;
        } else {
          return false;
        }
      };
    };

    //create spies to mimic output-methods to external real context
    FilterHolderProviderSpy = { createFilterHolder: function() {}};

    FilterHolderSpy = {
      get: function() {},
      set: function(value) {}
    };

    //redirect calls-to-this-external-methods to the stubs / spies
    $provide.value('LocalForage', LocalForageStub);
    $provide.value('FilterHoldersCollection', FilterHolderStub);
    $provide.value('FilterHolderProvider', FilterHolderProviderSpy);

    //spy on these methods to see if they get called
    spyOn(FilterHolderProviderSpy, 'createFilterHolder').and.returnValue(FilterHolderSpy);
    spyOn(FilterHolderSpy, 'set');

    currentUserUuid = null;
  }));


  beforeEach(inject(function(_$rootScope_, _MwListCollectionFilter_, _$q_) {
    $rootScope = _$rootScope_;
    MwListCollectionFilter = _MwListCollectionFilter_;
    $q = _$q_;
  }));



  describe('fetching applied filters', function() {
    var listCollectionFilter;

    beforeEach(function() {
      listCollectionFilter = new MwListCollectionFilter('IRRELEVANT');
    });

    it('returns null if no applied filter is in localstorage', function() {
      filterInLocalStorage = null;

      var appliedFilter = listCollectionFilter.fetchAppliedFilter();

      expect(appliedFilter).toBeNull();
    });

    it('returns filter in localstorage if present', function() {
      filterInLocalStorage = 'IRRELEVANT';

      var appliedFilter = listCollectionFilter.fetchAppliedFilter();

      expect(appliedFilter).not.toBeNull();
    });

    it('return true when current user is the filter owner', function(done) {
      listCollectionFilter.filterWasSetByUser({id: 'BELONGS_TO_USER'}).then(function(belongsToUser){
        expect(belongsToUser).toBeTruthy();
        done();
      });
      $rootScope.$digest();
    });

    it('returns false when current user is not the filter owner', function(done) {
      listCollectionFilter.filterWasSetByUser({id: 'BELONGS_NOT_TO_USER'}).then(function(belongsToUser){
        expect(belongsToUser).toBeFalsy();
        done();
      });
      $rootScope.$digest();
    });
  });

});
{'fraction_non_alphanumeric': 0.1193557268722467, 'fraction_numerical': 0.0008259911894273128, 'mean_word_length': 3.1514285714285712, 'pattern_counts': {'":': 0, '<': 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': '5107452', 'n_tokens_mistral': 2114, 'n_tokens_neox': 2030, 'n_words': 406}