Skip to content

Commit

Permalink
Refactor translation (#6)
Browse files Browse the repository at this point in the history
* test: should be able to able to change a value informational message

* test: should be able to able to throw error if value is different from string and undefined

* test: should be able to able to throw error if value does not have the reserved keys

* test: should be able to able to not change the informational message

* refactor: setTranslationMessage

* chore: add summary of changesets
  • Loading branch information
jukerah committed Dec 26, 2023
1 parent b3ecae8 commit ae8123a
Show file tree
Hide file tree
Showing 6 changed files with 463 additions and 272 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-jeans-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vkrun": minor
---

refactor setTranslationMessage function and create tests to achieve 100% test coverage
145 changes: 144 additions & 1 deletion src/modules/location/__tests__/set-translation-message.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { informativeMessage } from '../informative-message'
import { setTranslationMessage } from '../set-translation-message'
import { resetTranslationMessage } from '../reset-translation-message'

describe('Set translation message', () => {
it('Should be able to able to change informative message', () => {
beforeEach(() => {
resetTranslationMessage()
})

it('Should be able to able to change all informative message', () => {
const newInformativeMessage = {
validator: {
constructorParams: {
Expand Down Expand Up @@ -84,4 +89,142 @@ describe('Set translation message', () => {
expect(sut).toBeTruthy()
expect(informativeMessage).toEqual(newInformativeMessage)
})

it('Should be able to able to change a value informational message', () => {
const newInformativeMessage = {
validator: {
method: {
minWord: {
noMinimumWords: '[valueName] deve ter pelo menos [minWord] palavras!'
}
}
}
}

const sut = setTranslationMessage(newInformativeMessage)

expect(sut).toBeTruthy()
expect(informativeMessage).toEqual({
validator: {
constructorParams: {
valueName: {
missingClassParam: 'missing class param: valueName is required!',
invalidClassParam: 'invalid class param: errorType provided is not valid!'
}
},
method: {
string: {
strict: '[valueName] must be a string type!'
},
minWord: {
noMinimumWords: '[valueName] deve ter pelo menos [minWord] palavras!'
},
uuid: {
strict: '[valueName] must be a uuid type!'
},
email: {
strict: 'email [value] is invalid!'
},
maxLength: {
strict: '[valueName] must have a maximum of [maxLength] characters!'
},
minLength: {
strict: '[valueName] must have a minimum of [minLength] characters!'
},
number: {
strict: '[valueName] must be a number type!'
},
float: {
strict: '[valueName] must be a number and float!'
},
integer: {
strict: '[valueName] must be a number and integer!'
},
boolean: {
strict: '[valueName] must be a boolean type!'
},
required: {
strict: '[valueName] is required!'
},
date: {
invalidFormat: 'the date [valueName] is not in the format [type]!',
invalidParameter: 'date method received invalid parameter: type is required!'
},
dateGreaterThan: {
invalidDate: 'the provided date is invalid!',
limitExceeded: 'the date [valueName] must be greater than the reference date!'
},
dateLessThan: {
invalidDate: 'the provided date is invalid!',
limitExceeded: 'the date [valueName] must be less than the reference date!'
},
time: {
invalidParameter: 'time method received invalid parameter: type is required!',
invalidFormat: 'the time [value] is not in the format [type]'
}
}
},
schema: {
validateProperty: {
itemArray: {
valueName: 'all values in the [keyName]'
}
},
validateSchema: {
keyNotDeclaredInTheSchema: '[keyName] key was not declared in the schema'
},
validateObject: {
schemaKeyAbsent: '[keyName] key is required!',
notIsArray: '[keyName] value must be an array!'
}
}
})
})

it('Should be able to able to throw error if value is different from string and undefined', () => {
const newInformativeMessage: any = {
validator: {
method: {
minWord: {
noMinimumWords: true
}
}
}
}

const sut = (): any => setTranslationMessage(newInformativeMessage)

expect(sut).toThrow('setTranslationMessage: newMessages.validator.method.minWord.noMinimumWords must be a string type!')
})

it('Should be able to able to throw error if value does not have the reserved keys', () => {
const newInformativeMessage: any = {
validator: {
method: {
minWord: {
noMinimumWords: 'valueName deve ter pelo menos minWord palavras!'
}
}
}
}

const sut = (): any => setTranslationMessage(newInformativeMessage)

expect(sut).toThrow('setTranslationMessage: noMinimumWords must contain the reserved key(s) [valueName] and [minWord]!')
})

it('Should be able to able to not change the informational message', () => {
const newInformativeMessage: any = {
validator: {
constructorParams: {
valueName: 'invalid value'
}
}
}

const sut = setTranslationMessage(newInformativeMessage)
console.log(JSON.stringify(informativeMessage, null, 2))
expect(sut).toBeTruthy()
expect(informativeMessage).toEqual(informativeMessage)
})
})
4 changes: 3 additions & 1 deletion src/modules/location/informative-message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const informativeMessage = {
import { InformativeMessage } from '../types'

export const informativeMessage: InformativeMessage = {
validator: {
constructorParams: {
valueName: {
Expand Down
82 changes: 82 additions & 0 deletions src/modules/location/reset-translation-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { InformativeMessage } from '../types'
import { informativeMessage } from './informative-message'

export const resetTranslationMessage = (): void => {
const newInformativeMessage: InformativeMessage = {
validator: {
constructorParams: {
valueName: {
missingClassParam: 'missing class param: valueName is required!',
invalidClassParam: 'invalid class param: errorType provided is not valid!'
}
},
method: {
string: {
strict: '[valueName] must be a string type!'
},
minWord: {
noMinimumWords: '[valueName] must have at least [minWord] words!'
},
uuid: {
strict: '[valueName] must be a uuid type!'
},
email: {
strict: 'email [value] is invalid!'
},
maxLength: {
strict: '[valueName] must have a maximum of [maxLength] characters!'
},
minLength: {
strict: '[valueName] must have a minimum of [minLength] characters!'
},
number: {
strict: '[valueName] must be a number type!'
},
float: {
strict: '[valueName] must be a number and float!'
},
integer: {
strict: '[valueName] must be a number and integer!'
},
boolean: {
strict: '[valueName] must be a boolean type!'
},
required: {
strict: '[valueName] is required!'
},
date: {
invalidFormat: 'the date [valueName] is not in the format [type]!',
invalidParameter: 'date method received invalid parameter: type is required!'
},
dateGreaterThan: {
invalidDate: 'the provided date is invalid!',
limitExceeded: 'the date [valueName] must be greater than the reference date!'
},
dateLessThan: {
invalidDate: 'the provided date is invalid!',
limitExceeded: 'the date [valueName] must be less than the reference date!'
},
time: {
invalidParameter: 'time method received invalid parameter: type is required!',
invalidFormat: 'the time [value] is not in the format [type]'
}
}
},
schema: {
validateProperty: {
itemArray: {
valueName: 'all values in the [keyName]'
}
},
validateSchema: {
keyNotDeclaredInTheSchema: '[keyName] key was not declared in the schema'
},
validateObject: {
schemaKeyAbsent: '[keyName] key is required!',
notIsArray: '[keyName] value must be an array!'
}
}
}

Object.assign(informativeMessage, newInformativeMessage)
}
Loading

0 comments on commit ae8123a

Please sign in to comment.