-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(data): add first unit tests for DecksManager
- Loading branch information
1 parent
4ad16d5
commit 863b20f
Showing
1 changed file
with
77 additions
and
0 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,77 @@ | ||
import { expect, vi, it, describe, beforeEach } from 'vitest'; | ||
import { Vault } from 'obsidian'; | ||
import { DecksManager } from './decks-manager'; | ||
import { SpacedRepetitionAlgorithm } from 'src/spaced-repetition'; | ||
import { JsonFileManager } from '../json'; | ||
|
||
const mocks = vi.hoisted(() => ({ | ||
vault: {} as Vault, | ||
jsonFileManager: { | ||
writeJsonFile: vi.fn(), | ||
createJsonFile: vi.fn(), | ||
readJsonFile: vi.fn(), | ||
exists: vi.fn(), | ||
}, | ||
})); | ||
|
||
vi.mock('obsidian', async () => { | ||
const actual = await vi.importActual('obsidian'); | ||
return { ...actual, normalizePath: vi.fn(() => '') }; | ||
}); | ||
|
||
describe('DecksManager', () => { | ||
let decksManager: DecksManager; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
const mockAlgorithm = {} as SpacedRepetitionAlgorithm<unknown>; | ||
decksManager = new DecksManager(mocks.vault, mockAlgorithm, 'testPluginId'); | ||
decksManager['jsonFileManager'] = | ||
mocks.jsonFileManager as unknown as JsonFileManager; | ||
}); | ||
|
||
describe('load', () => { | ||
it('should create a new file if it does not exist', async () => { | ||
mocks.jsonFileManager.exists.mockResolvedValue(false); | ||
await decksManager.load(); | ||
expect(mocks.jsonFileManager.createJsonFile).toHaveBeenCalledWith( | ||
'decks.json', | ||
); | ||
}); | ||
|
||
it('should load existing decks', async () => { | ||
mocks.jsonFileManager.exists.mockResolvedValue(true); | ||
mocks.jsonFileManager.readJsonFile.mockResolvedValue({ | ||
decks: [ | ||
{ id: 'deck1', name: 'Deck 1', description: 'Test deck', cards: {} }, | ||
], | ||
}); | ||
await decksManager.load(); | ||
expect(decksManager.getDecks()).toHaveProperty('deck1'); | ||
}); | ||
}); | ||
|
||
// describe('create', () => {}); | ||
|
||
// describe('updateInformation', () => {}); | ||
|
||
// describe('addCard', () => {}); | ||
|
||
// describe('updateCardContent', () => {}); | ||
|
||
// describe('removeCard', () => {}); | ||
|
||
describe('delete', () => { | ||
it('should delete a deck', async () => { | ||
const deck = await decksManager.create('Test Deck', 'Test Description'); | ||
await decksManager.delete(deck.id); | ||
expect(decksManager.getDecks()).not.toHaveProperty(deck.id); | ||
}); | ||
|
||
it('should throw an error for non-existent deck', async () => { | ||
await expect(decksManager.delete('nonexistent')).rejects.toThrow( | ||
'Deck name does not exist', | ||
); | ||
}); | ||
}); | ||
}); |