Skip to content

Commit

Permalink
Merge pull request #39 from satyam-seth/unit-test-utils
Browse files Browse the repository at this point in the history
Unit test utils
  • Loading branch information
satyam-seth authored Oct 31, 2024
2 parents b29a513 + 988e05e commit 1318ac9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@satyam-seth/otp-field",
"version": "1.0.4",
"version": "1.0.5",
"description": "A configurable OTP field built using TypeScript and SCSS",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/ts/utils/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function getOTPRegexForValueType(valueType: OTPValueType): RegExp {
return /[^a-z0-9]/g; // Match anything except alphanumeric lower characters

case OTPValueType.ALPHANUMERIC_UPPER:
return /[^A-Za-z0-9]/g; // Match anything except alphanumeric upper characters
return /[^A-Z0-9]/g; // Match anything except alphanumeric upper characters

// throw error for invalid type
default:
Expand Down
46 changes: 46 additions & 0 deletions tests/utils/regex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { getOTPRegexForValueType, OTPValueType } from '../../src/ts/main';

describe('Test getOTPRegexForValueType function', () => {
it('should return valid regex for valid value type', () => {
const numericRegex = getOTPRegexForValueType(OTPValueType.NUMERIC);
expect(numericRegex.toString()).to.equal('/[^0-9]/g');

const alphabeticRegex = getOTPRegexForValueType(OTPValueType.ALPHABETIC);
expect(alphabeticRegex.toString()).to.equal('/[^A-Za-z]/g');

const alphabeticLowerRegex = getOTPRegexForValueType(
OTPValueType.ALPHABETIC_LOWER
);
expect(alphabeticLowerRegex.toString()).to.equal('/[^a-z]/g');

const alphabeticUpperRegex = getOTPRegexForValueType(
OTPValueType.ALPHABETIC_UPPER
);
expect(alphabeticUpperRegex.toString()).to.equal('/[^A-Z]/g');

const alphanumericRegex = getOTPRegexForValueType(
OTPValueType.ALPHANUMERIC
);
expect(alphanumericRegex.toString()).to.equal('/[^A-Za-z0-9]/g');

const alphanumericLowerRegex = getOTPRegexForValueType(
OTPValueType.ALPHANUMERIC_LOWER
);
expect(alphanumericLowerRegex.toString()).to.equal('/[^a-z0-9]/g');

const alphanumericUpperRegex = getOTPRegexForValueType(
OTPValueType.ALPHANUMERIC_UPPER
);
expect(alphanumericUpperRegex.toString()).to.equal('/[^A-Z0-9]/g');
});

it('should throw error for invalid value type', () => {
// @ts-ignore
expect(() => getOTPRegexForValueType('invalid')).to.throw(
Error,
'Invalid OTP field value type'
);
});
});

0 comments on commit 1318ac9

Please sign in to comment.