Skip to content

Commit

Permalink
feat: Unit tests implemented for the Tokenization SDK completed
Browse files Browse the repository at this point in the history
  • Loading branch information
milenacrios committed Feb 27, 2024
1 parent 0158203 commit 16e5b5a
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 367 deletions.
2 changes: 1 addition & 1 deletion src/async-tokenize/async-tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class AsyncTokenize {

removeFormElements(this.elements)
const tokenIdElement = createFormElement('tokenId', tokenId)
console.log(tokenIdElement)

form?.appendChild(tokenIdElement)

form?.submit()
Expand Down
10 changes: 7 additions & 3 deletions src/async-tokenize/tests/async-tokenize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
formElementsMock,
formValuesMock,
handleFormMock,
} from './mock/async-tokenize-mock'
} from '../../../tests/mocks/elements-values-mocks'
import { Malga } from '../../common/malga/malga'
import { AsyncTokenize } from '../async-tokenize'
import * as utilsValues from '../../common/utils/form-values/form-values'
Expand Down Expand Up @@ -286,7 +286,7 @@ describe('handle', () => {
const form = document.querySelector('form')
fireEvent.submit(form!)
})
test('asjnj', async () => {
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()
Expand Down Expand Up @@ -316,7 +316,11 @@ describe('handle', () => {
form.appendChild(expirationDateInput)
form.appendChild(cvvInput)

const malga = new Malga(MalgaConfigurations)
const MalgaConfigurationsProduction = {
apiKey: '17a64c8f-a387-4682-bdd8-d280493715e0',
clientId: 'd1d2b51a-0446-432a-b055-034518c2660e',
}
const malga = new Malga(MalgaConfigurationsProduction)

const asyncTokenizeObject = new AsyncTokenize(malga, formElementsMock)

Expand Down
1 change: 0 additions & 1 deletion src/common/utils/form-elements/form-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export function createFormElement(name: string, value: string) {
field.type = 'hidden'
field.name = name
field.value = value
console.log('token valor: ', field.value)

return field
}
109 changes: 28 additions & 81 deletions src/common/utils/tests/form-elements.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
import {
formElementsMock,
handleFormMock,
} from '../../../../tests/mocks/elements-values-mocks'
import {
createFormElement,
getFormElements,
removeFormElements,
} from '../form-elements/form-elements'
import { formElementsMock, handleFormMock } from './mocks/formElements-mock'

function Form() {
const { form, holderNameInput, cvvInput, expirationDateInput, numberInput } =
handleFormMock()

form.setAttribute(formElementsMock.form, '')
holderNameInput.setAttribute(formElementsMock.holderName, '')
numberInput.setAttribute(formElementsMock.number, '')
cvvInput.setAttribute(formElementsMock.cvv, '')
expirationDateInput.setAttribute(formElementsMock.expirationDate, '')

document.body.appendChild(form)
form.appendChild(holderNameInput)
form.appendChild(numberInput)
form.appendChild(expirationDateInput)
form.appendChild(cvvInput)
}

const tokenId = '54595fec-87db-44f8-996a-2f4d6bf270b9'

describe('getFormElements', () => {
test('should be returned the elements correctly', () => {
const {
Expand Down Expand Up @@ -42,25 +65,7 @@ describe('getFormElements', () => {
})

test('should be possible to find the elements in the dom', () => {
const {
form,
holderNameInput,
cvvInput,
expirationDateInput,
numberInput,
} = handleFormMock()

form.setAttribute(formElementsMock.form, '')
holderNameInput.setAttribute(formElementsMock.holderName, '')
numberInput.setAttribute(formElementsMock.number, '')
cvvInput.setAttribute(formElementsMock.cvv, '')
expirationDateInput.setAttribute(formElementsMock.expirationDate, '')

document.body.appendChild(form)
form.appendChild(holderNameInput)
form.appendChild(numberInput)
form.appendChild(expirationDateInput)
form.appendChild(cvvInput)
Form()

const formElements = getFormElements({
form: 'data-malga-tokenization-form',
Expand All @@ -78,25 +83,7 @@ describe('getFormElements', () => {
})

test("Should be returned null when elements aren't finded", () => {
const {
form,
holderNameInput,
cvvInput,
expirationDateInput,
numberInput,
} = handleFormMock()

form.setAttribute(formElementsMock.form, '')
holderNameInput.setAttribute(formElementsMock.holderName, '')
numberInput.setAttribute(formElementsMock.number, '')
cvvInput.setAttribute(formElementsMock.cvv, '')
expirationDateInput.setAttribute(formElementsMock.expirationDate, '')

document.body.appendChild(form)
form.appendChild(holderNameInput)
form.appendChild(numberInput)
form.appendChild(expirationDateInput)
form.appendChild(cvvInput)
Form()

const formElements = getFormElements({
form: 'data-malga-form',
Expand Down Expand Up @@ -133,25 +120,7 @@ describe('removeFormElements', () => {
document.body.innerHTML = ''
})
test('should be possible to return null when trying to find the elements in the dom after calling the function', () => {
const {
form,
holderNameInput,
cvvInput,
expirationDateInput,
numberInput,
} = handleFormMock()

form.setAttribute(formElementsMock.form, '')
holderNameInput.setAttribute(formElementsMock.holderName, '')
numberInput.setAttribute(formElementsMock.number, '')
cvvInput.setAttribute(formElementsMock.cvv, '')
expirationDateInput.setAttribute(formElementsMock.expirationDate, '')

document.body.appendChild(form)
form.appendChild(holderNameInput)
form.appendChild(numberInput)
form.appendChild(expirationDateInput)
form.appendChild(cvvInput)
Form()

removeFormElements({
form: 'data-malga-tokenization-form',
Expand All @@ -171,25 +140,7 @@ describe('removeFormElements', () => {
expect(document.querySelector('data-malga-tokenization-cvv')).toBeNull()
})
test('should be returned the elements in the DOM, when the function are called with selectores wrong, since the elements could not be removed', () => {
const {
form,
holderNameInput,
cvvInput,
expirationDateInput,
numberInput,
} = handleFormMock()

form.setAttribute(formElementsMock.form, '')
holderNameInput.setAttribute(formElementsMock.holderName, '')
numberInput.setAttribute(formElementsMock.number, '')
cvvInput.setAttribute(formElementsMock.cvv, '')
expirationDateInput.setAttribute(formElementsMock.expirationDate, '')

document.body.appendChild(form)
form.appendChild(holderNameInput)
form.appendChild(numberInput)
form.appendChild(expirationDateInput)
form.appendChild(cvvInput)
Form()

removeFormElements({
form: 'data-malga-form',
Expand All @@ -215,7 +166,6 @@ describe('removeFormElements', () => {

describe('createFormElements', () => {
test('should be possible to return a field with attributes of name, type and value which must be the passed tokenId', () => {
const tokenId = '54595fec-87db-44f8-996a-2f4d6bf270b9'
const tokenIdElement = createFormElement('tokenId', tokenId)

expect(tokenIdElement).toHaveAttribute('name', 'tokenId')
Expand All @@ -224,8 +174,6 @@ describe('createFormElements', () => {
})

test('should be possible for the field field to be the same as the one mounted in the dom', () => {
const tokenId = '54595fec-87db-44f8-996a-2f4d6bf270b9'

const tokenIdElement = createFormElement('tokenId', tokenId)

const field = document.createElement('input')
Expand All @@ -236,7 +184,6 @@ describe('createFormElements', () => {
expect(tokenIdElement).toEqual(field)
})
test('should be possible to insert the element in the body in the dom', () => {
const tokenId = '54595fec-87db-44f8-996a-2f4d6bf270b9'
const tokenIdElement = createFormElement('tokenId', tokenId)

document.body.appendChild(tokenIdElement)
Expand Down
80 changes: 21 additions & 59 deletions src/common/utils/tests/form-values.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@ import {
formElementsMock,
formValuesMock,
handleFormMock,
} from './mocks/formElements-mock'
} from '../../../../tests/mocks/elements-values-mocks'
import { getFormValues } from '../form-values/form-values'

function Form() {
const { form, holderNameInput, cvvInput, expirationDateInput, numberInput } =
handleFormMock()

form.setAttribute(formElementsMock.form, '')
holderNameInput.setAttribute(formElementsMock.holderName, '')
numberInput.setAttribute(formElementsMock.number, '')
cvvInput.setAttribute(formElementsMock.cvv, '')
expirationDateInput.setAttribute(formElementsMock.expirationDate, '')

document.body.appendChild(form)
form.appendChild(holderNameInput)
form.appendChild(numberInput)
form.appendChild(expirationDateInput)
form.appendChild(cvvInput)
}
describe('getFormValues', () => {
beforeEach(() => {
document.body.innerHTML = ''
})
test('should be the values of elements equals to sended', () => {
const {
form,
holderNameInput,
cvvInput,
expirationDateInput,
numberInput,
} = handleFormMock()

form.setAttribute(formElementsMock.form, '')
holderNameInput.setAttribute(formElementsMock.holderName, '')
numberInput.setAttribute(formElementsMock.number, '')
cvvInput.setAttribute(formElementsMock.cvv, '')
expirationDateInput.setAttribute(formElementsMock.expirationDate, '')

document.body.appendChild(form)
form.appendChild(holderNameInput)
form.appendChild(numberInput)
form.appendChild(expirationDateInput)
form.appendChild(cvvInput)
Form()

const inputs = document.querySelectorAll('input')
inputs[0].value = formValuesMock.holderName
Expand All @@ -45,30 +43,12 @@ describe('getFormValues', () => {
})

expect(formValue.holderName).toEqual('Taylor Swift')
expect(formValue.number).toEqual('5173 0002 6586 0114')
expect(formValue.number).toEqual('5173000265860114')
expect(formValue.expirationDate).toEqual('05/08/2024')
expect(formValue.cvv).toEqual('114')
})
test("should be returned empty when elements aren't finded", () => {
const {
form,
holderNameInput,
cvvInput,
expirationDateInput,
numberInput,
} = handleFormMock()

form.setAttribute(formElementsMock.form, '')
holderNameInput.setAttribute(formElementsMock.holderName, '')
numberInput.setAttribute(formElementsMock.number, '')
cvvInput.setAttribute(formElementsMock.cvv, '')
expirationDateInput.setAttribute(formElementsMock.expirationDate, '')

document.body.appendChild(form)
form.appendChild(holderNameInput)
form.appendChild(numberInput)
form.appendChild(expirationDateInput)
form.appendChild(cvvInput)
Form()

const formValue = getFormValues({
form: 'data-malga-tokenization-form',
Expand All @@ -84,25 +64,7 @@ describe('getFormValues', () => {
expect(formValue.cvv).toBe('')
})
test('should be possible to return error if the elements are not found', async () => {
const {
form,
holderNameInput,
cvvInput,
expirationDateInput,
numberInput,
} = handleFormMock()

form.setAttribute(formElementsMock.form, '')
holderNameInput.setAttribute(formElementsMock.holderName, '')
numberInput.setAttribute(formElementsMock.number, '')
cvvInput.setAttribute(formElementsMock.cvv, '')
expirationDateInput.setAttribute(formElementsMock.expirationDate, '')

document.body.appendChild(form)
form.appendChild(holderNameInput)
form.appendChild(numberInput)
form.appendChild(expirationDateInput)
form.appendChild(cvvInput)
Form()

const inputs = document.querySelectorAll('input')
inputs[0].value = formValuesMock.holderName
Expand Down
26 changes: 0 additions & 26 deletions src/common/utils/tests/mocks/formElements-mock.ts

This file was deleted.

Loading

0 comments on commit 16e5b5a

Please sign in to comment.