Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests and refactor todoService #17

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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: Run tests
run: |
npm install -D
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
Loading