diff --git a/packages/untp-test-suite/__tests__/core/processTestSuite.test.ts b/packages/untp-test-suite/__tests__/core/processTestSuite.test.ts index 7a8a61f6..dda72bd0 100644 --- a/packages/untp-test-suite/__tests__/core/processTestSuite.test.ts +++ b/packages/untp-test-suite/__tests__/core/processTestSuite.test.ts @@ -1,13 +1,19 @@ import * as commonUtils from '../../src/core/utils/common'; import * as testRunner from '../../src/core/processTestSuite'; import * as templateUtils from '../../src/templates/utils'; +import * as service from '../../src/core/services/dynamic-loading-schemas/loadingSchema.service'; import { TestSuiteMessageEnum, TestSuiteResultEnum } from '../../src/core/types'; +import { hasErrors } from '../../src/core/services/json-schema/validator.service'; jest.mock('../../src/utils/path', () => ({ getCurrentDirPath: jest.fn(() => '/test/data/data.json'), getCurrentFilePath: jest.fn(), })); +jest.mock('../../src/core/utils/common'); +jest.mock('../../src/core/services/dynamic-loading-schemas/loadingSchema.service'); +jest.mock('../../src/core/services/json-schema/validator.service'); + const credentialFileData = { credentials: [ { @@ -22,13 +28,25 @@ const passFinalReport = { finalMessage: 'Your credentials are UNTP compliant', }; -describe('processTestSuiteForConfigPath', () => { +describe('processTestSuiteForConfigPath with local credential type and version', () => { const credentialPath = 'src/config/credentials.json'; beforeEach(() => { JSON.parse = jest.fn().mockImplementation((value) => { return value; }); + + jest.spyOn(commonUtils, 'loadDataFromDataPath').mockResolvedValueOnce({ + data: { + id: 'https://example.com/product-passport/123456', + manufacturedDate: '2023-06-15', + }, + }); + + jest.spyOn(service, 'dynamicLoadingSchemaService').mockResolvedValueOnce({ + id: 'https://example.com/product-passport/123456', + manufacturedDate: '2023-06-15', + } as unknown as JSON); }); afterEach(() => { @@ -36,22 +54,19 @@ describe('processTestSuiteForConfigPath', () => { jest.resetAllMocks(); }); - it('should process the test suite and finalStatus PASS', async () => { + it('should process the test suite and finalStatus PASS without url', async () => { jest.spyOn(commonUtils, 'readJsonFile').mockResolvedValueOnce(credentialFileData); jest.spyOn(commonUtils, 'validateCredentialConfigs').mockReturnValueOnce([ { type: 'productPassport', version: 'v0.0.1', dataPath: 'data/productPassport.json', + url: '', errors: [], }, ]); - jest.spyOn(testRunner, 'processCheckDataBySchema').mockResolvedValueOnce({ - type: 'productPassport', - version: 'v0.0.1', - dataPath: 'data/productPassport.json', - errors: [], - }); + const mockHasErrors = hasErrors as jest.MockedFunction; + mockHasErrors.mockReturnValue([]); jest.spyOn(templateUtils, 'constructCredentialTestResults').mockResolvedValueOnce([ { credentialType: 'productPassport', @@ -257,7 +272,7 @@ describe('processTestSuiteForConfigPath', () => { }); }); - it('Should throw an error when validation of the credential configuration fails', async () => { + it('should throw an error when validation of the credential configuration fails', async () => { try { const emptyCredentialPath = 'config/empty-credentials.json'; jest.spyOn(commonUtils, 'readJsonFile').mockResolvedValueOnce({}); @@ -272,3 +287,416 @@ describe('processTestSuiteForConfigPath', () => { } }); }); + +describe('processTestSuiteForConfigPath with remote schema URL', () => { + const credentialPath = 'src/config/credentials.json'; + + beforeEach(() => { + JSON.parse = jest.fn().mockImplementation((value) => { + return value; + }); + + jest.spyOn(commonUtils, 'readJsonFile').mockResolvedValueOnce(credentialFileData); + + const mockData = { + data: { + id: 'https://example.com/product-passport/123456', + manufacturedDate: '2023-06-15', + }, + }; + jest.spyOn(commonUtils, 'loadDataFromDataPath').mockResolvedValueOnce({ + data: mockData, + }); + + jest.spyOn(commonUtils, 'fetchData').mockResolvedValueOnce({ + data: mockData, + error: '', + success: true, + }); + }); + + afterEach(() => { + jest.clearAllMocks(); + jest.resetAllMocks(); + }); + + it('should process the test suite and finalStatus PASS with empty type and version', async () => { + jest.spyOn(commonUtils, 'readJsonFile').mockResolvedValueOnce(credentialFileData); + jest.spyOn(commonUtils, 'validateCredentialConfigs').mockReturnValueOnce([ + { + type: '', + version: '', + dataPath: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + errors: [], + }, + ]); + + jest.spyOn(testRunner, 'processCheckDataBySchema').mockReturnValueOnce({ + type: '', + version: '', + dataPath: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + errors: [], + }); + + jest.spyOn(templateUtils, 'constructCredentialTestResults').mockResolvedValueOnce([ + { + credentialType: '', + version: '', + path: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + result: TestSuiteResultEnum.PASS, + }, + ]); + + jest.spyOn(templateUtils, 'constructFinalReport').mockResolvedValueOnce({ + finalStatus: TestSuiteResultEnum.PASS, + finalMessage: TestSuiteMessageEnum.PASS, + }); + + const result = await testRunner.processTestSuiteForConfigPath(credentialPath); + + expect(result).toEqual({ + credentials: [ + { + credentialType: '', + version: '', + path: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + result: 'PASS', + }, + ], + ...passFinalReport, + }); + }); + + it('should process the test suite and finalStatus PASS with type, version and url', async () => { + jest.spyOn(commonUtils, 'readJsonFile').mockResolvedValueOnce(credentialFileData); + jest.spyOn(commonUtils, 'validateCredentialConfigs').mockReturnValueOnce([ + { + type: 'productPassport', + version: 'v0.0.1', + dataPath: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + errors: [], + }, + ]); + + jest.spyOn(testRunner, 'processCheckDataBySchema').mockReturnValueOnce({ + type: 'productPassport', + version: 'v0.0.1', + dataPath: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + errors: [], + }); + + jest.spyOn(templateUtils, 'constructCredentialTestResults').mockResolvedValueOnce([ + { + credentialType: '', + version: '', + path: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + result: TestSuiteResultEnum.PASS, + }, + ]); + + jest.spyOn(templateUtils, 'constructFinalReport').mockResolvedValueOnce({ + finalStatus: TestSuiteResultEnum.PASS, + finalMessage: TestSuiteMessageEnum.PASS, + }); + + const result = await testRunner.processTestSuiteForConfigPath(credentialPath); + + expect(result).toEqual({ + credentials: [ + { + credentialType: '', + version: '', + path: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + result: 'PASS', + }, + ], + ...passFinalReport, + }); + }); + + it('should process the test suite with an array of errors and finalStatus FAIL', async () => { + jest.spyOn(commonUtils, 'validateCredentialConfigs').mockReturnValueOnce([ + { + type: 'productPassport', + version: 'v0.0.1', + dataPath: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + errors: [], + }, + { + type: 'productPassport', + version: 'v0.0.2', + dataPath: 'data/productPassport.json', + url: 'invalid-url.com', + errors: [ + { + instancePath: 'url', + message: 'The URL "invalid-url.com" must use http or https protocol.', + keyword: 'format', + dataPath: 'data/productPassport.json', + }, + ], + }, + ]); + + jest.spyOn(templateUtils, 'constructCredentialTestResults').mockResolvedValueOnce([ + { + credentialType: 'productPassport', + version: 'v0.0.1', + path: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + result: TestSuiteResultEnum.PASS, + }, + { + credentialType: 'productPassport', + version: 'v0.0.2', + path: 'data/productPassport.json', + url: 'invalid-url.com', + result: TestSuiteResultEnum.FAIL, + errors: [ + { + fieldName: 'url', + message: 'The URL "invalid-url.com" must use http or https protocol.', + errorType: 'format', + }, + ], + }, + ]); + + jest.spyOn(templateUtils, 'constructFinalReport').mockResolvedValueOnce({ + finalStatus: TestSuiteResultEnum.FAIL, + finalMessage: TestSuiteMessageEnum.FAIL, + }); + + const result = await testRunner.processTestSuiteForConfigPath(credentialPath); + expect(result).toEqual({ + credentials: [ + { + credentialType: 'productPassport', + version: 'v0.0.1', + path: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + result: 'PASS', + }, + { + credentialType: 'productPassport', + version: 'v0.0.2', + path: 'data/productPassport.json', + url: 'invalid-url.com', + result: 'FAIL', + errors: [ + { + fieldName: 'url', + message: 'The URL "invalid-url.com" must use http or https protocol.', + errorType: 'format', + }, + ], + }, + ], + finalStatus: 'FAIL', + finalMessage: 'Your credentials are not UNTP compliant', + }); + }); + + it('should process the test suite with an array of warnings and finalStatus WARN status', async () => { + jest.spyOn(commonUtils, 'validateCredentialConfigs').mockReturnValueOnce([ + { + type: '', + version: '', + dataPath: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + errors: [], + }, + ]); + jest.spyOn(templateUtils, 'constructCredentialTestResults').mockResolvedValueOnce([ + { + credentialType: '', + version: '', + path: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + result: TestSuiteResultEnum.WARN, + errors: [], + warnings: [ + { + fieldName: 'additionalFieldTest', + message: 'This schema must NOT have additional properties', + }, + ], + }, + ]); + jest.spyOn(templateUtils, 'constructFinalReport').mockResolvedValueOnce({ + finalStatus: TestSuiteResultEnum.WARN, + finalMessage: TestSuiteMessageEnum.WARN, + }); + + const result = await testRunner.processTestSuiteForConfigPath(credentialPath); + + expect(result).toEqual({ + credentials: [ + { + credentialType: '', + version: '', + path: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + result: TestSuiteResultEnum.WARN, + errors: [], + warnings: [ + { + fieldName: 'additionalFieldTest', + message: 'This schema must NOT have additional properties', + }, + ], + }, + ], + finalStatus: 'WARN', + finalMessage: 'Your credentials are UNTP compliant, but have extended the data model', + }); + }); + + it('should process the test suite with an array of FAIL when fetch data error', async () => { + jest.spyOn(commonUtils, 'validateCredentialConfigs').mockReturnValueOnce([ + { + type: '', + version: '', + dataPath: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + errors: [], + }, + ]); + jest.spyOn(commonUtils, 'fetchData').mockResolvedValueOnce({ + data: {}, + error: 'Failed to fetch data', + success: false, + }); + + jest.spyOn(templateUtils, 'constructCredentialTestResults').mockResolvedValueOnce([ + { + credentialType: '', + version: '', + path: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + result: TestSuiteResultEnum.FAIL, + errors: [ + { + fieldName: 'url', + message: 'Failed to fetch data', + errorType: 'FetchError', + }, + ], + }, + ]); + + jest.spyOn(templateUtils, 'constructFinalReport').mockResolvedValueOnce({ + finalStatus: TestSuiteResultEnum.FAIL, + finalMessage: TestSuiteMessageEnum.FAIL, + }); + + const result = await testRunner.processTestSuiteForConfigPath(credentialPath); + + expect(result).toEqual({ + credentials: [ + { + credentialType: '', + version: '', + path: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + result: 'FAIL', + errors: [ + { + fieldName: 'url', + message: 'Failed to fetch data', + errorType: 'FetchError', + }, + ], + }, + ], + finalStatus: 'FAIL', + finalMessage: 'Your credentials are not UNTP compliant', + }); + }); +}); + +describe('processTestSuiteForCredential', () => { + const mockData = { + data: { + id: 'https://example.com/product-passport/123456', + manufacturedDate: '2023-06-15', + }, + }; + + beforeEach(() => { + jest.spyOn(commonUtils, 'loadDataFromDataPath').mockResolvedValueOnce({ + data: mockData, + }); + }); + + afterEach(() => { + jest.clearAllMocks(); // Clear mock state between tests + }); + + it('should fetch schema from URL when configContent.url is provided', async () => { + jest.spyOn(commonUtils, 'fetchData').mockResolvedValueOnce({ + data: mockData, + error: '', + success: true, + }); + + const configContent = { + type: 'productPassport', + version: 'v0.0.1', + dataPath: 'data/productPassport.json', + url: 'https://example.com/product-passport/schema.json', + }; + jest.spyOn(commonUtils, 'fetchData').mockResolvedValueOnce({ + data: { + id: 'https://example.com/product-passport/123456', + manufacturedDate: '2023-06-15', + }, + error: '', + success: true, + }); + + const execptedResult = { + credentialType: 'productPassport', + version: 'v0.0.1', + path: 'data/productPassport.json', + result: TestSuiteResultEnum.PASS, + }; + jest.spyOn(templateUtils, 'constructCredentialTestResult').mockResolvedValueOnce(execptedResult); + + const result = await testRunner.processTestSuiteForCredential(configContent); + + expect(result).toEqual(execptedResult); + }); + + it('should load schema from dynamicLoadingSchemaService when configContent.url is not provided', async () => { + const configContent = { + type: 'productPassport', + version: 'v0.0.1', + dataPath: 'data/productPassport.json', + url: '', + }; + jest.spyOn(service, 'dynamicLoadingSchemaService').mockResolvedValueOnce(mockData as unknown as JSON); + + const execptedResult = { + credentialType: 'productPassport', + version: 'v0.0.1', + url: '', + dataPath: 'data/productPassport.json', + result: TestSuiteResultEnum.PASS, + }; + + jest.spyOn(templateUtils, 'constructCredentialTestResult').mockResolvedValueOnce(execptedResult); + const result = await testRunner.processTestSuiteForCredential(configContent); + + expect(result).toEqual(execptedResult); + }); +}); diff --git a/packages/untp-test-suite/__tests__/core/utils/common.test.ts b/packages/untp-test-suite/__tests__/core/utils/common.test.ts index afcb51d2..29634688 100644 --- a/packages/untp-test-suite/__tests__/core/utils/common.test.ts +++ b/packages/untp-test-suite/__tests__/core/utils/common.test.ts @@ -1,5 +1,11 @@ import * as fs from 'fs/promises'; -import { validateCredentialConfigs, readJsonFile } from '../../../src/core/utils/common'; +import { + validateCredentialConfigs, + readJsonFile, + validateUrlField, + fetchData, + loadDataFromDataPath, +} from '../../../src/core/utils/common'; jest.mock('fs/promises', () => ({ readFile: jest.fn(), @@ -64,9 +70,120 @@ describe('validateCredentialConfigs', () => { }, ]); }); + + it('should validate a local schema with valid type and version', () => { + const credentialConfigs = [ + { + type: 'objectEvent', + version: 'v1.0', + url: '', + dataPath: 'path/to/data', + }, + ]; + + const result = validateCredentialConfigs(credentialConfigs, credentialConfigsPath); + + expect(result).toEqual([ + { + type: 'objectEvent', + version: 'v1.0', + url: '', + dataPath: 'path/to/data', + errors: [], + }, + ]); + }); + + it('should validate a remote schema with a valid URL', () => { + const credentialConfigs = [ + { + type: '', + version: '', + url: 'https://example.com', + dataPath: 'path/to/data', + }, + ]; + + const result = validateCredentialConfigs(credentialConfigs, credentialConfigsPath); + + expect(result).toEqual([ + { + type: '', + version: '', + url: 'https://example.com', + dataPath: 'path/to/data', + errors: [], + }, + ]); + }); + + it('should validate a remote schema with an invalid URL', () => { + const credentialConfigs = [ + { + type: '', + version: '', + url: 'example.com', + dataPath: 'path/to/data', + }, + ]; + + const result = validateCredentialConfigs(credentialConfigs, credentialConfigsPath); + + expect(result).toEqual([ + { + type: '', + version: '', + url: 'example.com', + dataPath: 'path/to/data', + errors: [ + { + message: 'The URL "example.com" is not a valid URL.', + keyword: 'format', + dataPath: 'path/to/credentials', + instancePath: 'url', + }, + ], + }, + ]); + }); + + it('should throw an error when dataPath is missing', () => { + const credentialConfigs = [ + { + type: 'objectEvent', + version: 'v1.0', + url: '', + dataPath: '', + }, + ]; + + const result = validateCredentialConfigs(credentialConfigs, credentialConfigsPath); + + expect(result).toEqual([ + { + type: 'objectEvent', + version: 'v1.0', + url: '', + dataPath: '', + errors: [ + { + message: 'should have required property', + keyword: 'required', + dataPath: 'path/to/credentials', + instancePath: 'dataPath', + }, + ], + }, + ]); + }); }); describe('readFile', () => { + afterEach(() => { + jest.restoreAllMocks(); + jest.resetAllMocks(); + }); + it('should read the file', async () => { jest.spyOn(fs, 'readFile' as any).mockResolvedValue('{"test": "test"}'); JSON.parse = jest.fn().mockImplementation(() => { @@ -87,3 +204,157 @@ describe('readFile', () => { } }); }); + +describe('validUrlField', () => { + it('should return valid: false and error message if obj is not an object', () => { + const obj = 'test'; + // @ts-ignore + const result = validateUrlField(obj, 'url'); + expect(result).toEqual({ valid: false, errorMessage: 'The provided object is invalid.' }); + }); + + it('should return valid: false and error message if field is missing or not a string', () => { + const obj = {}; + const result = validateUrlField(obj, 'url'); + expect(result).toEqual({ valid: false, errorMessage: 'The field "url" is missing or not a valid string.' }); + }); + + it('should return valid: false and error message if the URL is invalid', () => { + const obj = { url: 'invalid-url' }; + const result = validateUrlField(obj, 'url'); + expect(result).toEqual({ valid: false, errorMessage: 'The URL "invalid-url" is not a valid URL.' }); + }); + + it('should return valid: false and error message if field is an empty string', () => { + const obj = { url: '' }; + const result = validateUrlField(obj, 'url'); + expect(result).toEqual({ valid: false, errorMessage: 'The field "url" is missing or not a valid string.' }); + }); + + it('should return valid: false and error message if URL does not use http or https protocol', () => { + const obj = { url: 'ftp://example.com' }; + const result = validateUrlField(obj, 'url'); + expect(result).toEqual({ + valid: false, + errorMessage: 'The URL "ftp://example.com" must use http or https protocol.', + }); + }); + + it('should return valid: true if URL is valid and uses http protocol', () => { + const obj = { url: 'http://example.com' }; + const result = validateUrlField(obj, 'url'); + expect(result).toEqual({ valid: true }); + }); + + it('should return valid: true if URL is valid and uses https protocol', () => { + const obj = { url: 'https://example.com' }; + const result = validateUrlField(obj, 'url'); + expect(result).toEqual({ valid: true }); + }); +}); + +describe('fetchData', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should return an error when the URL is invalid', async () => { + const result = await fetchData(''); + + expect(result).toEqual({ + data: null, + error: 'Invalid URL provided.', + success: false, + }); + }); + + it('should return an error if URL is not a string', async () => { + const result = await fetchData(''); + + expect(result).toEqual({ + data: null, + error: 'Invalid URL provided.', + success: false, + }); + }); + + it('should fetch data successfully with a valid URL', async () => { + const mockData = { test: 'test' }; + const response = { + json: jest.fn().mockResolvedValue(mockData), + }; + + global.fetch = jest.fn().mockResolvedValue(response); + + const result = await fetchData('https://example.com'); + expect(result).toEqual({ + data: mockData, + error: null, + success: true, + }); + }); + + it('should throw an error when fetching data fails', async () => { + const mockError = new Error('Network error'); + + global.fetch = jest.fn().mockRejectedValue(mockError); + + const result = await fetchData('https://example.com/data'); + + expect(result).toEqual({ + data: null, + error: 'Failed to fetch data from the URL: https://example.com/data.', + success: false, + }); + expect(global.fetch).toHaveBeenCalledWith('https://example.com/data'); + }); +}); + +describe('loadDataFromDataPath', () => { + afterEach(() => { + jest.restoreAllMocks(); + jest.resetAllMocks(); + }); + + it('should load data from dataPath using readJsonFile', async () => { + const credentialConfig = { + type: 'objectEvent', + version: 'v1.0', + url: '', + dataPath: 'path/to/data.json', + }; + const mockData = { name: 'John Doe' }; + JSON.parse = jest.fn().mockImplementation(() => { + return mockData; + }); + jest.spyOn(fs, 'readFile' as any).mockResolvedValue(JSON.stringify(mockData)); + + const result = await loadDataFromDataPath(credentialConfig); + + expect(fs.readFile).toHaveBeenCalledWith('path/to/data.json', 'utf-8'); + expect(result).toEqual({ data: mockData }); + }); + + it('should return provided data when data is provided and dataPath is not provided', async () => { + const credentialConfig = { + type: 'objectEvent', + version: 'v1.0', + url: '', + dataPath: '', + }; + const data = { name: 'John Doe' }; + const result = await loadDataFromDataPath(credentialConfig, data); + + expect(fs.readFile).not.toHaveBeenCalled(); // Ensure readJsonFile is not called + expect(result).toEqual({ data }); + }); + + it('should throw an error when data and dataPath are missing', async () => { + try { + // @ts-ignore + await loadDataFromDataPath({}); + } catch (error) { + expect(error).toEqual(new Error('Must provide either data or dataPath to check data by schema.')); + } + }); +}); diff --git a/packages/untp-test-suite/__tests__/interfaces/utils/credentials.test.ts b/packages/untp-test-suite/__tests__/interfaces/utils/credentials.test.ts index e82f6112..67f12944 100644 --- a/packages/untp-test-suite/__tests__/interfaces/utils/credentials.test.ts +++ b/packages/untp-test-suite/__tests__/interfaces/utils/credentials.test.ts @@ -12,7 +12,6 @@ const schemasPath = 'src/schemas'; const schemaVersionMock = 'v0.0.3'; describe('getLastestVersionFolder', () => { - it('should return the latest version folder name', async () => { jest.spyOn(semver, 'maxSatisfying').mockReturnValueOnce(schemaVersionMock); const eventType = 'aggregationEvent'; @@ -36,19 +35,21 @@ describe('getLastestVersionFolder', () => { }); describe('getLatestCredentialVersions', () => { - it('should retrieve latest credential versions successfully for each event type folder', async () => { - const readdirSpy = (jest.spyOn(fs, 'readdir')) as unknown as jest.SpyInstance>; + const readdirSpy = jest.spyOn(fs, 'readdir') as unknown as jest.SpyInstance>; readdirSpy.mockResolvedValueOnce(['aggregationEvent']); jest.spyOn(credentials, 'getLastestVersionFolder').mockResolvedValueOnce('v0.0.3'); const latestCredentialVersions = await credentials.getLatestCredentialVersions(schemasPath); - expect(latestCredentialVersions).toEqual([{ - type: 'aggregationEvent', - version: 'v0.0.3', - dataPath: '', - }]); + expect(latestCredentialVersions).toEqual([ + { + type: 'aggregationEvent', + version: 'v0.0.3', + dataPath: '', + url: '', + }, + ]); }); it('should throw an error when invalid schemas path', async () => { @@ -67,9 +68,11 @@ describe('getLatestCredentialVersions', () => { const invalidEventType = 'invalid-event-type'; try { - const readdirSpy = (jest.spyOn(fs, 'readdir')) as unknown as jest.SpyInstance>; + const readdirSpy = jest.spyOn(fs, 'readdir') as unknown as jest.SpyInstance>; readdirSpy.mockResolvedValueOnce([invalidEventType]); - jest.spyOn(credentials, 'getLastestVersionFolder').mockRejectedValueOnce(`Invalid event type: ${invalidEventType}`); + jest + .spyOn(credentials, 'getLastestVersionFolder') + .mockRejectedValueOnce(`Invalid event type: ${invalidEventType}`); await credentials.getLatestCredentialVersions(invalidSchemasPath); } catch (error) { @@ -82,21 +85,26 @@ describe('generateCredentialFile', () => { const storePath = './test/credentials.json'; it('should generate latest credential file successfully', async () => { - const latestCredentialVersions = [{ type: 'aggregationEvent', version: 'v0.0.3', dataPath: '' }]; + const latestCredentialVersions = [{ type: 'aggregationEvent', version: 'v0.0.3', dataPath: '', url: '' }]; jest.spyOn(path, 'resolve').mockReturnValueOnce('../../../src/schemas'); jest.spyOn(credentials, 'getLatestCredentialVersions').mockResolvedValueOnce(latestCredentialVersions); jest.spyOn(fs, 'writeFile').mockResolvedValueOnce(); const credentialFileData = await credentials.generateCredentialFile(storePath); - expect(fs.writeFile).toHaveBeenCalledWith(storePath, JSON.stringify({ credentials: latestCredentialVersions }, null, 2)); + expect(fs.writeFile).toHaveBeenCalledWith( + storePath, + JSON.stringify({ credentials: latestCredentialVersions }, null, 2), + ); expect(credentialFileData).toEqual({ credentials: latestCredentialVersions }); }); it('should throw an error when invalid store path', async () => { try { jest.spyOn(path, 'resolve').mockReturnValueOnce('../../../src/schemas'); - jest.spyOn(credentials, 'getLatestCredentialVersions').mockResolvedValueOnce([{ type: 'aggregationEvent', version: 'v0.0.3', dataPath: '' }]); + jest + .spyOn(credentials, 'getLatestCredentialVersions') + .mockResolvedValueOnce([{ type: 'aggregationEvent', version: 'v0.0.3', dataPath: '', url: '' }]); jest.spyOn(fs, 'writeFile').mockRejectedValueOnce('invalid store path'); const invalidStorePath = 'invalid-store-path'; @@ -116,5 +124,4 @@ describe('generateCredentialFile', () => { expect(error).toContain('invalid schemas path'); } }); - -}); \ No newline at end of file +});