<commit_before>/* eslint-env jest, jasmine */
/* eslint-disable no-console */

import createHistoryStateManager from './createHistoryStateManager';
jest.unmock('./createHistoryStateManager');
jest.unmock('qs');

import MockDate from 'mockdate';
jest.unmock('mockdate');

const createMockHistory = (initialLocation, isV3) => {
  const listeners = [];
  let currentLocation = initialLocation;
  const dispatch = () =>
    listeners.forEach(listener => listener(currentLocation));
  return {
    listen: jest.fn(listener => {
      listeners.push(listener);
      if (!isV3) {
        listener(initialLocation);
      }
      return () => {
        listeners.splice(listeners.indexOf(listener), 1);
      };
    }),
    push: jest.fn(location => {
      currentLocation = location;
      dispatch();
    }),
    replace: jest.fn(location => {
      currentLocation = location;
      dispatch();
    }),
    // Would normally take in a location object and return a string, but we
    // want to inspect the object.
    createHref: a => a,
    getCurrentLocation: isV3 ? () => currentLocation : undefined,
  };
};

describe('createHistoryStateManager', () => {
  describe('getStateFromCurrentLocation', () => {
    it('retrieves the state from the current location', () => {
      const initialLocation = {
        search: '?hello=yes',
      };
      const history = createMockHistory(initialLocation);
      const hsm = createHistoryStateManager({
        history,
        threshold: 0,
        onInternalStateUpdate: () => null,
        getKnownKeys: () => ['hello'],
      });
      expect(hsm.getStateFromCurrentLocation()).toEqual({hello: 'yes'});
    });

    it('also supports history V3', () => {
      const initialLocation = {
        search: '?hello=yes',
      };
      const history = createMockHistory(initialLocation, true);
      const hsm = createHistoryStateManager({
        history,
        threshold: 0,
        onInternalStateUpdate: () => null,
        getKnownKeys: () => ['hello'],
      });
      expect(hsm.getStateFromCurrentLocation()).toEqual({hello: 'yes'});
    });

    it('also supports the query enhancer', () => {
      const initialLocation = {
        query: {hello: 'yes'},
      };
      const history = createMockHistory(initialLocation);
      const hsm = createHistoryStateManager({
        history,
        threshold: 0,
        onInternalStateUpdate: () => null,
        getKnownKeys: () => ['hello'],
      });
      expect(hsm.getStateFromCurrentLocation()).toEqual({hello: 'yes'});
    });

    it('ignores unknown keys', () => {
      const initialLocation = {
        query: {hello: 'yes', not: 'cool'},
      };
      const history = createMockHistory(initialLocation);
      const hsm = createHistoryStateManager({
        history,
        threshold: 0,
        onInternalStateUpdate: () => null,
        getKnownKeys: () => ['hello'],
      });
      expect(hsm.getStateFromCurrentLocation()).toEqual({hello: 'yes'});
    });
  });

  it('ignores unknown keys', () => {
    const initialLocation = {
      query: {hello: 'yes', not: 'cool'},
    };
    const history = createMockHistory(initialLocation);
    const hsm = createHistoryStateManager({
      history,
      threshold: 0,
      onInternalStateUpdate: () => null,
      getKnownKeys: () => ['hello'],
    });
    expect(hsm.getStateFromCurrentLocation()).toEqual({hello: 'yes'});
  });

  describe('onExternalStateUpdate', () => {
    it('causes a new location to be pushed', () => {
      const initialLocation = {
        query: {hello: 'yes', oy: 'sup'},
      };<commit_msg>Remove tests for deprecated behavior<commit_after>/* eslint-env jest, jasmine */
/* eslint-disable no-console */

import createHistoryStateManager from './createHistoryStateManager';
jest.unmock('./createHistoryStateManager');
jest.unmock('qs');

import MockDate from 'mockdate';
jest.unmock('mockdate');

const createMockHistory = (initialLocation, isV3) => {
  const listeners = [];
  let currentLocation = initialLocation;
  const dispatch = () =>
    listeners.forEach(listener => listener(currentLocation));
  return {
    listen: jest.fn(listener => {
      listeners.push(listener);
      if (!isV3) {
        listener(initialLocation);
      }
      return () => {
        listeners.splice(listeners.indexOf(listener), 1);
      };
    }),
    push: jest.fn(location => {
      currentLocation = location;
      dispatch();
    }),
    replace: jest.fn(location => {
      currentLocation = location;
      dispatch();
    }),
    // Would normally take in a location object and return a string, but we
    // want to inspect the object.
    createHref: a => a,
    getCurrentLocation: isV3 ? () => currentLocation : undefined,
  };
};

describe('createHistoryStateManager', () => {
  describe('getStateFromCurrentLocation', () => {
    it('retrieves the state from the current location', () => {
      const initialLocation = {
        search: '?hello=yes',
      };
      const history = createMockHistory(initialLocation);
      const hsm = createHistoryStateManager({
        history,
        threshold: 0,
        onInternalStateUpdate: () => null,
        getKnownKeys: () => ['hello'],
      });
      expect(hsm.getStateFromCurrentLocation()).toEqual({hello: 'yes'});
    });

    it('also supports history V3', () => {
      const initialLocation = {
        search: '?hello=yes',
      };
      const history = createMockHistory(initialLocation, true);
      const hsm = createHistoryStateManager({
        history,
        threshold: 0,
        onInternalStateUpdate: () => null,
        getKnownKeys: () => ['hello'],
      });
      expect(hsm.getStateFromCurrentLocation()).toEqual({hello: 'yes'});
    });

    it('also supports the query enhancer', () => {
      const initialLocation = {
        query: {hello: 'yes'},
      };
      const history = createMockHistory(initialLocation);
      const hsm = createHistoryStateManager({
        history,
        threshold: 0,
        onInternalStateUpdate: () => null,
        getKnownKeys: () => ['hello'],
      });
      expect(hsm.getStateFromCurrentLocation()).toEqual({hello: 'yes'});
    });
  });

  describe('onExternalStateUpdate', () => {
    it('causes a new location to be pushed', () => {
      const initialLocation = {
        query: {hello: 'yes', oy: 'sup'},
      };
{'fraction_non_alphanumeric': 0.1504020179725682, 'fraction_numerical': 0.0028377739240107204, 'mean_word_length': 2.847180109157065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 53, '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': '4044568', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1742, 'n_words': 388}