-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
121 use cypress best practises (#130)
- [x] do not use deleteTestUser at end of tests to clean up state (it should also not use the ui with cy.) - [x] use cy.task('db:reset') to clean up state before tests when required - [x] do not use createTestUser before tests to login (its using the UI as well, which should not be) - [x] use minimal login implementation as command (using fetch and storing the response (access token) into localStorage - known issue at Cypress cypress-io/cypress#25397 (comment) - workaround as descibred in the cypress comment. (triggering a fetch to the spa, before all tests run)
- Loading branch information
Showing
12 changed files
with
220 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,46 @@ | ||
describe('auth-corner should', () => { | ||
beforeEach(() => { | ||
cy.visit('/') | ||
}) | ||
describe('auth-corner', () => { | ||
describe('when authorized should', () => { | ||
beforeEach(() => { | ||
cy.login() | ||
cy.visit('/') | ||
}) | ||
|
||
it('navigate to login on login button click', () => { | ||
cy.byTestAttr('login-button').click() | ||
cy.url().should('not.include', 'register') | ||
cy.url().should('include', 'login') | ||
}) | ||
it('contain logout button', () => { | ||
cy.byTestAttr('logout-button') | ||
.should('be.visible') | ||
.invoke('text') | ||
.should('have.length.above', 1) | ||
}) | ||
|
||
it('logout on logoutClick', () => { | ||
cy.byTestAttr('logout-button').click() | ||
cy.byTestAttr('login-button').should('be.visible') | ||
}) | ||
|
||
it('navigate to register on register button click', () => { | ||
cy.byTestAttr('register-button').click() | ||
cy.url().should('not.include', 'login') | ||
cy.url().should('include', 'register') | ||
it('navigate to home on logout button click', () => { | ||
cy.byTestAttr('logout-button').click() | ||
const hostUrl = Cypress.env()['baseUrl'] | ||
cy.url().should('equal', hostUrl + '/') | ||
cy.byTestAttr('login-button').should('be.visible') | ||
cy.byTestAttr('register-button').should('be.visible') | ||
}) | ||
}) | ||
|
||
it('contain logout button', () => { | ||
cy.createTestUser() | ||
describe('when not authorized should', () => { | ||
beforeEach(() => { | ||
cy.visit('/') | ||
}) | ||
|
||
cy.byTestAttr('logout-button') | ||
.should('be.visible') | ||
.invoke('text') | ||
.should('have.length.above', 1) | ||
it('navigate to login on login button click', () => { | ||
cy.byTestAttr('login-button').click() | ||
cy.url().should('not.include', 'register') | ||
cy.url().should('include', 'login') | ||
}) | ||
|
||
cy.deleteTestUser() | ||
it('navigate to register on register button click', () => { | ||
cy.byTestAttr('register-button').click() | ||
cy.url().should('not.include', 'login') | ||
cy.url().should('include', 'register') | ||
}) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,32 @@ | ||
describe('login should', () => { | ||
it('not log in with empty inputs', () => { | ||
cy.visit('/login') | ||
cy.byTestAttr('login-submit-button').click({ force: true }) | ||
cy.url().should('include', 'login') | ||
describe('login', () => { | ||
describe('when unauthorized should', () => { | ||
beforeEach(() => { | ||
cy.task('db:reset') | ||
cy.visit('/login') | ||
}) | ||
|
||
it('not log in with empty inputs', () => { | ||
cy.byTestAttr('login-submit-button').click({ force: true }) | ||
cy.url().should('include', 'login') | ||
}) | ||
|
||
it('work with enter', () => { | ||
cy.byTestAttr('login-name-input').type('Cypress Testuser') | ||
cy.byTestAttr('password-input').type('iLoveJesus<3!{enter}') | ||
}) | ||
|
||
it('work with submit button', () => { | ||
cy.byTestAttr('login-name-input').type('Cypress Testuser') | ||
cy.byTestAttr('password-input').type('iLoveJesus<3!{enter}') | ||
cy.byTestAttr('login-submit-button').click() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('login redirects', () => { | ||
it('when logged in already', () => { | ||
cy.createTestUser() | ||
cy.visit('/login') | ||
cy.url().should('not.include', 'login') | ||
cy.deleteTestUser() | ||
describe('when authorized should', () => { | ||
it('redirect', () => { | ||
cy.login() | ||
cy.visit('/login') | ||
cy.url().should('not.include', 'login') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
describe('register', () => { | ||
describe('when unauthorized should', () => { | ||
beforeEach(() => { | ||
cy.task('db:reset') | ||
cy.visit('/register') | ||
}) | ||
|
||
it('not register with empty inputs', () => { | ||
cy.intercept( | ||
Cypress.env('apiBaseUrl') + '/Auth/RegisterAsync', | ||
cy.spy().as('register') | ||
) | ||
cy.byTestAttr('register-submit-button').click({ force: true }) | ||
cy.url().should('include', 'register') | ||
cy.get('@register').should('not.have.been.called') | ||
}) | ||
|
||
it('register with enter', () => { | ||
cy.byTestAttr('register-name-input').type('Cypress Testuser') | ||
cy.byTestAttr('password-input').type('iLoveJesus<3!{enter}') | ||
}) | ||
|
||
it('register with submit button', () => { | ||
cy.byTestAttr('register-name-input').type('Cypress Testuser') | ||
cy.byTestAttr('password-input').type('iLoveJesus<3!{enter}') | ||
cy.byTestAttr('register-submit-button').click() | ||
}) | ||
|
||
it('register with email', () => { | ||
cy.byTestAttr('register-name-input').type('Cypress Testuser') | ||
cy.byTestAttr('email-input').type('[email protected]') | ||
cy.byTestAttr('password-input').type('iLoveJesus<3!{enter}') | ||
cy.byTestAttr('register-submit-button').click() | ||
}) | ||
}) | ||
|
||
describe('when authorized should', () => { | ||
it('redirect', () => { | ||
cy.login() | ||
cy.visit('/register') | ||
cy.url().should('not.include', 'register') | ||
}) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,12 @@ | ||
describe('view-recipe should', () => { | ||
it(`redirect to recipe/{recipeId} recipe is created sucessfully`, () => { | ||
const recipeId = crypto.randomUUID().toString() | ||
const title = 'Cypress Recipe' | ||
cy.task('db:reset') | ||
cy.createTestUser() | ||
// TODO this should use a seeded database | ||
|
||
cy.visit('/create-recipe') | ||
|
||
const recipeTitle = 'valid recipe title' | ||
|
||
cy.byTestAttr('recipe-title-input').type(recipeTitle) | ||
|
||
cy.intercept({ | ||
path: '/Recipe/AddAsync', | ||
times: 1 | ||
}).as('create-recipe') | ||
|
||
cy.byTestAttr('create-recipe-submit-button').click() | ||
|
||
cy.wait('@create-recipe').then((stuff) => { | ||
const newRecipe: { id: string } = stuff.response?.body | ||
cy.url().should('include', 'recipe/' + newRecipe.id) | ||
}) | ||
|
||
cy.byTestAttr('title').contains(recipeTitle) | ||
|
||
cy.deleteTestUser() | ||
cy.task('db:seed:recipe', { id: recipeId, title: title }) | ||
cy.login() | ||
cy.visit('/recipe/' + recipeId) | ||
cy.url().should('include', 'recipe/' + recipeId) | ||
cy.byTestAttr('title').contains(title) | ||
}) | ||
}) |
Oops, something went wrong.