Skip to content

Commit

Permalink
Added test for ability saving lastName with special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
SofiiaYevush committed Dec 3, 2024
1 parent e231169 commit 5a69cdb
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 12 deletions.
21 changes: 21 additions & 0 deletions tests/unit/containers/edit-profile/ProfileTab.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ vi.mock(
required
value={data.firstName}
/>
<input
onBlur={handleBlur('lastName')}
onChange={handleInputChange('lastName')}
placeholder={'lastName'}
required
value={data.lastName}
/>
</div>
)
})
Expand Down Expand Up @@ -155,4 +162,18 @@ describe('ProfileTab', () => {
expect(firstNameInput).toBeInTheDocument()
expect(firstNameInput).toHaveFocus()
})

it('should place cursor in the "Last name" field when clicked', async () => {
renderWithMockData();

const lastNameInput = screen.getByPlaceholderText('lastName');
expect(lastNameInput).toBeInTheDocument();
expect(document.activeElement).not.toBe(lastNameInput);

await userEvent.click(lastNameInput);

expect(document.activeElement).toBe(lastNameInput);
expect(lastNameInput).toHaveFocus()
});

})
37 changes: 37 additions & 0 deletions tests/unit/pages/edit-profile/EditProfile.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ const userMock = {

const mockData = {
firstName: '',
lastName: '',
photo: null,
videoLink: '',
errors: {
firstName: '',
lastName: '',
videoLink: '',
},
};
Expand Down Expand Up @@ -473,4 +475,39 @@ describe('EditProfile', () => {
expect(updateButton).not.toBeDisabled();
}
});

it('should replace the existing text in the "Last name" field with test data and Update button becomes anable and active', () => {
const testData = ["Mc'Neil", "O'Neill-Johnson", "Van Gogh"];

const mockT = vi.fn((key) => {
const translations = {
'common.labels.lastName': 'Last Name',
'editProfilePage.updateBtn': 'Update',
};
return translations[key] || key;
});

const mockHandleInputChange = vi.fn();

render(
<ProfileTabForm
t={mockT}
data={mockData}
errors={mockData.errors}
handleInputChange={mockHandleInputChange}
handleBlur={() => {}}
openAlert={() => {}}
/>
);

const lastNameInput = screen.getByLabelText(/common.labels.lastName/i)
expect(lastNameInput).toBeInTheDocument();

for (const data of testData) {
fireEvent.change(lastNameInput, { target: { value: data } });

const updateButton = screen.getByText('editProfilePage.updateBtn')
expect(updateButton).not.toBeDisabled();
}
});
});
42 changes: 30 additions & 12 deletions tests/unit/utils/common.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ const mockedValues = {
longText: 'wwwwwwwwwwwwwwwwwwwwwwwwwwwwwww',
emptyField: '',
spaceField: ' ',
nameWithApostropheEn: "O'braian",
nameWithApostropheUa: "Мар'яна",
nameWithHyphen: "Анна-Марія",
nameWithSpace: "Анна Марія",
firstNameWithApostropheEn: "O'braian",
firstNameWithApostropheUa: "Мар'яна",
firstNameWithHyphen: "Анна-Марія",
firstNameWithSpace: "Анна Марія",
lastNameWithApostrophe: "Mc'Neil",
lastNameWithApostropheAndHyphen: "O'Neill-Johnson",
lastNameWithSpace: "Van Gogh",
}

const errorMessages = {
Expand Down Expand Up @@ -51,23 +54,38 @@ export const passwordField = (value) => {
}

describe('commonValidation', () => {
it('Should accept name in English with apostrophe', () => {
const result = nameField(mockedValues.nameWithApostropheEn)
it('Should accept firstName in English with apostrophe', () => {
const result = nameField(mockedValues.firstNameWithApostropheEn)
expect(result).not.toBe(errorMessages.nameCharacters)
})

it('Should accept name in Ukrainian with apostrophe', () => {
const result = nameField(mockedValues.nameWithApostropheUa)
it('Should accept firstName in Ukrainian with apostrophe', () => {
const result = nameField(mockedValues.firstNameWithApostropheUa)
expect(result).not.toBe(errorMessages.nameCharacters)
})

it('Should accept name with space', () => {
const result = nameField(mockedValues.nameWithSpace)
it('Should accept firstName with space', () => {
const result = nameField(mockedValues.firstNameWithSpace)
expect(result).not.toBe(errorMessages.nameCharacters)
})

it('Should accept name with hyphen', () => {
const result = nameField(mockedValues.nameWithHyphen)
it('Should accept firstName with hyphen', () => {
const result = nameField(mockedValues.firstNameWithHyphen)
expect(result).not.toBe(errorMessages.nameCharacters)
})

it('Should accept lastName with apostrophe', () => {
const result = nameField(mockedValues.lastNameWithApostrophe)
expect(result).not.toBe(errorMessages.nameCharacters)
})

it('Should accept lastName with apostrophe and hyphen', () => {
const result = nameField(mockedValues.lastNameWithApostropheAndHyphen)
expect(result).not.toBe(errorMessages.nameCharacters)
})

it('Should accept lastName with space', () => {
const result = nameField(mockedValues.lastNameWithSpace)
expect(result).not.toBe(errorMessages.nameCharacters)
})

Expand Down

0 comments on commit 5a69cdb

Please sign in to comment.