From bd48eb6aff6653fb2691b8dd929732d7500eb0e9 Mon Sep 17 00:00:00 2001 From: Giselle Hoekveld Date: Sat, 13 Apr 2024 21:42:07 -0300 Subject: [PATCH] Add Jest unit testing workflow and update package.json and test files --- .github/workflows/run-unit-tests.yml | 17 +++++++++ package.json | 6 ++- .../schedule/utils/allocateTask.test.ts | 9 ++--- src/helpers/schedule/utils/allocateTask.ts | 2 +- .../utils/sortByPriorityAndDeadline.test.ts | 38 +++++++++++++++---- 5 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 .github/workflows/run-unit-tests.yml diff --git a/.github/workflows/run-unit-tests.yml b/.github/workflows/run-unit-tests.yml new file mode 100644 index 0000000..5c9b32d --- /dev/null +++ b/.github/workflows/run-unit-tests.yml @@ -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 diff --git a/package.json b/package.json index 7070d97..bb7dbf1 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,11 @@ }, "testMatch": [ "**/*.test.ts" - ] + ], + "moduleNameMapper": { + "^@/(.*)$": "/src/$1" + }, + "testPathIgnorePatterns": ["/.next/", "/node_modules/", "/src/errors/*.ts"] }, "dependencies": { "@emotion/react": "^11.11.4", diff --git a/src/helpers/schedule/utils/allocateTask.test.ts b/src/helpers/schedule/utils/allocateTask.test.ts index 0607902..466ce9c 100644 --- a/src/helpers/schedule/utils/allocateTask.test.ts +++ b/src/helpers/schedule/utils/allocateTask.test.ts @@ -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(() => { @@ -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')) @@ -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', @@ -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) }) }) diff --git a/src/helpers/schedule/utils/allocateTask.ts b/src/helpers/schedule/utils/allocateTask.ts index 7327d7f..475b729 100644 --- a/src/helpers/schedule/utils/allocateTask.ts +++ b/src/helpers/schedule/utils/allocateTask.ts @@ -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[], diff --git a/src/helpers/schedule/utils/sortByPriorityAndDeadline.test.ts b/src/helpers/schedule/utils/sortByPriorityAndDeadline.test.ts index 3127510..f72d34f 100644 --- a/src/helpers/schedule/utils/sortByPriorityAndDeadline.test.ts +++ b/src/helpers/schedule/utils/sortByPriorityAndDeadline.test.ts @@ -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[] = [ { @@ -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'), }, {