Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test/delimiter #163

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions test/delimiter.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { cleanup, fireEvent, getByRole, render } from '@testing-library/react';
import { ReactMultiEmail } from '../react-multi-email';
import React from 'react';

afterEach(cleanup);

describe('ReactMultiEmail delimiter TEST', () => {
it('Default delimiter test', () => {
const { getByRole } = render(
<ReactMultiEmail
getLabel={(email, index, removeEmail) => (
<div data-tag key={index}>
<div data-tag-item>{email}</div>
<span data-tag-handle onClick={() => removeEmail(index)}>
×
</span>
</div>
)}
/>,
);

const inputElem = getByRole('textbox');
const emailsWrapper = document.querySelector('.data-labels');
const MAX_TEST_CASE = 3;

for (let i = 0; i < MAX_TEST_CASE; i++) {
const testValue = i === 0 ? 'test@gmail.com' : i === 1 ? 'test@naver.com' : 'test@tt.com';
const delimiter = i === 0 ? ' ' : i === 1 ? ',' : ';';

fireEvent.change(inputElem, { target: { value: testValue } });
expect(emailsWrapper?.childElementCount).toEqual(i);
fireEvent.change(inputElem, { target: { value: `${testValue}${delimiter}` } });
expect(emailsWrapper?.childElementCount).toEqual(i + 1);
}
// 중복 체크
const duplicationValue = 'test@gmail.com';
fireEvent.change(inputElem, { target: { value: duplicationValue } });
expect(emailsWrapper?.childElementCount).toEqual(MAX_TEST_CASE);
fireEvent.change(inputElem, { target: { value: `${duplicationValue};` } });
expect(emailsWrapper?.childElementCount).toEqual(MAX_TEST_CASE);
});

it('Custom delimiter test', () => {
const { getByRole } = render(
<ReactMultiEmail
delimiter='[&/]'
getLabel={(email, index, removeEmail) => (
<div data-tag key={index}>
<div data-tag-item>{email}</div>
<span data-tag-handle onClick={() => removeEmail(index)}>
×
</span>
</div>
)}
/>,
);
const inputElem = getByRole('textbox');
const emailsWrapper = document.querySelector('.data-labels');
const MAX_TEST_CASE = 2;

for (let i = 0; i < MAX_TEST_CASE; i++) {
const testValue = i === 0 ? 'test@gmail.com' : 'test@naver.com';
const delimiter = i === 0 ? '&' : '/';

fireEvent.change(inputElem, { target: { value: testValue } });
expect(emailsWrapper?.childElementCount).toEqual(i);
fireEvent.change(inputElem, { target: { value: `${testValue}${delimiter}` } });
expect(emailsWrapper?.childElementCount).toEqual(i + 1);
}
// 중복 체크
const duplicationValue = 'test@gmail.com';
fireEvent.change(inputElem, { target: { value: duplicationValue } });
expect(emailsWrapper?.childElementCount).toEqual(MAX_TEST_CASE);
fireEvent.change(inputElem, { target: { value: `${duplicationValue}/` } });
expect(emailsWrapper?.childElementCount).toEqual(MAX_TEST_CASE);
});
});