Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow find to work on both Pascal case and camel case #1398

Merged
merged 8 commits into from
Jan 13, 2020

Conversation

lmiller1990
Copy link
Member

resolves #1232

I think it's fine to support finding a component named TestComponent with test-component and vice versa, or event testComponent - especially for third party packages, it can be hard to know the name of a component. Just makes find a bit more forgiving and improves the DX with no real downside.

I forked from #1396, will retarget once that is merged. I love the new workflow (run tests against src).

@lmiller1990 lmiller1990 changed the base branch from dev to feat/allow-testing-src-directly January 9, 2020 13:24
@lmiller1990 lmiller1990 changed the base branch from feat/allow-testing-src-directly to dev January 10, 2020 07:20

export function vmMatchesName(vm, name) {
function vmMatchesName(vm, name) {
console.log(name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some debugging stuff over here! ;)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want a lint rule for no console log? We do this at work. Same for debugger statements

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its OK to have one yeah :) With the new linting on commit, it will warn you.

vm.$options && vm.$options.name === capitalize(name) ||
vm.$options && vm.$options.name && vm.$options.name === capitalize(camelize(name)) ||
vm.$options && vm.$options.name && capitalize(camelize(vm.$options.name)) === name
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a proposal, lets save the vm.$options.name to a variable, something like

const componentName = vm.$options && vm.$options.name || ''

then we can easily reduce those those lines. Yes it will use a bit more memory, but for readability sake I think it makes sense?
Also that line 18 has an extra check others dont.
Unrelated:
You know whats the diff between vm.name and vm.$options.name? One is the one you set in its options and the other is the one that it is registered by?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add comments for each of the conditionals? Eg what use case each line is handling? A 7 line if statement is hard to grok easily.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep agreed, I'll make these changes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dobromir-hristov I am not sure on the difference between vm.name and vm.$options.name. vm.name is undefined in every test... I removed it and everything is still passing. If it serves no purpose, we may as well omit that check. What do you think?

We should check the source code for Vue and see if we can figure out the difference.

Regarding your comment about the extra variable - I agree, that is more readable. I updated it the code.

@JessicaSachs I added some comments explaining why we do each of the matches. What do you think? If you think you can make the comment clearer, you can add some commits or I can add some if you leave an updated comment here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also gave a quick search and did not see vm.name defined. Its not in the Vue types as well.
Wonder if its something from "old days"?
New code is much more readable, gj

Copy link
Collaborator

@JessicaSachs JessicaSachs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments. They’re mostly minor nits.

@dobromir-hristov dobromir-hristov self-requested a review January 12, 2020 14:06
export function vmMatchesName(vm, name) {
function vmMatchesName(vm, name) {
// We want to mirror how Vue resolves component names in SFCs:
// For example, <test-component />, <TestComponent /> and `<testComponent />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// For example, <test-component />, <TestComponent /> and `<testComponent />
// For example, <test-component />, <TestComponent /> and <testComponent />

expect(wrapper.find({ name: 'camel-case' }).name()).to.equal('CamelCase')
})

it('returns a Wrapper matching a Pascal Case name option and a kebab-casecomponent name ', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('returns a Wrapper matching a Pascal Case name option and a kebab-casecomponent name ', () => {
it('returns a Wrapper matching a PascalCase name option and a kebab-case component name', () => {

expect(wrapper.find({ name: 'camelCase' }).name()).to.equal('CamelCase')
})

it('returns a Wrapper matching a kebab-case name option and a Pascal Case component name ', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('returns a Wrapper matching a kebab-case name option and a Pascal Case component name ', () => {
it('returns a Wrapper matching a kebab-case name option and a PascalCase component name', () => {

@@ -431,6 +431,33 @@ describeWithShallowAndMount('find', mountingMethod => {
)
})

it('returns a Wrapper matching a camelCase name option and a Pascal Case component name ', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('returns a Wrapper matching a camelCase name option and a Pascal Case component name ', () => {
it('returns a Wrapper matching a camelCase name option and a PascalCase component name', () => {

@lmiller1990 lmiller1990 merged commit 5569325 into dev Jan 13, 2020
@lmiller1990 lmiller1990 deleted the feature/issue-1232 branch January 13, 2020 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vue-test-utils can not resolve PascalCase names of components
5 participants