Exercise 7#15
Conversation
…, delete, posts, added login/logout functionality
aloparev
left a comment
There was a problem hiding this comment.
Looks good to me. You apparently did not implement the edit button, but since your delete works, it is not a big loss. And you even implemented the optional storybooks. However, I think the following files could be skipped from review: webapp/layouts/default.vue, webapp/test/NewsForm.test.js. Total result for objectives: 12/13 ⭐.
| const mutation = gql` | ||
| mutation ($title: ID!) { | ||
| upvote(title: $title) { | ||
| votes |
There was a problem hiding this comment.
Request the ID here and let vue-apollo do all the work to update the Post object! 💪
| mutation, | ||
| variables: { title: this.newsItem.title } | ||
| }) | ||
| this.$emit('updateNews', {...this.newsItem, votes: upvote.votes}); |
There was a problem hiding this comment.
You could remove all code to update things that have been previously cached. vue-apollo will do it for you.
| const variables = { post: { title } }; | ||
| try { | ||
| const { data: { write } } = await this.$apollo.mutate({ mutation, variables }) | ||
| this.newsItems.push({ title: write.title, votes: write.votes, author: write.author}); |
There was a problem hiding this comment.
Use update hook to update the cached posts query: https://apollo.vuejs.org/guide/apollo/mutations.html
| async getNews() { | ||
| const query = gql` | ||
| query { | ||
| posts { | ||
| title | ||
| votes | ||
| author { | ||
| id | ||
| } | ||
| } | ||
| } | ||
| `; | ||
| try { | ||
| const { data: { posts }} = await this.$apollo.query( { query } ); | ||
| return posts; | ||
| } | ||
| catch (e) { | ||
| return []; | ||
| } | ||
| } |
There was a problem hiding this comment.
| async getNews() { | |
| const query = gql` | |
| query { | |
| posts { | |
| title | |
| votes | |
| author { | |
| id | |
| } | |
| } | |
| } | |
| `; | |
| try { | |
| const { data: { posts }} = await this.$apollo.query( { query } ); | |
| return posts; | |
| } | |
| catch (e) { | |
| return []; | |
| } | |
| } | |
| apollo: { | |
| posts: gql` | |
| query { | |
| posts { | |
| title | |
| votes | |
| author { | |
| id | |
| } | |
| } | |
| } | |
| ` | |
| } |
Will automatically populate this.posts for you! 💪
| async mounted() { | ||
| this.newsItems = await this.getNews(); | ||
| }, |
There was a problem hiding this comment.
| async mounted() { | |
| this.newsItems = await this.getNews(); | |
| }, |
Using queries according to the docs will work even in SSR. This mounted hook only runs client-side.
| const [emailExists, emailIsNotTooLong] = wrapper.vm.nameRules | ||
|
|
||
| expect(emailExists(email)).toBe(true); | ||
| expect(emailIsNotTooLong(email)).toBe("User name cannot exceed 64 chars!"); |
There was a problem hiding this comment.
Why do you call it User name if it's the email? 🤔
| it('keeps the login button disabled if the email is too long', async () => { | ||
| const email = 'testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest' | ||
| const submitButton = wrapper.find('#login'); | ||
| const [emailExists, emailIsNotTooLong] = wrapper.vm.nameRules |
There was a problem hiding this comment.
Consider wrapper.vm as private. If you want to unit test your validations, export them in your component:
<script>
export const validations = ...
export default {
data() {
validations,
// ...
}
}
</script>|
|
||
| describe('NewsForm', () => { | ||
| let wrapper; | ||
There was a problem hiding this comment.
You could have merged whitespace changes to keep the PR diff small.
| expect(paragraph.text()).toEqual("The list is empty :("); | ||
| wrapper.destroy(); | ||
| expect(paragraph.text()).toEqual("The list is empty :("); | ||
| testWrapper.destroy(); |
There was a problem hiding this comment.
Why is it necessary to call testWrapper.destroy()?
| }, shallowMount) | ||
| await testWrapper.vm.addItem('Title_3'); | ||
| expect(writeMutateMock).toBeCalled() | ||
| expect(queryMock).toHaveBeenCalledTimes(2); // After writing, we also expect vue to rerender the component and therefore query the news again |
There was a problem hiding this comment.
Ideally, this is not necessary. Once the write mutation completes, it adds the new post to the cached posts query, thus triggering a re-render without unnecessary graphql queries.
|
Hi Team! Let's summarize first: ⭐ For instructions in the README.md on how to build your webapp for production. As it was correctly pointed out by @aloparev, you did not implement an edit button. that is very unfortunate as it basically only one line missing. The exercise stated that you did not have to implement the functionality behind it (neither front- nor backend!). But it's still 12/13 ⭐ which is great! Two more things:
|


Pull request Group CountryRoads