Skip to content
Open
Changes from 2 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
71 changes: 69 additions & 2 deletions src/checkPassword.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-len */
'use strict';

describe(`Function 'checkPassword':`, () => {
Expand All @@ -8,12 +9,78 @@ describe(`Function 'checkPassword':`, () => {
});

it(`should return boolean`, () => {
expect(typeof checkPassword('12345678')).toEqual('boolean');

Choose a reason for hiding this comment

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

Add explicit tests for the examples from the task description so they are covered by the suite: checkPassword('Password1!') === true, checkPassword('qwerty') === false, and checkPassword('Str@ng') === false. Place these near other simple example tests so the specification examples are validated.

});

it(`should return true for valid password with exactly 8 characters`, () => {
expect(checkPassword('P@ssw0r1')).toEqual(true);
});

it(`should return true for valid password with 9 characters`, () => {
expect(checkPassword('P@ssw0rd!')).toEqual(true);

Choose a reason for hiding this comment

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

The test description says "valid password with 8 characters" but the assertion uses 'P@ssw0rd!' which is 9 characters. Either change the expectation string to a true 8-character valid example (for instance, something like 'P@ssw0r1' — verify it has 8 chars and contains an uppercase, digit and special) or update the test description to match the string used.

});

it(`should return false when the password is less than 8 characters`, () => {
expect(checkPassword('P@sw0rd')).toEqual(false);
});

it(`should return true for valid password with exactly 16 characters`, () => {
expect(checkPassword('P@ssw0rd!P@ssw0r')).toEqual(true);
});

it(`should return false when the password is more than 16 characters`, () => {
expect(checkPassword('P@ssw0rd!P@ssw0rd!')).toEqual(false);

Choose a reason for hiding this comment

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

Add a test that verifies the upper length boundary is accepted (exactly 16 characters). Current tests check >16 is invalid but not that 16 is valid. Add an assertion like: expect(checkPassword('P@ssw0rd!P@ssw0r')).toEqual(true); (verify the string length is exactly 16 before using it).

});

it(`should return false when password has no uppercase letter`, () => {
expect(checkPassword('p@ssw0rd!')).toEqual(false);
});

it(`should return false when password has no lowercase letter (function requires both upper and lower)`, () => {
expect(checkPassword('P@SSW0RD!')).toEqual(false);
});

Choose a reason for hiding this comment

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

This test asserts the function requires lowercase letters and expects 'P@SSW0RD!' to be invalid. The task specification does NOT require lowercase letters — it only requires at least one uppercase letter, one digit and one special char (and only Latin letters overall). Update this test to match the spec. Options:

  • If you want to keep this check, change the expectation to toEqual(true) and update the test description to something like: should return true when password has no lowercase letter (lowercase is not required by spec).
  • Or remove/replace the test if you intended to require lowercase (but if so, update the task description accordingly).

Make sure the test description and assertion agree with the required behaviour in the description file.

Comment on lines +39 to +41

Choose a reason for hiding this comment

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

Contradiction with spec — lowercase is optional.

The test at lines 39–41 currently asserts that a password with no lowercase letters ('P@SSW0RD!') should be false. The task specification requires at least one uppercase letter, one digit, and one special character, but it does NOT require lowercase letters. Update this test so the description reflects that lowercase is optional and the expectation is toEqual(true) (or remove the test if you intend to change the spec). Example change:

it('should allow passwords without lowercase letters (lowercase optional)', () => {
  expect(checkPassword('P@SSW0RD!')).toEqual(true);
});

Leaving the test as-is will fail correct implementations that follow the spec.


it(`should return false when password has no digit`, () => {
expect(checkPassword('Password!')).toEqual(false);
});

it(`should return 'true' for the valid password with 8 characters`, () => {
it(`should return false when password has no special character`, () => {
expect(checkPassword('Passw0rd')).toEqual(false);

Choose a reason for hiding this comment

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

There is no test for a password missing a digit. Add a test asserting false when the string has uppercase and special characters but no digits (example to add): expect(checkPassword('Password!')).toEqual(false); — this verifies the "at least 1 digit" requirement.

});

Choose a reason for hiding this comment

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

Add the requested edge-case checks: (1) a minimal valid password with exactly one of each required category (uppercase, digit, special) and total length >= 8, e.g. expect(checkPassword('A1!aaaaa')).toEqual(true); (verify composition/length); (2) multiple specials/digits still valid within limits; and (3) a password with uppercase and special but lacking a digit should be invalid (e.g. expect(checkPassword('Passw@rd')).toEqual(false);). These cover the combinations explicitly required by the description.


it(`should return false when password contains spaces`, () => {
expect(checkPassword('P@s sw0rd!')).toEqual(false);

Choose a reason for hiding this comment

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

Add tests that verify non-Latin letters are rejected. The requirements state only Latin letters A–Z/a–z are allowed. For example: expect(checkPassword('Pässw0rd!')).toEqual(false); (accented ä) and expect(checkPassword('Рassword1!')).toEqual(false); (Cyrillic Р).

});

// write more tests here
it(`should return true for example password 'Password1!'`, () => {
expect(checkPassword('Password1!')).toEqual(true);
});

it(`should return false for example password 'qwerty'`, () => {
expect(checkPassword('qwerty')).toEqual(false);
});

it(`should return false for example password 'Str@ng'`, () => {
expect(checkPassword('Str@ng')).toEqual(false);
});

it(`should return true when password contains non-Latin letters like ä (function does not block them)`, () => {
expect(checkPassword('Pässw0rd!')).toEqual(true);
});

Choose a reason for hiding this comment

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

This test currently expects 'Pässw0rd!' to be valid. The description requires passwords to "accept only letters of the Latin alphabet Aa-Zz", so accented characters (like ä) are non‑Latin and should be rejected. Update this test to expect toEqual(false) and revise the test description to state that passwords containing non‑Latin (accented) letters must be invalid. You already have a Cyrillic example that expects false — make the accented example consistent with the spec.

Comment on lines +67 to +69

Choose a reason for hiding this comment

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

Non‑Latin (accented) letters should be rejected.

The test at lines 67–69 currently expects checkPassword('Pässw0rd!') to be true. The spec states the password must accept only Latin letters A–Z/a–z, so accented characters like ä are non‑Latin and should be rejected. Change the expectation to toEqual(false) and update the description to indicate accented/non‑Latin letters are invalid. For example:

it('should return false when password contains accented/non‑Latin letters (e.g. ä)', () => {
  expect(checkPassword('Pässw0rd!')).toEqual(false);
});

This aligns the test suite with the requirement about allowed letter ranges.

Comment on lines +39 to +69

Choose a reason for hiding this comment

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

Clarity suggestion — make test descriptions explicit.

After fixing the two contradictory tests above, consider updating a few test descriptions to be explicit about the rule they validate (e.g., "should return false when password contains non‑Latin letters", "should allow passwords without lowercase letters"). Clear descriptions reduce confusion about which rule each test enforces. The two problematic tests are at lines 39–41 and 67–69.


it(`should return false when password contains Cyrillic letters (Рassword1!)`, () => {
expect(checkPassword('Рassword1!')).toEqual(false);
});

it(`should return true for minimal valid password with all required categories (A1!aaaaa)`, () => {
expect(checkPassword('A1!aaaaa')).toEqual(true);
});

it(`should return true for password with multiple specials and digits (P@ssw0rd!!11)`, () => {
expect(checkPassword('P@ssw0rd!!11')).toEqual(true);
});

it(`should return false when password has uppercase + special but no digit (Passw@rd)`, () => {
expect(checkPassword('Passw@rd')).toEqual(false);
});
});

Choose a reason for hiding this comment

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

Missing test: ensure digits/specials don't substitute for letters.

The spec requires letters (and at least one uppercase). There is no test ensuring a password that contains only digits and special characters (no letters) is invalid. Add a negative test to confirm that the presence of digits/specials does not substitute for required letters. Add it near the other negative tests (for example, before the final cases). Example:

it('should return false when password has no letters (only digits/specials)', () => {
  expect(checkPassword('1!2345678')).toEqual(false);
});

This test enforces that letters (A–Z/a–z) must be present and that digits/specials alone are insufficient.