Skip to content

Commit

Permalink
⭐ new: support vue 2.0.0.beta later
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Jul 24, 2016
1 parent 8dc9086 commit 0e1d2f7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 63 deletions.
46 changes: 27 additions & 19 deletions test/specs/component.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import assert from 'power-assert'
import Vue from 'vue'
import locales from './fixture/locales'
import compare from '../../src/compare'


describe('component locales', () => {
const version = Number(Vue.version.split('.')[0])

before(done => {
Object.keys(locales).forEach(lang => {
Vue.locale(lang, locales[lang])
Expand All @@ -15,31 +16,37 @@ describe('component locales', () => {

let vm
beforeEach(done => {
const options = {
el: document.createElement('div'),
components: {
component1: {
locales: {
en: {
foo: {
bar: {
buz: 'hello world'
}
}
const el = document.createElement('div')
const compOptions = {
locales: {
en: {
foo: {
bar: {
buz: 'hello world'
}
}
}
}
}

if (compare(Vue.version, '2.0.0-alpha') < 0) {
options.template = '<div><component1></component1></div>'
if (version >= 2) {
compOptions.render = function (h) {
return h('p', {}, [this.$t('foo.bar.buz')])
}
} else {
options.render = function () {
return this.$createElement('div', {}, [
this.$createElement('component1', {})
])
compOptions.template = '<p>{{* $t("foo.bar.buz") }}</p>'
}

const options = {
el,
components: { component1: compOptions }
}

if (version >= 2) {
options.render = function (h) {
return h('div', {}, [h('component1', {})])
}
} else {
options.template = '<div><component1></component1></div>'
}

vm = new Vue(options)
Expand All @@ -50,6 +57,7 @@ describe('component locales', () => {
it('should be translated', () => {
const comp1 = vm.$children[0] // component1
assert(comp1.$t('foo.bar.buz') === 'hello world')
assert(comp1.$el.innerText === 'hello world')
})
})

Expand Down
86 changes: 42 additions & 44 deletions test/specs/i18n.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import assert from 'power-assert'
import Vue from 'vue'
import compare from '../../src/compare'
import locales from './fixture/locales'


describe('i18n', () => {
const version = Number(Vue.version.split('.')[0])

before(done => {
Object.keys(locales).forEach(lang => {
Vue.locale(lang, locales[lang])
Expand Down Expand Up @@ -200,36 +201,35 @@ describe('i18n', () => {


describe('reactive translation', () => {
let el
beforeEach(() => {
el = document.createElement('div')
document.body.appendChild(el)
})

it('should translate', done => {
const options = {
data: () => {
el,
data () {
return { lang: 'en' }
},
el: () => {
const el = document.createElement('div')
document.body.appendChild(el)
return el
}
}

if (compare(Vue.version, '2.0.0-alpha') < 0) {
options.template = '<p>{{ $t("message.hello", lang) }}</p>'
} else {
options.render = function () {
return this.$createElement('p', {}, [this.__toString__(this.$t('message.hello', this.lang))])
if (version >= 2) {
options.render = function (h) {
return h('p', {}, [this.$t('message.hello', this.lang)])
}
} else {
options.template = '<p>{{ $t("message.hello", lang) }}</p>'
}

const ViewModel = Vue.extend(options)
const vm = new ViewModel()

const el = vm.$el
const vm = new Vue(options)
Vue.nextTick(() => {
assert(el.textContent === locales.en.message.hello)
assert(vm.$el.textContent === locales.en.message.hello)

Vue.set(vm, 'lang', 'ja') // set japanese
vm.lang = 'ja' // set japanese
Vue.nextTick(() => {
assert(el.textContent === locales.ja.message.hello)
assert(vm.$el.textContent === locales.ja.message.hello)
done()
})
})
Expand All @@ -238,45 +238,43 @@ describe('i18n', () => {


describe('translate component', () => {
let el
beforeEach(() => {
el = document.createElement('div')
document.body.appendChild(el)
})

it('should translate', done => {
const compOptions = {}
if (compare(Vue.version, '2.0.0-alpha') < 0) {
compOptions.template = '<p>{{* $t("message.hoge") }}</p>'
} else {
compOptions.render = function () {
return this.$createElement('p', {}, [this.__toString__(this.$t('message.hoge'))])
if (version >= 2) {
compOptions.render = function (h) {
return h('p', {}, [this.$t('message.hoge')])
}
} else {
compOptions.template = '<p>{{* $t("message.hoge") }}</p>'
}

const options = {
el: () => {
const el = document.createElement('div')
document.body.appendChild(el)
return el
},
el,
components: { hoge: compOptions }
}

if (compare(Vue.version, '2.0.0-alpha') < 0) {
options.template = '<div><p>{{ $t("message.hello") }}</p><hoge></hoge></div>'
} else {
options.render = function () {
return this.$createElement('div', {}, [
this.$createElement('p', {}, [this.__toString__(this.$t('message.hello'))]),
this.$createElement('hoge', {})
if (version >= 2) {
options.render = function (h) {
return h('div', {}, [
h('p', {}, [this.$t('message.hello')]),
h('hoge', {})
])
}
} else {
options.template = '<div><p>{{ $t("message.hello") }}</p><hoge></hoge></div>'
}

const ViewModel = Vue.extend(options)
const vm = new ViewModel()

const vm = new Vue(options)
Vue.nextTick(() => {
const child = vm.$el.children[1]
assert(child.textContent === locales.en.message.hoge)

const parent = vm.$el.children[0]
assert(parent.textContent === locales.en.message.hello)
const children = vm.$el.querySelectorAll('p')
assert(children[0].innerText === locales.en.message.hello)
assert(children[1].innerText === locales.en.message.hoge)

done()
})
Expand Down

0 comments on commit 0e1d2f7

Please sign in to comment.