Skip to content

Commit

Permalink
Add tests and refactor todoService
Browse files Browse the repository at this point in the history
  • Loading branch information
joeriddles committed Apr 3, 2024
1 parent 3c500cd commit c453c50
Show file tree
Hide file tree
Showing 13 changed files with 4,809 additions and 1,104 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Run Tests

on:
push:
branches:
- main
pull_request:

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: "18.x"

- name: Build plugin
run: |
npm run test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# npm
node_modules

# jest
coverage

# Don't include the compiled main.js file in the repo.
# They should be uploaded to GitHub releases instead.
main.js
Expand Down
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--runInBand",
"--verbose"
],
"console": "internalConsole",
"internalConsoleOptions": "neverOpen"
}
]
}
5 changes: 1 addition & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@
"source.fixAll": "explicit"
},
},
"eslint.codeActionsOnSave.mode": "problems",
"cSpell.words": [
"todos"
]
"eslint.codeActionsOnSave.mode": "problems"
}
16 changes: 16 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/

import type { Config } from 'jest';

const config: Config = {
preset: "ts-jest/presets/js-with-ts",
clearMocks: true,
collectCoverage: true,
coverageDirectory: "coverage",
coverageProvider: "v8",
};

export default config;
16 changes: 11 additions & 5 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Plugin, TFile, Vault } from 'obsidian';
import { DEFAULT_SETTINGS, ExtendedTaskListsSettingTab, ExtendedTaskListsSettings } from 'settings';
import TodoService, { Todo } from './todoService';
import { IFile, VaultFileService } from 'src/fileService';
import { DEFAULT_SETTINGS, ExtendedTaskListsSettingTab, ExtendedTaskListsSettings } from 'src/settings';
import TodoService, { Todo } from 'src/todoService';

export default class ExtendedTaskListsPlugin extends Plugin {
settings: ExtendedTaskListsSettings
Expand Down Expand Up @@ -76,13 +77,18 @@ export default class ExtendedTaskListsPlugin extends Plugin {

updateTodo = async () => {
const vault = this.app.vault
const service = new TodoService(vault, this.settings)
const fileService = new VaultFileService(vault)
const service = new TodoService(fileService, this.settings)
const todoFiles = await service.findTodosFiles()
const todos: Todo[] = todoFiles
.map(todoFile => service.parseTodos(todoFile))
.map(todoFile => {
const todos = service.parseTodos(todoFile.contents)
todos.forEach(todo => todo.file = todoFile.file)
return todos
})
.reduce((prev, cur) => prev.concat(cur), [])
const todoFile = await this.getOrCreateTodoFile(vault);
await service.saveTodos(todoFile, todos)
await service.saveTodos(todoFile as IFile, todos)
}

getOrCreateTodoFile = async (vault: Vault): Promise<TFile> => {
Expand Down
Loading

0 comments on commit c453c50

Please sign in to comment.