<commit_before>import MemoryHistory from '../../src/memory';

describe('Memory history', () => {
  describe('constructor', () => {
    it('creates location/path creator functions', () => {
      const testHistory = new MemoryHistory();
      expect(typeof testHistory.createLocation).toBe('function');
      expect(typeof testHistory.createPath).toBe('function');
    });

    it('initializes with root location (/) if none provided', () => {
      const testHistory = new MemoryHistory();
      expect(testHistory.locations.length).toBe(1);
      expect(testHistory.index).toBe(0);
      expect(testHistory.location).toMatchObject({
        pathname: '/',
        hash: '',
        query: ''
      });
    });

    it('converts passed location values to location objects', () => {
      const testHistory = new MemoryHistory({
        locations: [
          '/one',
          { pathname: '/two' },
          [ '/three', { tres: 3 }]
        ]
      });

      expect(testHistory.locations.length).toBe(3);
      expect(testHistory.index).toBe(0);

      const [one, two, three] = testHistory.locations;
      expect(one).toMatchObject({
        pathname: '/one'
      });
      expect(two).toMatchObject({
        pathname: '/two'
      });
      expect(three).toMatchObject({
        pathname: '/three',
        state: { tres: 3 }
      });
    });

    it('sets the index if provided', () => {
      const testHistory = new MemoryHistory({
        locations: ['/one', '/two', '/three'],
        index: 2
      });
      expect(testHistory.index).toBe(2);
    });

    it('defaults to index 0 if provided value is out of bounds', () => {
      [-1, 3].forEach(value => {
        const testHistory = new MemoryHistory({
          locations: ['/one', '/two', '/three'],
          index: value
        });
        expect(testHistory.index).toBe(0);
      });
    });
  });

  describe('navigate', () => {
    it('pushes when given a location that creates a new path', () => {
      const testHistory = new MemoryHistory({
        locations: ['/first#test']
      });
      const subscriber = jest.fn();
      testHistory.subscribe(subscriber);

      testHistory.navigate({ pathname: 'first', hash: 'not-a-test' });

      const args = subscriber.mock.calls[0];
      expect(args[1]).toBe('PUSH');
    });

    it('replace when given a new location that creates the same path', () => {
      const testHistory = new MemoryHistory({
        locations: ['/first#test']
      });
      const subscriber = jest.fn();
      testHistory.subscribe(subscriber);

      testHistory.navigate({ pathname: 'first', hash: 'test' });

      const args = subscriber.mock.calls[0];
      expect(args[1]).toBe('REPLACE');
    });
  });

  describe('push', () => {
    it('pushes new location onto locations array', () => {
      const testHistory = new MemoryHistory();
      expect(testHistory.locations.length).toBe(1);
      expect(testHistory.index).toBe(0);

      testHistory.push('/next');

      expect(testHistory.locations.length).toBe(2);
      expect(testHistory.index).toBe(1);
      expect(testHistory.location).toMatchObject({
        pathname: '/next'
      });
    });

    it('emits the new location and action to any subscribers', () => {
      const testHistory = new MemoryHistory();
      const subscriber = jest.fn();
      testHistory.subscribe(subscriber);

      testHistory.push('/next');
      expect(subscriber.mock.calls.length).toBe(1);
      const args = subscriber.mock.calls[0];
      expect(args[0]).toMatchObject({
        pathname: '/next'
      });
      expect(args[1]).toBe('PUSH');
    });

    it('adds key with incremented major value and minor set to 0', () => {
      const testHistory = new MemoryHistory();

      let [ initMajor ] = testHistory.location.key.split('.');
      initMajor = parseInt(initMajor, 10);

      testHistory.push('/next');

      const current = testHistory.location;
      let [ currentMajor, currentMinor ] = current.key.split('.');
      currentMajor = parseInt(currentMajor, 10);

      expect(currentMajor).toEqual(initMajor + 1);
      expect(currentMinor).toBe('0');
    });

    it('sets history.action to "PUSH"', () => {
      const testHistory = new MemoryHistory();
      testHistory.push('/next');
      expect(testHistory.action).toBe('PUSH');
    });
  });

  describe('replace', () => {
    it('pushes new location onto locations array', () => {
      const testHistory = new MemoryHistory();
      expect(testHistory.locations.length).toBe(1);
      expect(testHistory.index).toBe(0);

      testHistory.replace('/same');

      expect(testHistory.locations.length).toBe(1);
      expect(testHistory.index).toBe(0);<commit_msg>Verify key when not pushing from end<commit_after>import MemoryHistory from '../../src/memory';

describe('Memory history', () => {
  describe('constructor', () => {
    it('creates location/path creator functions', () => {
      const testHistory = new MemoryHistory();
      expect(typeof testHistory.createLocation).toBe('function');
      expect(typeof testHistory.createPath).toBe('function');
    });

    it('initializes with root location (/) if none provided', () => {
      const testHistory = new MemoryHistory();
      expect(testHistory.locations.length).toBe(1);
      expect(testHistory.index).toBe(0);
      expect(testHistory.location).toMatchObject({
        pathname: '/',
        hash: '',
        query: ''
      });
    });

    it('converts passed location values to location objects', () => {
      const testHistory = new MemoryHistory({
        locations: [
          '/one',
          { pathname: '/two' },
          [ '/three', { tres: 3 }]
        ]
      });

      expect(testHistory.locations.length).toBe(3);
      expect(testHistory.index).toBe(0);

      const [one, two, three] = testHistory.locations;
      expect(one).toMatchObject({
        pathname: '/one'
      });
      expect(two).toMatchObject({
        pathname: '/two'
      });
      expect(three).toMatchObject({
        pathname: '/three',
        state: { tres: 3 }
      });
    });

    it('sets the index if provided', () => {
      const testHistory = new MemoryHistory({
        locations: ['/one', '/two', '/three'],
        index: 2
      });
      expect(testHistory.index).toBe(2);
    });

    it('defaults to index 0 if provided value is out of bounds', () => {
      [-1, 3].forEach(value => {
        const testHistory = new MemoryHistory({
          locations: ['/one', '/two', '/three'],
          index: value
        });
        expect(testHistory.index).toBe(0);
      });
    });
  });

  describe('navigate', () => {
    it('pushes when given a location that creates a new path', () => {
      const testHistory = new MemoryHistory({
        locations: ['/first#test']
      });
      const subscriber = jest.fn();
      testHistory.subscribe(subscriber);

      testHistory.navigate({ pathname: 'first', hash: 'not-a-test' });

      const args = subscriber.mock.calls[0];
      expect(args[1]).toBe('PUSH');
    });

    it('replace when given a new location that creates the same path', () => {
      const testHistory = new MemoryHistory({
        locations: ['/first#test']
      });
      const subscriber = jest.fn();
      testHistory.subscribe(subscriber);

      testHistory.navigate({ pathname: 'first', hash: 'test' });

      const args = subscriber.mock.calls[0];
      expect(args[1]).toBe('REPLACE');
    });
  });

  describe('push', () => {
    it('pushes new location onto locations array', () => {
      const testHistory = new MemoryHistory();
      expect(testHistory.locations.length).toBe(1);
      expect(testHistory.index).toBe(0);

      testHistory.push('/next');

      expect(testHistory.locations.length).toBe(2);
      expect(testHistory.index).toBe(1);
      expect(testHistory.location).toMatchObject({
        pathname: '/next'
      });
    });

    it('emits the new location and action to any subscribers', () => {
      const testHistory = new MemoryHistory();
      const subscriber = jest.fn();
      testHistory.subscribe(subscriber);

      testHistory.push('/next');
      expect(subscriber.mock.calls.length).toBe(1);
      const args = subscriber.mock.calls[0];
      expect(args[0]).toMatchObject({
        pathname: '/next'
      });
      expect(args[1]).toBe('PUSH');
    });

    it('adds key with incremented major value and minor set to 0', () => {
      const testHistory = new MemoryHistory();

      let [ initMajor ] = testHistory.location.key.split('.');
      initMajor = parseInt(initMajor, 10);

      testHistory.push('/next');

      const current = testHistory.location;
      let [ currentMajor, currentMinor ] = current.key.split('.');
      currentMajor = parseInt(currentMajor, 10);

      expect(currentMajor).toEqual(initMajor + 1);
      expect(currentMinor).toBe('0');
    });

    it('increments from current location\'s key when not at end of locations', () => {
      const testHistory = new MemoryHistory({
        locations: ['/one', '/two', '/three', '/four', '/five'],
        index: 2
      });

      testHistory.push('/new-four');

      expect(testHistory.location.key).toBe('3.0');

    });

    it('sets history.action to "PUSH"', () => {
      const testHistory = new MemoryHistory();
      testHistory.push('/next');
      expect(testHistory.action).toBe('PUSH');
    });
  });

  describe('replace', () => {
    it('pushes new location onto locations array', () => {
      const testHistory = new MemoryHistory();
      expect(testHistory.locations.length).toBe(1);
      expect(testHistory.index).toBe(0);

      testHistory.replace('/same');

      expect(testHistory.locations.length).toBe(1);
      expect(testHistory.index).toBe(0);
{'fraction_non_alphanumeric': 0.16654694715238583, 'fraction_numerical': 0.007491021036428938, 'mean_word_length': 2.640642510272693, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 40, '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': '6169397', 'n_tokens_mistral': 2824, 'n_tokens_neox': 2716, 'n_words': 588}