Skip to content

Commit

Permalink
test: cover branches with unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dbajpeyi committed Feb 20, 2025
1 parent 16e2873 commit c339dd1
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Form/Form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,21 @@ describe('Test the form class reading values correctly', () => {
},
},
},
{
testCase: 'signup form with email address and password, with first name and last name in adjacent fields ',
form: `
<form method="post" id="signupForm">
<input type="text" value="Dax" id="firstName" placeholder="First name" />
<input type="text" value="McDax" id="lastName" placeholder="Last name" />
<input type="email" value="dax@mcdax.com" autocomplete="email" />
<input type="password" value="123456" autocomplete="new-password" />
<button type="submit">Sign up</button>
</form>`,
expHasValues: true,
expValues: {
credentials: { username: 'dax@mcdax.com', password: '123456' },
},
},
];

test.each(testCases)('Test $testCase', ({ form, expHasValues, expValues }) => {
Expand Down
93 changes: 93 additions & 0 deletions src/Form/formatters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,98 @@ describe('prepareFormValuesForStorage()', () => {
);
expect(values.credentials).toEqual(inputCredentials);
});

it('accepts email address as username when other identity data is present', () => {
const inputCredentials = { username: 'dax@example.com', password: '123456' };
const values = prepareFormValuesForStorage({
// @ts-ignore
credentials: { password: inputCredentials.password },
// @ts-ignore
creditCards: {},
// @ts-ignore
identities: { emailAddress: 'dax@example.com', firstName: 'Dax', lastName: 'McDax' },
});
expect(values.credentials).toEqual(inputCredentials);
});

it('accepts email address as username with identity and card data is present', () => {
const inputCredentials = { username: 'dax@example.com', password: '123456' };
const values = prepareFormValuesForStorage({
// @ts-ignore
credentials: { password: inputCredentials.password },
// @ts-ignore
creditCards: { cardNumber: '1234567890123456', cardName: 'Dax McDax' },
// @ts-ignore
identities: { emailAddress: 'dax@example.com', firstName: 'Dax', lastName: 'McDax', phone: '+133451234563' },
});
expect(values.credentials).toEqual(inputCredentials);
});

it('accepts phone as username', () => {
const inputCredentials = { username: '+133451234563', password: '123456' };
const values = prepareFormValuesForStorage({
// @ts-ignore
credentials: {
password: inputCredentials.password,
},
// @ts-ignore
creditCards: {},
// @ts-ignore
identities: {
phone: inputCredentials.username,
},
});
expect(values.credentials).toEqual(inputCredentials);
});

it("doesn't accept phone as username if other identity data is present", () => {
const inputCredentials = { username: '+133451234563', password: '123456' };
const values = prepareFormValuesForStorage({
// @ts-ignore
credentials: {
password: inputCredentials.password,
},
// @ts-ignore
creditCards: {},
// @ts-ignore
identities: {
phone: inputCredentials.username,
firstName: 'Dax',
lastName: 'McDax',
},
});
expect(values.credentials).toEqual({ password: inputCredentials.password });
});

it('accepts credit card number as username', () => {
const inputCredentials = { username: '1234567890123456', password: '123456' };
const values = prepareFormValuesForStorage({
// @ts-ignore
credentials: { password: inputCredentials.password },
// @ts-ignore
creditCards: {
cardNumber: inputCredentials.username,
},
// @ts-ignore
identities: {},
});
expect(values.credentials).toEqual(inputCredentials);
});

it("doesn't accept credit card number as username if other card data is present", () => {
const inputCredentials = { username: '1234567890123456', password: '123456' };
const values = prepareFormValuesForStorage({
// @ts-ignore
credentials: { password: inputCredentials.password },
// @ts-ignore
creditCards: {
cardNumber: inputCredentials.username,
cardName: 'Dax McDax',
},
// @ts-ignore
identities: {},
});
expect(values.credentials).toEqual({ password: inputCredentials.password });
});
});
});

0 comments on commit c339dd1

Please sign in to comment.