"use strict";

var translate = require('./translate').translate;

var config = require('./config');

describe('translate', function () {
  beforeAll(function () {
    config.testConfig();
  });
  it('handles not translated', function () {
    expect(translate(undefined)).toBe('NOT-TRANSLATED');
    expect(translate('NOT-TRANSLATED:Some phrase')).toBe('Some phrase');
  });
  it('performs simple translation', function () {
    expect(translate('foo {num}', {
      num: 1
    })).toBe('foo 1');
  });
  it('performs service web style translation', function () {
    var conf = {
      one: 'You have # message',
      other: 'You have # messages'
    };
    expect(translate(conf, 1)).toBe('You have 1 message');
    expect(translate(conf, {
      count: 1
    })).toBe('You have 1 message');
    expect(translate(conf, {
      count: 10
    })).toBe('You have 10 messages');
  });
  it('fails service web style translation if arguments are bad', function () {
    var conf = {
      one: 'You have # message',
      other: 'You have # messages'
    };
    var error = 'You are using translate() to handle plural strings, args should be an object like {count:1} or a number.';
    expect(function () {
      translate(conf);
    }).toThrow(error);
    expect(function () {
      translate(conf, {});
    }).toThrow(error);
    expect(function () {
      translate({
        foo: 'bar'
      });
    }).toThrow('Object {"foo":"bar"} is not a valid message resource.');
  });
  it('handles branded translation', function () {
    beforeAll(function () {
      this.userLanguage = config.getConfig().userLanguage;
      this.brandId = config.getConfig().brandId;
    });
    var conf = {
      0: 'Default',
      1234: 'Custom'
    };
    expect(translate(conf)).toBe('Default');
    config.getConfig().brandId = 1234;
    expect(translate(conf)).toBe('Custom');
    afterAll(function () {
      config.getConfig().userLanguage = this.userLanguage;
      config.getConfig().brandId = this.brandId;
    });
  });
});