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

Refactor existing tests and add new ones #254

Merged
merged 4 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cypress.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"apiUrl": "http://localhost:3000",
"experimentalSessionSupport": true,
"baseUrl": "http://localhost:9000",
"fixturesFolder": false,
"pluginsFile": false
"pluginsFile": false,
"requestTimeout": 10000
}
39 changes: 29 additions & 10 deletions cypress/integration/login.spec.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,66 @@
describe('Login', () => {
beforeEach(() => cy.visit('/'))
const userLoginData = {
user: Cypress.env('user'),
password: Cypress.env('password'),
useCachedSession: {
cacheSession: false
}
}

const { user, password, useCachedSession } = userLoginData

it('logs in successfully', () => {
cy.login()
cy.intercept('GET', '/models?userId=*').as('getUserModels')

cy.login(user, password, useCachedSession)

cy.wait('@getUserModels')
cy.url()
.should('be.equal', `${Cypress.config('baseUrl')}/#!/main`)
.should('be.equal', `${Cypress.config('baseUrl')}/#!/main`)
cy.contains('h2', 'Modelagens')
.should('be.visible')
})

context('Form validations', () => {
it('alerts on invalid user email', () => {
cy.login('invalid#user.com', Cypress.env('password'))
cy.login('invalid#user.com', password, useCachedSession)

cy.contains('.alert', 'Preencha os campos em vermelho')
cy.contains('.alert-danger', 'Preencha os campos em vermelho')
.should('be.visible')
cy.get('#userEmail')
.should('have.class', 'error')
cy.get('.error')
.should('have.length', 1)
.and('be.visible')
})

it('alerts when clicking ENTER without filling the form', () => {
cy.visit('/')
cy.contains('button', 'Entrar')
.click()

cy.contains('.alert', 'Preencha os campos em vermelho')
cy.contains('.alert-danger', 'Preencha os campos em vermelho')
.should('be.visible')
cy.get('#userEmail')
.should('have.class', 'error')
cy.get('#userPassword')
.should('have.class', 'error')
cy.get('.error')
.should('have.length', 2)
.and('be.visible')
})

it('alerts on invalid user', () => {
cy.login('[email protected]', Cypress.env('password'))
cy.login('[email protected]', password, useCachedSession)

cy.contains('.alert', 'Login ou senha incorretos')
cy.contains('.alert-danger', 'Login ou senha incorretos')
.should('be.visible')
})

it('alerts on invalid password', () => {
cy.login(Cypress.env('user'), 'invalid-pwd')
cy.login(Cypress.env('user'), 'invalid-pwd', useCachedSession)

cy.contains('.alert', 'Login ou senha incorretos')
cy.contains('.alert-danger', 'Login ou senha incorretos')
.should('be.visible')
})
})
Expand Down
56 changes: 56 additions & 0 deletions cypress/integration/models.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
describe('Models', () => {
beforeEach(() => {
cy.intercept('GET', '/models?userId=*').as('getUserModels')
cy.login()
cy.visit('/#!/main')
cy.cleanUpUserModelsAndReoload()
cy.contains('a', 'Nova Modelagem').click()
cy.get('create-model-modal').should('be.visible')
})

it('closes the "New Model" modal', () => {
cy.contains('button', 'Cancelar').click()
cy.get('create-model-modal').should('not.exist')
})

it('alerts when clicking SAVE without filling the title', () => {
cy.contains('button', 'Salvar').click()
cy.get('#name').should('have.class', 'error')
})

context('Model creation', () => {
const modelTitle = 'User'

beforeEach(() => cy.get('#name').type(modelTitle))

it('creates a conceptual model', () => {
cy.contains('button', 'Salvar').click()

cy.contains('h2', `Modelo conceitual de: ${modelTitle}`)
.should('be.visible')
})

it('creates a logical model', () => {
cy.get('.modelselect').click()
cy.contains('li span', 'Lógico').click()
cy.contains('button', 'Salvar').click()

cy.contains('h2', `Modelo lógico de: ${modelTitle}`)
.should('be.visible')
})
})
})

Cypress.Commands.add('cleanUpUserModelsAndReoload', () => {
cy.wait('@getUserModels').then(userModels => {
cy.request('GET', userModels.response.url).then(userModelsResponse => {
userModelsResponse.body.forEach(model => {
cy.request(
'DELETE',
`${Cypress.config('apiUrl')}/models/:modelId?modelId=${model._id}`
)
})
})
})
cy.reload()
})
24 changes: 17 additions & 7 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,22 @@

Cypress.Commands.add('login', (
user = Cypress.env('user'),
password = Cypress.env('password')
password = Cypress.env('password'),
{ cacheSession = true } = {}
) => {
cy.get('#userEmail')
.type(user)
cy.get('#userPassword')
.type(password, { log: false })
cy.contains('button', 'Entrar')
.click()
const fillLoginFormAndSubmit = () => {
cy.visit('/')
cy.get('#userEmail')
.type(user)
cy.get('#userPassword')
.type(password, { log: false })
cy.contains('button', 'Entrar')
.click()
}

if (cacheSession) {
cy.session([user], fillLoginFormAndSubmit)
} else {
fillLoginFormAndSubmit()
}
})