Skip to content

Commit

Permalink
Add more test for Time component (#1213)
Browse files Browse the repository at this point in the history
* Add more test for Time component

* Add more test for Time component

* Disable annotations

* Bump jest-coverage-report
  • Loading branch information
baires authored Jul 29, 2024
1 parent 8f6b7c0 commit 1bc574b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ArtiomTr/jest-coverage-report-action@v2.2.6
- uses: ArtiomTr/jest-coverage-report-action@v2.3.0
with:
#coverage-file: ./test_reports/report.json
# base-coverage-file: coverage/report-final.json
test-script: npm test
annotations: none
threshold: 90
github-token: ${{ secrets.GH_COVERAGE }}
87 changes: 83 additions & 4 deletions tests/days.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ describe('Time class', () => {
expect(time.isFriday13th()).toBe(true)
})

it('isWeekend', () => {
mockDate('2023-04-01T00:00:00') // Saturday, April 1st, 2023
const time = new Time('UTC')
expect(time.isWeekend()).toBe(true)
it('isWeekend should return true for Saturday and Sunday', () => {
const saturdayTime = new Time('UTC', '2023-01-07')
const sundayTime = new Time('UTC', '2023-01-08')
expect(saturdayTime.isWeekend()).toBe(true)
expect(sundayTime.isWeekend()).toBe(true)
})

it('isDayBeforeChristmas', () => {
Expand Down Expand Up @@ -97,4 +98,82 @@ describe('Time class', () => {
const time4 = new Time('UTC')
expect(time4.isHolidays()).toBe(true)
})
it('setTimezone - valid timezone', () => {
const time = new Time('UTC')
time.setTimezone('America/New_York')
expect(time.timezone).toBe('America/New_York')
})

it('setTimezone - invalid timezone', () => {
const time = new Time('UTC')
time.setTimezone('Invalid/Timezone')
expect(time.timezone).toBe('UTC') // should remain unchanged
})

it('validOrNull - valid timezone', () => {
const time = Time.validOrNull('America/New_York')
expect(time).not.toBeNull()
expect(time?.timezone).toBe('America/New_York')
})

it('validOrNull - invalid timezone', () => {
const time = Time.validOrNull('Invalid/Timezone')
expect(time).toBeNull()
})

it('validOrNull - null timezone', () => {
const time = Time.validOrNull(null)
expect(time).not.toBeNull()
expect(time?.timezone).toBe(Time.DEFAULT_TIMEZONE)
})

it('isAfternoon - exactly 16:00', () => {
mockDate('2023-03-30T16:00:00') // March 30th, 2023, 16:00 (4 PM)
const time = new Time('UTC')
expect(time.isAfternoon()).toBe(true)
})

it('isThursdayAfternoon - exactly 16:00', () => {
mockDate('2023-03-30T16:00:00') // Thursday, March 30th, 2023, 16:00 (4 PM)
const time = new Time('UTC')
expect(time.isThursdayAfternoon()).toBe(true)
})

it('isFridayAfternoon - exactly 16:00', () => {
mockDate('2023-03-31T16:00:00') // Friday, March 31st, 2023, 16:00 (4 PM)
const time = new Time('UTC')
expect(time.isFridayAfternoon()).toBe(true)
})

it('isNewYear - exactly 16:00 on Dec 31st', () => {
mockDate('2023-12-31T16:00:00') // December 31st, 2023, 16:00 (4 PM)
const time = new Time('UTC')
expect(time.isNewYear()).toBe(true)
})

it('isNewYear - exactly midnight on Jan 1st', () => {
mockDate('2024-01-01T00:00:00') // January 1st, 2024, 00:00 (midnight)
const time = new Time('UTC')
expect(time.isNewYear()).toBe(true)
})

it('toObject should return correct object', () => {
const time = new Time('UTC', '2023-01-01')
const obj = time.toObject()
expect(obj).toEqual({
timezone: 'UTC',
customDate: expect.any(Date)
})
})

it('now should return current date for custom date', () => {
const customDate = '2023-01-01'
const time = new Time('UTC', customDate)
expect(time.now()).toEqual(time.getDate())
})

it('should use default timezone when not provided', () => {
const time = new Time()
expect(time.timezone).toBe(Intl.DateTimeFormat().resolvedOptions().timeZone)
})
})

1 comment on commit 1bc574b

@baires
Copy link
Owner Author

@baires baires commented on 1bc574b Jul 29, 2024

Choose a reason for hiding this comment

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

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 100% 61/61
🟢 Branches 93.75% 30/32
🟢 Functions 100% 20/20
🟢 Lines 100% 60/60

Test suite run success

37 tests passing in 2 suites.

Report generated by 🧪jest coverage report action from 1bc574b

Please sign in to comment.