-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit tests for email-input config file tckt-362
- Loading branch information
kalasgarov
committed
Nov 8, 2024
1 parent
3857b59
commit 270daad
Showing
2 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
packages/forms/src/patterns/email-input/email-input.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { | ||
createEmailSchema, | ||
emailInputConfig, | ||
type EmailInputPattern, | ||
} from './email-input'; | ||
|
||
describe('EmailInputPattern tests', () => { | ||
describe('createEmailSchema', () => { | ||
it('should create schema for required email input', () => { | ||
const data: EmailInputPattern['data'] = { | ||
label: 'Test Email Input Label', | ||
required: true, | ||
}; | ||
|
||
const schema = createEmailSchema(data); | ||
const validInput = { email: '[email protected]' }; | ||
const invalidInput = { email: 'testEmail.com' }; | ||
|
||
expect(schema.safeParse(validInput).success).toBe(true); | ||
expect(schema.safeParse(invalidInput).success).toBe(false); | ||
}); | ||
|
||
it('should create schema for optional email input', () => { | ||
const data: EmailInputPattern['data'] = { | ||
label: 'Test Email Input Label', | ||
required: false, | ||
}; | ||
|
||
const schema = createEmailSchema(data); | ||
const validInput = { email: '[email protected]' }; | ||
const emptyInput = {}; | ||
|
||
expect(schema.safeParse(validInput).success).toBe(true); | ||
expect(schema.safeParse(emptyInput).success).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('emailInputConfig', () => { | ||
it('should parse user input correctly', () => { | ||
const pattern: EmailInputPattern = { | ||
type: 'email-input', | ||
id: 'test', | ||
data: { | ||
label: 'Test Email Input Label', | ||
required: true, | ||
}, | ||
}; | ||
|
||
const inputValue = { email: '[email protected]' }; | ||
if (!emailInputConfig.parseUserInput) { | ||
expect.fail('emailInputConfig.parseUserInput is undefined'); | ||
} | ||
const result = emailInputConfig.parseUserInput(pattern, inputValue); | ||
if (result.success) { | ||
expect(result.data).toEqual(inputValue); | ||
} else { | ||
throw new Error('Unexpected validation failure'); | ||
} | ||
}); | ||
|
||
it('should handle validation error for user input', () => { | ||
const pattern: EmailInputPattern = { | ||
type: 'email-input', | ||
id: 'test', | ||
data: { | ||
label: 'Test Email Input Label', | ||
required: true, | ||
}, | ||
}; | ||
|
||
const inputValue = { email: 'testEmail.co' }; | ||
if (!emailInputConfig.parseUserInput) { | ||
expect.fail('emailInputConfig.parseUserInput is undefined'); | ||
} | ||
const result = emailInputConfig.parseUserInput(pattern, inputValue); | ||
if (!result.success) { | ||
expect(result.error).toBeDefined(); | ||
} else { | ||
throw new Error('Unexpected validation success'); | ||
} | ||
}); | ||
|
||
it('should parse config data correctly', () => { | ||
const obj = { | ||
label: 'Test Email Input Label', | ||
required: true, | ||
}; | ||
|
||
if (!emailInputConfig.parseConfigData) { | ||
expect.fail('emailInputConfig.parseConfigData is undefined'); | ||
} | ||
const result = emailInputConfig.parseConfigData(obj); | ||
if (result.success) { | ||
expect(result.data.label).toBe('Test Email Input Label'); | ||
expect(result.data.required).toBe(true); | ||
} else { | ||
throw new Error('Unexpected validation failure'); | ||
} | ||
}); | ||
|
||
it('should handle invalid config data', () => { | ||
const obj = { | ||
label: '', | ||
required: true, | ||
}; | ||
|
||
if (!emailInputConfig.parseConfigData) { | ||
expect.fail('emailInputConfig.parseConfigData is undefined'); | ||
} | ||
const result = emailInputConfig.parseConfigData(obj); | ||
if (!result.success) { | ||
expect(result.error).toBeDefined(); | ||
} else { | ||
throw new Error('Unexpected validation success'); | ||
} | ||
}); | ||
}); | ||
}); |