Skip to content

Commit

Permalink
Move tests to src
Browse files Browse the repository at this point in the history
  • Loading branch information
frozzare committed Mar 14, 2024
1 parent ba2c9c2 commit 935cb9c
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 12 deletions.
9 changes: 5 additions & 4 deletions test.ts → src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { describe, it, expect } from 'vitest';
import { request } from 'undici';
import { diffInYears } from './src/utils';
import { diffInYears } from './utils';

const Personnummer = process.env.FILE?.includes('cjs')
? require(process.env.FILE)
: (await import(process.env.FILE)).default;
const file = process.env.FILE || '.';
const Personnummer = file.includes('cjs')
? require(file)
: (await import(file)).default;

const availableListFormats = [
'long_format',
Expand Down
104 changes: 104 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { describe, it, expect } from 'vitest';
import { compareAsc } from 'date-fns';

Check failure on line 2 in src/utils.test.ts

View workflow job for this annotation

GitHub Actions / build (lts/*)

'compareAsc' is defined but never used

Check failure on line 2 in src/utils.test.ts

View workflow job for this annotation

GitHub Actions / build (latest)

'compareAsc' is defined but never used
import { diffInYears, luhn, testDate } from './utils';

describe('diffInYears', () => {
it('should return 0 if both dates are the same', () => {
const date = new Date();
expect(diffInYears(date, date)).toBe(0);
});

it('should return 1 if the second date is exactly one year ahead of the first date', () => {
const dateLeft = new Date(2021, 1, 1);
const dateRight = new Date(2020, 1, 1);
expect(diffInYears(dateLeft, dateRight)).toBe(1);
});

it('should return -1 if the second date is exactly one year behind the first date', () => {
const dateLeft = new Date(2020, 1, 1);
const dateRight = new Date(2021, 1, 1);
expect(diffInYears(dateLeft, dateRight)).toBe(-1);
});

it('should return the correct difference for dates with different years', () => {
const dateLeft = new Date(2023, 1, 1);
const dateRight = new Date(2020, 1, 1);
expect(diffInYears(dateLeft, dateRight)).toBe(3);
});

it('should return the correct difference for dates with the same year but different months', () => {
const dateLeft = new Date(2020, 0, 1);
const dateRight = new Date(2020, 11, 31);
expect(diffInYears(dateLeft, dateRight)).toBe(0);
});

it('should return the correct difference for dates with the same year and month but different days', () => {
const dateLeft = new Date(2020, 0, 1);
const dateRight = new Date(2020, 0, 31);
expect(diffInYears(dateLeft, dateRight)).toBe(0);
});
});

describe('luhn', () => {
it('should return 0 for an empty string', () => {
expect(luhn('')).toBe(0);
});

it('should return 0 for a non-numeric string', () => {
expect(luhn('abcd')).toBe(0);
});

it('should return 0 for a string with non-numeric characters', () => {
expect(luhn('1234abcd')).toBe(0);
});

it('should return the correct check digit for a valid number', () => {
expect(luhn('1212121212')).toBe(0);
});

it('should return the correct check digit for a long valid number', () => {
expect(luhn('1234567812345670')).toBe(0);
});
})

describe('testDate', () => {
it('should return true for a valid date', () => {
expect(testDate(2024, 2, 14)).toBe(true);
});

it('should return false for an invalid date with a non-existent month', () => {
expect(testDate(2024, 13, 14)).toBe(false);
});

it('should return false for an invalid date with a non-existent day in February', () => {
expect(testDate(2024, 2, 30)).toBe(false);
});

it('should return false for an invalid date with a non-existent day in April', () => {
expect(testDate(2024, 4, 31)).toBe(false);
});

it('should return false for an invalid date with a non-existent day in June', () => {
expect(testDate(2024, 6, 31)).toBe(false);
});

it('should return false for an invalid date with a non-existent day in September', () => {
expect(testDate(2024, 9, 31)).toBe(false);
});

it('should return false for an invalid date with a non-existent day in November', () => {
expect(testDate(2024, 11, 31)).toBe(false);
});

it('should return true for a valid date in a leap year February', () => {
expect(testDate(2024, 1, 28)).toBe(true);
});

it('should return true for a valid date in a non-leap year February', () => {
expect(testDate(2023, 1, 28)).toBe(true);
});

it('should return true for a valid date in a leap year February', () => {
expect(testDate(2024, 2, 29)).toBe(true);
});
})
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const luhn = (str: string): number => {
sum += v;
}

return Math.ceil(sum / 10) * 10 - sum;
return Math.ceil(sum / 10) * 10 - sum || 0;
};

/**
Expand Down
7 changes: 0 additions & 7 deletions vitest.config.mjs

This file was deleted.

0 comments on commit 935cb9c

Please sign in to comment.