diff --git a/src/async-tokenize/tests/async-tokenize.test.ts b/src/async-tokenize/tests/async-tokenize.test.ts index b4e2c79..25a4e08 100644 --- a/src/async-tokenize/tests/async-tokenize.test.ts +++ b/src/async-tokenize/tests/async-tokenize.test.ts @@ -3,19 +3,14 @@ import { formElementsMock, formValuesMock, handleFormMock, -} from '../../../tests/mocks/elements-values-mocks' + malgaConfigurations, + configureFormSubmissionMock, +} from '../../../tests/mocks/malga-tests-mocks' import { Malga } from '../../common/malga/malga' import { AsyncTokenize } from '../async-tokenize' import * as utilsValues from '../../common/utils/form-values/form-values' import * as utilsElements from '../../common/utils/form-elements/form-elements' -const MalgaConfigurations = { - apiKey: '17a64c8f-a387-4682-bdd8-d280493715e0', - clientId: 'd1d2b51a-0446-432a-b055-034518c2660e', - options: { - sandbox: true, - }, -} const onSubmit = vi.fn() vi.mock('../../common/malga/malga', async (importOriginal) => { @@ -27,12 +22,12 @@ vi.mock('../../common/malga/malga', async (importOriginal) => { } }) -function Form(props: any) { +function generateForm(onSubmit: any) { const { form, holderNameInput, cvvInput, expirationDateInput, numberInput } = handleFormMock() form.setAttribute(formElementsMock.form, '') - form.onsubmit = props.onSubmit + form.onsubmit = onSubmit form.id = 'form' form.method = 'POST' form.action = '/test' @@ -60,14 +55,10 @@ describe('handle', () => { document.body.innerHTML = '' }) test('should be possible to find a tokenId element in the DOM and consequently contained in the form element', async () => { - window.HTMLFormElement.prototype.submit = () => {} - onSubmit.mockImplementation((event) => { - event.preventDefault() - }) - - Form(onSubmit) + configureFormSubmissionMock() + generateForm(onSubmit) - const malga = new Malga(MalgaConfigurations) + const malga = new Malga(malgaConfigurations(false)) const asyncTokenizeObject = new AsyncTokenize(malga, formElementsMock) @@ -91,22 +82,11 @@ describe('handle', () => { }) }) test('should be possible to return a value in tokenIdElement and if the settings include the sandbox: true option, the value must be sandox-token-id ', async () => { - window.HTMLFormElement.prototype.submit = () => {} - onSubmit.mockImplementation((event) => { - event.preventDefault() - }) + configureFormSubmissionMock() - Form(onSubmit) + generateForm(onSubmit) - const MalgaConfigurationsSandBox = { - apiKey: '17a64c8f-a387-4682-bdd8-d280493715e0', - clientId: 'd1d2b51a-0446-432a-b055-034518c2660e', - options: { - sandbox: true, - }, - } - - const malga = new Malga(MalgaConfigurationsSandBox) + const malga = new Malga(malgaConfigurations(true)) const asyncTokenizeObject = new AsyncTokenize(malga, formElementsMock) @@ -123,19 +103,16 @@ describe('handle', () => { }) }) test('should be possible to return a value in tokenIdElement and if the settings not include the sandbox: true option, the value must be production-token-id', async () => { - window.HTMLFormElement.prototype.submit = () => {} - onSubmit.mockImplementation((event) => { - event.preventDefault() - }) + configureFormSubmissionMock() - Form(onSubmit) + generateForm(onSubmit) - const MalgaConfigurationsProduction = { + const malgaConfigurationsProduction = { apiKey: '17a64c8f-a387-4682-bdd8-d280493715e0', clientId: 'd1d2b51a-0446-432a-b055-034518c2660e', } - const malga = new Malga(MalgaConfigurationsProduction) + const malga = new Malga(malgaConfigurationsProduction) const asyncTokenizeObject = new AsyncTokenize(malga, formElementsMock) @@ -153,14 +130,11 @@ describe('handle', () => { }) }) test('should be possible to remove the elements and thus there is only 1 after the creation of the tokenIdElement and 4 before its creation', async () => { - window.HTMLFormElement.prototype.submit = () => {} - onSubmit.mockImplementation((event) => { - event.preventDefault() - }) + configureFormSubmissionMock() - Form(onSubmit) + generateForm(onSubmit) - const malga = new Malga(MalgaConfigurations) + const malga = new Malga(malgaConfigurations(false)) const asyncTokenizeObject = new AsyncTokenize(malga, formElementsMock) @@ -178,14 +152,11 @@ describe('handle', () => { }) }) test('should be possible for handle to call the getFormElements, getFormValues, Tokenization, removeFormElements and createFormElements functions passing the elements correctly', async () => { - window.HTMLFormElement.prototype.submit = () => {} - onSubmit.mockImplementation((event) => { - event.preventDefault() - }) + configureFormSubmissionMock() - Form(onSubmit) + generateForm(onSubmit) - const malga = new Malga(MalgaConfigurations) + const malga = new Malga(malgaConfigurations(false)) const asyncTokenizeObject = new AsyncTokenize(malga, formElementsMock) @@ -216,16 +187,13 @@ describe('handle', () => { }) test('should be possible to check if the form was submitted and event.preventDefault was called afterwards', async () => { const submit = vi.fn() - window.HTMLFormElement.prototype.submit = submit - onSubmit.mockImplementation((event) => { - event.preventDefault() - }) + configureFormSubmissionMock(submit) const event = new Event('submit') const preventDefault = vi.spyOn(event, 'preventDefault') - Form(onSubmit) + generateForm(onSubmit) - const malga = new Malga(MalgaConfigurations) + const malga = new Malga(malgaConfigurations(false)) const asyncTokenizeObject = new AsyncTokenize(malga, formElementsMock) @@ -240,19 +208,16 @@ describe('handle', () => { }) }) test('should be possible to return an error if empty apiKey and clientId are sent to the Malga constructor', async () => { - window.HTMLFormElement.prototype.submit = () => {} - onSubmit.mockImplementation((event) => { - event.preventDefault() - }) + configureFormSubmissionMock() - Form(onSubmit) + generateForm(onSubmit) - const MalgaConfigurationsEmpty = { + const malgaConfigurationsEmpty = { apiKey: '', clientId: '', } - const malga = new Malga(MalgaConfigurationsEmpty) + const malga = new Malga(malgaConfigurationsEmpty) const asyncTokenizeObject = new AsyncTokenize(malga, formElementsMock) @@ -262,14 +227,11 @@ describe('handle', () => { fireEvent.submit(form!) }) test('should be possible to throw an error when the elements passed are incompatible with those in the DOM', async () => { - window.HTMLFormElement.prototype.submit = () => {} - onSubmit.mockImplementation((event) => { - event.preventDefault() - }) + configureFormSubmissionMock() - Form(onSubmit) + generateForm(onSubmit) - const malga = new Malga(MalgaConfigurations) + const malga = new Malga(malgaConfigurations(false)) const asyncTokenizeObject = new AsyncTokenize(malga, { form: 'data-form', @@ -287,10 +249,7 @@ describe('handle', () => { fireEvent.submit(form!) }) test('should be possible to return an error if the form inputs do not have values assigned', async () => { - window.HTMLFormElement.prototype.submit = () => {} - onSubmit.mockImplementation((event) => { - event.preventDefault() - }) + configureFormSubmissionMock() const { form, holderNameInput, @@ -316,11 +275,7 @@ describe('handle', () => { form.appendChild(expirationDateInput) form.appendChild(cvvInput) - const MalgaConfigurationsProduction = { - apiKey: '17a64c8f-a387-4682-bdd8-d280493715e0', - clientId: 'd1d2b51a-0446-432a-b055-034518c2660e', - } - const malga = new Malga(MalgaConfigurationsProduction) + const malga = new Malga(malgaConfigurations(false)) const asyncTokenizeObject = new AsyncTokenize(malga, formElementsMock) diff --git a/src/common/utils/tests/form-elements.test.ts b/src/common/utils/tests/form-elements.test.ts index 0adee3d..7563e56 100644 --- a/src/common/utils/tests/form-elements.test.ts +++ b/src/common/utils/tests/form-elements.test.ts @@ -1,7 +1,7 @@ import { formElementsMock, handleFormMock, -} from '../../../../tests/mocks/elements-values-mocks' +} from '../../../../tests/mocks/malga-tests-mocks' import { createFormElement, getFormElements, diff --git a/src/common/utils/tests/form-values.test.ts b/src/common/utils/tests/form-values.test.ts index 89d4ffd..4d64aa6 100644 --- a/src/common/utils/tests/form-values.test.ts +++ b/src/common/utils/tests/form-values.test.ts @@ -2,7 +2,7 @@ import { formElementsMock, formValuesMock, handleFormMock, -} from '../../../../tests/mocks/elements-values-mocks' +} from '../../../../tests/mocks/malga-tests-mocks' import { getFormValues } from '../form-values/form-values' function Form() { diff --git a/src/tokenization.test.ts b/src/tokenization.test.ts index 2e35e85..5be096f 100644 --- a/src/tokenization.test.ts +++ b/src/tokenization.test.ts @@ -3,13 +3,10 @@ import { formElementsMock, formValuesMock, handleFormMock, -} from '../tests/mocks/elements-values-mocks' + malgaConfigurations, +} from '../tests/mocks/malga-tests-mocks' import { MalgaTokenization } from './tokenization' -const MalgaConfigurations = { - apiKey: '17a64c8f-a387-4682-bdd8-d280493715e0', - clientId: 'd1d2b51a-0446-432a-b055-034518c2660e', -} vi.mock('./common/malga', async (importOriginal) => { const Malga = await importOriginal() return { @@ -85,7 +82,9 @@ describe('init', () => { FormForInit(onSubmit) - const malgaTokenizationObject = new MalgaTokenization(MalgaConfigurations) + const malgaTokenizationObject = new MalgaTokenization( + malgaConfigurations(false), + ) malgaTokenizationObject.init() @@ -132,7 +131,9 @@ describe('init', () => { form.appendChild(expirationDateInput) form.appendChild(cvvInput) - const malgaTokenizationObject = new MalgaTokenization(MalgaConfigurations) + const malgaTokenizationObject = new MalgaTokenization( + malgaConfigurations(false), + ) await waitFor(() => { expect(malgaTokenizationObject.init).rejects.toThrowError() @@ -149,13 +150,13 @@ describe('init', () => { FormForInit(onSubmit) - const MalgaConfigurationsEmpty = { + const malgaConfigurationsEmpty = { apiKey: '', clientId: '', } const malgaTokenizationObject = new MalgaTokenization( - MalgaConfigurationsEmpty, + malgaConfigurationsEmpty, ) await waitFor(() => { @@ -172,7 +173,9 @@ describe('tokenize', () => { }) test('should be possible to return a not falsy value equal to production-token-id', async () => { window.HTMLFormElement.prototype.submit = () => {} - const malgaTokenizationObject = new MalgaTokenization(MalgaConfigurations) + const malgaTokenizationObject = new MalgaTokenization( + malgaConfigurations(false), + ) FormForTokenize(handleSubmit) @@ -195,7 +198,9 @@ describe('tokenize', () => { test('should be possible to return an error if form elements do not have values assigned', async () => { window.HTMLFormElement.prototype.submit = () => {} - const malgaTokenizationObject = new MalgaTokenization(MalgaConfigurations) + const malgaTokenizationObject = new MalgaTokenization( + malgaConfigurations(false), + ) const { form, @@ -234,13 +239,13 @@ describe('tokenize', () => { test('should be possible to return an error if apiKey and clientId are passed empty', () => { window.HTMLFormElement.prototype.submit = () => {} - const MalgaConfigurationsEmpty = { + const malgaConfigurationsEmpty = { apiKey: '', clientId: '', } const malgaTokenizationObject = new MalgaTokenization( - MalgaConfigurationsEmpty, + malgaConfigurationsEmpty, ) FormForTokenize(handleSubmit) diff --git a/src/tokenize/tests/tokenize.test.ts b/src/tokenize/tests/tokenize.test.ts index a01fa2b..abd2703 100644 --- a/src/tokenize/tests/tokenize.test.ts +++ b/src/tokenize/tests/tokenize.test.ts @@ -2,16 +2,12 @@ import { formElementsMock, formValuesMock, handleFormMock, -} from '../../../tests/mocks/elements-values-mocks' + malgaConfigurations, +} from '../../../tests/mocks/malga-tests-mocks' import { Malga } from '../../common/malga/malga' import { Tokenize } from '../tokenize' import * as utils from '../../common/utils/form-values/form-values' -const MalgaConfigurations = { - apiKey: '17a64c8f-a387-4682-bdd8-d280493715e0', - clientId: 'd1d2b51a-0446-432a-b055-034518c2660e', -} - vi.mock('../../common/malga/malga', async (importOriginal) => { const Malga = await importOriginal() @@ -21,7 +17,7 @@ vi.mock('../../common/malga/malga', async (importOriginal) => { } }) -function Form() { +function generateForm() { const { form, holderNameInput, cvvInput, expirationDateInput, numberInput } = handleFormMock() @@ -49,9 +45,9 @@ describe('handle', () => { document.body.innerHTML = '' }) test('should be an asynchronous function and return a promise', async () => { - Form() + generateForm() - const malga = new Malga(MalgaConfigurations) + const malga = new Malga(malgaConfigurations(false)) const tokenizeObject = new Tokenize(malga, formElementsMock) @@ -59,9 +55,9 @@ describe('handle', () => { await expect(tokenizeObject.handle()).resolves.toBeDefined() }) test('should be possible for a tokenId to exist when elements are passed correctly', async () => { - Form() + generateForm() - const malga = new Malga(MalgaConfigurations) + const malga = new Malga(malgaConfigurations(false)) const tokenizeObject = new Tokenize(malga, formElementsMock) const tokenId = await tokenizeObject.handle() @@ -69,17 +65,9 @@ describe('handle', () => { expect(tokenId).toBeTruthy() }) test('should be possible to return a tokenId equal to sandbox-token-id when configurations include sandbox equal to true', async () => { - Form() - - const MalgaConfigurationsSandbox = { - apiKey: '17a64c8f-a387-4682-bdd8-d280493715e0', - clientId: 'd1d2b51a-0446-432a-b055-034518c2660e', - options: { - sandbox: true, - }, - } + generateForm() - const malga = new Malga(MalgaConfigurationsSandbox) + const malga = new Malga(malgaConfigurations(true)) const tokenizeObject = new Tokenize(malga, formElementsMock) @@ -87,14 +75,9 @@ describe('handle', () => { expect(tokenId).toMatchObject({ tokenId: 'sandbox-token-id' }) }) test('should be possible to return a tokenId equal to sandbox-token-id when configurations include production equal to true', async () => { - Form() - - const MalgaConfigurationsProduction = { - apiKey: '17a64c8f-a387-4682-bdd8-d280493715e0', - clientId: 'd1d2b51a-0446-432a-b055-034518c2660e', - } + generateForm() - const malga = new Malga(MalgaConfigurationsProduction) + const malga = new Malga(malgaConfigurations(false)) const tokenizeObject = new Tokenize(malga, formElementsMock) @@ -102,9 +85,9 @@ describe('handle', () => { expect(tokenId).toMatchObject({ tokenId: 'production-token-id' }) }) test('should be possible to return error when elements are not passed correctly', async () => { - Form() + generateForm() - const malga = new Malga(MalgaConfigurations) + const malga = new Malga(malgaConfigurations(false)) const elementsMock = { form: 'jenjen', @@ -120,23 +103,23 @@ describe('handle', () => { ) }) test('should be possible to return an error if the apiKey and clientId settings are empty', async () => { - Form() + generateForm() - const MalgaConfigurationsEmpty = { + const malgaConfigurationsEmpty = { apiKey: '', clientId: '', } - const malga = new Malga(MalgaConfigurationsEmpty) + const malga = new Malga(malgaConfigurationsEmpty) const tokenizeObject = new Tokenize(malga, formElementsMock) await expect(tokenizeObject.handle).rejects.toThrowError() }) test('should be possible to perform the tokenization function and getFormValues with the respective elements correctly', async () => { - Form() + generateForm() - const malga = new Malga(MalgaConfigurations) + const malga = new Malga(malgaConfigurations(false)) const tokenizeObject = new Tokenize(malga, formElementsMock) @@ -154,9 +137,9 @@ describe('handle', () => { expect(getFormValuesSpy).toHaveBeenCalledWith(formElementsMock) }) test('should be possible that the getFormValues function is correctly returning the values assigned in the DOM', async () => { - Form() + generateForm() - const malga = new Malga(MalgaConfigurations) + const malga = new Malga(malgaConfigurations(false)) const tokenizeObject = new Tokenize(malga, formElementsMock) @@ -192,7 +175,7 @@ describe('handle', () => { form.appendChild(expirationDateInput) form.appendChild(cvvInput) - const malga = new Malga(MalgaConfigurations) + const malga = new Malga(malgaConfigurations(false)) const tokenizeObject = new Tokenize(malga, formElementsMock) diff --git a/tests/mocks/elements-values-mocks.ts b/tests/mocks/malga-tests-mocks.ts similarity index 59% rename from tests/mocks/elements-values-mocks.ts rename to tests/mocks/malga-tests-mocks.ts index b104873..c9f56e7 100644 --- a/tests/mocks/elements-values-mocks.ts +++ b/tests/mocks/malga-tests-mocks.ts @@ -1,5 +1,25 @@ import { MalgaFormElements } from '../../src/common/interfaces/form' +export const malgaConfigurations = (isSandbox: boolean) => { + return { + apiKey: '17a64c8f-a387-4682-bdd8-d280493715e0', + clientId: 'd1d2b51a-0446-432a-b055-034518c2660e', + options: { + sandbox: isSandbox, + }, + } +} + +export const configureFormSubmissionMock = (eventSubmit?: any) => { + eventSubmit + ? (window.HTMLFormElement.prototype.submit = eventSubmit) + : (window.HTMLFormElement.prototype.submit = () => {}) + const onSubmit = vi.fn() + onSubmit.mockImplementation((event) => { + event.preventDefault() + }) +} + export const formValuesMock = { holderName: 'Taylor Swift', number: '5173000265860114',