Skip to content

Commit

Permalink
feat: unit tests for tokenize class
Browse files Browse the repository at this point in the history
  • Loading branch information
milenacrios committed Feb 21, 2024
1 parent b919063 commit 0ebf7f7
Show file tree
Hide file tree
Showing 13 changed files with 435 additions and 53 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 @@ -5,7 +5,7 @@ import {
removeFormElements,
createFormElement,
getFormValues,
} from 'src/common/utils'
} from '../common/utils'

export class AsyncTokenize {
constructor(
Expand Down
64 changes: 64 additions & 0 deletions src/async-tokenize/tests/async-tokenize.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Malga } from '../../common/malga/malga'
import { AsyncTokenize } from '../async-tokenize'
import {
formElementsMock,
formValuesMock,
handleFormMock,
} from './mock/async-tokenize-mock'

const MalgaConfigurations = {
apiKey: '17a64c8f-a387-4682-bdd8-d280493715e0',
clientId: 'd1d2b51a-0446-432a-b055-034518c2660e',
options: {
sandbox: true,
},
}

vi.mock('../../common/malga/malga', async (importOriginal) => {
const Malga =
await importOriginal<typeof import('../../common/malga/malga')>()
return {
...Malga,
tokenization: vi.fn(),
}
})

describe('handle', () => {
test('askmsk', () => {
const {
form,
holderNameInput,
cvvInput,
expirationDateInput,
numberInput,
} = handleFormMock()

form.action = '/teste'
form.method = 'POST'
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 inputs = document.querySelectorAll('input')
inputs[0].value = formValuesMock.holderName
inputs[1].value = formValuesMock.number
inputs[2].value = formValuesMock.expirationDate
inputs[3].value = formValuesMock.cvv

const malga = new Malga(MalgaConfigurations)

const asyncTokenizeObject = new AsyncTokenize(malga, formElementsMock)

asyncTokenizeObject.handle()
console.log(inputs.length)
})
})
26 changes: 26 additions & 0 deletions src/async-tokenize/tests/mock/async-tokenize-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { MalgaFormElements } from 'src/common/interfaces'

export const formValuesMock = {
holderName: 'Taylor Swift',
number: '5173000265860114',
expirationDate: '05/08/2024',
cvv: '114',
}

export const handleFormMock = () => {
return {
form: document.createElement('form'),
holderNameInput: document.createElement('input'),
numberInput: document.createElement('input'),
expirationDateInput: document.createElement('input'),
cvvInput: document.createElement('input'),
}
}

export const formElementsMock: MalgaFormElements = {
form: 'data-malga-tokenization-form',
holderName: 'data-malga-tokenization-holder-name',
number: 'data-malga-tokenization-number',
expirationDate: 'data-malga-tokenization-expiration-date',
cvv: 'data-malga-tokenization-cvv',
}
8 changes: 4 additions & 4 deletions src/common/malga/interfaces/tokenization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface TokenizationPayload {
holderName: string
number: string
expirationDate: string
cvv: string
holderName?: string
number?: string
expirationDate?: string
cvv?: string
}
2 changes: 1 addition & 1 deletion src/common/malga/malga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Malga', () => {
cvv: '951',
expirationDate: '05/2030',
})

console.log('response: ', response)
expect(response).toMatchObject({ tokenId: 'sandbox-token-id' })
})

Expand Down
8 changes: 4 additions & 4 deletions src/common/utils/form-values/form-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export function getFormValues(elements: MalgaFormElements) {
const formElements = getFormElements(elements)

return {
holderName: formElements.holderName?.value,
cvv: formElements.cvv?.value,
expirationDate: formElements.expirationDate?.value,
number: formElements.number?.value,
holderName: formElements.holderName!.value,
cvv: formElements.cvv!.value,
expirationDate: formElements.expirationDate!.value,
number: formElements.number!.value,
}
}
39 changes: 0 additions & 39 deletions src/common/utils/tests/form-values.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,43 +86,4 @@ describe('getFormValues', () => {
expect(formValue.expirationDate).toBe('')
expect(formValue.cvv).toBe('')
})
test('should be possible to return undefined if the elements are not found', () => {
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 inputs = document.querySelectorAll('input')
inputs[0].value = formValuesMock.holderName
inputs[1].value = formValuesMock.number
inputs[2].value = formValuesMock.expirationDate
inputs[3].value = formValuesMock.cvv

const formValue = getFormValues({
form: 'data-malga-form',
holderName: 'data-malga-holder-name',
number: 'data-malga-number',
expirationDate: 'data-malga-expiration-date',
cvv: 'data-malga-cvv',
})
expect(formValue.holderName).toBeUndefined()
expect(formValue.number).toBeUndefined()
expect(formValue.expirationDate).toBeUndefined()
expect(formValue.cvv).toBeUndefined()
})
})
20 changes: 20 additions & 0 deletions src/tokenization.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MalgaTokenization } from './tokenization'

describe('Constructor', () => {
test('should be possible for MalgaTokenizationObject to be an instance of the MalgaTokenization class', () => {
const MalgaConfigurations = {
apiKey: '44829c89-ae91-4271-b347-5024a06a3fc3',
clientId: 'af1f76f7-b461-430e-abac-4f1b010a1629',
options: {
sandbox: true,
},
}
const MalgaTokenizationObject = new MalgaTokenization(MalgaConfigurations)

expect(MalgaTokenizationObject).toBeInstanceOf(MalgaTokenization)
})
})

describe('init', () => {
test('ajsjns', () => {})
})
6 changes: 3 additions & 3 deletions src/tokenization.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Malga } from './common/malga'

import { AsyncTokenize } from './async-tokenize'
import { Tokenize } from './tokenize'

import type {
MalgaConfigurations,
MalgaConfigurationsElements,
MalgaFormElements,
} from 'src/common/interfaces'

import { AsyncTokenize } from './async-tokenize'
import { Tokenize } from './tokenize'

export class MalgaTokenization {
private readonly malga: Malga
private readonly elements: MalgaFormElements
Expand Down
34 changes: 34 additions & 0 deletions src/tokenize/tests/mock/tokenize-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { MalgaFormElements } from 'src/common/interfaces'

export const formValuesMock = {
holderName: 'Taylor Swift',
number: '5173000265860114',
expirationDate: '05/08/2024',
cvv: '114',
}

export const handleFormMock = () => {
return {
form: document.createElement('form'),
holderNameInput: document.createElement('input'),
numberInput: document.createElement('input'),
expirationDateInput: document.createElement('input'),
cvvInput: document.createElement('input'),
}
}

export const formElementsMock: MalgaFormElements = {
form: 'data-malga-tokenization-form',
holderName: 'data-malga-tokenization-holder-name',
number: 'data-malga-tokenization-number',
expirationDate: 'data-malga-tokenization-expiration-date',
cvv: 'data-malga-tokenization-cvv',
}

export const formElementsMock2: MalgaFormElements = {
form: 'data-tokenization-form',
holderName: 'data-tokenization-holder-name',
number: 'data-tokenization-number',
expirationDate: 'data-tokenization-expiration-date',
cvv: 'data-tokenization-cvv',
}
Loading

0 comments on commit 0ebf7f7

Please sign in to comment.