This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
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.
feat: add tests for validateLocalFile func
Signed-off-by: michalrozekariane <michal.rozek@arianelabs.com>
- Loading branch information
1 parent
3104fd9
commit 50de01e
Showing
3 changed files
with
69 additions
and
3 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
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
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,54 @@ | ||
import fs from 'fs'; | ||
import { Hip412Validator } from '../../src/utils/services/Hip412Validator'; | ||
import { dictionary } from '../../src/utils/constants/dictionary'; | ||
|
||
jest.mock('fs'); | ||
|
||
const mockReadFileSync = fs.readFileSync as jest.Mock; | ||
|
||
describe('Hip412Validator.validateLocalFile', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should throw errors during file reading without permissions', () => { | ||
mockReadFileSync.mockImplementation(() => { | ||
throw new Error(dictionary.validation.filePermissionDenied); | ||
}); | ||
const validationResult = Hip412Validator.validateLocalFile('mockPath.json'); | ||
expect(validationResult.isValid).toBe(false); | ||
expect(validationResult.errors.general).toContain(dictionary.validation.filePermissionDenied); | ||
}); | ||
|
||
it('should handle JSON files with formatting errors', () => { | ||
mockReadFileSync.mockImplementation(() => { | ||
throw new Error(dictionary.validation.fileEmptyOrFormattingError); | ||
}); | ||
const validationResult = Hip412Validator.validateLocalFile('mockPath.json'); | ||
expect(validationResult.isValid).toBe(false); | ||
expect(validationResult.errors.general).toEqual([ | ||
dictionary.validation.fileEmptyOrFormattingError, | ||
]); | ||
}); | ||
|
||
it('should handle empty or non-existent JSON files', () => { | ||
mockReadFileSync.mockReturnValue(''); | ||
const validationResult = Hip412Validator.validateLocalFile('path/to/empty.json'); | ||
expect(validationResult.isValid).toBe(false); | ||
expect(validationResult.errors.general).toEqual([ | ||
dictionary.validation.fileEmptyOrFormattingError, | ||
]); | ||
}); | ||
|
||
it('should validate correctly structured JSON file', () => { | ||
const validJson = JSON.stringify({ | ||
name: 'Example NFT', | ||
image: 'https://example.com/nft.jpg', | ||
type: 'image/jpeg', | ||
}); | ||
mockReadFileSync.mockReturnValue(validJson); | ||
const validationResult = Hip412Validator.validateLocalFile('path/to/valid.json'); | ||
expect(validationResult.isValid).toBe(true); | ||
expect(validationResult.errors.general).toHaveLength(0); | ||
}); | ||
}); |