test task model
This commit is contained in:
10
src/use-cases/task/models/step.fixture.ts
Normal file
10
src/use-cases/task/models/step.fixture.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { Stepable } from '@/use-cases/task/interfaces/stepable'
|
||||
import { Step } from '@/use-cases/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
|
||||
)
|
||||
@@ -1,27 +1,19 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { Step } from '@/use-cases/task/models/step'
|
||||
import { faker } from '@faker-js/faker'
|
||||
import type { Stepable } from '@/use-cases/task/interfaces/stepable'
|
||||
|
||||
const createStep = (partialStep?: Partial<Stepable>) =>
|
||||
new Step(
|
||||
partialStep?.id ?? faker.datatype.uuid(),
|
||||
partialStep?.title ?? faker.animal.bird(),
|
||||
partialStep?.estimation
|
||||
)
|
||||
import { createStepFixture } from '@/use-cases/task/models/step.fixture'
|
||||
|
||||
describe('Step', () => {
|
||||
it('adds substeps', () => {
|
||||
const step = createStep()
|
||||
const step = createStepFixture()
|
||||
|
||||
step.addSteps(createStep())
|
||||
step.addSteps(createStepFixture())
|
||||
|
||||
expect(step.steps.length).toEqual(1)
|
||||
})
|
||||
|
||||
it('removes substeps', () => {
|
||||
const step = createStep()
|
||||
step.addSteps(createStep())
|
||||
const step = createStepFixture()
|
||||
step.addSteps(createStepFixture())
|
||||
|
||||
step.removeStep(0)
|
||||
|
||||
@@ -29,19 +21,19 @@ describe('Step', () => {
|
||||
})
|
||||
|
||||
it('tells the estimation based on the sum of its substeps', () => {
|
||||
const step = createStep()
|
||||
const step = createStepFixture()
|
||||
.addSteps(
|
||||
createStep({
|
||||
createStepFixture({
|
||||
estimation: 1
|
||||
})
|
||||
)
|
||||
.addSteps(
|
||||
createStep({
|
||||
createStepFixture({
|
||||
estimation: 2
|
||||
})
|
||||
)
|
||||
.addSteps(
|
||||
createStep({
|
||||
createStepFixture({
|
||||
estimation: 3
|
||||
})
|
||||
)
|
||||
@@ -50,16 +42,16 @@ describe('Step', () => {
|
||||
})
|
||||
|
||||
it('tells the total estimation if the step estimation is set even with substeps', () => {
|
||||
const step = createStep({
|
||||
const step = createStepFixture({
|
||||
estimation: 8
|
||||
})
|
||||
.addSteps(
|
||||
createStep({
|
||||
createStepFixture({
|
||||
estimation: 1
|
||||
})
|
||||
)
|
||||
.addSteps(
|
||||
createStep({
|
||||
createStepFixture({
|
||||
estimation: 2
|
||||
})
|
||||
)
|
||||
@@ -69,20 +61,20 @@ describe('Step', () => {
|
||||
|
||||
it('flattens the substeps', () => {
|
||||
const leafs = [
|
||||
createStep({
|
||||
createStepFixture({
|
||||
id: 'leaf-1'
|
||||
}),
|
||||
createStep({
|
||||
createStepFixture({
|
||||
id: 'leaf-2'
|
||||
}),
|
||||
createStep({
|
||||
createStepFixture({
|
||||
id: 'leaf-3'
|
||||
})
|
||||
]
|
||||
|
||||
const step = createStep().addSteps(
|
||||
createStep().addSteps(leafs[0], leafs[1]),
|
||||
createStep().addSteps(createStep().addSteps(leafs[2]))
|
||||
const step = createStepFixture().addSteps(
|
||||
createStepFixture().addSteps(leafs[0], leafs[1]),
|
||||
createStepFixture().addSteps(createStepFixture().addSteps(leafs[2]))
|
||||
)
|
||||
const steps = Step.getStepLeafs([step])
|
||||
|
||||
|
||||
56
src/use-cases/task/models/task.test.ts
Normal file
56
src/use-cases/task/models/task.test.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import type { Stepable } from '@/use-cases/task/interfaces/stepable'
|
||||
import type { Taskable } from '@/use-cases/task/interfaces/taskable'
|
||||
import { createStepFixture } from '@/use-cases/task/models/step.fixture'
|
||||
import { Task } from '@/use-cases/task/models/task'
|
||||
import { faker } from '@faker-js/faker'
|
||||
import exp from 'constants'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
describe('Task', () => {
|
||||
it('has a simple id', () => {
|
||||
const uuid = faker.datatype.uuid()
|
||||
|
||||
const task = new Task(uuid)
|
||||
|
||||
expect(task.id).equals(uuid)
|
||||
})
|
||||
|
||||
it('allows a new task from a taskable object', () => {
|
||||
const taskable: Taskable = {
|
||||
id: faker.datatype.uuid(),
|
||||
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())
|
||||
|
||||
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())
|
||||
expect(Task.validate(task)).toEqual(false)
|
||||
|
||||
task.title = faker.animal.bird()
|
||||
expect(Task.validate(task)).toEqual(false)
|
||||
|
||||
task.addSteps(createStepFixture())
|
||||
expect(Task.validate(task)).toEqual(true)
|
||||
})
|
||||
})
|
||||
@@ -19,18 +19,14 @@ export class Task implements Taskable {
|
||||
return this
|
||||
}
|
||||
|
||||
if (index < this.steps.length) {
|
||||
if (index >= this.steps.length) {
|
||||
return this
|
||||
}
|
||||
|
||||
this.steps.splice(index)
|
||||
this.steps.splice(index, 1)
|
||||
return this
|
||||
}
|
||||
|
||||
public get flattenSteps() {
|
||||
return this.steps
|
||||
}
|
||||
|
||||
public static fromTaskable(taskable: Taskable) {
|
||||
const task = new Task(taskable.id)
|
||||
task.title = taskable.title
|
||||
|
||||
Reference in New Issue
Block a user