<commit_before>import localeQueryParamsMiddleware from '../localeQueryParamsMiddleware';

describe('localeQueryParamsMiddleware', () => {
  let getState;

  beforeEach(() => {
    const store = new Map();

    store.set('i18n_locales', { locales: [] });
    store.set('permissionsManager', { userPermissions: [] });
    store.set('permissionsManager', {
      collectionTypesRelatedPermissions: {
        'plugins::content-manager.explorer.read': [],
        'plugins::content-manager.explorer.create': [],
      },
    });

    getState = () => store;
  });

  it('does nothing on unknown actions', () => {
    const middleware = localeQueryParamsMiddleware()({ getState });
    const nextFn = jest.fn();
    const action = { type: 'UNKNOWN' };

    middleware(nextFn)(action);

    expect(nextFn).toBeCalledWith(action);
    expect(action).toEqual({
      type: 'UNKNOWN',
    });
  });

  it('does nothing when there s no i18n.localized key in the action', () => {
    const middleware = localeQueryParamsMiddleware()({ getState });
    const nextFn = jest.fn();
    const action = {
      type: 'ContentManager/ListView/SET_LIST_LAYOUT ',
      contentType: { pluginOptions: {} },

      initialParams: {},
    };

    middleware(nextFn)(action);

    expect(nextFn).toBeCalledWith(action);
    expect(action).toEqual({
      contentType: { pluginOptions: {} },
      initialParams: {},
      type: 'ContentManager/ListView/SET_LIST_LAYOUT ',
    });
  });

  it('creates a plugins key with a locale when initialParams does not have a plugins key and the field is localized', () => {
    const middleware = localeQueryParamsMiddleware()({ getState });
    const nextFn = jest.fn();
    const action = {
      type: 'ContentManager/ListView/SET_LIST_LAYOUT ',
      contentType: {
        pluginOptions: {
          i18n: { localized: true },
        },
      },
      initialParams: {},
    };

    middleware(nextFn)(action);

    expect(nextFn).toBeCalledWith(action);
    expect(action).toEqual({
      contentType: { pluginOptions: { i18n: { localized: true } } },
      initialParams: { plugins: { i18n: { locale: null } } },
      type: 'ContentManager/ListView/SET_LIST_LAYOUT ',
    });
  });

  it('adds a key to plugins with a locale when initialParams has a plugins key and the field is localized', () => {
    const middleware = localeQueryParamsMiddleware()({ getState });
    const nextFn = jest.fn();
    const action = {
      type: 'ContentManager/ListView/SET_LIST_LAYOUT ',
      contentType: {
        pluginOptions: {
          i18n: { localized: true },
        },
      },
      initialParams: {
        plugins: {
          hello: 'world',
        },
      },
    };

    middleware(nextFn)(action);

    expect(nextFn).toBeCalledWith(action);
    expect(action).toEqual({
      contentType: { pluginOptions: { i18n: { localized: true } } },
      initialParams: { plugins: { i18n: { locale: null }, hello: 'world' } },
      type: 'ContentManager/ListView/SET_LIST_LAYOUT ',
    });
  });
});<commit_msg>test fixing<commit_after>import localeQueryParamsMiddleware from '../localeQueryParamsMiddleware';

describe('localeQueryParamsMiddleware', () => {
  let getState;

  beforeEach(() => {
    const store = new Map();

    store.set('i18n_locales', { locales: [] });
    store.set('permissionsManager', { userPermissions: [] });
    store.set('permissionsManager', {
      collectionTypesRelatedPermissions: {
        'plugins::content-manager.explorer.read': [],
        'plugins::content-manager.explorer.create': [],
      },
    });

    getState = () => store;
  });

  it('does nothing on unknown actions', () => {
    const middleware = localeQueryParamsMiddleware()({ getState });
    const nextFn = jest.fn();
    const action = { type: 'UNKNOWN' };

    middleware(nextFn)(action);

    expect(nextFn).toBeCalledWith(action);
    expect(action).toEqual({
      type: 'UNKNOWN',
    });
  });

  it('does nothing when there s no i18n.localized key in the action', () => {
    const middleware = localeQueryParamsMiddleware()({ getState });
    const nextFn = jest.fn();
    const action = {
      type: 'ContentManager/ListView/SET_LIST_LAYOUT ',
      contentType: { pluginOptions: {} },

      initialParams: {},
    };

    middleware(nextFn)(action);

    expect(nextFn).toBeCalledWith(action);
    expect(action).toEqual({
      contentType: { pluginOptions: {} },
      initialParams: {},
      type: 'ContentManager/ListView/SET_LIST_LAYOUT ',
    });
  });

  it('creates a plugins key with a locale when initialParams does not have a plugins key and the field is localized', () => {
    const middleware = localeQueryParamsMiddleware()({ getState });
    const nextFn = jest.fn();
    const action = {
      type: 'ContentManager/ListView/SET_LIST_LAYOUT ',
      displayedHeaders: [],
      contentType: {
        pluginOptions: {
          i18n: { localized: true },
        },
      },
      initialParams: {},
    };

    middleware(nextFn)(action);

    expect(nextFn).toBeCalledWith(action);
    // The anonymous function of cellFormatter creates problem, because it's anonymous
    // In our scenario, it's even more tricky because we use a closure in order to pass
    // the locales.
    // Stringifying the action allows us to have a name inside the expectation for the "cellFormatter" key
    expect(JSON.stringify(action)).toBe(
      '{"type":"ContentManager/ListView/SET_LIST_LAYOUT ","displayedHeaders":[{"key":"__locale_key__","fieldSchema":{"type":"string"},"metadatas":{"label":"Content available in","searchable":false,"sortable":false},"name":"locales"}],"contentType":{"pluginOptions":{"i18n":{"localized":true}}},"initialParams":{"plugins":{"i18n":{"locale":null}}}}'
    );
  });

  it('adds a key to plugins with a locale when initialParams has a plugins key and the field is localized', () => {
    const middleware = localeQueryParamsMiddleware()({ getState });
    const nextFn = jest.fn();
    const action = {
      type: 'ContentManager/ListView/SET_LIST_LAYOUT ',
      displayedHeaders: [],
      contentType: {
        pluginOptions: {
          i18n: { localized: true },
        },
      },
      initialParams: {
        plugins: {
          hello: 'world',
        },
      },
    };

    middleware(nextFn)(action);

    expect(nextFn).toBeCalledWith(action);
    expect(JSON.stringify(action)).toBe(
      '{"type":"ContentManager/ListView/SET_LIST_LAYOUT ","displayedHeaders":[{"key":"__locale_key__","fieldSchema":{"type":"string"},"metadatas":{"label":"Content available in","searchable":false,"sortable":false},"name":"locales"}],"contentType":{"pluginOptions":{"i18n":{"localized":true}}},"initialParams":{"plugins":{"hello":"world","i18n":{"locale":null}}}}'
    );
  });
});
{'fraction_non_alphanumeric': 0.1570626477541371, 'fraction_numerical': 0.004728132387706856, 'mean_word_length': 3.3418858242463116, 'pattern_counts': {'":': 37, '<': 3, '<?xml version=': 0, '>': 17, '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': '835160', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1919, 'n_words': 397}