|
1 | | -import JsonFileCRUD from '../lib/json-file-crud.js'; |
| 1 | +import JsonFileCRUD, { createCrud } from '../lib/json-file-crud.js'; |
| 2 | +import fs from 'fs'; |
2 | 3 |
|
3 | 4 | let passed = 0; |
4 | 5 | let total = 0; |
@@ -44,5 +45,43 @@ test('has required methods', () => { |
44 | 45 | }); |
45 | 46 | }); |
46 | 47 |
|
| 48 | +// createCrud convenience function |
| 49 | +test('createCrud convenience function works', () => { |
| 50 | + const testFile = "./test-create-crud.json"; |
| 51 | + |
| 52 | + // Clean up |
| 53 | + if (fs.existsSync(testFile)) { |
| 54 | + fs.unlinkSync(testFile); |
| 55 | + } |
| 56 | + |
| 57 | + const convenienceCrud = createCrud(testFile); |
| 58 | + |
| 59 | + if (!convenienceCrud || typeof convenienceCrud.create !== 'function') { |
| 60 | + throw new Error('createCrud failed to create valid instance'); |
| 61 | + } |
| 62 | + |
| 63 | + // Clean up |
| 64 | + if (fs.existsSync(testFile)) { |
| 65 | + fs.unlinkSync(testFile); |
| 66 | + } |
| 67 | +}); |
| 68 | + |
| 69 | +// Directory creation |
| 70 | +test('automatically creates directories', () => { |
| 71 | + const deepPath = "./test-deep/nested/directory/data.json"; |
| 72 | + |
| 73 | + // Create instance (should create directories) |
| 74 | + const deepCrud = new JsonFileCRUD(deepPath); |
| 75 | + |
| 76 | + if (!deepCrud || !deepCrud.filePath.includes('test-deep')) { |
| 77 | + // Clean up |
| 78 | + fs.rmSync("./test-deep", { recursive: true, force: true }); |
| 79 | + throw new Error('failed to create instance with deep path'); |
| 80 | + } |
| 81 | + |
| 82 | + // Clean up |
| 83 | + fs.rmSync("./test-deep", { recursive: true, force: true }); |
| 84 | +}); |
| 85 | + |
47 | 86 | console.log(`\n${passed}/${total} tests passed`); |
48 | 87 | process.exit(passed === total ? 0 : 1); |
0 commit comments