Skip to content

Commit

Permalink
Merge pull request #17 from suissa/develop
Browse files Browse the repository at this point in the history
Develop - Cypress new test pattern for all
  • Loading branch information
suissa authored Jan 20, 2024
2 parents 6b0a725 + 826faa8 commit be84a6e
Show file tree
Hide file tree
Showing 24 changed files with 3,582 additions and 1,303 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ yarn-error.log*
*.tsbuildinfo
next-env.d.ts

src/config.ts
cypress/screenshots
44 changes: 25 additions & 19 deletions cypress/e2e/integrations/companies.cy.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
describe("Página de Empresas", () => {
// const ENV = "development"
const ENV = "production"
const BASE_URL_DEV = "http://localhost"
const BASE_URL_PROD = "http://137.184.81.207"
const BASE_URL = ENV == "development" ? BASE_URL_DEV : BASE_URL_PROD
const LOGIN_URL = `${BASE_URL}:9000/login`
const API_URL = `${BASE_URL}:9000/companies`
const TEST_URL = `${BASE_URL}:3000/companies`
const BUTTON = "Nova Empresa"
const ENTITY = { _id: "1", name: "Empresa 1", phone: "123456789", status: "Ativo", dueDate: "16/01/2024", recurrence: "Mensal"}
const ENTITY_PLURAL_NAME = "Mensagens"
const TEST_NAME = `Página de ${ENTITY_PLURAL_NAME}`

describe(TEST_NAME, () => {
beforeEach(() => {

cy.fixture("token").then((token) => {
console.log(token);
cy.intercept("POST", "http://137.184.81.207:9000/login", {
cy.intercept("POST", `${LOGIN_URL}`, {
statusCode: 200,
body: {
token: token.token
}
body: { token: token.token }
});

cy.intercept("GET", "http://137.184.81.207:9000/companies", {
cy.intercept("GET", `${API_URL}`, {
statusCode: 200,
body: [
{ _id: "1", name: "Empresa 1", phone: "123456789", status: "Ativo", dueDate: "16/01/2024", recurrence: "Mensal"},
]
}).as("getCompanies");
body: [ ENTITY ]
}).as("getPlans");

localStorage.setItem("token", token.token);
cy.visit("http://137.184.81.207:3000/companies");

cy.wait("@getCompanies");
cy.visit(`${TEST_URL}`);
})

});

it("deve exibir uma lista de empresas", () => {
it(`deve exibir uma lista de ${ENTITY_PLURAL_NAME}`, () => {
cy.get("table").should("exist");
cy.get("table tbody tr").should("have.length.at.least", 1);
});

it("deve mudar da tabela para o formulário ao clicar em Nova Empresa", () => {
cy.get("button").contains("Nova Empresa").click();
it(`deve mudar da tabela para o formulário ao clicar em ${BUTTON}`, () => {
cy.get("button").contains(BUTTON).click();
cy.get("form").should("exist");
cy.get("table").should("not.exist");
});

it("deve mudar do formulário para a tabela ao clicar em Cancelar", () => {
cy.get("button").contains("Nova Empresa").click();
cy.get("button").contains(BUTTON).click();
cy.get("form").should("exist");
cy.get("table").should("not.exist");
cy.get("button").contains("Cancelar").click();
cy.get("table").should("exist");
cy.get("form").should("not.exist");
});
});

48 changes: 26 additions & 22 deletions cypress/e2e/integrations/connections.cy.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,53 @@
describe("Página de Conexões", () => {
beforeEach(() => {
// const ENV = "development"
const ENV = "production"
const BASE_URL_DEV = "http://localhost"
const BASE_URL_PROD = "http://137.184.81.207"
const BASE_URL = ENV == "development" ? BASE_URL_DEV : BASE_URL_PROD
const LOGIN_URL = `${BASE_URL}:9000/login`
const API_URL = `${BASE_URL}:9000/connections`
const TEST_URL = `${BASE_URL}:3000/connections`
const BUTTON = "Nova Conexão"
const ENTITY = { _id: "1", name: "Conexão 1", phone: "123456789", instanceName: "Conexao_1-123456789" }
const ENTITY_PLURAL_NAME = "Mensagens"
const TEST_NAME = `Página de ${ENTITY_PLURAL_NAME}`


describe(TEST_NAME, () => {
beforeEach(() => {
cy.fixture("token").then((token) => {
console.log(token);
cy.intercept("POST", "http://137.184.81.207:9000/auth/login", {
cy.intercept("POST", `${LOGIN_URL}`, {
statusCode: 200,
body: {
token: token.token
}
body: { token: token.token }
});

cy.intercept("GET", "http://137.184.81.207:9000/connections", {
cy.intercept("GET", `${API_URL}`, {
statusCode: 200,
body: [
{ _id: "1", name: "Conexão 1", phone: "123456789", instanceName: "Conexao_1-123456789" },
]
}).as("getConnections");
body: [ ENTITY ]
}).as("getPlans");

localStorage.setItem("token", token.token);
cy.visit("http://137.184.81.207:3000/connections");

// Espera a requisição GET /api/connections ser chamada
cy.wait("@getConnections", { timeout: 10000 });
});
cy.visit(`${TEST_URL}`);
})
});

it("deve exibir uma lista de connections", () => {
it(`deve exibir uma lista de ${ENTITY_PLURAL_NAME}`, () => {
cy.get("table").should("exist");
cy.get("table tbody tr").should("have.length.at.least", 1);
});

it("deve mudar da tabela para o formulário ao clicar em Nova Conexão", () => {
cy.get("button").contains("Nova Conexão").click();
cy.wait(1000);
it(`deve mudar da tabela para o formulário ao clicar em ${BUTTON}`, () => {
cy.get("button").contains(BUTTON).click();
cy.get("form").should("exist");
cy.get("table").should("not.exist");
});

it("deve mudar do formulário para a tabela ao clicar em Cancelar", () => {
cy.get("button").contains("Nova Conexão").click();
cy.get("button").contains(BUTTON).click();
cy.get("form").should("exist");
cy.get("table").should("not.exist");
cy.get("button").contains("Cancelar").click();
cy.get("table").should("exist");
cy.get("form").should("not.exist");
});
});

60 changes: 33 additions & 27 deletions cypress/e2e/integrations/contacts.cy.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,52 @@
describe("Página de Contatos", () => {
beforeEach(() => {
// const ENV = "development"
const ENV = "production"
const BASE_URL_DEV = "http://localhost"
const BASE_URL_PROD = "http://137.184.81.207"
const BASE_URL = ENV == "development" ? BASE_URL_DEV : BASE_URL_PROD
const LOGIN_URL = `${BASE_URL}:9000/login`
const API_URL = `${BASE_URL}:9000/contacts`
const TEST_URL = `${BASE_URL}:3000/contacts`
const BUTTON = "Novo Contato"
const ENTITY = { _id: "1", name: "Contato 1", phone: "123456789"}
const ENTITY_PLURAL_NAME = "Mensagens"
const TEST_NAME = `Página de ${ENTITY_PLURAL_NAME}`

describe(TEST_NAME, () => {
beforeEach(() => {
cy.fixture("token").then((token) => {
console.log(token);
// Mock da resposta da API de login (se necessário)
cy.intercept("POST", "http://137.184.81.207:9000/login", {
cy.intercept("POST", `${LOGIN_URL}`, {
statusCode: 200,
body: {
token: token.token
}
body: { token: token.token }
});

cy.fixture("contacts").then((contacts) => {
console.log(contacts);
})
cy.intercept("GET", "http://137.184.81.207:9000/contacts", {
cy.intercept("GET", `${API_URL}`, {
statusCode: 200,
body: [
{ _id: "1", name: "Contato 1", phone: "123456789"},
]
}).as("getContacts");
body: [ ENTITY ]
}).as("getPlans");

localStorage.setItem("token", token.token);
cy.visit("http://137.184.81.207:3000/contacts");

cy.wait("@getContacts");
cy.visit(`${TEST_URL}`);
})

});

Cypress.on("uncaught:exception", (err, runnable) => {
console.log("uncaught:exception:", err);
return false;
});
it("deve exibir uma lista de contatos", () => {
it(`deve exibir uma lista de ${ENTITY_PLURAL_NAME}`, () => {
cy.get("table").should("exist");
cy.get("table tbody tr").should("have.length.at.least", 1);
});

it("deve mudar da tabela para o formulário ao clicar em Novo Contato", () => {
cy.get("button").contains("Novo Contato").click();
it(`deve mudar da tabela para o formulário ao clicar em ${BUTTON}`, () => {
cy.get("button").contains(BUTTON).click();
cy.get("form").should("exist");
cy.get("table").should("not.exist");
});
});

it("deve mudar do formulário para a tabela ao clicar em Cancelar", () => {
cy.get("button").contains(BUTTON).click();
cy.get("form").should("exist");
cy.get("table").should("not.exist");
cy.get("button").contains("Cancelar").click();
cy.get("table").should("exist");
cy.get("form").should("not.exist");
});
});
48 changes: 48 additions & 0 deletions cypress/e2e/integrations/kanban.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// describe("Teste do Kanban com Cypress", () => {
// beforeEach(() => {
// // Antes de cada teste, você pode realizar configurações iniciais, como visitar a página.
// cy.visit("http://www.zapzapticket.online:3000/kanban"); // Certifique-se de ajustar o URL conforme necessário.
// Cypress.Commands.add('dragTo', {prevSubject: 'element'}, (subject, targetEl) => {
// cy.wrap(subject).trigger('mousedown', {which: 1});
// cy.get(targetEl).trigger('mousemove').trigger('mouseup', {force: true});
// });
// });

// it("Deve arrastar e soltar um cartão no Kanban", () => {
// return true;
// // Exemplo fictício:
// // cy.get(".smooth-dnd-draggable-wrapper article header > span").first().dragTo(".react-trello-lane");
// // cy.get(".react-trello-lane").should("have.length", 1);
// });
// });

describe("Página de Kanban", () => {
beforeEach(() => {

cy.fixture("token").then((token) => {
console.log(token);
// Mock da resposta da API de login (se necessário)
cy.intercept("POST", "http://137.184.81.207:9000/login", {
statusCode: 200,
body: {
token: token.token
}
});

localStorage.setItem("token", token.token);
cy.visit("http://137.184.81.207:3000/kanban");

})

});

Cypress.on("uncaught:exception", (err, runnable) => {
console.log("uncaught:exception:", err);
return false;
});
it("deve exibir pelo menos 2 lanes", () => {
cy.get(".react-trello-lane").should("exist");
cy.get(".react-trello-lane").should("have.length.at.least", 2);
});

});
69 changes: 0 additions & 69 deletions cypress/e2e/integrations/login.cy.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
// FUNCIONANDO
// describe("Página de Login", () => {
// it("deve logar com sucesso", () => {
// cy.visit("http://localhost:3000/login");
// cy.get("input[type=\"email\"]").type("a@a.com");
// cy.get("input[type=\"password\"]").type("a");
// cy.get("button").contains("Entrar").click();
// cy.url().should("include", "/");

// // Verificar se o token existe apenas após redirecionar para a página '/'
// cy.window().then((window) => {
// // Aguarde até que o token exista no localStorage
// cy.wrap(null, { timeout: 10000 }).should(() => {
// const token = window.localStorage.getItem('token');
// expect(token).to.exist;
// // Realize outras verificações conforme necessário, por exemplo, se o token possui um formato específico
// });
// });
// });
// });
describe("Página de Login", () => {
it("deve logar com sucesso", () => {
cy.visit("http://137.184.81.207:3000/login");
Expand Down Expand Up @@ -65,52 +45,3 @@ describe("Página de Login", () => {

});
});


// describe("Página de Login", () => {
// it("deve logar com sucesso", () => {
// cy.visit("http://localhost:3000/login");
// cy.get("input[type='email']").type("a@a.com");
// cy.get("input[type='password']").type("a");
// cy.get("button").contains("Entrar").click();
// cy.url().should("include", "/");

// // Função para aguardar o token estar disponível
// const waitForToken = () => {
// return new Cypress.Promise((resolve, reject) => {
// let attempts = 0;
// const maxAttempts = 20; // Número máximo de tentativas

// const checkToken = () => {
// console.log("checkToken");
// cy.window().then((window) => {
// console.log("cy.window then");
// const token = window.localStorage.getItem('token');
// console.log("cy.window then token", token);
// if (token) {
// console.log("cy.window then token if resolve", token);
// resolve(token);
// } else if (attempts < maxAttempts) {
// console.log("cy.window then else if", attempts);
// attempts++;
// setTimeout(checkToken, 1000); // Verifica novamente após 1 segundo
// } else {
// reject(new Error("Token não encontrado após várias tentativas"));
// }
// });
// };
// checkToken();
// });
// };

// // Aguarde o token e então escreva no arquivo
// waitForToken().then((token) => {
// console.log("cy.writeFile", token);
// cy.writeFile('cypress/fixtures/token.json', { token: token });
// }).catch((error) => {
// // Tratar erro se o token não for encontrado
// console.error(error);
// });

// });
// });
Loading

0 comments on commit be84a6e

Please sign in to comment.