-
Notifications
You must be signed in to change notification settings - Fork 278
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
cleanup app-link for tests #4526
Changes from all commits
21caa76
2886218
ab8e534
ac325f1
d806089
e313e43
76d140a
2de7d08
e13d1af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,8 @@ | |
</script> | ||
<script> | ||
Vue.component('app-link', { | ||
name: 'app-link', | ||
template: '#app-link-template', | ||
store, | ||
props: { | ||
to: [String, Object], | ||
href: String, | ||
|
@@ -22,80 +23,110 @@ Vue.component('app-link', { | |
placeholder: { | ||
type: String, | ||
default: 'indexer-to-name' | ||
} | ||
}, | ||
base: String | ||
}, | ||
computed: { | ||
config() { | ||
return this.$store.state.config; | ||
}, | ||
indexerName() { | ||
const { config, indexerId } = this; | ||
const { indexers } = config.indexers.config; | ||
if (!indexerId) return undefined; | ||
// Returns `undefined` if not found | ||
return Object.keys(indexers).find(indexer => indexers[indexer].id === parseInt(indexerId, 10)); | ||
}, | ||
computedBase() { | ||
// Return prop before HTML element to allow tests to mock | ||
if (this.base) { | ||
return this.base; | ||
} | ||
|
||
return document.getElementsByTagName('base')[0].getAttribute('href'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see any problem with doing this: const { base } = this;
// Return prop before HTML element to allow tests to mock
return this.base || document.getElementsByTagName('base')[0].getAttribute('href'); It's just a personal preference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to make sure document isn't ever called since it'll throw an error in the tests. I know how bad that sounds but I'll fix it once I move this to a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK, JavaScript logical statements are evaluated from left to right and not all at once. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not that. It's the testing framework seeing it in the AST that's the issue. |
||
}, | ||
computedHref() { | ||
const { href, indexerId, placeholder, indexerName } = this; | ||
if (indexerId && placeholder) { | ||
return href.replace(placeholder, indexerName); | ||
} | ||
return href; | ||
}, | ||
isIRC() { | ||
if (!this.computedHref) return; | ||
return this.computedHref.startsWith('irc://'); | ||
}, | ||
isAbsolute() { | ||
const href = this.computedHref; | ||
if (!href) return; | ||
return /^[a-z][a-z0-9+.-]*:/.test(href); | ||
}, | ||
isExternal() { | ||
const base = this.computedBase; | ||
const href = this.computedHref; | ||
if (!href) return; | ||
return !href.startsWith(base) && !href.startsWith('webcal://'); | ||
}, | ||
isHashPath() { | ||
if (!this.computedHref) return; | ||
return this.computedHref.startsWith('#'); | ||
}, | ||
anonymisedHref() { | ||
const { anonRedirect } = this.config; | ||
const href = this.computedHref; | ||
if (!href) return; | ||
return anonRedirect ? anonRedirect + href : href; | ||
}, | ||
linkProperties() { | ||
const {to, indexerId, placeholder} = this; | ||
const href = indexerId && placeholder ? this.href.replace(placeholder, MEDUSA.config.indexers.indexerIdToName(indexerId)) : this.href; | ||
const base = document.getElementsByTagName('base')[0].getAttribute('href'); | ||
const isIRC = url => { | ||
return url.startsWith('irc://'); | ||
}; | ||
const isAbsolute = url => { | ||
return /^[a-z][a-z0-9+.-]*:/.test(url); | ||
const { to, isIRC, isAbsolute, isExternal, isHashPath, isAnonymised, anonymisedHref } = this; | ||
const base = this.computedBase; | ||
const href = this.computedHref; | ||
|
||
// Return normal router-link | ||
if (to) { | ||
return { | ||
is: 'router-link', | ||
to: { name: href } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side note... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How did you test this? The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When on the help and info page: <app-link to="not-found">Not Found</app-link>
<app-link to="config">Help & Info</app-link> And the elements I got in the DOM are: <a class="router-link-exact-active router-link-active">Not Found</a>
<a class="router-link-exact-active router-link-active">Help & Info</a> It doesn't work even when I put the links inside the routes, like in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
<router-link :to="{ name: 'not-found' }">Not Found</router-link>
<router-link :to="{ name: 'config' }">Help & Info</router-link> <a href="/not-found" class="">Not Found</a>
<a href="/config" class="router-link-exact-active router-link-active">Help & Info</a> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Just tried that, doesn't seem to work still.
If we can't figure it out, then yes... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you check what the AppLink's computed props were? This is why we need tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we merge and then fix this in the test PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, the app link component is broken on this branch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll try and fix this another time. Fixed the other errors. |
||
}; | ||
} | ||
const isExternal = url => { | ||
return !url.startsWith(base) && !url.startsWith('webcal://'); | ||
}; | ||
const isHashPath = url => { | ||
return url.startsWith('#') | ||
}; | ||
const isAnonymised = url => { | ||
return MEDUSA.config.anonRedirect && url.startsWith(MEDUSA.config.anonRedirect); | ||
}; | ||
const anonymise = url => MEDUSA.config.anonRedirect ? MEDUSA.config.anonRedirect + url : url; | ||
if (!to) { | ||
if (href) { | ||
// @TODO: Remove this once we move to vue only | ||
if (isAnonymised(href)) { | ||
throw new Error('Still using anon_url in Python!'); | ||
} | ||
const resolvedHref = () => { | ||
if (isHashPath(href)) { | ||
const {location} = window; | ||
if (location.hash.length === 0) { | ||
// current location might be `url#` | ||
const newHash = location.href.endsWith('#') ? href.substr(1) : href; | ||
return location.href + newHash; | ||
} | ||
return location.href.replace(location.hash, '') + href; | ||
} | ||
if (isIRC(href)) { | ||
return href; | ||
} | ||
if (isAbsolute(href)) { | ||
if (isExternal(href)) { | ||
return anonymise(href) | ||
} else { | ||
return href; | ||
} | ||
} else { | ||
return new URL(href, base).href | ||
} | ||
}; | ||
return { | ||
is: 'a', | ||
target: isAbsolute(href) && isExternal(href) ? '_blank' : '_self', | ||
href: resolvedHref(), | ||
rel: isAbsolute(href) && isExternal(href) ? 'noreferrer' : undefined | ||
}; | ||
} | ||
|
||
// Just return a boring link with other attrs | ||
// @NOTE: This is for scroll achors as it uses the id | ||
// Just return a boring link with other attrs | ||
// @NOTE: This is for scroll achors as it uses the id | ||
if (!href) { | ||
return { | ||
is: 'a', | ||
falseLink: true | ||
}; | ||
} | ||
|
||
return { | ||
is: 'router-link', | ||
to: { name: href } | ||
is: 'a', | ||
target: isAbsolute && isExternal ? '_blank' : '_self', | ||
href: (() => { | ||
if (isHashPath) { | ||
const { location } = window; | ||
if (location.hash.length === 0) { | ||
// Current location might be `url#` | ||
const newHash = location.href.endsWith('#') ? href.substr(1) : href; | ||
return location.href + newHash; | ||
} | ||
return location.href.replace(location.hash, '') + href; | ||
} | ||
if (isIRC) { | ||
return href; | ||
} | ||
if (isAbsolute) { | ||
if (isExternal) { | ||
return anonymisedHref; | ||
} | ||
return href; | ||
} | ||
return new URL(href, base).href; | ||
})(), | ||
rel: isAbsolute && isExternal ? 'noreferrer' : undefined | ||
}; | ||
} | ||
}, | ||
template: `#app-link-template` | ||
} | ||
}); | ||
</script> | ||
<style> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that for future usage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for future usage.