Skip to content

Commit

Permalink
test: add unit tests for email-input config file tckt-362
Browse files Browse the repository at this point in the history
  • Loading branch information
kalasgarov committed Nov 8, 2024
1 parent 3857b59 commit 270daad
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { FormProvider, useForm } from 'react-hook-form';
import { type Meta, type StoryObj } from '@storybook/react';

import { EmailInputPattern } from './EmailInput.js'; // Ensure the path is correct if different
import { EmailInputPattern } from './EmailInput.js';

const meta: Meta<typeof EmailInputPattern> = {
title: 'patterns/EmailInputPattern',
Expand Down
119 changes: 119 additions & 0 deletions packages/forms/src/patterns/email-input/email-input.test.ts
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');
}
});
});
});

0 comments on commit 270daad

Please sign in to comment.