Skip to content

Commit

Permalink
Add Jest unit testing workflow and update package.json and test files
Browse files Browse the repository at this point in the history
  • Loading branch information
gihoekveld committed Apr 14, 2024
1 parent 997d419 commit bd48eb6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 15 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/run-unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Jest Unit Testing
on:
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: read
deployments: write
name: Run Jest Unit Tests
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Dependencies
run: npm install
- name: Running Tests
run: npm test
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
},
"testMatch": [
"**/*.test.ts"
]
],
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
},
"testPathIgnorePatterns": ["<rootDir>/.next/", "<rootDir>/node_modules/", "<rootDir>/src/errors/*.ts"]
},
"dependencies": {
"@emotion/react": "^11.11.4",
Expand Down
9 changes: 3 additions & 6 deletions src/helpers/schedule/utils/allocateTask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { advanceTo } from 'jest-date-mock'
import { allocateTask } from './allocateTask'
import { Schedule } from '@/entities/Schedule'
import { UnallocatedTask } from '@/entities/UnallocatedTask'
import { DeadlineExceededException } from '@/errors/DeadlineExceededException'

describe('allocateTask', () => {
beforeAll(() => {
Expand Down Expand Up @@ -46,8 +47,6 @@ describe('allocateTask', () => {
},
})

console.log(result)

expect(result).not.toBeNull()
expect(result?.startAt).toEqual(new Date('2022-01-01T09:00:00'))
expect(result?.endAt).toEqual(new Date('2022-01-01T09:30:00'))
Expand Down Expand Up @@ -96,7 +95,7 @@ describe('allocateTask', () => {
expect(result?.endAt).toEqual(new Date('2022-01-01T10:30:00'))
})

it('should throw an error if the task cannot be allocated before the deadline', () => {
it('should throw an error if the task cannot be allocated before the deadline', async () => {
const schedules: Schedule[] = [
{
id: '1',
Expand Down Expand Up @@ -134,8 +133,6 @@ describe('allocateTask', () => {
weekDays: [],
},
}),
).toThrowError(
'Não há tempo disponível para alocar a tarefa antes do prazo de entrega.',
)
).toThrowError(DeadlineExceededException)
})
})
2 changes: 1 addition & 1 deletion src/helpers/schedule/utils/allocateTask.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DeadlineExceededException } from '@/errors/DeadlineExceededException'
import { Schedule } from '@/entities/Schedule'
import { UnallocatedTask } from '@/entities/UnallocatedTask'
import { BlockedTimeType } from '@/entities/Profile'
import { DeadlineExceededException } from '@/errors/DeadlineExceededException'

export const allocateTask = (
schedules: Schedule[],
Expand Down
38 changes: 31 additions & 7 deletions src/helpers/schedule/utils/sortByPriorityAndDeadline.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import { describe, expect, it } from '@jest/globals'
import { UnallocatedTask } from '../entities/UnallocatedTask'
import { sortByPriorityAndDeadline } from './sortByPriorityAndDeadline'
import { UnallocatedTask } from '@/entities/UnallocatedTask'

describe('sortByPriorityAndDeadline', () => {
it('should sort tasks by deadline', () => {
const tasks: UnallocatedTask[] = [
{
id: '2',
title: 'Test Task 2',
duration: '01:00',
priority: 'low',
deadline: new Date('2022-01-01'),
},
{
id: '1',
title: 'Test Task 1',
duration: '01:00',
priority: 'low',
deadline: new Date('2022-01-02'),
},
]

const sortedTasks = sortByPriorityAndDeadline(tasks)

expect(sortedTasks[0].id).toBe('2')
expect(sortedTasks[1].id).toBe('1')
})

it('should sort tasks by priority and deadline', () => {
const tasks: UnallocatedTask[] = [
{
Expand Down Expand Up @@ -32,17 +56,17 @@ describe('sortByPriorityAndDeadline', () => {

expect(result).toEqual([
{
id: '2',
title: 'Test Task 2',
id: '3',
title: 'Test Task 3',
duration: '01:00',
priority: 'high',
priority: 'medium',
deadline: new Date('2022-01-01T09:00:00'),
},
{
id: '3',
title: 'Test Task 3',
id: '2',
title: 'Test Task 2',
duration: '01:00',
priority: 'medium',
priority: 'high',
deadline: new Date('2022-01-01T09:00:00'),
},
{
Expand Down

0 comments on commit bd48eb6

Please sign in to comment.