Skip to content

Commit

Permalink
Test/validate email (#161)
Browse files Browse the repository at this point in the history
* test : validateEmail props

* test : email test logic 수정
  • Loading branch information
hovelopin authored Oct 10, 2023
1 parent a353fd0 commit 25fbc64
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions test/validateEmail.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { cleanup, fireEvent, render, waitFor } from '@testing-library/react';
import { ReactMultiEmail } from '../react-multi-email';
import React from 'react';

afterEach(cleanup);

it('ReactMultiEmail validateEmail function works test', async () => {
const mockValidateEmailFunc = jest.fn();

const { getByRole } = render(
<ReactMultiEmail
validateEmail={mockValidateEmailFunc}
getLabel={(email, index, removeEmail) => {
return (
<div data-tag key={index}>
<div data-tag-item>{email}</div>
<span data-tag-handle onClick={() => removeEmail(index)}>
×
</span>
</div>
);
}}
/>,
);

const input = getByRole('textbox') as HTMLElement;

fireEvent.change(input, { target: { value: 'abc@gmail.com' } });
fireEvent.keyUp(input, { key: 'Enter', code: 'Enter' });

await waitFor(() => {
expect(mockValidateEmailFunc).toHaveBeenCalled();
});
});

it('validateEmail = true , test code ending in .com', async () => {
const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.(com)$/;
const mockValidateEmailFunc = jest.fn().mockImplementation(email => regex.test(email));

const { getByRole } = render(
<ReactMultiEmail
validateEmail={mockValidateEmailFunc}
getLabel={(email, index, removeEmail) => {
return (
<div data-tag key={index}>
<div data-tag-item>{email}</div>
<span data-tag-handle onClick={() => removeEmail(index)}>
×
</span>
</div>
);
}}
/>,
);

const input = getByRole('textbox') as HTMLInputElement;

fireEvent.change(input, { target: { value: 'abc@gmail.com' } });

await waitFor(() => {
expect(mockValidateEmailFunc(input.value)).toBe(true);
});
});

it('validateEmail = false', async () => {
const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.(com)$/;
const mockValidateEmailFunc = jest.fn().mockImplementation(email => regex.test(email));

const { getByRole } = render(
<ReactMultiEmail
validateEmail={mockValidateEmailFunc}
getLabel={(email, index, removeEmail) => {
return (
<div data-tag key={index}>
<div data-tag-item>{email}</div>
<span data-tag-handle onClick={() => removeEmail(index)}>
×
</span>
</div>
);
}}
/>,
);

const input = getByRole('textbox') as HTMLInputElement;

fireEvent.change(input, { target: { value: 'abc@gmail.kr' } });

await waitFor(() => {
expect(mockValidateEmailFunc(input.value)).toBe(false);
});
});

export {};

1 comment on commit 25fbc64

@vercel
Copy link

@vercel vercel bot commented on 25fbc64 Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

react-multi-email – ./

react-multi-email.vercel.app
react-multi-email-axframe.vercel.app
react-multi-email-git-master-axframe.vercel.app

Please sign in to comment.