Skip to content
Snippets Groups Projects
Commit 3988b7af authored by Kristof De Langhe's avatar Kristof De Langhe
Browse files

57557: Added JSDocs and Specs for EntityPageFieldsComponent operators

parent 1ba7d1e2
No related branches found
No related tags found
No related merge requests found
......@@ -17,6 +17,7 @@ import { RemoteData } from '../../../../core/data/remote-data';
import { Relationship } from '../../../../core/shared/entities/relationship.model';
import { Observable } from 'rxjs/Observable';
import { PageInfo } from '../../../../core/shared/page-info.model';
import { compareArraysUsing, compareArraysUsingIds } from './entity-page-fields.component';
/**
* Create a generic test for an entity-page-fields component using a mockItem and the type of component
......@@ -99,3 +100,210 @@ export function createRelationshipsObservable() {
})
])));
}
describe('EntityPageFieldsComponent', () => {
const arr1 = [
{
id: 1,
name: 'test'
},
{
id: 2,
name: 'another test'
},
{
id: 3,
name: 'one last test'
}
];
const arrWithWrongId = [
{
id: 1,
name: 'test'
},
{
id: 5, // Wrong id on purpose
name: 'another test'
},
{
id: 3,
name: 'one last test'
}
];
const arrWithWrongName = [
{
id: 1,
name: 'test'
},
{
id: 2,
name: 'wrong test' // Wrong name on purpose
},
{
id: 3,
name: 'one last test'
}
];
const arrWithDifferentOrder = [arr1[0], arr1[2], arr1[1]];
const arrWithOneMore = [...arr1, {
id: 4,
name: 'fourth test'
}];
const arrWithAddedProperties = [
{
id: 1,
name: 'test',
extra: 'extra property'
},
{
id: 2,
name: 'another test',
extra: 'extra property'
},
{
id: 3,
name: 'one last test',
extra: 'extra property'
}
];
const arrOfPrimitiveTypes = [1, 2, 3, 4];
const arrOfPrimitiveTypesWithOneWrong = [1, 5, 3, 4];
const arrOfPrimitiveTypesWithDifferentOrder = [1, 3, 2, 4];
const arrOfPrimitiveTypesWithOneMore = [1, 2, 3, 4, 5];
describe('when calling compareArraysUsing', () => {
describe('and comparing by id', () => {
const compare = compareArraysUsing<any>((o) => o.id);
it('should return true when comparing the same array', () => {
expect(compare(arr1, arr1)).toBeTruthy();
});
it('should return true regardless of the order', () => {
expect(compare(arr1, arrWithDifferentOrder)).toBeTruthy();
});
it('should return true regardless of other properties being different', () => {
expect(compare(arr1, arrWithWrongName)).toBeTruthy();
});
it('should return true regardless of extra properties', () => {
expect(compare(arr1, arrWithAddedProperties)).toBeTruthy();
});
it('should return false when the ids don\'t match', () => {
expect(compare(arr1, arrWithWrongId)).toBeFalsy();
});
it('should return false when the sizes don\'t match', () => {
expect(compare(arr1, arrWithOneMore)).toBeFalsy();
});
});
describe('and comparing by name', () => {
const compare = compareArraysUsing<any>((o) => o.name);
it('should return true when comparing the same array', () => {
expect(compare(arr1, arr1)).toBeTruthy();
});
it('should return true regardless of the order', () => {
expect(compare(arr1, arrWithDifferentOrder)).toBeTruthy();
});
it('should return true regardless of other properties being different', () => {
expect(compare(arr1, arrWithWrongId)).toBeTruthy();
});
it('should return true regardless of extra properties', () => {
expect(compare(arr1, arrWithAddedProperties)).toBeTruthy();
});
it('should return false when the names don\'t match', () => {
expect(compare(arr1, arrWithWrongName)).toBeFalsy();
});
it('should return false when the sizes don\'t match', () => {
expect(compare(arr1, arrWithOneMore)).toBeFalsy();
});
});
describe('and comparing by full objects', () => {
const compare = compareArraysUsing<any>((o) => o);
it('should return true when comparing the same array', () => {
expect(compare(arr1, arr1)).toBeTruthy();
});
it('should return true regardless of the order', () => {
expect(compare(arr1, arrWithDifferentOrder)).toBeTruthy();
});
it('should return false when extra properties are added', () => {
expect(compare(arr1, arrWithAddedProperties)).toBeFalsy();
});
it('should return false when the ids don\'t match', () => {
expect(compare(arr1, arrWithWrongId)).toBeFalsy();
});
it('should return false when the names don\'t match', () => {
expect(compare(arr1, arrWithWrongName)).toBeFalsy();
});
it('should return false when the sizes don\'t match', () => {
expect(compare(arr1, arrWithOneMore)).toBeFalsy();
});
});
describe('and comparing with primitive objects as source', () => {
const compare = compareArraysUsing<any>((o) => o);
it('should return true when comparing the same array', () => {
expect(compare(arrOfPrimitiveTypes, arrOfPrimitiveTypes)).toBeTruthy();
});
it('should return true regardless of the order', () => {
expect(compare(arrOfPrimitiveTypes, arrOfPrimitiveTypesWithDifferentOrder)).toBeTruthy();
});
it('should return false when at least one is wrong', () => {
expect(compare(arrOfPrimitiveTypes, arrOfPrimitiveTypesWithOneWrong)).toBeFalsy();
});
it('should return false when the sizes don\'t match', () => {
expect(compare(arrOfPrimitiveTypes, arrOfPrimitiveTypesWithOneMore)).toBeFalsy();
});
});
});
describe('when calling compareArraysUsingIds', () => {
const compare = compareArraysUsingIds();
it('should return true when comparing the same array', () => {
expect(compare(arr1 as any, arr1 as any)).toBeTruthy();
});
it('should return true regardless of the order', () => {
expect(compare(arr1 as any, arrWithDifferentOrder as any)).toBeTruthy();
});
it('should return true regardless of other properties being different', () => {
expect(compare(arr1 as any, arrWithWrongName as any)).toBeTruthy();
});
it('should return true regardless of extra properties', () => {
expect(compare(arr1 as any, arrWithAddedProperties as any)).toBeTruthy();
});
it('should return false when the ids don\'t match', () => {
expect(compare(arr1 as any, arrWithWrongId as any)).toBeFalsy();
});
it('should return false when the sizes don\'t match', () => {
expect(compare(arr1 as any, arrWithOneMore as any)).toBeFalsy();
});
});
});
......@@ -13,9 +13,12 @@ import { ITEM } from '../../../../shared/entities/switcher/entity-type-switcher.
/**
* Operator for comparing arrays using a mapping function
* The mapping function should turn the source array into an array of basic types, so that the array can
* be compared using these basic types.
* For example: "(o) => o.id" will compare the two arrays by comparing their content by id.
* @param mapFn Function for mapping the arrays
*/
const compareArraysUsing = <T>(mapFn: (t: T) => any) =>
export const compareArraysUsing = <T>(mapFn: (t: T) => any) =>
(a: T[], b: T[]): boolean => {
if (!Array.isArray(a) || ! Array.isArray(b)) {
return false
......@@ -32,7 +35,7 @@ const compareArraysUsing = <T>(mapFn: (t: T) => any) =>
/**
* Operator for comparing arrays using the object's ids
*/
const compareArraysUsingIds = <T extends { id: string }>() =>
export const compareArraysUsingIds = <T extends { id: string }>() =>
compareArraysUsing((t: T) => hasValue(t) ? t.id : undefined);
/**
......
import { CacheableObject } from '../../cache/object-cache.reducer';
import { ResourceType } from '../resource-type';
/**
* Describes a type of Entity
*/
export class EntityType implements CacheableObject {
/**
* The identifier of this EntityType
......
......@@ -4,6 +4,9 @@ import { RemoteData } from '../../data/remote-data';
import { ResourceType } from '../resource-type';
import { EntityType } from './entity-type.model';
/**
* Describes a type of Relationship between multiple possible Entities
*/
export class RelationshipType implements CacheableObject {
/**
* The link to the rest endpoint where this object can be found
......
......@@ -4,6 +4,9 @@ import { RemoteData } from '../../data/remote-data';
import { ResourceType } from '../resource-type';
import { RelationshipType } from './relationship-type.model';
/**
* Describes a Relationship between two Entities
*/
export class Relationship implements CacheableObject {
/**
* The link to the rest endpoint where this object can be found
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment