Skip to content

Commit

Permalink
test intercept cypress
Browse files Browse the repository at this point in the history
  • Loading branch information
pivilartisant committed Nov 2, 2023
1 parent 1d991d9 commit 4d8fdca
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 19 deletions.
62 changes: 44 additions & 18 deletions web-frontend/cypress/e2e/acceptance/home/send.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,51 @@ describe('E2E | Acceptance | Home | Send', () => {
cy.url().should('eq', `${baseUrl}/${account.nickname}/home`);
});

it('should process any % as input', () => {
navigateToTransfercoins(2);

cy.wait(1000);

cy.get(`[data-testid="send-percent-25"]`).click();
cy.get('[data-testid="currency-field"').should('have.value', '250 MAS');

cy.get(`[data-testid="send-percent-50"]`).click();
cy.get('[data-testid="currency-field"').should('have.value', '500 MAS');

cy.get(`[data-testid="send-percent-75"]`).click();
cy.get('[data-testid="currency-field"').should('have.value', '750 MAS');
it('should process any % as input', async () => {
server.trackRequest = true;
const account = mockedAccounts.at(2);

cy.get(`[data-testid="send-percent-100"]`).click();
cy.get('[data-testid="currency-field"').should(
'have.value',
'999.999999 MAS',
);
cy.visit('/');
cy.get('[data-testid="account-2"]')
.click()
.then(() => {
cy.url().should('eq', `${baseUrl}/${account.nickname}/home`);
cy.waitForRequest(server, '/accounts/MarioX', 'GET').then(() => {
cy.get('[data-testid="send-button"]')
.click()
.then(() => {
cy.waitForRequest(server, '/accounts/MarioX', 'GET');
cy.url().should(
'eq',
`${baseUrl}/${account.nickname}/transfer-coins`,
);
cy.get(`[data-testid="send-percent-25"]`).click();
cy.get('[data-testid="currency-field"').should(
'have.value',
'250 MAS',
);

cy.get(`[data-testid="send-percent-50"]`).click();
cy.get('[data-testid="currency-field"').should(
'have.value',
'500 MAS',
);

cy.get(`[data-testid="send-percent-75"]`).click();
cy.get('[data-testid="currency-field"').should(
'have.value',
'750 MAS',
);

cy.get(`[data-testid="send-percent-100"]`).click();
cy.get('[data-testid="currency-field"').should(
'have.value',
'999.999999 MAS',
);
});
});
});
server.trackRequest = false;
});

it('should transfer to accounts', () => {
Expand Down
37 changes: 37 additions & 0 deletions web-frontend/cypress/support/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,40 @@ Cypress.on('window:before:load', (win) => {
});
};
});

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import 'cypress-wait-until';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Cypress.Commands.add('waitForRequest', (server, urlPattern, method = '') => {
const wildcardRegex = /\*/g;
const questionMarkRegex = /\?/g;

const replacedWildCard = urlPattern
.replace(wildcardRegex, '[^ ]*')
.replace(questionMarkRegex, '\\?');
const regexUrl = new RegExp(replacedWildCard, 'g');
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const checkMatches = (r) => {
let isMatched = r.url.match(regexUrl);
if (method) {
isMatched = isMatched && r.method.toLowerCase() === method.toLowerCase();
}

return isMatched;
};

const requestFound = () =>
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
server.pretender.handledRequests.some(checkMatches);
const errorMsg = `[Request [${method}] to ${urlPattern} didn't happen`;

return cy
.waitUntil(requestFound, { errorMsg })
.should('be.true')
.log(`Request [${method}] to ${urlPattern} has been finished`);
});
7 changes: 7 additions & 0 deletions web-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions web-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"babel-jest": "^29.5.0",
"cypress": "^13.3.1",
"cypress-image-diff-js": "^1.31.0",
"cypress-wait-until": "^2.0.1",
"eslint": "^8.38.0",
"eslint-plugin-html": "^7.1.0",
"eslint-plugin-import": "^2.28.0",
Expand Down
1 change: 1 addition & 0 deletions web-frontend/src/mirage/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AccountObject } from '@/models/AccountModel';

export function mockServer(environment = ENV.DEV) {
const server = createServer({
trackRequests: true,
environment,
models,
factories,
Expand Down
2 changes: 1 addition & 1 deletion web-frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"@/*": ["./src/*"]
}
},
"include": ["src", "cypress"],
"include": ["src", "cypress", "cypress-wait-until"],
"references": [{ "path": "./tsconfig.node.json" }]
}

0 comments on commit 4d8fdca

Please sign in to comment.