1
0
mirror of synced 2024-11-22 05:16:05 +03:00

test: Tests logic refactor

This commit is contained in:
Zaytsev Kirill 2021-05-25 13:20:00 +03:00
parent aee0dc977a
commit 7f864c7951
2 changed files with 68 additions and 79 deletions

View File

@ -1,79 +1,73 @@
import regexForFormat from '@/utils/regexForFormat' import regexForFormat from '@/utils/regexForFormat'
describe('regexForFormat', () => { describe('regexForFormat', () => {
it('Allows MM format with other characters', () => { test('allows MM format with other characters', () => {
expect(regexForFormat('abc/MM').test('abc/01')).toBe(true) expect(regexForFormat('abc/MM').test('abc/01')).toBe(true)
}) })
it('Fails MM format with single digit', () => { test('fails MM format with single digit', () => {
expect(regexForFormat('abc/MM').test('abc/1')).toBe(false) expect(regexForFormat('abc/MM').test('abc/1')).toBe(false)
}) })
it('Allows M format with single digit', () => { test('allows M format with single digit', () => {
expect(regexForFormat('M/abc').test('1/abc')).toBe(true) expect(regexForFormat('M/abc').test('1/abc')).toBe(true)
}) })
it('Fails MM format when out of range', () => { test.each([
expect(regexForFormat('M/abc').test('13/abc')).toBe(false) ['13/abc'],
}) ['55/abc'],
])('fails M format when out of range', (string) => {
expect(regexForFormat('M/abc').test(string)).toBe(false)
})
it('Fails M format when out of range', () => { test('replaces double digits before singles', () => {
expect(regexForFormat('M/abc').test('55/abc')).toBe(false) expect(regexForFormat('MMM').test('313131')).toBe(false)
}) })
it('Replaces double digits before singles', () => { test('allows DD format with zero digit', () => {
expect(regexForFormat('MMM').test('313131')).toBe(false) const regex = regexForFormat('xyz/DD')
})
it('Allows DD format with zero digit', () => { expect(regex.test('xyz/01')).toBe(true)
expect(regexForFormat('xyz/DD').test('xyz/01')).toBe(true) expect(regex.test('xyz/9')).toBe(false)
}) })
it('Fails DD format with single digit', () => { test('allows D format with single digit', () => {
expect(regexForFormat('xyz/DD').test('xyz/9')).toBe(false) expect(regexForFormat('xyz/D').test('xyz/9')).toBe(true)
}) })
it('Allows D format with single digit', () => { test.each([
expect(regexForFormat('xyz/D').test('xyz/9')).toBe(true) ['xyz/92'],
}) ['xyz/32'],
])('fails D format with out of range digit', string => {
expect(regexForFormat('xyz/D').test(string)).toBe(false)
})
it('Fails D format with out of range digit', () => { test.each([
expect(regexForFormat('xyz/D').test('xyz/92')).toBe(false) ['00', true],
}) ['0000', false],
])('allows YY format', (string, matches) => {
expect(regexForFormat('YY').test(string)).toBe(matches)
})
it('Fails DD format with out of range digit', () => { test('allows YYYY format with four zeros', () => {
expect(regexForFormat('xyz/D').test('xyz/32')).toBe(false) expect(regexForFormat('YYYY').test('0000')).toBe(true)
}) })
it('Allows YY format with double zeros', () => { test.each([
expect(regexForFormat('YY').test('00')).toBe(true) ['MD-YY', '12-00'],
}) ['DM-YY', '12-00'],
])('allows $format', (format, string) => {
expect(regexForFormat(format).test(string)).toBe(true)
})
it('Fails YY format with four zeros', () => { test.each([
expect(regexForFormat('YY').test('0000')).toBe(false) ['MM/DD/YYYY', '12/18/1987'],
}) ['YYYY-MM-DD', '1987-01-31']
])('$date matches $format', (format, date) => {
expect(regexForFormat(format).test(date)).toBe(true)
})
it('Allows YYYY format with four zeros', () => { test('Fails date like YYYY-MM-DD with out of bounds day', () => {
expect(regexForFormat('YYYY').test('0000')).toBe(true) expect(regexForFormat('YYYY-MM-DD').test('1987-01-32')).toBe(false)
}) })
it('Allows MD-YY', () => {
expect(regexForFormat('MD-YY').test('12-00')).toBe(true)
})
it('Allows DM-YY', () => {
expect(regexForFormat('DM-YY').test('12-00')).toBe(true)
})
it('Allows date like MM/DD/YYYY', () => {
expect(regexForFormat('MM/DD/YYYY').test('12/18/1987')).toBe(true)
})
it('Allows date like YYYY-MM-DD', () => {
expect(regexForFormat('YYYY-MM-DD').test('1987-01-31')).toBe(true)
})
it('Fails date like YYYY-MM-DD with out of bounds day', () => {
expect(regexForFormat('YYYY-MM-DD').test('1987-01-32')).toBe(false)
})
}) })

View File

@ -1,27 +1,22 @@
import snakeToCamel from '@/utils/snakeToCamel' import snakeToCamel from '@/utils/snakeToCamel'
describe('snakeToCamel', () => { describe('snakeToCamel', () => {
it('Converts underscore separated words to camelCase', () => { test.each([
expect(snakeToCamel('this_is_snake_case')).toBe('thisIsSnakeCase') ['this_is_snake_case', 'thisIsSnakeCase'],
['this_is_snake_case_2nd_example', 'thisIsSnakeCase2ndExample'],
])('converts snake_case to camelCase', (raw, expected) => {
expect(snakeToCamel(raw)).toBe(expected)
}) })
it('Converts underscore separated words to camelCase even if they start with a number', () => { test('Does not capitalize the first word or strip first underscore if a phrase starts with an underscore', () => {
expect(snakeToCamel('this_is_snake_case_2nd_example')).toBe('thisIsSnakeCase2ndExample')
})
it('Has no effect on already camelCase words', () => {
expect(snakeToCamel('thisIsCamelCase')).toBe('thisIsCamelCase')
})
it('Does not capitalize the first word or strip first underscore if a phrase starts with an underscore', () => {
expect(snakeToCamel('_this_starts_with_an_underscore')).toBe('_thisStartsWithAnUnderscore') expect(snakeToCamel('_this_starts_with_an_underscore')).toBe('_thisStartsWithAnUnderscore')
}) })
it('Ignores double underscores anywhere in a word', () => { test.each([
expect(snakeToCamel('__unlikely__thing__')).toBe('__unlikely__thing__') ['thisIsCamelCase'],
}) ['__double__underscores__'],
['this-is-kebab-case'],
it('Has no effect hyphenated words', () => { ])('has no effect', (raw) => {
expect(snakeToCamel('not-a-good-name')).toBe('not-a-good-name') expect(snakeToCamel(raw)).toBe(raw)
}) })
}) })