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

cleanup app-link for tests #4526

Merged
merged 9 commits into from
Jul 5, 2018
Merged

cleanup app-link for tests #4526

merged 9 commits into from
Jul 5, 2018

Conversation

OmgImAlexis
Copy link
Collaborator

@OmgImAlexis OmgImAlexis commented Jul 2, 2018

This will allow us to use Ava tests against this component once #4374 is merged.

Related #4519 see #4526 (comment)

return this.base;
}

return document.getElementsByTagName('base')[0].getAttribute('href');
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 .vue file.

Copy link
Contributor

@sharkykh sharkykh Jul 2, 2018

Choose a reason for hiding this comment

The 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.
Meaning if Boolean(this.base) is true, it will return that value. As long as you pass the base in the tests, should be good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 { indexerId, placeholder } = this;
if (indexerId && placeholder) {
return this.href.replace(placeholder, MEDUSA.config.indexers.indexerIdToName(indexerId));
Copy link
Contributor

Choose a reason for hiding this comment

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

Might as well destructure href from this (note the return statement too)
Also to make the switch later easier, I suggest destructuring config from MEDUSA (one level would suffice)

const { config } = MEDUSA;

isHashPath() {
return this.computedHref.startsWith('#');
},
isAnonymised() {
Copy link
Contributor

Choose a reason for hiding this comment

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

It can already be removed. No references remain (on href attributes in mako files).
I'll remove the last one which is the Donations link in another PR (I also changed it on #4519)
It didn't raise an error because it uses this method:

<app-link href="${app.DONATIONS_URL}" rel="noreferrer" onclick="window.open('${app.ANON_REDIRECT}' + this.href); return false;">

// @TODO: Remove this once we move to vue only
if (isAnonymised) {
throw new Error('Still using anon_url in Python!');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same, can be removed.

};
}
},
template: `#app-link-template`
template: '#app-link-template'
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 our style is to put the template property right below the component name. Let's do that :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We need to create a styles guide for things like this on the wiki.

@@ -13,6 +13,7 @@
<script>
Vue.component('app-link', {
name: 'app-link',
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is redundant, please remove it.

Copy link
Collaborator Author

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.

Copy link
Contributor

@sharkykh sharkykh Jul 2, 2018

Choose a reason for hiding this comment

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

Okay, then remove the one above it, which overrides the first argument to Vue.component.

},
computed: {
config() {
return this.$store.state.config;
},
Copy link
Contributor

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?

Copy link
Collaborator Author

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.

@sharkykh

This comment has been minimized.

if (config.indexers[indexer].id === parseInt(indexerId, 10)) {
return config.indexers[indexer].name;
}
})[0];
Copy link
Contributor

@sharkykh sharkykh Jul 3, 2018

Choose a reason for hiding this comment

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

  1. The keys of config.indexers don't contain the indexer names,
    it's config.indexers.config.indexers. see below [1].
    It's because of the MEDUSA.config structure. When we stop using it we should be able to fix that...
  2. This can use find instead of filter, and be much simpler.
    This array iteration is so confusing. We don't return the name, we return the key (which is the identifier).
    name=TVDBv2 | identifier=tvdb
const { indexers } = config.indexers.config; // This is weird, I know...
// Returns `undefined` if not found.
return Object.keys(indexers).find(indexer => indexers[indexer].id === parseInt(indexerId, 10));

[1]
{ store.state
  "config": {
    "indexers": {
      "config": {
        "indexers": {
          "tvdb": {
            "xemOrigin": "tvdb",
            "apiParams": {
              "useZip": true,
              "language": "en"
            },
            "showUrl": "https://www.thetvdb.com/dereferrer/series/",
            "id": 1,
            "icon": "thetvdb16.png",
            "name": "TVDBv2",
            "scene_loc": "https://cdn.pymedusa.com/scene_exceptions/scene_exceptions_tvdb.json",
            "enabled": true,
            "baseUrl": "https://api.thetvdb.com/",
            "mappedTo": "tvdb_id",
            "identifier": "tvdb"
          },
          "tmdb": {
            "xemOrigin": null,
            "apiParams": {
              "useZip": true,
              "language": "en"
            },
            "showUrl": "https://www.themoviedb.org/tv/",
            "id": 4,
            "icon": "tmdb16.png",
            "name": "TMDB",
            "scene_loc": "https://cdn.pymedusa.com/scene_exceptions/scene_exceptions_tmdb.json",
            "enabled": true,
            "baseUrl": "https://www.themoviedb.org/",
            "mappedTo": "tmdb_id",
            "identifier": "tmdb"
          },
          "tvmaze": {
            "xemOrigin": null,
            "apiParams": {
              "useZip": true,
              "language": "en"
            },
            "showUrl": "http://www.tvmaze.com/shows/",
            "id": 3,
            "icon": "tvmaze16.png",
            "name": "TVmaze",
            "scene_loc": "https://cdn.pymedusa.com/scene_exceptions/scene_exceptions_tvmaze.json",
            "enabled": true,
            "baseUrl": "http://api.tvmaze.com/",
            "mappedTo": "tvmaze_id",
            "identifier": "tvmaze"
          }
        },
        "main": {
          "externalMappings": {<truncated>},
          "statusMap": {<truncated>},
          "traktIndexers": {<truncated>},
          "validLanguages": [<truncated>],
          "langabbvToId": {<truncated>}
        }
      }
    }
  }
}

sharkykh
sharkykh previously approved these changes Jul 4, 2018
if (to) {
return {
is: 'router-link',
to: { name: href }
Copy link
Contributor

Choose a reason for hiding this comment

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

Side note...
Have you tried it? This technique doesn't seem to actually work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How did you test this? The to needs to be the name of a loaded route.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 &amp; 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 &amp; Info</a>

It doesn't work even when I put the links inside the routes, like in config.vue and 404.vue.

Copy link
Contributor

@sharkykh sharkykh Jul 4, 2018

Choose a reason for hiding this comment

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

<router-link> in the same locations, works just fine.

<router-link :to="{ name: 'not-found' }">Not Found</router-link>
<router-link :to="{ name: 'config' }">Help &amp; Info</router-link>
<a href="/not-found" class="">Not Found</a>
<a href="/config" class="router-link-exact-active router-link-active">Help &amp; Info</a>

Copy link
Collaborator Author

@OmgImAlexis OmgImAlexis Jul 4, 2018

Choose a reason for hiding this comment

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

I think we should replace to: { name: href } with to then it'll just pass the whole object through or we could just straight out remove that ability and move those links to router-link?

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 we should replace to: { name: href } with to then it'll just pass the whole object through

Just tried that, doesn't seem to work still.

or we could just straight out remove that ability and move those links to router-link?

If we can't figure it out, then yes...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Shall we merge and then fix this in the test PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we merge and then fix this in the test PR?

No, the app link component is broken on this branch.
Give me a few minutes I'll push a commit to fix everything.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll try and fix this another time. Fixed the other errors.

@sharkykh
Copy link
Contributor

sharkykh commented Jul 4, 2018

wait, I'm getting console errors on this branch (let me check)

sharkykh added 3 commits July 4, 2018 19:04
The first argument for `Vue.component` must be the name.
It won't register the component without it.
```
vue.js:597 [Vue warn]: Error in render: "TypeError: Cannot read property 'startsWith' of null"

found in

---> <AppLink>
       <Config>
         <Root>
```
@OmgImAlexis OmgImAlexis merged commit d67c588 into develop Jul 5, 2018
@OmgImAlexis OmgImAlexis deleted the feature/cleanup-app-link branch July 5, 2018 00:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants