Skip to content

Commit

Permalink
fix: add trow erros
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsad committed Oct 9, 2023
1 parent 7d224c3 commit c71b446
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/JsonDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,23 @@ export class JsonDB {
return prev
}, {} as {[key: string]:string})

let router = ''
let normalPath: string[] = []

for await (const pathKey of Object.keys(pathObject)) {
router += `/${pathKey}`
normalPath.push(`/${pathKey}`)

const pathValue = pathObject[pathKey]
const pathIndex = await this.getIndex(router, pathValue)
router += `[${pathIndex}]`
try {
const pathIndex = await this.getIndex(normalPath.join(""), pathValue)
if(pathIndex === -1){
throw new DataError(`DataPath: ${normalPath.join("")}/${pathValue} not found.`, 13)
}

Check warning on line 447 in src/JsonDB.ts

View check run for this annotation

Codecov / codecov/patch

src/JsonDB.ts#L446-L447

Added lines #L446 - L447 were not covered by tests
normalPath.push(`[${pathIndex}]`)
} catch (error) {
throw new DataError(`DataPath: ${normalPath.join("")}/${pathValue} not found.`, 13, error)
}
}

return router
return normalPath.join("")
}
}
17 changes: 17 additions & 0 deletions test/JsonDB.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ describe('JsonDB', () => {
})
})

describe('toPath()', () => {
test('should throw path error when path not found', () => {
db.getData = jest.fn(async () => ({
a: [
{
id: '1'
}
]
}))
expect(async () => {
await db.toPath('/a/2')
}).rejects.toThrow(
'DataPath: /a/2 not found.'
)
})
})

// Test was made for code coverage for getParentData, but this cannot return null or undefined.
// Commented out the test and the checks in JsonDB.ts.
// describe('push()', () => {
Expand Down

0 comments on commit c71b446

Please sign in to comment.