diff --git a/cypress/integration/article.spec.js b/cypress/integration/article.spec.js index 5312ca5..e37b658 100644 --- a/cypress/integration/article.spec.js +++ b/cypress/integration/article.spec.js @@ -3,12 +3,11 @@ import article from '../selectors/article.sel' import home from '../selectors/home.sel' describe('Article', () => { - const readMoreLink = 'https://github.com/helenanull/cypress-example' + const seeMoreLink = 'https://github.com/helenanull/cypress-example' beforeEach(() => { - cy.register().then((email) => { - cy.wrap(email.split('@')[0]).as('username') - cy.login(email) + cy.register().then((response) => { + cy.wrap(response.username).as('username') }) }) @@ -16,7 +15,7 @@ describe('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! ${readMoreLink}`) + cy.get(editor.bodyField).type(`Cypress is so cool awyeah! ${seeMoreLink}`) cy.get(editor.publishButton).click() cy.get(article.title).should('be.visible') .and('have.text', 'My post title') @@ -26,7 +25,7 @@ describe('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! ${readMoreLink}`) + cy.get(editor.bodyField).type(`Cypress is so cool awyeah! ${seeMoreLink}`) cy.get(editor.tagsField).type('cypress{enter}') cy.get(editor.tagsField).should('have.value', '') cy.get(editor.addedTags).should('be.visible') @@ -57,14 +56,14 @@ describe('Article', () => { // to check field value, use have.value not have.text .and('have.value', 'Article created by Cypress test') cy.get(editor.aboutField).should('be.visible') - .and('have.value', readMoreLink) + .and('have.value', seeMoreLink) cy.get(editor.bodyField).clear() - .type(`Test can edit an article. ${readMoreLink}`) + .type(`Test can edit an article. ${seeMoreLink}`) cy.get(editor.publishButton).click() cy.url().should('contain', '/article/article-created-by-cypress-test-') cy.get(article.title).should('be.visible') cy.get(article.body).should('be.visible') - .and('have.text', `Test can edit an article. ${readMoreLink}`) + .and('have.text', `Test can edit an article. ${seeMoreLink}`) }) it('can delete an article', () => { diff --git a/cypress/integration/comments.spec.js b/cypress/integration/comments.spec.js index ef58bb9..3154c35 100644 --- a/cypress/integration/comments.spec.js +++ b/cypress/integration/comments.spec.js @@ -2,9 +2,8 @@ import article from '../selectors/article.sel' describe('Comments', () => { beforeEach(() => { - cy.register().then((email) => { - cy.wrap(email.split('@')[0]).as('username') - cy.login(email) + cy.register().then((response) => { + cy.wrap(response.username).as('username') }) cy.createArticle().then((link) => { cy.intercept('/api/articles/*/comments').as('commentsRequest') diff --git a/cypress/integration/header.spec.js b/cypress/integration/header.spec.js index 2460a97..c5a7fa7 100644 --- a/cypress/integration/header.spec.js +++ b/cypress/integration/header.spec.js @@ -12,9 +12,7 @@ describe('Header', () => { }) it('contains correct elements when logged in', () => { - cy.register().then((email) => { - cy.login(email) - }) + cy.register() cy.visit('') cy.get(header.navbarLinks).should('be.visible') .and('have.length', 4) diff --git a/cypress/integration/home.spec.js b/cypress/integration/home.spec.js index cbd6ddc..6926483 100644 --- a/cypress/integration/home.spec.js +++ b/cypress/integration/home.spec.js @@ -21,9 +21,7 @@ describe('Home page', () => { cy.intercept(`${apiUrl}/articles/feed?limit=10*`, { fixture: 'my_feed' }) - cy.register().then((email) => { - cy.login(email) - }) + cy.register() cy.visit('') cy.get(home.yourFeedTab).should('be.visible') cy.get(home.globalFeedTab).should('be.visible') diff --git a/cypress/integration/login.spec.js b/cypress/integration/login.spec.js index 90f0669..f75b03b 100644 --- a/cypress/integration/login.spec.js +++ b/cypress/integration/login.spec.js @@ -42,9 +42,12 @@ describe('Login', () => { context('successful', () => { beforeEach(() => { // we need a new user - cy.register().then((email) => { - cy.wrap(email).as('email') + cy.register().then((response) => { + cy.wrap(response.email).as('email') }) + // log out - clear cookies and localstorage + cy.clearCookies() + cy.clearLocalStorage() cy.visit('/login') }) diff --git a/cypress/integration/profile.spec.js b/cypress/integration/profile.spec.js index 11f22f8..c6e40ab 100644 --- a/cypress/integration/profile.spec.js +++ b/cypress/integration/profile.spec.js @@ -2,10 +2,9 @@ import profile from '../selectors/profile.sel' describe('Profile page', () => { beforeEach(() => { - cy.register().then((email) => { + cy.register().then((response) => { // we need username to visit profile url - cy.wrap(email.split('@')[0]).as('username') - cy.login(email) + cy.wrap(response.username).as('username') }) }) diff --git a/cypress/integration/settings.spec.js b/cypress/integration/settings.spec.js index b11957d..85637c6 100644 --- a/cypress/integration/settings.spec.js +++ b/cypress/integration/settings.spec.js @@ -3,9 +3,7 @@ import profile from '../selectors/profile.sel' describe('Settings', () => { beforeEach(() => { - cy.register().then((email) => { - cy.login(email) - }) + cy.register() cy.visit('/settings') }) diff --git a/cypress/support/index.js b/cypress/support/index.js index 019da5d..daffee3 100644 --- a/cypress/support/index.js +++ b/cypress/support/index.js @@ -14,7 +14,6 @@ // *********************************************************** // Import commands.js using ES2015 syntax: -import './login.cmd' import './register.cmd' import './article.cmd' diff --git a/cypress/support/login.cmd.js b/cypress/support/login.cmd.js deleted file mode 100644 index 1ecd134..0000000 --- a/cypress/support/login.cmd.js +++ /dev/null @@ -1,20 +0,0 @@ -Cypress.Commands.add('login', (email = Cypress.env('email'), password = Cypress.env('password')) => { - const apiUrl = Cypress.env('apiUrl') - - cy.request({ - // here we can't use just '/users/login' because baseUrl is different than API url - // if they are the same, - // then we can just use url: '/users/login' without prefix, like in visit() command - url: `${apiUrl}/users/login`, - method: 'POST', - body: { - user: { - email: email, - password: password - } - } - }).then((response) => { - expect(response.status).to.eq(200) - window.localStorage.setItem('jwtToken', response.body.user.token) - }) -}) diff --git a/cypress/support/register.cmd.js b/cypress/support/register.cmd.js index 81819ea..9ece97a 100644 --- a/cypress/support/register.cmd.js +++ b/cypress/support/register.cmd.js @@ -5,6 +5,9 @@ Cypress.Commands.add('register', () => { const password = Cypress.env('password') cy.request({ + // here we can't use just '/users' url because baseUrl is different than API url + // if they are the same, + // then we can just use: '/users' without prefix, like in visit() command url: `${apiUrl}/users`, method: 'POST', body: { @@ -21,6 +24,16 @@ Cypress.Commands.add('register', () => { cy.log(`**email: ${email}**`) cy.log(`**password: ${password}**`) }) - // return email so that we can use that to log in - .then(() => email) + .then((response) => { + expect(response.status).to.eq(200) + // user is also logged in after registering + // so we can just save token + window.localStorage.setItem('jwtToken', response.body.user.token) + + return { + // we need email and username in tests + email: email, + username: username + } + }) })