Skip to content

Commit c1cdd57

Browse files
committed
test: Fix unit tests and add missing tests
1 parent a48343a commit c1cdd57

3 files changed

Lines changed: 97 additions & 25 deletions

File tree

app/utilities/variables.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import variables, { computeYearsExperience } from './variables'
2+
3+
describe('variables', () => {
4+
describe('static string values', () => {
5+
it('LINKEDIN_URL should be the correct LinkedIn profile URL', () => {
6+
expect(variables.LINKEDIN_URL).toBe('https://linkedin.com/in/jackdomleo7/')
7+
})
8+
9+
it('GITHUB_URL should be the correct GitHub profile URL', () => {
10+
expect(variables.GITHUB_URL).toBe('https://github.com/jackdomleo7')
11+
})
12+
13+
it('CODEPEN_URL should be the correct CodePen profile URL', () => {
14+
expect(variables.CODEPEN_URL).toBe('https://codepen.io/jackdomleo7')
15+
})
16+
17+
it('OCCUPATION should be Lead Frontend Engineer', () => {
18+
expect(variables.OCCUPATION).toBe('Lead Frontend Engineer')
19+
})
20+
})
21+
22+
describe('CURRENT_YEAR', () => {
23+
it('should be a 4-digit calendar year', () => {
24+
expect(variables.CURRENT_YEAR).toBeGreaterThanOrEqual(2024)
25+
expect(variables.CURRENT_YEAR).toBeLessThanOrEqual(2100)
26+
})
27+
28+
it('should match the actual current year at runtime', () => {
29+
expect(variables.CURRENT_YEAR).toBe(new Date().getFullYear())
30+
})
31+
})
32+
33+
describe('computeYearsExperience()', () => {
34+
// Career start date: 2018-09-03
35+
// The function counts complete career quarters and maps remainders:
36+
// remainder 0 → exactly N years → "N"
37+
// remainder 1 → just past a year mark → "just over N"
38+
// remainder 2 → mid-year → "N and a half"
39+
// remainder 3 → approaching next year → "nearly N+1"
40+
41+
it('returns exact years when on an anniversary (remainder 0)', () => {
42+
// 2025-09-03 = exactly 28 quarters (7 years) from 2018-09-03
43+
expect(computeYearsExperience(new Date('2025-09-03'))).toBe('7')
44+
})
45+
46+
it('returns "just over N" when 1 quarter past an anniversary (remainder 1)', () => {
47+
// 2025-12-04 = 29 complete quarters from 2018-09-03, remainder 1
48+
expect(computeYearsExperience(new Date('2025-12-04'))).toBe('just over 7')
49+
})
50+
51+
it('returns "N and a half" when 2 quarters past an anniversary (remainder 2)', () => {
52+
// 2026-03-04 = 30 complete quarters from 2018-09-03, remainder 2
53+
expect(computeYearsExperience(new Date('2026-03-04'))).toBe('7 and a half')
54+
})
55+
56+
it('returns "nearly N+1" when 3 quarters past an anniversary (remainder 3)', () => {
57+
// 2026-06-04 = 31 complete quarters from 2018-09-03, remainder 3
58+
expect(computeYearsExperience(new Date('2026-06-04'))).toBe('nearly 8')
59+
})
60+
61+
it('handles earlier career milestones correctly', () => {
62+
// 2020-09-03 = exactly 8 quarters (2 years)
63+
expect(computeYearsExperience(new Date('2020-09-03'))).toBe('2')
64+
})
65+
})
66+
67+
describe('NO_OF_YEARS_EXPERIENCE', () => {
68+
it('should be a non-empty string', () => {
69+
expect(typeof variables.NO_OF_YEARS_EXPERIENCE).toBe('string')
70+
expect(variables.NO_OF_YEARS_EXPERIENCE.length).toBeGreaterThan(0)
71+
})
72+
73+
it('should match computeYearsExperience() called at the same moment', () => {
74+
// Both are evaluated at import time so they should be identical
75+
expect(variables.NO_OF_YEARS_EXPERIENCE).toBe(computeYearsExperience())
76+
})
77+
})
78+
})

app/utilities/variables.ts

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
import dayjs from "dayjs"
22

3+
/** Returns a human-readable years-of-experience string based on complete
4+
* career quarters from 2018-09-03 to the given date (defaults to now).
5+
* Exported so it can be unit-tested with arbitrary dates.
6+
*/
7+
export function computeYearsExperience(now: Date = new Date()): string {
8+
const firstDayAsDeveloper = dayjs(new Date('2018-09-03'))
9+
const quarterDifference = Math.abs(firstDayAsDeveloper.diff(now, 'quarter'))
10+
const yearsDifference = Math.floor(quarterDifference / 4)
11+
const yearsDifferenceRemainder = quarterDifference % 4
12+
13+
// TODO: From Sept 2028, change to just outputting yearsDifference
14+
if (yearsDifferenceRemainder === 1) return `just over ${yearsDifference}`
15+
if (yearsDifferenceRemainder === 2) return `${yearsDifference} and a half`
16+
if (yearsDifferenceRemainder === 3) return `nearly ${yearsDifference + 1}`
17+
return `${yearsDifference}`
18+
}
19+
320
export default {
421
LINKEDIN_URL: 'https://linkedin.com/in/jackdomleo7/',
522
GITHUB_URL: 'https://github.com/jackdomleo7',
623
CODEPEN_URL: 'https://codepen.io/jackdomleo7',
724
OCCUPATION: 'Lead Frontend Engineer',
825
CURRENT_YEAR: new Date().getFullYear(),
9-
NO_OF_YEARS_EXPERIENCE: (() => {
10-
const firstDayAsDeveloper = dayjs(new Date('2018-09-03'))
11-
const quarterDifference = Math.abs(firstDayAsDeveloper.diff(new Date(), 'quarter'))
12-
const yearsDifference = Math.floor(quarterDifference / 4)
13-
const yearsDifferenceRemainder = quarterDifference % 4
14-
15-
let yearsExperience = ''
16-
17-
// TODO: From Sept 2028, change to just outputting yearsDifference
18-
if (yearsDifferenceRemainder === 1) {
19-
yearsExperience = `just over ${yearsDifference}`
20-
}
21-
else if (yearsDifferenceRemainder === 2) {
22-
yearsExperience = `${yearsDifference} and a half`
23-
}
24-
else if (yearsDifferenceRemainder === 3) {
25-
yearsExperience = `nearly ${yearsDifference + 1}`
26-
}
27-
else {
28-
yearsExperience = `${yearsDifference}`
29-
}
30-
31-
return yearsExperience
32-
})()
26+
NO_OF_YEARS_EXPERIENCE: computeYearsExperience()
3327
}

vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default defineConfig({
1616
provider: 'v8',
1717
reporter: ['text', 'json', 'html'],
1818
all: true,
19-
include: ['app/components', 'app/layouts', 'app/pages', 'app/utilities']
19+
include: ['app/utilities/**/*.ts']
2020
},
2121
setupFiles: ['./vitest.setup.ts'],
2222
restoreMocks: true

0 commit comments

Comments
 (0)