From 62742d1f9eb10a850d6d550241337b7ecb560fd5 Mon Sep 17 00:00:00 2001 From: Zewa Date: Wed, 19 Aug 2015 21:03:29 +0200 Subject: [PATCH] feat(test): setup unit test structure for i18n attribute this sets up the necessary infrastructure to test the i18n attribute implementation --- dist/amd/base-i18n.js | 7 ++++++- dist/amd/t.js | 9 ++++++--- dist/commonjs/base-i18n.js | 7 ++++++- dist/commonjs/t.js | 9 +++++++-- dist/es6/base-i18n.js | 6 +++++- dist/es6/t.js | 3 +++ dist/system/base-i18n.js | 7 ++++++- dist/system/t.js | 13 +++++++++---- package.json | 4 ++-- src/base-i18n.js | 6 +++++- src/t.js | 3 +++ test/unit/i18n-attribute.spec.js | 4 ++-- test/unit/mocks/i18n.js | 5 +++++ 13 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 test/unit/mocks/i18n.js diff --git a/dist/amd/base-i18n.js b/dist/amd/base-i18n.js index 3433a6b..b5d7034 100644 --- a/dist/amd/base-i18n.js +++ b/dist/amd/base-i18n.js @@ -18,7 +18,7 @@ define(['exports', './i18n', 'aurelia-event-aggregator'], function (exports, _i1 this.i18n = i18n; this.element = element; - ea.subscribe('i18n:locale:changed', function (payload) { + this.__i18nDisposer = ea.subscribe('i18n:locale:changed', function (payload) { _this.i18n.updateTranslations(_this.element); }); } @@ -28,6 +28,11 @@ define(['exports', './i18n', 'aurelia-event-aggregator'], function (exports, _i1 value: function attached() { this.i18n.updateTranslations(this.element); } + }, { + key: 'detached', + value: function detached() { + this.__i18nDisposer(); + } }], [{ key: 'inject', value: [_i18n.I18N, Element, _aureliaEventAggregator.EventAggregator], diff --git a/dist/amd/t.js b/dist/amd/t.js index 649a18c..61f4bba 100644 --- a/dist/amd/t.js +++ b/dist/amd/t.js @@ -1,4 +1,4 @@ -define(['exports', './i18n'], function (exports, _i18n) { +define(['exports', './i18n', 'aurelia-templating'], function (exports, _i18n, _aureliaTemplating) { 'use strict'; var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }; @@ -35,13 +35,15 @@ define(['exports', './i18n'], function (exports, _i18n) { var TCustomAttribute = (function () { function TCustomAttribute(element, i18n) { - _classCallCheck(this, TCustomAttribute); + _classCallCheck(this, _TCustomAttribute); this.element = element; this.service = i18n; } - _createClass(TCustomAttribute, [{ + var _TCustomAttribute = TCustomAttribute; + + _createClass(_TCustomAttribute, [{ key: 'valueChanged', value: function valueChanged() { if (this.element.parentElement !== undefined) { @@ -54,6 +56,7 @@ define(['exports', './i18n'], function (exports, _i18n) { enumerable: true }]); + TCustomAttribute = _aureliaTemplating.customAttribute('t')(TCustomAttribute) || TCustomAttribute; return TCustomAttribute; })(); diff --git a/dist/commonjs/base-i18n.js b/dist/commonjs/base-i18n.js index 05b376c..9cd8393 100644 --- a/dist/commonjs/base-i18n.js +++ b/dist/commonjs/base-i18n.js @@ -21,7 +21,7 @@ var BaseI18N = (function () { this.i18n = i18n; this.element = element; - ea.subscribe('i18n:locale:changed', function (payload) { + this.__i18nDisposer = ea.subscribe('i18n:locale:changed', function (payload) { _this.i18n.updateTranslations(_this.element); }); } @@ -31,6 +31,11 @@ var BaseI18N = (function () { value: function attached() { this.i18n.updateTranslations(this.element); } + }, { + key: 'detached', + value: function detached() { + this.__i18nDisposer(); + } }], [{ key: 'inject', value: [_I18N.I18N, Element, _EventAggregator.EventAggregator], diff --git a/dist/commonjs/t.js b/dist/commonjs/t.js index e5f9cf9..51866ac 100644 --- a/dist/commonjs/t.js +++ b/dist/commonjs/t.js @@ -10,6 +10,8 @@ Object.defineProperty(exports, '__esModule', { var _I18N = require('./i18n'); +var _customAttribute = require('aurelia-templating'); + var TValueConverter = (function () { function TValueConverter(i18n) { _classCallCheck(this, TValueConverter); @@ -36,13 +38,15 @@ exports.TValueConverter = TValueConverter; var TCustomAttribute = (function () { function TCustomAttribute(element, i18n) { - _classCallCheck(this, TCustomAttribute); + _classCallCheck(this, _TCustomAttribute); this.element = element; this.service = i18n; } - _createClass(TCustomAttribute, [{ + var _TCustomAttribute = TCustomAttribute; + + _createClass(_TCustomAttribute, [{ key: 'valueChanged', value: function valueChanged() { if (this.element.parentElement !== undefined) { @@ -55,6 +59,7 @@ var TCustomAttribute = (function () { enumerable: true }]); + TCustomAttribute = _customAttribute.customAttribute('t')(TCustomAttribute) || TCustomAttribute; return TCustomAttribute; })(); diff --git a/dist/es6/base-i18n.js b/dist/es6/base-i18n.js index 6f9ac0e..4de2c7d 100644 --- a/dist/es6/base-i18n.js +++ b/dist/es6/base-i18n.js @@ -9,7 +9,7 @@ export class BaseI18N { this.i18n = i18n; this.element = element; - ea.subscribe('i18n:locale:changed', payload => { + this.__i18nDisposer = ea.subscribe('i18n:locale:changed', payload => { this.i18n.updateTranslations(this.element); }); } @@ -17,4 +17,8 @@ export class BaseI18N { attached(){ this.i18n.updateTranslations(this.element); } + + detached() { + this.__i18nDisposer(); + } } diff --git a/dist/es6/t.js b/dist/es6/t.js index 3277dc3..75141d0 100644 --- a/dist/es6/t.js +++ b/dist/es6/t.js @@ -1,4 +1,6 @@ import {I18N} from './i18n'; +import {customAttribute} from 'aurelia-templating'; + export class TValueConverter { static inject() { return [I18N]; } @@ -11,6 +13,7 @@ export class TValueConverter { } } +@customAttribute('t') export class TCustomAttribute { static inject = [Element, I18N]; diff --git a/dist/system/base-i18n.js b/dist/system/base-i18n.js index ac71535..ea5138f 100644 --- a/dist/system/base-i18n.js +++ b/dist/system/base-i18n.js @@ -23,7 +23,7 @@ System.register(['./i18n', 'aurelia-event-aggregator'], function (_export) { this.i18n = i18n; this.element = element; - ea.subscribe('i18n:locale:changed', function (payload) { + this.__i18nDisposer = ea.subscribe('i18n:locale:changed', function (payload) { _this.i18n.updateTranslations(_this.element); }); } @@ -33,6 +33,11 @@ System.register(['./i18n', 'aurelia-event-aggregator'], function (_export) { value: function attached() { this.i18n.updateTranslations(this.element); } + }, { + key: 'detached', + value: function detached() { + this.__i18nDisposer(); + } }], [{ key: 'inject', value: [I18N, Element, EventAggregator], diff --git a/dist/system/t.js b/dist/system/t.js index 2956165..44b1a76 100644 --- a/dist/system/t.js +++ b/dist/system/t.js @@ -1,9 +1,11 @@ -System.register(['./i18n'], function (_export) { - var I18N, _classCallCheck, _createClass, TValueConverter, TCustomAttribute; +System.register(['./i18n', 'aurelia-templating'], function (_export) { + var I18N, customAttribute, _classCallCheck, _createClass, TValueConverter, TCustomAttribute; return { setters: [function (_i18n) { I18N = _i18n.I18N; + }, function (_aureliaTemplating) { + customAttribute = _aureliaTemplating.customAttribute; }], execute: function () { 'use strict'; @@ -38,13 +40,15 @@ System.register(['./i18n'], function (_export) { TCustomAttribute = (function () { function TCustomAttribute(element, i18n) { - _classCallCheck(this, TCustomAttribute); + _classCallCheck(this, _TCustomAttribute); this.element = element; this.service = i18n; } - _createClass(TCustomAttribute, [{ + var _TCustomAttribute = TCustomAttribute; + + _createClass(_TCustomAttribute, [{ key: 'valueChanged', value: function valueChanged() { if (this.element.parentElement !== undefined) { @@ -57,6 +61,7 @@ System.register(['./i18n'], function (_export) { enumerable: true }]); + TCustomAttribute = customAttribute('t')(TCustomAttribute) || TCustomAttribute; return TCustomAttribute; })(); diff --git a/package.json b/package.json index a031a83..127e3f8 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aurelia-i18next", - "version": "0.6.2", + "version": "0.7.0", "description": "An Aurelia-Wrapper for the library i18next", "keywords": [ "aurelia", @@ -51,11 +51,11 @@ "Intl.js": "github:andyearnshaw/Intl.js@^0.1.4", "aurelia-event-aggregator": "github:aurelia/event-aggregator@^0.7.0", "aurelia-loader-default": "github:aurelia/loader-default@^0.9.5", + "aurelia-templating": "github:aurelia/templating@^0.14.4", "i18next": "github:i18next/i18next@^1.9.0" }, "devDependencies": { "aurelia-dependency-injection": "github:aurelia/dependency-injection@^0.9.2", - "aurelia-templating": "github:aurelia/templating@^0.14.4", "babel": "npm:babel-core@^5.1.13", "babel-runtime": "npm:babel-runtime@^5.1.13", "core-js": "npm:core-js@^0.9.4", diff --git a/src/base-i18n.js b/src/base-i18n.js index 6f9ac0e..4de2c7d 100644 --- a/src/base-i18n.js +++ b/src/base-i18n.js @@ -9,7 +9,7 @@ export class BaseI18N { this.i18n = i18n; this.element = element; - ea.subscribe('i18n:locale:changed', payload => { + this.__i18nDisposer = ea.subscribe('i18n:locale:changed', payload => { this.i18n.updateTranslations(this.element); }); } @@ -17,4 +17,8 @@ export class BaseI18N { attached(){ this.i18n.updateTranslations(this.element); } + + detached() { + this.__i18nDisposer(); + } } diff --git a/src/t.js b/src/t.js index 3277dc3..75141d0 100644 --- a/src/t.js +++ b/src/t.js @@ -1,4 +1,6 @@ import {I18N} from './i18n'; +import {customAttribute} from 'aurelia-templating'; + export class TValueConverter { static inject() { return [I18N]; } @@ -11,6 +13,7 @@ export class TValueConverter { } } +@customAttribute('t') export class TCustomAttribute { static inject = [Element, I18N]; diff --git a/test/unit/i18n-attribute.spec.js b/test/unit/i18n-attribute.spec.js index 9504f86..02a041f 100644 --- a/test/unit/i18n-attribute.spec.js +++ b/test/unit/i18n-attribute.spec.js @@ -11,14 +11,14 @@ describe('testing i18n attributes', () => { }); - it('should raise value change on simple custom attribute', done => { + it('should raise value change on i18n custom attribute', done => { var i18nAttribute = BehaviorInstance.createForUnitTest(TCustomAttribute); spyOn(i18nAttribute, 'valueChanged'); i18nAttribute.value = 'foo'; setTimeout(() => { - expect(att.valueChanged).toHaveBeenCalledWith('foo', undefined); + expect(i18nAttribute.valueChanged).toHaveBeenCalledWith('foo', undefined); done(); }); }); diff --git a/test/unit/mocks/i18n.js b/test/unit/mocks/i18n.js new file mode 100644 index 0000000..22f91a3 --- /dev/null +++ b/test/unit/mocks/i18n.js @@ -0,0 +1,5 @@ +export class I18N { + updateTranslations() { + + } +}