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

fix: handle elements script request block #209

Merged
merged 1 commit into from Jul 19, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions cypress/fixtures/elements.html
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<title>Elements example</title>
<script src="../../dist/basis-theory-js.bundle.js"></script>
<script src="./event-informer.js"></script>
</head>
<body>
<script>
window.addEventListener('load', async () => {
try {
await BasisTheory.init('04ab9d12-4959-4c48-ba03-9ef722efcc5a', {
elements: true,
});
} catch (error) {
informEventCypress(error?.message || error, 'initError');
}
});
</script>
</body>
</html>
13 changes: 13 additions & 0 deletions cypress/fixtures/event-informer.js
@@ -0,0 +1,13 @@
window.informEventCypress = (event, type = event.type) => {
if (window.Cypress) {
return fetch(`http://cypress.test/events/${type}`, {
body: JSON.stringify(event),
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
}

return Promise.resolve();
};
17 changes: 16 additions & 1 deletion cypress/integration/elements.spec.ts
Expand Up @@ -11,11 +11,26 @@ context('Elements example', () => {
window.BasisTheory.elements = window.BasisTheoryElements;
}`,
});
cy.visit('examples/elements.html');
cy.visit('cypress/fixtures/elements.html');
cy.on('window:alert', (val) => {
expect(val).to.equal(
`BasisTheoryElements 04ab9d12-4959-4c48-ba03-9ef722efcc5a https://elements.basistheory.com`
);
});
});
it('should handle blocked BasisTheoryElements script request', () => {
cy.intercept(`http://cypress.test/events/initError`, (req) => {
req.reply({});
}).as(`initErrorEvent`);
cy.intercept(/https:\/\/.+?\/elements/u, {
statusCode: 400,
});
cy.visit('cypress/fixtures/elements.html');
cy.wait('@initErrorEvent')
.its('request.body')
.should(
'equal',
'There was an unknown error when loading Basis Theory Elements. Check the console for details.'
);
});
});
22 changes: 0 additions & 22 deletions examples/elements.html

This file was deleted.

4 changes: 2 additions & 2 deletions src/elements/constants.ts
Expand Up @@ -5,10 +5,10 @@ const ELEMENTS_NOM_DOM_ERROR_MESSAGE =
'Tried to load BasisTheoryElements in a non-DOM environment.';

const ELEMENTS_SCRIPT_LOAD_ERROR_MESSAGE =
'BasisTheoryElements did not load properly.';
'Basis Theory Elements did not load properly.';

const ELEMENTS_SCRIPT_UNKNOWN_ERROR_MESSAGE =
'There was an unknown error when loading BasisTheoryElements';
'There was an unknown error when loading Basis Theory Elements. Check the console for details.';

const CARD_BRANDS = [
'visa',
Expand Down
4 changes: 2 additions & 2 deletions test/elements.test.ts
Expand Up @@ -176,7 +176,7 @@ describe('Elements', () => {

loadCallback();
expect(promise).rejects.toThrow(
'BasisTheoryElements did not load properly.'
'Basis Theory Elements did not load properly.'
);
});

Expand All @@ -185,7 +185,7 @@ describe('Elements', () => {

errorCallback();
expect(promise).rejects.toThrow(
'There was an unknown error when loading BasisTheoryElements'
'There was an unknown error when loading Basis Theory Elements. Check the console for details.'
);
});

Expand Down