Skip to content

Commit

Permalink
feat: Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seebeen committed Oct 21, 2021
1 parent 7e3e013 commit e75a848
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 2 deletions.
39 changes: 39 additions & 0 deletions lib/jmbg-validator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { validJMBG } from '.';

test('Valid JMBG', () => {
expect(validJMBG('1703990715178')).toEqual(true);
expect(validJMBG(1703990715178)).toEqual(true);
});

test('Invalid JMBG', () => {
expect(validJMBG('1703990715170')).toEqual(false);
expect(validJMBG('1703000715170')).toEqual(false);
expect(validJMBG(1703990715170)).toEqual(false);
});

test('Invalid day in JMBG', () => {
expect(validJMBG('8803990715178')).toEqual(false);
expect(validJMBG(8803990715178)).toEqual(false);
});

test('Invalid month in JMBG', () => {
expect(validJMBG('1217990715178')).toEqual(false);
expect(validJMBG(1217990715178)).toEqual(false);
});

test('Foreign person JMBG', () => {
expect(validJMBG('1703990605178')).toEqual(true);
expect(validJMBG('1703990665178')).toEqual(true);
expect(validJMBG(1703990605178)).toEqual(true);
expect(validJMBG(1703990665178)).toEqual(true);
});

test('Long JMBG', () => {
expect(validJMBG('1703990715178000')).toEqual(false);
expect(validJMBG(1703990715178000)).toEqual(false);
});

test('short JMBG', () => {
expect(validJMBG('1703990715')).toEqual(false);
expect(validJMBG(1703990715)).toEqual(false);
});
2 changes: 1 addition & 1 deletion lib/jmbg-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export function validJMBG(jmbg: number | string): boolean {
return true;
}

return parseInt(jmbg[12], 10) === mod11(jmbg.substring(0, 12));
return jmbg.length == 13 && parseInt(jmbg[12], 10) === mod11(jmbg.substring(0, 12));
}
21 changes: 21 additions & 0 deletions lib/mb-validator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { validMB } from '.';

test('Valid MB', () => {
expect(validMB('66143627')).toEqual(true);
expect(validMB(66143627)).toEqual(true);
});

test('Invalid MB', () => {
expect(validMB('66143628')).toEqual(false);
expect(validMB(66143628)).toEqual(false);
});

test('Short MB', () => {
expect(validMB('6614362')).toEqual(false);
expect(validMB(6614362)).toEqual(false);
});

test('Long MB', () => {
expect(validMB('661436200')).toEqual(false);
expect(validMB(661436200)).toEqual(false);
});
22 changes: 22 additions & 0 deletions lib/pib-validator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { validPIB } from '.';

test('Valid PIB', () => {
expect(validPIB('112497859')).toEqual(true);
expect(validPIB(112497859)).toEqual(true);
});

test('Invalid PIB', () => {
expect(validPIB('112497851')).toEqual(false);
expect(validPIB(112497851)).toEqual(false);
expect(validPIB(100000000)).toEqual(false);
});

test('Short PIB', () => {
expect(validPIB('11249785')).toEqual(false);
expect(validPIB(11249785)).toEqual(false);
});

test('Long PIB', () => {
expect(validPIB('11249785900')).toEqual(false);
expect(validPIB(11249785900)).toEqual(false);
});
5 changes: 4 additions & 1 deletion lib/pib-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export function validPIB(pib: number | string): boolean {
.slice(0, 8)
.forEach((value) => {
sum = (sum + parseInt(value, 10)) % 10;
sum = ((sum === 0 ? 10 : sum) * 2) % 11;
if (sum === 0) {
sum = 10;
}
sum = (sum * 2) % 11;
});
sum = (11 - sum) % 10;

Expand Down
14 changes: 14 additions & 0 deletions lib/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { mod11 } from './utils';

test('Mod 11 => 0', () => {
expect(mod11('111114')).toEqual(0);
});

test('Mod 11 => 1', () => {
expect(mod11('111123')).toEqual(null);
});

test('Mod 11 => anything else', () => {
expect(mod11('111118')).not.toEqual(null);
expect(mod11('111118')).not.toEqual(0);
});

0 comments on commit e75a848

Please sign in to comment.