mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-18 14:41:25 +00:00
fix(app): Throw error only if value is undefined (#4206)
This commit is contained in:
parent
1adad4676b
commit
208eba3867
@ -130,7 +130,7 @@ async function createApp(ssrContext) {
|
|||||||
<% if (plugins.length) { %>
|
<% if (plugins.length) { %>
|
||||||
const inject = function (key, value) {
|
const inject = function (key, value) {
|
||||||
if (!key) throw new Error('inject(key, value) has no key provided')
|
if (!key) throw new Error('inject(key, value) has no key provided')
|
||||||
if (!value) throw new Error('inject(key, value) has no value provided')
|
if (typeof value === 'undefined') throw new Error('inject(key, value) has no value provided')
|
||||||
key = '$' + key
|
key = '$' + key
|
||||||
// Add into app
|
// Add into app
|
||||||
app[key] = value
|
app[key] = value
|
||||||
|
3
test/fixtures/basic/nuxt.config.js
vendored
3
test/fixtures/basic/nuxt.config.js
vendored
@ -63,7 +63,8 @@ export default {
|
|||||||
transition: false,
|
transition: false,
|
||||||
plugins: [
|
plugins: [
|
||||||
'~/plugins/vuex-module',
|
'~/plugins/vuex-module',
|
||||||
'~/plugins/dir-plugin'
|
'~/plugins/dir-plugin',
|
||||||
|
'~/plugins/inject'
|
||||||
],
|
],
|
||||||
build: {
|
build: {
|
||||||
scopeHoisting: true,
|
scopeHoisting: true,
|
||||||
|
16
test/fixtures/basic/plugins/inject.js
vendored
Normal file
16
test/fixtures/basic/plugins/inject.js
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
export default ({ route, params }, inject) => {
|
||||||
|
const { injectValue } = route.query
|
||||||
|
if (typeof injectValue === 'undefined') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const key = 'injectedProperty'
|
||||||
|
const map = {
|
||||||
|
'undefined': undefined,
|
||||||
|
'null': null,
|
||||||
|
'false': false,
|
||||||
|
'0': 0,
|
||||||
|
'empty': ''
|
||||||
|
}
|
||||||
|
const value = map[injectValue]
|
||||||
|
inject(key, value)
|
||||||
|
}
|
@ -18,6 +18,22 @@ describe('with-config', () => {
|
|||||||
expect(window.__test_plugin).toBe(true)
|
expect(window.__test_plugin).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('inject fails if value is undefined', async () => {
|
||||||
|
// inject('injectedProperty', undefined)
|
||||||
|
await expect(nuxt.renderRoute('/?injectValue=undefined')).rejects.toThrowError('inject(key, value) has no value provided')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('inject succeeds if value is defined but evaluates to false', async () => {
|
||||||
|
// inject('injectedProperty', null)
|
||||||
|
await expect(nuxt.renderRoute('/?injectValue=null')).resolves.not.toThrowError()
|
||||||
|
// inject('injectedProperty', false)
|
||||||
|
await expect(nuxt.renderRoute('/?injectValue=false')).resolves.not.toThrowError()
|
||||||
|
// inject('injectedProperty', 0)
|
||||||
|
await expect(nuxt.renderRoute('/?injectValue=0')).resolves.not.toThrowError()
|
||||||
|
// inject('injectedProperty', '')
|
||||||
|
await expect(nuxt.renderRoute('/?injectValue=empty')).resolves.not.toThrowError()
|
||||||
|
})
|
||||||
|
|
||||||
// Close server and ask nuxt to stop listening to file changes
|
// Close server and ask nuxt to stop listening to file changes
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await nuxt.close()
|
await nuxt.close()
|
||||||
|
Loading…
Reference in New Issue
Block a user