-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9dd42e4
commit 3a4a00c
Showing
9 changed files
with
267 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = () => {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = () => { | ||
return [ | ||
{ | ||
titles: 'parent 1', | ||
tasks: [ | ||
{ | ||
titles: 'child 1', | ||
}, | ||
], | ||
}, | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { describe, it, expect, beforeEach } from 'vitest' | ||
import { Builder } from './builder' | ||
import { arrayOfTasksSchema, taskSchema } from './validator' | ||
|
||
describe('Builder', () => { | ||
let builder | ||
|
||
beforeEach(() => { | ||
builder = new Builder() | ||
}) | ||
|
||
describe('task', () => { | ||
it('adds a task definition to the list of tasks', () => { | ||
builder.task('Task 1', () => {}) | ||
builder.task('Task 2', () => {}) | ||
|
||
const { tasks } = builder.build() | ||
|
||
expect(tasks).toHaveLength(2) | ||
expect(tasks[0].title).toBe('Task 1') | ||
expect(tasks[1].title).toBe('Task 2') | ||
}) | ||
|
||
it('should be validated using the schema using the builder', () => { | ||
builder.task('Task 1', () => {}) | ||
builder.task('Task 2', () => {}) | ||
|
||
const { tasks } = builder.build() | ||
const { error } = arrayOfTasksSchema.validate(tasks) | ||
expect(error).toBe(undefined) | ||
}) | ||
}) | ||
|
||
describe('subTask', () => { | ||
it('sets the current task to the newly added task definition', () => { | ||
builder.task('Task 1', () => {}) | ||
builder.subTask('Subtask 1', () => {}) | ||
builder.task('Task 2', () => {}) | ||
|
||
const { tasks } = builder.build() | ||
|
||
expect(tasks[0].title).toBe('Task 1') | ||
expect(tasks[0].tasks).toHaveLength(1) | ||
expect(tasks[0].tasks[0].title).toBe('Subtask 1') | ||
|
||
expect(tasks[1].title).toBe('Task 2') | ||
expect(tasks[1].tasks).toHaveLength(0) | ||
}) | ||
|
||
it('adds a subtask definition to the current task', () => { | ||
builder.task('Task 1', () => {}) | ||
builder.subTask('Subtask 1', () => {}) | ||
builder.subTask('Subtask 2', () => {}) | ||
|
||
const { tasks } = builder.build() | ||
|
||
expect(tasks[0].tasks).toHaveLength(2) | ||
expect(tasks[0].tasks[0].title).toBe('Subtask 1') | ||
expect(tasks[0].tasks[1].title).toBe('Subtask 2') | ||
}) | ||
|
||
it('does not add a subtask definition if there is no current task', () => { | ||
builder.subTask('Subtask 1', () => {}) | ||
|
||
const { tasks } = builder.build() | ||
|
||
expect(tasks).toHaveLength(0) | ||
}) | ||
}) | ||
|
||
describe('setConcurrent', () => { | ||
it('sets the concurrent option to true', () => { | ||
builder.setConcurrent() | ||
|
||
const { options } = builder.build() | ||
|
||
expect(options.concurrent).toBe(true) | ||
}) | ||
|
||
it('sets the concurrent option to false', () => { | ||
builder.setConcurrent(false) | ||
|
||
const { options } = builder.build() | ||
|
||
expect(options.concurrent).toBe(false) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { it, describe, expect, vi } from 'vitest' | ||
import { startFile } from './reader' | ||
import path from 'node:path' | ||
|
||
vi.mock('listr2') | ||
|
||
describe('startFile', () => { | ||
it('should throw an error if no data is returned from the config file', async () => { | ||
const filePath = path.resolve(__dirname, '../examples/empty.config.js') | ||
await expect(startFile(filePath)).rejects.toThrow( | ||
'No data returned from config file' | ||
) | ||
}) | ||
|
||
it('should not throw an error for a raw mode', async () => { | ||
const filePath = path.resolve(__dirname, '../examples/raw.config.js') | ||
await expect(startFile(filePath)).resolves.toBeTruthy() | ||
}) | ||
|
||
it('should not throw an error for a builder mode', async () => { | ||
const filePath = path.resolve(__dirname, '../examples/builder.config.js') | ||
await expect(startFile(filePath)).resolves.toBeTruthy() | ||
}) | ||
|
||
it('should throw an error for an invalid schema', async () => { | ||
const filePath = path.resolve(__dirname, '../examples/notvalid.config.js') | ||
await expect(startFile(filePath)).rejects.toThrowError( | ||
'"[0].title" is required. "[0].tasks[0].title" is required. "[0].tasks[0].titles" is not allowed. "[0].titles" is not allowed' | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Joi from 'joi' | ||
|
||
export const taskSchema = Joi.object({ | ||
title: Joi.string().required(), | ||
task: Joi.function(), | ||
tasks: Joi.array().items(Joi.link('#task')), | ||
}).id('task') | ||
|
||
export const arrayOfTasksSchema = Joi.array() | ||
.items(taskSchema) | ||
.id('arrayOfTasks') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { taskSchema } from './validator.js' | ||
|
||
describe('taskSchema', () => { | ||
it('should validate a valid task object', () => { | ||
const validTask = { | ||
title: 'My Task', | ||
task: () => {}, | ||
tasks: [ | ||
{ | ||
title: 'Subtask 1', | ||
task: () => {}, | ||
tasks: [ | ||
{ | ||
title: 'Subtask 1.1', | ||
task: () => {}, | ||
}, | ||
], | ||
}, | ||
], | ||
} | ||
|
||
const { error } = taskSchema.validate(validTask) | ||
expect(error).toBe(undefined) | ||
}) | ||
|
||
it('should not validate an invalid task object', () => { | ||
const invalidTask = { | ||
title: 'My Task', | ||
task: () => {}, | ||
tasks: [ | ||
{ | ||
title: 'Subtask 1', | ||
task: () => {}, | ||
tasks: [ | ||
{ | ||
title: 'Subtask 1.1', | ||
task: 'not a function', | ||
}, | ||
], | ||
}, | ||
], | ||
} | ||
|
||
const { error } = taskSchema.validate(invalidTask) | ||
expect(error).not.toBe(undefined) | ||
}) | ||
}) |