From 109f11d3035914c656867fe6b21bacd62c02619b Mon Sep 17 00:00:00 2001
From: helenanull <helena.github@gmail.com>
Date: Tue, 30 Mar 2021 22:18:18 +0300
Subject: [PATCH] some fixes and added more tests

---
 cypress/integration/article.spec.js  | 26 +++++++++++++++++++++++++-
 cypress/integration/comments.spec.js | 25 +++++++++++++++++++++++++
 cypress/selectors/article.sel.js     |  6 +++++-
 cypress/selectors/editor.sel.js      |  1 +
 cypress/support/article.cmd.js       |  2 +-
 5 files changed, 57 insertions(+), 3 deletions(-)
 create mode 100644 cypress/integration/comments.spec.js

diff --git a/cypress/integration/article.spec.js b/cypress/integration/article.spec.js
index 1435ce3..c527171 100644
--- a/cypress/integration/article.spec.js
+++ b/cypress/integration/article.spec.js
@@ -15,12 +15,36 @@ describe('Article', () => {
         cy.get(editor.titleField).type('My post title')
         cy.get(editor.aboutField).type('Cypress')
         cy.get(editor.bodyField).type(`Cypress is so cool awyeah! ${articleLink}`)
-        cy.get(editor.tagsField).type('cypress, automation')
         cy.get(editor.publishButton).click()
         cy.get(article.title).should('be.visible')
             .and('have.text', 'My post title')
     })
 
+    it('can add tags to article', () => {
+        cy.visit('/editor/')
+        cy.get(editor.titleField).type('My post title')
+        cy.get(editor.aboutField).type('Cypress')
+        cy.get(editor.bodyField).type(`Cypress is so cool awyeah! ${articleLink}`)
+        cy.get(editor.tagsField).type('cypress{enter}')
+        cy.get(editor.tagsField).should('have.value', '')
+        cy.get(editor.addedTags).should('be.visible')
+            .and('have.length', 1)
+            .and('contain', 'cypress')
+        cy.get(editor.tagsField).type('test-automation{enter}')
+        cy.get(editor.tagsField).should('have.value', '')
+        cy.get(editor.addedTags).should('be.visible')
+            .and('have.length', 2)
+            .and('contain', 'cypress')
+            .and('contain', 'test-automation')
+        cy.get(editor.publishButton).click()
+        cy.get(article.title).should('be.visible')
+            .and('have.text', 'My post title')
+        cy.get(article.tags).should('be.visible')
+            .and('have.length', 2)
+            .and('contain', 'cypress')
+            .and('contain', 'test-automation')
+    })
+
     it('can edit an article', () => {
         // we already know if creating an article works or not from the first test
         // we can now use shortcut (cy.createArticle() command) to test other scenarios
diff --git a/cypress/integration/comments.spec.js b/cypress/integration/comments.spec.js
new file mode 100644
index 0000000..db8e622
--- /dev/null
+++ b/cypress/integration/comments.spec.js
@@ -0,0 +1,25 @@
+import article from '../selectors/article.sel'
+
+describe('Comments', () => {
+    beforeEach(() => {
+        cy.register().then((email) => {
+            cy.wrap(email.split('@')[0]).as('username')
+            cy.login(email)
+        })
+        cy.createArticle().then((link) => {
+            cy.intercept('/api/articles/*/comments').as('commentsRequest')
+            cy.visit(`/article/${link}`)
+            // wait for comments to be loaded before starting with tests
+            cy.wait('@commentsRequest')
+        })
+    })
+
+    it('can add a comment to article', () => {
+        cy.get(article.comments).should('have.length', 0)
+        cy.get(article.commentField).type('Cypress comment')
+        cy.get(article.postCommentButton).should('contain', 'Post Comment').click()
+        cy.get(article.comments).should('be.visible')
+            .and('have.length', 1)
+        cy.get(article.commentField).should('have.value', '')
+    })
+})
diff --git a/cypress/selectors/article.sel.js b/cypress/selectors/article.sel.js
index 4841bfc..41a0457 100644
--- a/cypress/selectors/article.sel.js
+++ b/cypress/selectors/article.sel.js
@@ -2,5 +2,9 @@ module.exports = {
     title: '[ng-bind="::$ctrl.article.title"]',
     body: '[ng-bind-html*="ctrl.article.body"] p',
     editButton: 'h1 + article-actions [ui-sref*="ctrl.article.slug"]',
-    deleteButton: 'h1 + article-actions .btn-outline-danger'
+    deleteButton: 'h1 + article-actions .btn-outline-danger',
+    tags: '.tag-list li',
+    comments: '[ng-repeat*=".comments"] .card-block',
+    commentField: '.card.comment-form textarea',
+    postCommentButton: '.card.comment-form .btn'
 }
diff --git a/cypress/selectors/editor.sel.js b/cypress/selectors/editor.sel.js
index f0d0919..91e2600 100644
--- a/cypress/selectors/editor.sel.js
+++ b/cypress/selectors/editor.sel.js
@@ -3,5 +3,6 @@ module.exports = {
     aboutField: '[ng-model="$ctrl.article.description"]',
     bodyField: '[ng-model="$ctrl.article.body"]',
     tagsField: '[ng-model="$ctrl.tagField"]',
+    addedTags: '.tag-list span',
     publishButton: '[ng-click="$ctrl.submit()"]'
 }
diff --git a/cypress/support/article.cmd.js b/cypress/support/article.cmd.js
index 8852865..8fa6437 100644
--- a/cypress/support/article.cmd.js
+++ b/cypress/support/article.cmd.js
@@ -10,7 +10,7 @@ Cypress.Commands.add('createArticle', () => {
                 title: 'My Cypress article',
                 description: 'https://github.com/helenanull/cypress-example',
                 body: 'This article is created by createArticle Cypress command',
-                tagList: []
+                tagList: ['cypress', 'test-automation', 'simple']
             }
         }
     })