Skip to content

Commit

Permalink
I18n tests (elastic#20306) (elastic#21638)
Browse files Browse the repository at this point in the history
* Add unit-tests for i18n engine

* tests for angular wrappers under i18n engine

* split to packages in accordance to specific library

* split to packages depending on tech and environment

* make env modules names consistent

* remove intl polyfilling

* move laoder to root, i18n folder to core

* Add unit-tests for i18n engine

* tests for angular wrappers under i18n engine

* rebase on split package, injectIntl component

* eslint fixes

* use method names for checking, test i18n-values in directive

* use real html content while testing directive

* add test for angular directive without values
  • Loading branch information
LeanidShutau authored Aug 3, 2018
1 parent 9852e6b commit 1f7c1bc
Show file tree
Hide file tree
Showing 17 changed files with 1,719 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"a.b.c": "bar",
"d.e.f": "foo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"a.b.c": "foo",
"d.e.f": "bar"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"a.b.c.custom": "foo.custom",
"d.e.f.custom": "bar.custom"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
test: 'test' // JSON5 test
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test": "test"
}
5 changes: 5 additions & 0 deletions packages/kbn-i18n/src/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`I18n loader registerTranslationFile should throw error if path to translation file is not an absolute 1`] = `"Paths to translation files must be absolute. Got relative path: \\"./en.json\\""`;

exports[`I18n loader registerTranslationFile should throw error if path to translation file is not specified 1`] = `"Path must be a string. Received undefined"`;
77 changes: 77 additions & 0 deletions packages/kbn-i18n/src/angular/directive.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import angular from 'angular';
import 'angular-mocks';
import { i18nDirective } from './directive';
import { i18nProvider } from './provider';

angular
.module('app', [])
.provider('i18n', i18nProvider)
.directive('i18nId', i18nDirective);

describe('i18nDirective', () => {
let compile;
let scope;

beforeEach(angular.mock.module('app'));
beforeEach(
angular.mock.inject(($compile, $rootScope) => {
compile = $compile;
scope = $rootScope.$new();
})
);

it('inserts correct translation html content', () => {
const id = 'id';
const defaultMessage = 'default-message';

const element = angular.element(
`<div
i18n-id="${id}"
i18n-default-message="${defaultMessage}"
/>`
);

compile(element)(scope);
scope.$digest();

expect(element.html()).toEqual(defaultMessage);
});

it('inserts correct translation html content with values', () => {
const id = 'id';
const defaultMessage = 'default-message {word}';
const compiledContent = 'default-message word';

const element = angular.element(
`<div
i18n-id="${id}"
i18n-default-message="${defaultMessage}"
i18n-values="{ word: 'word' }"
/>`
);

compile(element)(scope);
scope.$digest();

expect(element.html()).toEqual(compiledContent);
});
});
59 changes: 59 additions & 0 deletions packages/kbn-i18n/src/angular/filter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import angular from 'angular';
import 'angular-mocks';
import { i18nProvider } from './provider';
import { i18nFilter } from './filter';
import * as i18n from '../core/i18n';

jest.mock('../core/i18n', () => ({
translate: jest.fn().mockImplementation(() => 'translation'),
}));

angular
.module('app', [])
.provider('i18n', i18nProvider)
.filter('i18n', i18nFilter);

describe('i18nFilter', () => {
let filter;

beforeEach(angular.mock.module('app'));
beforeEach(
angular.mock.inject(i18nFilter => {
filter = i18nFilter;
})
);
afterEach(() => {
jest.resetAllMocks();
});

it('provides wrapper around i18n engine', () => {
const id = 'id';
const defaultMessage = 'default-message';
const values = {};

const result = filter(id, { defaultMessage, values });

expect(i18n.translate).toHaveBeenCalledTimes(1);
expect(i18n.translate).toHaveBeenCalledWith(id, { defaultMessage, values });
expect(result).toEqual('translation');
});
});
57 changes: 57 additions & 0 deletions packages/kbn-i18n/src/angular/provider.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import angular from 'angular';
import 'angular-mocks';
import { i18nProvider } from './provider';
import * as i18n from '../core/i18n';

angular.module('app', []).provider('i18n', i18nProvider);

describe('i18nProvider', () => {
let provider;
let service;

beforeEach(
angular.mock.module('app', [
'i18nProvider',
i18n => {
service = i18n;
},
])
);
beforeEach(
angular.mock.inject(i18n => {
provider = i18n;
})
);

it('provides wrapper around i18n engine', () => {
expect(provider).toEqual(i18n.translate);
});

it('provides service wrapper around i18n engine', () => {
const serviceMethodNames = Object.keys(service);
const pluginMethodNames = Object.keys(i18n);

expect([...serviceMethodNames, 'translate'].sort()).toEqual(
[...pluginMethodNames, '$get'].sort()
);
});
});
49 changes: 49 additions & 0 deletions packages/kbn-i18n/src/core/__snapshots__/i18n.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`I18n engine addMessages should throw error if locale is not specified or empty 1`] = `"[I18n] A \`locale\` must be a non-empty string to add messages."`;

exports[`I18n engine addMessages should throw error if locale is not specified or empty 2`] = `"[I18n] A \`locale\` must be a non-empty string to add messages."`;

exports[`I18n engine addMessages should throw error if locale specified in messages is different from one provided as second argument 1`] = `"[I18n] A \`locale\` in the messages object is different from the one provided as a second argument."`;

exports[`I18n engine translate should throw error if id is not a non-empty string 1`] = `"[I18n] An \`id\` must be a non-empty string to translate a message."`;

exports[`I18n engine translate should throw error if id is not a non-empty string 2`] = `"[I18n] An \`id\` must be a non-empty string to translate a message."`;

exports[`I18n engine translate should throw error if id is not a non-empty string 3`] = `"[I18n] An \`id\` must be a non-empty string to translate a message."`;

exports[`I18n engine translate should throw error if id is not a non-empty string 4`] = `"[I18n] An \`id\` must be a non-empty string to translate a message."`;

exports[`I18n engine translate should throw error if id is not a non-empty string 5`] = `"[I18n] An \`id\` must be a non-empty string to translate a message."`;

exports[`I18n engine translate should throw error if id is not a non-empty string 6`] = `"[I18n] An \`id\` must be a non-empty string to translate a message."`;

exports[`I18n engine translate should throw error if translation message and defaultMessage are not provided 1`] = `"[I18n] Cannot format message: \\"foo\\". Default message must be provided."`;

exports[`I18n engine translate should throw error if used format is not specified 1`] = `
"[I18n] Error formatting message: \\"a.b.c\\" for locale: \\"en\\".
SyntaxError: Expected \\"date\\", \\"number\\", \\"plural\\", \\"select\\", \\"selectordinal\\" or \\"time\\" but \\"f\\" found."
`;

exports[`I18n engine translate should throw error if used format is not specified 2`] = `
"[I18n] Error formatting the default message for: \\"d.e.f\\".
SyntaxError: Expected \\"date\\", \\"number\\", \\"plural\\", \\"select\\", \\"selectordinal\\" or \\"time\\" but \\"b\\" found."
`;

exports[`I18n engine translate should throw error if wrong context is provided to the translation string 1`] = `
"[I18n] Error formatting message: \\"a.b.c\\" for locale: \\"en\\".
Error: The intl string context variable 'numPhotos' was not provided to the string 'You have {numPhotos, plural,
=0 {no photos.}
=1 {one photo.}
other {# photos.}
}'"
`;

exports[`I18n engine translate should throw error if wrong context is provided to the translation string 2`] = `
"[I18n] Error formatting the default message for: \\"d.e.f\\".
Error: The intl string context variable 'numPhotos' was not provided to the string 'You have {numPhotos, plural,
=0 {no photos.}
=1 {one photo.}
other {# photos.}
}'"
`;
Loading

0 comments on commit 1f7c1bc

Please sign in to comment.