Skip to content

Commit

Permalink
refactor:
Browse files Browse the repository at this point in the history
creation of functions to reduce repetitions
  • Loading branch information
milenacrios committed Mar 1, 2024
1 parent 16e5b5a commit 3cad06c
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 131 deletions.
111 changes: 33 additions & 78 deletions src/async-tokenize/tests/async-tokenize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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'
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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',
Expand All @@ -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,
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/tests/form-elements.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
formElementsMock,
handleFormMock,
} from '../../../../tests/mocks/elements-values-mocks'
} from '../../../../tests/mocks/malga-tests-mocks'
import {
createFormElement,
getFormElements,
Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/tests/form-values.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
31 changes: 18 additions & 13 deletions src/tokenization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof import('./common/malga')>()
return {
Expand Down Expand Up @@ -85,7 +82,9 @@ describe('init', () => {

FormForInit(onSubmit)

const malgaTokenizationObject = new MalgaTokenization(MalgaConfigurations)
const malgaTokenizationObject = new MalgaTokenization(
malgaConfigurations(false),
)

malgaTokenizationObject.init()

Expand Down Expand Up @@ -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()
Expand All @@ -149,13 +150,13 @@ describe('init', () => {

FormForInit(onSubmit)

const MalgaConfigurationsEmpty = {
const malgaConfigurationsEmpty = {
apiKey: '',
clientId: '',
}

const malgaTokenizationObject = new MalgaTokenization(
MalgaConfigurationsEmpty,
malgaConfigurationsEmpty,
)

await waitFor(() => {
Expand All @@ -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)

Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 3cad06c

Please sign in to comment.