Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Zoltan Magyari <[email protected]>
  • Loading branch information
Zoltan Magyari committed Sep 25, 2024
1 parent c8481cc commit 5c891ff
Show file tree
Hide file tree
Showing 23 changed files with 5,074 additions and 71 deletions.
19 changes: 8 additions & 11 deletions src/components/resources/helpers/resourceFilterHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { Resource } from '../../../types/resources.model';
import { ResourceFilterState } from './resourceFilterReducer';
import { getPropertyValue } from './resourcesHelper';

export const TYPE_ASSETS = 'typeAssets';
export const FORMAT_ASSETS = 'formatAssets';
export const VENDOR_ASSETS = 'vendorAssets';
export type AssetTypes = 'typeAssets' | 'formatAssets' | 'vendorAssets';

/**
Expand Down Expand Up @@ -82,7 +79,7 @@ export const createFormatAssets = (
* @param resources the list of resources to look for the formats in.
* @return the set of available labels.
*/
const getAllFormats = (resources: Resource[]) =>
export const getAllFormats = (resources: Resource[]) =>
new Set(
resources
.map(resource => resource.format)
Expand Down Expand Up @@ -131,15 +128,15 @@ export const getResourceVendors = (resources: Resource[]) =>
* Represents a list of assets that are selected. It has an ALL option which means that everything is selected. No
* list should be provided.
*/
export type SelectedAssets = 'ALL' | string[];
export type SelectedAssets = 'NOTHING' | string[];

/**
* Returns the selected assets from the list. It has an ALL option which means that everything is selected. No list
* should be provided.
*
* An asset is considered selected when it is not disabled and its value is true.
*
* If no asset is selected it is considered that everything is selected and in that case it is returned 'ALL'.
* If no asset is selected it is considered that everything is selected and in that case it is returned 'NOTHING'.
*
* @param assets list to be filtered for the selected one.
*/
Expand All @@ -150,9 +147,9 @@ export const getSelectedAssets = (assets: Asset[]): SelectedAssets => {

// If type filter is selected it is considered that all enabled ones are selected
if (!selectedAssets.length) {
return 'ALL'
return 'NOTHING'
}
return selectedAssets;
return selectedAssets
}

/**
Expand All @@ -176,7 +173,7 @@ export const calculateResourceFiltersAssetState = (
const resourcesWithTypeFilterApplied = resources
.filter((resource) => {
const selectedAssets = getSelectedAssets(filters.typeAssets)
return selectedAssets === 'ALL' || selectedAssets
return selectedAssets === 'NOTHING' || selectedAssets
.some(type => resource.labels.includes(type))
});

Expand All @@ -186,7 +183,7 @@ export const calculateResourceFiltersAssetState = (
const resourcesWithFormatFilterApplied = resourcesWithTypeFilterApplied
.filter(resource => {
const selectedAssets = getSelectedAssets(filters.formatAssets)
return selectedAssets === 'ALL' || selectedAssets
return selectedAssets === 'NOTHING' || selectedAssets
.some(format => resource.format === format)
});

Expand All @@ -196,7 +193,7 @@ export const calculateResourceFiltersAssetState = (
const resourcesWithVendorFilterApplied = resourcesWithFormatFilterApplied
.filter(resource => {
const selectedAssets = getSelectedAssets(filters.vendorAssets)
return selectedAssets === 'ALL' || selectedAssets
return selectedAssets === 'NOTHING' || selectedAssets
.some(vendor => resource.vendor === vendor)
});

Expand Down
6 changes: 3 additions & 3 deletions src/components/resources/helpers/resourceFilterReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { Asset, calculateResourceFiltersAssetState } from './resourceFilterHelpe
////////////////////////////////////////////////////////////////////////////////
// Action types
////////////////////////////////////////////////////////////////////////////////
const SET_RESOURCE_FILTER_ASSETS = 'SET_RESOURCE_FILTER_ASSETS';
const SET_SEARCH_TEXT = 'SET_SEARCH_TEXT';
const UPDATE_FILTER_ASSET = 'UPDATE_FILTER_ASSET';
export const SET_RESOURCE_FILTER_ASSETS = 'SET_RESOURCE_FILTER_ASSETS';
export const SET_SEARCH_TEXT = 'SET_SEARCH_TEXT';
export const UPDATE_FILTER_ASSET = 'UPDATE_FILTER_ASSET';

////////////////////////////////////////////////////////////////////////////////
// Type definitions
Expand Down
6 changes: 3 additions & 3 deletions src/components/resources/helpers/resourcesHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export const getPropertyValue = (objectEntry: [string, any]) => String(objectEnt
* This has relevance when displaying the resource list. We do not want to show the above-mentioned labels.
*
* @param resources the original resource list.
* @param reosurceTypes the types to be considered as resource types.
* @param resourceTypes the types to be considered as resource types.
* @return returns copy of the original resources without the labels.
*/
export const removeNonResourceTypeLabels = (resources: Resource[], reosurceTypes: string[]) => {
export const removeNonResourceTypeLabels = (resources: Resource[], resourceTypes: string[]) => {
return resources.map(resource => ({
...resource,
labels: resource.labels.filter(label => reosurceTypes.includes(label))
labels: resource.labels.filter(label => resourceTypes.includes(label))
}));
}

24 changes: 12 additions & 12 deletions src/components/resources/helpers/resourcesReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ import { Resource } from '../../../types/resources.model';
////////////////////////////////////////////////////////////////////////////////
// Action types
////////////////////////////////////////////////////////////////////////////////
const SET_ALL_RESOURCES = 'SET_ALL_RESOURCES';
const SET_ALL_RESOURCES_LOADING_ERROR = 'SET_ALL_RESOURCES_LOADING_ERROR';
export const SET_ALL_RESOURCES = 'SET_ALL_RESOURCES';
export const SET_ALL_RESOURCES_LOADING_ERROR = 'SET_ALL_RESOURCES_LOADING_ERROR';

////////////////////////////////////////////////////////////////////////////////
// Type definitions
////////////////////////////////////////////////////////////////////////////////
type AllResources = {
export type AllResources = {
resources: Resource[]
};

type AllResourceError = {
error: string;
error: string | Error;
}

type IsLoading = { isLoading: boolean; }
export type AllResourcesState = (AllResources & { hasError: false } | AllResourceError & { hasError: true }) & IsLoading

type AllResourcesAction =
{ type: 'SET_ALL_RESOURCES', payload: AllResources } |
{ type: 'SET_ALL_RESOURCES_LOADING_ERROR', payload: AllResourceError }
{ type: 'SET_ALL_RESOURCES', payload: Resource[] } |
{ type: 'SET_ALL_RESOURCES_LOADING_ERROR', payload: string | Error }

////////////////////////////////////////////////////////////////////////////////
// Initial state
Expand All @@ -38,11 +38,11 @@ export const initialResourceState: AllResourcesState = {
////////////////////////////////////////////////////////////////////////////////
// Reducer
////////////////////////////////////////////////////////////////////////////////
export const resourcesReducer = (state: AllResourcesState, action: AnyAction) => {
export const resourcesReducer = (state: AllResourcesState, action: AnyAction): AllResourcesState => {
switch (action.type) {

case SET_ALL_RESOURCES:
return { ...action.payload, isLoading: false, hasError: false }
return { resources: action.payload, isLoading: false, hasError: false }

case SET_ALL_RESOURCES_LOADING_ERROR:
return { error: action.payload, isLoading: false, hasError: true }
Expand All @@ -55,12 +55,12 @@ export const resourcesReducer = (state: AllResourcesState, action: AnyAction) =>
////////////////////////////////////////////////////////////////////////////////
// Actions
////////////////////////////////////////////////////////////////////////////////
export const resourcesLoadedAction = (payload: AllResources): AllResourcesAction => ({
export const resourcesLoadedAction = (resources: Resource[]): AllResourcesAction => ({
type: SET_ALL_RESOURCES,
payload
payload: resources
})

export const resourcesLoadingErrorAction = (payload: AllResourceError): AllResourcesAction => ({
export const resourcesLoadingErrorAction = (error: string | Error): AllResourcesAction => ({
type: SET_ALL_RESOURCES_LOADING_ERROR,
payload
payload: error
})
1 change: 1 addition & 0 deletions src/components/resources/hooks/useResourceFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const useResourceFilter = (ontologies: Ontology[], resources: Resource[])
dispatch(setResourceFilterAssetsAction(ontologies, resources));
}, [ontologies, resources]);

// Remove the "searchText" property from the "filters"
const { searchText, ...resourceFilterAssetSate } = filters;
return {
...resourceFilterAssetSate,
Expand Down
20 changes: 11 additions & 9 deletions src/components/resources/hooks/useResources.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Reducer, useEffect, useMemo, useReducer } from 'react';
import { AnyAction } from 'redux';
import { useEffect, useMemo, useReducer } from 'react';

import { useSchemas } from '../../../hooks/useSchemas';
import { getResourceTypes } from '../../../services/ontologyService.utils';
import { loadResources } from '../helpers/resourceDataFlow';
import { removeNonResourceTypeLabels } from '../helpers/resourcesHelper';
import {
AllResourcesState,
initialResourceState,
resourcesLoadedAction,
resourcesLoadingErrorAction,
Expand All @@ -23,7 +21,7 @@ export const useResources = () => {
!schemas.isLoading && !schemas.hasError ? schemas.ontologies : [],
[schemas.isLoading]);

const [state, dispatch] = useReducer<Reducer<AllResourcesState, AnyAction>>(resourcesReducer, initialResourceState);
const [state, dispatch] = useReducer(resourcesReducer, initialResourceState);
const { isLoading, resources } = useMemo(() => ({
isLoading: state.isLoading,
resources: !state.isLoading && !state.hasError ? state.resources : []
Expand All @@ -39,11 +37,15 @@ export const useResources = () => {
} = useResourceFilter(ontologies, resources);

useEffect(() => {
if (!schemas.isLoading && !schemas.hasError) {
const resourceTypes = Array.from(getResourceTypes(schemas.ontologies));
loadResources(resourceTypes)
.then(resources => dispatch(resourcesLoadedAction({ resources })))
.catch(error => dispatch(resourcesLoadingErrorAction(error)))
if (!schemas.isLoading) {
if (!schemas.hasError) {
const resourceTypes = Array.from(getResourceTypes(schemas.ontologies));
loadResources(resourceTypes)
.then(resources => dispatch(resourcesLoadedAction(resources)))
.catch(error => dispatch(resourcesLoadingErrorAction(error)))
} else {
dispatch(resourcesLoadingErrorAction(schemas.error))
}
}
}, [schemas.isLoading]);

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/schemasReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type AllSchemas = {
}

type AllSchemasError = {
error: string;
error: string | Error;
}

// TODO: Extend the AnyAction type
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { loadSchemas } from '../helpers/schemasDataFlow';
import { AllSchemasState, schemasReducer } from '../helpers/schemasReducer';
import { useThunkReducer } from '../reducers/useThunkReducer';

export const initialState: AllSchemasState = {
export const initialSchemaState: AllSchemasState = {
isLoading: true,
hasError: false,
shapes: [],
ontologies: []
}

export const useSchemas = (): AllSchemasState => {
const [schemas, dispatch] = useThunkReducer(schemasReducer, initialState);
const [schemas, dispatch] = useThunkReducer(schemasReducer, initialSchemaState);

useEffect(
() => dispatch(loadSchemas),
Expand Down
7 changes: 6 additions & 1 deletion src/services/schemaApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const fetchAllSchemas = async (): Promise<ShapesAndOntologiesInput> => {
return response.data;
} catch (error) {
console.error(error);
return { ontologies: [], shapes: [], vocabularies: [] }
return { ontologies: [], shapes: [], vocabularies: [] };
}
};

Expand All @@ -25,9 +25,14 @@ export const getSchemaById = async (id: string) => {
return response.data;
} catch (error) {
console.error('Error:', error);
throw error;
}
};

/**
*
* @param id
*/
export const getConvertedFile = async (id: string) => {
try {
const textContent = await getSchemaById(id);
Expand Down
Loading

0 comments on commit 5c891ff

Please sign in to comment.