-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from fringers/silent-on-production-flag
Silent on production flag
- Loading branch information
Showing
10 changed files
with
208 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Vue from 'vue'; | ||
import { printDeprecated } from '@/logger'; | ||
import { getComponentOptions } from '@/helpers'; | ||
|
||
export const checkComponent = (component: Vue) => { | ||
const options = getComponentOptions(component); | ||
if (!options || !options.deprecated) { | ||
return; | ||
} | ||
|
||
if (typeof options.deprecated === "string") { | ||
printDeprecated(options.deprecated); | ||
} else { | ||
printDeprecated('Component ' + options.name + ' is deprecated'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Vue from 'vue'; | ||
|
||
export const getComponentOptions = (component: Vue) => { | ||
if (!component.$vnode) { | ||
return; | ||
} | ||
|
||
return (component.$vnode.componentOptions?.Ctor as any).options; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const printDeprecated = (message: string) => { | ||
console.warn(`[DEPRECATED] ${message}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Vue from 'vue'; | ||
import { printDeprecated } from '@/logger'; | ||
import { getComponentOptions } from '@/helpers'; | ||
|
||
export const checkProps = (component: Vue) => { | ||
const options = getComponentOptions(component); | ||
if (!options || !options.props || typeof options.props !== 'object') { | ||
return; | ||
} | ||
|
||
const propsData = component.$options.propsData as any; | ||
if (!propsData) { | ||
return | ||
} | ||
|
||
Object.keys(options.props).forEach(key => { | ||
const value = options.props[key]; | ||
if (!propsData[key] || !value.deprecated) { | ||
return; | ||
} | ||
|
||
if (typeof value.deprecated === "string") { | ||
printDeprecated(value.deprecated); | ||
} else { | ||
printDeprecated(`Property ${key} in component ${options.name} is deprecated`); | ||
} | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { shallowMount, createLocalVue } from '@vue/test-utils'; | ||
import VueDeprecate from '@/deprecate'; | ||
|
||
describe('Options', () => { | ||
|
||
const deprecatedComponent = { | ||
name: 'DeprecatedComponent', | ||
deprecated: true, | ||
props: { | ||
deprecatedProp: { deprecated: true }, | ||
}, | ||
template: '<div>this is component</div>' | ||
}; | ||
|
||
// TODO: fix types | ||
function mountWithPluginSpy(component: any, options?: any) { | ||
const installSpy = jest.spyOn(VueDeprecate, 'install'); | ||
|
||
const localVue = createLocalVue() | ||
localVue.use(VueDeprecate, options); | ||
|
||
shallowMount(component, { | ||
localVue, | ||
propsData: { | ||
deprecatedProp: 'some value', | ||
} | ||
}); | ||
|
||
expect(installSpy).toHaveBeenCalled(); | ||
} | ||
|
||
describe('enabledOnProduction', () => { | ||
const OLD_ENV = process.env; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
process.env = { ...OLD_ENV }; | ||
delete process.env.NODE_ENV; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = OLD_ENV; | ||
}); | ||
|
||
it('log warnings if no options and not production', () => { | ||
const warnMock = jest.fn(); | ||
console.warn = warnMock; | ||
|
||
mountWithPluginSpy(deprecatedComponent); | ||
|
||
expect(warnMock).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('log warnings if other options and not production', () => { | ||
const warnMock = jest.fn(); | ||
console.warn = warnMock; | ||
|
||
mountWithPluginSpy(deprecatedComponent, { | ||
someProp: 'value', | ||
}); | ||
|
||
expect(warnMock).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('log warnings if enabledOnProduction=true and not production', () => { | ||
const warnMock = jest.fn(); | ||
console.warn = warnMock; | ||
|
||
mountWithPluginSpy(deprecatedComponent, { | ||
enabledOnProduction: true, | ||
}); | ||
|
||
expect(warnMock).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('log warnings if enabledOnProduction=false and not production', () => { | ||
const warnMock = jest.fn(); | ||
console.warn = warnMock; | ||
|
||
mountWithPluginSpy(deprecatedComponent, { | ||
enabledOnProduction: false, | ||
}); | ||
|
||
expect(warnMock).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('no warnings if no options and production', () => { | ||
const warnMock = jest.fn(); | ||
console.warn = warnMock; | ||
|
||
process.env.NODE_ENV = 'production'; | ||
mountWithPluginSpy(deprecatedComponent); | ||
|
||
expect(warnMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('no warnings if other options and production', () => { | ||
const warnMock = jest.fn(); | ||
console.warn = warnMock; | ||
|
||
process.env.NODE_ENV = 'production'; | ||
mountWithPluginSpy(deprecatedComponent, { | ||
someProp: 'value', | ||
}); | ||
|
||
expect(warnMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('log warnings if enabledOnProduction=true and production', () => { | ||
const warnMock = jest.fn(); | ||
console.warn = warnMock; | ||
|
||
process.env.NODE_ENV = 'production'; | ||
mountWithPluginSpy(deprecatedComponent, { | ||
enabledOnProduction: true, | ||
}); | ||
|
||
expect(warnMock).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('no warnings if enabledOnProduction=false and production', () => { | ||
const warnMock = jest.fn(); | ||
console.warn = warnMock; | ||
|
||
process.env.NODE_ENV = 'production'; | ||
mountWithPluginSpy(deprecatedComponent, { | ||
enabledOnProduction: false, | ||
}); | ||
|
||
expect(warnMock).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters