mirror of https://github.com/ghostfolio/ghostfolio
committed by
GitHub
13 changed files with 720 additions and 88 deletions
@ -0,0 +1,123 @@ |
|||
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
|||
|
|||
import { DataSource } from '@prisma/client'; |
|||
|
|||
import { |
|||
encodeDataSource, |
|||
isValidEncodedDataSource, |
|||
transformDataSourceInRequest, |
|||
transformDataSourceInResponse |
|||
} from './data-source.helper'; |
|||
|
|||
function createConfigurationService({ |
|||
isSubscriptionEnabled |
|||
}: { |
|||
isSubscriptionEnabled: boolean; |
|||
}) { |
|||
const configuration: Record<string, unknown> = { |
|||
DATA_SOURCES_GHOSTFOLIO_DATA_PROVIDER: [DataSource.YAHOO], |
|||
ENABLE_FEATURE_SUBSCRIPTION: isSubscriptionEnabled |
|||
}; |
|||
|
|||
return { |
|||
get: jest.fn((key: string) => { |
|||
return configuration[key]; |
|||
}) |
|||
} as unknown as ConfigurationService; |
|||
} |
|||
|
|||
describe('DataSourceHelper', () => { |
|||
describe('isValidEncodedDataSource', () => { |
|||
it('Accepts an encoded data source', () => { |
|||
expect(isValidEncodedDataSource(encodeDataSource(DataSource.YAHOO))).toBe( |
|||
true |
|||
); |
|||
}); |
|||
|
|||
it('Refuses a data source which is not encoded', () => { |
|||
expect(isValidEncodedDataSource(DataSource.YAHOO)).toBe(false); |
|||
}); |
|||
|
|||
it('Refuses an unknown encoded data source', () => { |
|||
expect(isValidEncodedDataSource('ffffffff')).toBe(false); |
|||
}); |
|||
}); |
|||
|
|||
describe('transformDataSourceInRequest', () => { |
|||
const configurationService = createConfigurationService({ |
|||
isSubscriptionEnabled: true |
|||
}); |
|||
|
|||
it('Decodes an encoded data source', () => { |
|||
expect( |
|||
transformDataSourceInRequest({ |
|||
configurationService, |
|||
dataSource: encodeDataSource(DataSource.COINGECKO) |
|||
}) |
|||
).toBe(DataSource.COINGECKO); |
|||
}); |
|||
|
|||
it('Resolves the mask of the data source of the Ghostfolio data provider', () => { |
|||
expect( |
|||
transformDataSourceInRequest({ |
|||
configurationService, |
|||
dataSource: DataSource.GHOSTFOLIO |
|||
}) |
|||
).toBe(DataSource.YAHOO); |
|||
}); |
|||
|
|||
it('Keeps a data source which is not encoded', () => { |
|||
expect( |
|||
transformDataSourceInRequest({ |
|||
configurationService, |
|||
dataSource: DataSource.COINGECKO |
|||
}) |
|||
).toBe(DataSource.COINGECKO); |
|||
}); |
|||
|
|||
it('Keeps an absent data source', () => { |
|||
expect( |
|||
transformDataSourceInRequest({ |
|||
configurationService, |
|||
dataSource: undefined |
|||
}) |
|||
).toBeUndefined(); |
|||
}); |
|||
}); |
|||
|
|||
describe('transformDataSourceInResponse', () => { |
|||
it('Encodes the data source if the subscription is enabled', () => { |
|||
expect( |
|||
transformDataSourceInResponse({ |
|||
configurationService: createConfigurationService({ |
|||
isSubscriptionEnabled: true |
|||
}), |
|||
dataSource: DataSource.YAHOO |
|||
}) |
|||
).toBe(encodeDataSource(DataSource.YAHOO)); |
|||
}); |
|||
|
|||
it('Keeps the data sources GHOSTFOLIO and MANUAL if the subscription is enabled', () => { |
|||
const configurationService = createConfigurationService({ |
|||
isSubscriptionEnabled: true |
|||
}); |
|||
|
|||
for (const dataSource of [DataSource.GHOSTFOLIO, DataSource.MANUAL]) { |
|||
expect( |
|||
transformDataSourceInResponse({ configurationService, dataSource }) |
|||
).toBe(dataSource); |
|||
} |
|||
}); |
|||
|
|||
it('Keeps the data source if the subscription is not enabled', () => { |
|||
expect( |
|||
transformDataSourceInResponse({ |
|||
configurationService: createConfigurationService({ |
|||
isSubscriptionEnabled: false |
|||
}), |
|||
dataSource: DataSource.YAHOO |
|||
}) |
|||
).toBe(DataSource.YAHOO); |
|||
}); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,5 @@ |
|||
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
|||
|
|||
export interface DataSourceTableContext { |
|||
configurationService: ConfigurationService; |
|||
} |
|||
@ -1,6 +1,8 @@ |
|||
import { AssetClass, AssetSubClass } from '@prisma/client'; |
|||
|
|||
export interface HoldingsTableContext { |
|||
import { DataSourceTableContext } from './data-source-table-context.interface'; |
|||
|
|||
export interface HoldingsTableContext extends DataSourceTableContext { |
|||
assetClassTranslations: Record<AssetClass, string>; |
|||
assetSubClassTranslations: Record<AssetSubClass, string>; |
|||
} |
|||
|
|||
Loading…
Reference in new issue