huridocs/uwazi

View on GitHub
app/react/I18N/specs/t.spec.js

Summary

Maintainability
A
0 mins
Test Coverage
import { store } from 'app/store';
import Immutable from 'immutable';
import t from '../t';

describe('t', () => {
  let state;

  beforeEach(() => {
    t.resetCachedTranslation();
    const dictionaries = [
      {
        locale: 'en',
        contexts: [
          {
            id: 'System',
            label: 'System',
            values: {
              Search: 'Search',
              confirmDeleteDocument: 'Are you sure you want to delete this document?',
              confirmDeleteEntity: 'Are you sure you want to delete this entity?',
            },
          },
        ],
      },
      {
        locale: 'es',
        contexts: [
          {
            id: 'System',
            label: 'System',
            values: {
              Search: 'Buscar',
              confirmDeleteDocument: '¿Esta seguro que quiere borrar este documento?',
            },
          },
        ],
      },
    ];

    state = {
      locale: 'es',
      translations: Immutable.fromJS(dictionaries),
      user: Immutable.fromJS({ _id: 'abc' }),
    };

    spyOn(store, 'getState').and.returnValue(state);
  });

  it('should return the translation', () => {
    expect(
      t('System', 'confirmDeleteDocument', 'Are you sure you want to delete this document?')
    ).toBe('¿Esta seguro que quiere borrar este documento?');
  });

  describe('when there is no translation', () => {
    it('should return the default text', () => {
      expect(
        t('System', 'confirmDeleteEntity', 'Are you sure you want to delete this entity?')
      ).toBe('Are you sure you want to delete this entity?');
    });
  });

  describe('only passing context and key', () => {
    it('should use it as default text', () => {
      expect(t('System', 'not translated', 'not translated'));
    });
  });

  describe('when no context', () => {
    it('should throw an error', () => {
      spyOn(console, 'warn');
      t(undefined, 'confirmDeleteEntity', 'Are you sure you want to delete this entity?');
      //eslint-disable-next-line no-console
      expect(console.warn).toHaveBeenCalled();
    });
  });
});