|
1 | 1 | describe('silent', () => { |
2 | | - it('should be suppressed translate warnings', () => { |
3 | | - const vm = new Vue({ |
4 | | - i18n: new VueI18n({ |
5 | | - locale: 'en', |
6 | | - silentTranslationWarn: true, |
| 2 | + let spy |
| 3 | + beforeEach(() => { |
| 4 | + spy = sinon.spy(console, 'warn') |
| 5 | + }) |
| 6 | + afterEach(() => { |
| 7 | + spy.restore() |
| 8 | + }) |
| 9 | + |
| 10 | + describe('silentTranslationWarn', () => { |
| 11 | + it('should be suppressed translate warnings', () => { |
| 12 | + const vm = new Vue({ |
| 13 | + i18n: new VueI18n({ |
| 14 | + locale: 'en', |
| 15 | + silentTranslationWarn: true, |
| 16 | + messages: { |
| 17 | + en: { who: 'root' }, |
| 18 | + ja: { who: 'ルート' } |
| 19 | + } |
| 20 | + }) |
| 21 | + }) |
| 22 | + |
| 23 | + vm.$t('foo.bar.buz') |
| 24 | + assert(spy.notCalled === true) |
| 25 | + |
| 26 | + // change |
| 27 | + vm.$i18n.silentTranslationWarn = false |
| 28 | + vm.$t('foo.bar.buz') |
| 29 | + assert(spy.callCount === 2) |
| 30 | + }) |
| 31 | + }) |
| 32 | + |
| 33 | + describe('silentFallbackWarn', () => { |
| 34 | + let i18n |
| 35 | + beforeEach(() => { |
| 36 | + i18n = new VueI18n({ |
| 37 | + locale: 'hu', |
| 38 | + fallbackLocale: 'en', |
| 39 | + silentFallbackWarn: true, |
7 | 40 | messages: { |
8 | | - en: { who: 'root' }, |
9 | | - ja: { who: 'ルート' } |
| 41 | + en: { winner: 'winner' }, |
| 42 | + hu: { chickenDinner: 'csirkevacsora' } |
10 | 43 | } |
11 | 44 | }) |
12 | 45 | }) |
13 | 46 |
|
14 | | - const spy = sinon.spy(console, 'warn') |
15 | | - vm.$t('foo.bar.buz') |
16 | | - assert(spy.notCalled === true) |
| 47 | + it('should suppress `Fall back to ${fallback} locale` warnings', () => { |
| 48 | + const vm = new Vue({ i18n }) |
| 49 | + const warningRegex = /Fall back to .* 'en' locale./ |
| 50 | + vm.$t('winner') |
| 51 | + assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === false) |
17 | 52 |
|
18 | | - // change |
19 | | - vm.$i18n.silentTranslationWarn = false |
20 | | - vm.$t('foo.bar.buz') |
21 | | - assert(spy.callCount === 2) |
| 53 | + vm.$i18n.silentFallbackWarn = false |
| 54 | + vm.$t('winner') |
| 55 | + assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === true) |
| 56 | + }) |
22 | 57 |
|
23 | | - spy.restore() |
| 58 | + it('should suppress `Fall back to root locale` warnings.', () => { |
| 59 | + const el = document.createElement('div') |
| 60 | + const root = new Vue({ |
| 61 | + i18n, |
| 62 | + components: { |
| 63 | + subComponent: { |
| 64 | + i18n: { messages: { hu: { name: 'Név' } } }, |
| 65 | + render (h) { return h('p') } |
| 66 | + } |
| 67 | + }, |
| 68 | + render (h) { return h('sub-component') } |
| 69 | + }).$mount(el) |
| 70 | + const vm = root.$children[0] |
| 71 | + const warningRegex = /Fall back to .* root locale./ |
| 72 | + |
| 73 | + vm.$t('chickenDinner') |
| 74 | + assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === false) |
| 75 | + |
| 76 | + vm.$i18n.silentFallbackWarn = false |
| 77 | + vm.$t('chickenDinner') |
| 78 | + assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === true) |
| 79 | + }) |
| 80 | + |
| 81 | + describe('if first try is null or undefined,', () => { |
| 82 | + it('should suppress `not a string` warnings for fallback to fallbackLocale.', () => { |
| 83 | + const vm = new Vue({ i18n }) |
| 84 | + const warningRegex = /Value of .* is not a string./ |
| 85 | + vm.$t('winner') |
| 86 | + assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === false) |
| 87 | + |
| 88 | + vm.$i18n.silentFallbackWarn = false |
| 89 | + vm.$t('winner') |
| 90 | + assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === true) |
| 91 | + }) |
| 92 | + |
| 93 | + it('should supress `not a string` warnings for fallback to root.', () => { |
| 94 | + const el = document.createElement('div') |
| 95 | + const root = new Vue({ |
| 96 | + i18n, |
| 97 | + components: { |
| 98 | + subComponent: { |
| 99 | + i18n: { messages: { hu: { name: 'Név' } } }, |
| 100 | + render (h) { return h('p') } |
| 101 | + } |
| 102 | + }, |
| 103 | + render (h) { return h('sub-component') } |
| 104 | + }).$mount(el) |
| 105 | + const vm = root.$children[0] |
| 106 | + const warningRegex = /Value of .* is not a string./ |
| 107 | + vm.$t('chickenDinner') |
| 108 | + assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === false) |
| 109 | + |
| 110 | + vm.$i18n.silentFallbackWarn = false |
| 111 | + vm.$t('chickenDinner') |
| 112 | + assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === true) |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | + describe('if first try is not null, undefined, array, plain object or string,', () => { |
| 117 | + it('should suppress `not a string` warnings for fallback to fallbackLocale.', () => { |
| 118 | + const vm = new Vue({ |
| 119 | + i18n: new VueI18n({ |
| 120 | + locale: 'hu', |
| 121 | + fallbackLocale: 'en', |
| 122 | + silentFallbackWarn: true, |
| 123 | + messages: { |
| 124 | + en: { winner: 'winner' }, |
| 125 | + hu: { winner: true } // translation value is boolean |
| 126 | + } |
| 127 | + }) |
| 128 | + }) |
| 129 | + const warningRegex = /Value of .* is not a string./ |
| 130 | + vm.$t('winner') |
| 131 | + assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === false) |
| 132 | + |
| 133 | + vm.$i18n.silentFallbackWarn = false |
| 134 | + vm.$t('winner') |
| 135 | + assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === true) |
| 136 | + }) |
| 137 | + |
| 138 | + it('should supress `not a string` warnings for fallback to root.', () => { |
| 139 | + const el = document.createElement('div') |
| 140 | + const root = new Vue({ |
| 141 | + i18n, |
| 142 | + components: { |
| 143 | + subComponent: { |
| 144 | + i18n: { messages: { hu: { chickenDinner: 11 } } }, // translation value is number |
| 145 | + render (h) { return h('p') } |
| 146 | + } |
| 147 | + }, |
| 148 | + render (h) { return h('sub-component') } |
| 149 | + }).$mount(el) |
| 150 | + const vm = root.$children[0] |
| 151 | + const warningRegex = /Value of .* is not a string./ |
| 152 | + vm.$t('chickenDinner') |
| 153 | + assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === false) |
| 154 | + |
| 155 | + vm.$i18n.silentFallbackWarn = false |
| 156 | + vm.$t('chickenDinner') |
| 157 | + assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === true) |
| 158 | + }) |
| 159 | + }) |
| 160 | + |
| 161 | + it('should not suppress `not a string` warnings when no further fallback is possible.', () => { |
| 162 | + const vm = new Vue({ i18n }) |
| 163 | + const warningRegex = /Value of .* is not a string./ |
| 164 | + vm.$t('loser') |
| 165 | + assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === true) |
| 166 | + }) |
24 | 167 | }) |
25 | 168 | }) |
0 commit comments