Skip to content
This repository has been archived by the owner on May 19, 2022. It is now read-only.

Commit

Permalink
refactor(directive): deprecate 'language' prop for directive
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiocro committed Jul 28, 2018
1 parent d8f5fba commit 5a4d082
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 12 deletions.
19 changes: 12 additions & 7 deletions src/directive.js
@@ -1,4 +1,6 @@
/* eslint-disable no-param-reassign, no-console, no-unused-vars */
/* eslint-disable no-param-reassign, no-unused-vars */

import { warn, deprecate } from './utils';

function equalLanguage(el, vnode) {
const vm = vnode.context;
Expand All @@ -21,7 +23,7 @@ function assert(vnode) {
const vm = vnode.context;

if (!vm.$i18n) {
console.log('No VueI18Next instance found in the Vue instance');
warn('No VueI18Next instance found in the Vue instance');
return false;
}

Expand Down Expand Up @@ -49,19 +51,22 @@ function t(el, binding, vnode) {

const { path, language, args } = parseValue(value);
if (!path && !language && !args) {
console.log('v-t: invalid value');
warn('v-t: invalid value');
return;
}

if (!path) {
console.log('v-t: `path` is required');
warn('v-t: "path" is required');
return;
}

if (language) {
deprecate(`v-t: "language" is deprecated.Use the "lng" property in args.
https://www.i18next.com/overview/configuration-options#configuration-options`);
}

const vm = vnode.context;
el.textContent = language
? vm.$i18n.i18next.getFixedT(language)(path, args)
: vm.$i18n.i18next.t(path, args);
el.textContent = vm.$i18n.i18next.t(path, { ...(language ? { lng: language } : {}), ...args });

el._i18nLanguage = vm.$i18n.i18next.language;
}
Expand Down
16 changes: 16 additions & 0 deletions src/utils.js
@@ -0,0 +1,16 @@

/* eslint-disable import/prefer-default-export */

export function log(message) {
if (typeof console !== 'undefined') {
console.warn(message); // eslint-disable-line no-console
}
}

export function warn(message) {
log(`[vue-i18next warn]: ${message}`);
}

export function deprecate(message) {
log(`[vue-i18next deprecated]: ${message}`);
}
9 changes: 4 additions & 5 deletions test/unit/directive.test.js
@@ -1,5 +1,4 @@
import sinon from 'sinon';
import { t } from '../../src/directive';

function nextTick() {
return new Promise(resolve => Vue.nextTick(resolve));
Expand Down Expand Up @@ -29,7 +28,7 @@ describe('directive', () => {
});
});

it('should if value is only a key', async () => {
it('if value is only a key', async () => {
const el = document.createElement('div');
const vm = new Vue({
i18n: vueI18Next,
Expand Down Expand Up @@ -214,7 +213,7 @@ describe('directive', () => {

it('value warning', async () => {
const el = document.createElement('div');
const spy = sinon.spy(console, 'log');
const spy = sinon.spy(console, 'warn');
new Vue({
i18n: vueI18Next,
render(h) {
Expand All @@ -241,7 +240,7 @@ describe('directive', () => {

it('path warning', async () => {
const el = document.createElement('div');
const spy = sinon.spy(console, 'log');
const spy = sinon.spy(console, 'warn');
new Vue({
i18n: vueI18Next,
render(h) {
Expand All @@ -268,7 +267,7 @@ describe('directive', () => {

it('vuei18Next instance warning', async () => {
const el = document.createElement('div');
const spy = sinon.spy(console, 'log');
const spy = sinon.spy(console, 'warn');
new Vue({
render(h) {
// <p ref="text" v-t="{ language: 'de', args: { name: 'Hans' } }"></p>
Expand Down
49 changes: 49 additions & 0 deletions test/unit/utils.test.js
@@ -0,0 +1,49 @@

import { deprecate, log, warn } from '../../src/utils';

describe('utils', () => {
describe('log', () => {
it('call console.warn if console is present', () => {
const spy = sinon.spy(console, 'warn');

log('test warning');
expect(spy.notCalled).to.equal(false);
expect(spy.callCount).to.equal(1);
spy.restore();
});

it('does nothing if console is not present', () => {
const spy = sinon.spy(console, 'warn');
const _console = window.console;

window.console = undefined;
log('test warning');
window.console = _console;

expect(spy.notCalled).to.equal(true);
spy.restore();
});
});

describe('warn', () => {
it('print error to console if called', () => {
const spy = sinon.spy(console, 'warn');

warn('warned');
expect(spy.notCalled).to.equal(false);
expect(spy.getCall(0).calledWith('[vue-i18next warn]: warned')).to.equal(true);
spy.restore();
});
});

describe('deprecate', () => {
it('print deprecation message to console if called', () => {
const spy = sinon.spy(console, 'warn');

deprecate('use something other');
expect(spy.notCalled).to.equal(false);
expect(spy.getCall(0).calledWith('[vue-i18next deprecated]: use something other')).to.equal(true);
spy.restore();
});
});
});

0 comments on commit 5a4d082

Please sign in to comment.