rename use-case to modules
This commit is contained in:
14
src/modules/task/models/step.fixture.ts
Normal file
14
src/modules/task/models/step.fixture.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import type { Stepable } from '@/modules/task/interfaces/stepable'
|
||||
import { Step } from '@/modules/task/models/step'
|
||||
import { faker } from '@faker-js/faker'
|
||||
|
||||
export const createStepFixture = (partialStep?: Partial<Stepable>) =>
|
||||
new Step(
|
||||
partialStep?.id ?? faker.datatype.uuid(),
|
||||
partialStep?.title ?? faker.animal.bird(),
|
||||
partialStep?.estimation ??
|
||||
faker.datatype.number({
|
||||
min: 0,
|
||||
max: 40
|
||||
})
|
||||
)
|
||||
17
src/modules/task/models/step.ts
Normal file
17
src/modules/task/models/step.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import type { Stepable } from '@/modules/task/interfaces/stepable'
|
||||
|
||||
export class Step implements Stepable {
|
||||
constructor(
|
||||
readonly id: string,
|
||||
readonly title: string,
|
||||
readonly estimation: number
|
||||
) {
|
||||
return this
|
||||
}
|
||||
|
||||
public static fromStepable(...stepables: Stepable[]): Step[] {
|
||||
return stepables.map(
|
||||
(stepable) => new Step(stepable.id, stepable.title, stepable.estimation)
|
||||
)
|
||||
}
|
||||
}
|
||||
14
src/modules/task/models/task.fixture.ts
Normal file
14
src/modules/task/models/task.fixture.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { faker } from '@faker-js/faker'
|
||||
import type { Stepable } from '../interfaces/stepable'
|
||||
import type { Taskable } from '../interfaces/taskable'
|
||||
import { createStepFixture } from './step.fixture'
|
||||
import { Task } from './task'
|
||||
|
||||
export const createTaskFixture = (
|
||||
partialTask?: Partial<Taskable>,
|
||||
...steps: Stepable[]
|
||||
) =>
|
||||
new Task(
|
||||
partialTask?.id ?? faker.datatype.uuid(),
|
||||
partialTask?.title ?? faker.animal.bird()
|
||||
).addSteps(...(steps ?? createStepFixture()))
|
||||
64
src/modules/task/models/task.test.ts
Normal file
64
src/modules/task/models/task.test.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import type { Taskable } from '@/modules/task/interfaces/taskable'
|
||||
import { createStepFixture } from '@/modules/task/models/step.fixture'
|
||||
import { Task } from '@/modules/task/models/task'
|
||||
import { faker } from '@faker-js/faker'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
describe('Task', () => {
|
||||
it('has a simple id', () => {
|
||||
const uuid = faker.datatype.uuid()
|
||||
|
||||
const task = new Task(uuid, faker.animal.bear())
|
||||
|
||||
expect(task.id).equals(uuid)
|
||||
})
|
||||
|
||||
it('allows a new task from a taskable object', () => {
|
||||
const taskable: Taskable = {
|
||||
id: faker.datatype.uuid(),
|
||||
date: faker.date.recent(),
|
||||
title: faker.animal.lion(),
|
||||
link: faker.internet.url(),
|
||||
steps: [createStepFixture()]
|
||||
}
|
||||
const task = Task.fromTaskable(taskable)
|
||||
|
||||
expect(task).toEqual(taskable)
|
||||
})
|
||||
|
||||
it('adds steps and removes them', () => {
|
||||
const task = new Task(faker.datatype.uuid(), faker.color.human())
|
||||
|
||||
const [firstStep, secondStep] = [createStepFixture(), createStepFixture()]
|
||||
|
||||
task.addSteps(firstStep, secondStep)
|
||||
|
||||
expect(task.steps).toEqual([firstStep, secondStep])
|
||||
|
||||
task.removeStep(0)
|
||||
expect(task.steps).toEqual([secondStep])
|
||||
|
||||
task.removeStep(0)
|
||||
expect(task.steps).toEqual([])
|
||||
})
|
||||
|
||||
it('must have an id, a title and steps to be valid', () => {
|
||||
const task = new Task(faker.datatype.uuid(), faker.color.human())
|
||||
expect(Task.validate(task)).toEqual(false)
|
||||
|
||||
task.addSteps(createStepFixture())
|
||||
expect(Task.validate(task)).toEqual(true)
|
||||
})
|
||||
|
||||
it('calculates the total estimation of steps', () => {
|
||||
const task = new Task(faker.datatype.uuid(), faker.color.human())
|
||||
|
||||
task.addSteps(
|
||||
createStepFixture({ estimation: 1 }),
|
||||
createStepFixture({ estimation: 2 }),
|
||||
createStepFixture({ estimation: 3 })
|
||||
)
|
||||
|
||||
expect(task.totalEstimation).toEqual(6)
|
||||
})
|
||||
})
|
||||
46
src/modules/task/models/task.ts
Normal file
46
src/modules/task/models/task.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import type { Stepable } from '@/modules/task/interfaces/stepable'
|
||||
import type { Taskable } from '@/modules/task/interfaces/taskable'
|
||||
import { Step } from '@/modules/task/models/step'
|
||||
|
||||
export class Task implements Taskable {
|
||||
public date = new Date()
|
||||
public steps: Step[] = []
|
||||
public link: string | null = null
|
||||
|
||||
constructor(public readonly id: string, public readonly title: string) {}
|
||||
|
||||
public addSteps(...steps: Stepable[]) {
|
||||
this.steps.push(...Step.fromStepable(...steps))
|
||||
return this
|
||||
}
|
||||
|
||||
public removeStep(index: number) {
|
||||
if (index < 0) {
|
||||
return this
|
||||
}
|
||||
|
||||
if (index >= this.steps.length) {
|
||||
return this
|
||||
}
|
||||
|
||||
this.steps.splice(index, 1)
|
||||
return this
|
||||
}
|
||||
|
||||
public get totalEstimation() {
|
||||
return this.steps.map((step) => step.estimation).reduce((a, b) => a + b, 0)
|
||||
}
|
||||
|
||||
public static fromTaskable(taskable: Taskable) {
|
||||
const task = new Task(taskable.id, taskable.title)
|
||||
task.link = taskable.link
|
||||
task.date = taskable.date
|
||||
task.addSteps(...taskable.steps)
|
||||
|
||||
return task
|
||||
}
|
||||
|
||||
public static validate(task: Taskable) {
|
||||
return !!task.id && !!task.title && task.steps.length > 0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user