Skip to content

Commit

Permalink
more tests and minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Enngage committed Oct 3, 2017
1 parent e7c7284 commit 94f760f
Show file tree
Hide file tree
Showing 16 changed files with 275 additions and 90 deletions.
6 changes: 6 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export * from './models/common/cloud-error.class';
export * from './services/type-resolver.service';
export * from './interfaces/common/iquery.config';

// filters
export * from './models/common/filters';

// parameters
export * from './models/common/parameters';

// items
export * from './models/item/type-resolver.class';
export * from './models/item/content-item.class';
Expand Down
68 changes: 35 additions & 33 deletions lib/models/common/filters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { IQueryParameter } from '../../interfaces/common/iquery-parameter.interface';

export namespace Filters {


var defaultValue: string = '';

export class EqualsFilter implements IQueryParameter {
constructor(
public field: string,
Expand All @@ -17,9 +19,9 @@ export namespace Filters {
return this.field.trim();
}

public GetParamValue(): string | null{
public GetParamValue(): string | null {
if (!this.value) {
return null;
return defaultValue;
}

return this.value
Expand All @@ -40,9 +42,9 @@ export namespace Filters {
return `${this.field.trim()}[all]`;
}

public GetParamValue(): string | null{
if (!this.values) {
return null;
public GetParamValue(): string | null {
if (!this.values || !Array.isArray(this.values)) {
return defaultValue;
}

return this.values.map(m => m.trim()).join(',');
Expand All @@ -63,9 +65,9 @@ export namespace Filters {
return `${this.field.trim()}[any]`;
}

public GetParamValue(): string | null{
if (!this.values) {
return null;
public GetParamValue(): string | null {
if (!this.values || !Array.isArray(this.values)) {
return defaultValue;
}

return this.values.map(m => m.trim()).join(',');
Expand All @@ -86,9 +88,9 @@ export namespace Filters {
return `${this.field.trim()}[contains]`;
}

public GetParamValue(): string | null{
if (!this.values) {
return null;
public GetParamValue(): string | null {
if (!this.values || !Array.isArray(this.values)) {
return defaultValue;
}

return this.values.map(m => m.trim()).join(',');
Expand All @@ -110,9 +112,9 @@ export namespace Filters {
return `${this.field.trim()}[gt]`;
}

public GetParamValue(): string | null{
public GetParamValue(): string | null {
if (!this.value) {
return null;
return defaultValue;
}

return this.value;
Expand All @@ -124,7 +126,7 @@ export namespace Filters {
public field: string,
public value: string
) {
if (!this.field.trim) {
if (!this.field) {
throw Error(`Field specified in 'GreaterThanOrEqualFilter' is null or empty`);
}
}
Expand All @@ -134,9 +136,9 @@ export namespace Filters {
return `${this.field.trim()}[gte]`;
}

public GetParamValue(): string | null{
public GetParamValue(): string | null {
if (!this.value) {
return null;
return defaultValue;
}

return this.value;
Expand All @@ -157,15 +159,12 @@ export namespace Filters {
return `${this.field.trim()}[in]`;
}

public GetParamValue(): string | null{
if (!this.values) {
return null;
public GetParamValue(): string | null {
if (!this.values || !Array.isArray(this.values)) {
return defaultValue;
}

return this.values.map(m => {
if (!m) {
throw Error(`Elements in 'InFilter' cannot be null or empty`);
}
return m.trim()
}
).join(',');
Expand All @@ -186,9 +185,9 @@ export namespace Filters {
return `${this.field.trim()}[lt]`;
}

public GetParamValue(): string | null{
public GetParamValue(): string | null {
if (!this.value) {
return null;
return defaultValue;
}
return this.value;
}
Expand All @@ -209,9 +208,9 @@ export namespace Filters {
return `${this.field.trim()}[lte]`;
}

public GetParamValue(): string | null{
public GetParamValue(): string | null {
if (!this.value) {
return null;
return defaultValue;
}

return this.value;
Expand All @@ -237,16 +236,19 @@ export namespace Filters {
return `${this.field.trim()}[range]`;
}

public GetParamValue(): string | null{
if (!this.lowerValue) {
return null;
public GetParamValue(): string {
var lowerVal = defaultValue;
var higherVal = defaultValue;

if (this.lowerValue) {
lowerVal = this.lowerValue.toString();
}

if (!this.higherValue) {
return null;
if (this.higherValue) {
higherVal = this.higherValue.toString();
}

return `${this.lowerValue},${this.higherValue}`;
return `${lowerVal},${higherVal}`;
}
}
}
10 changes: 7 additions & 3 deletions lib/models/common/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { IQueryParameter } from '../../interfaces/common/iquery-parameter.interf
import { SortOrder } from './sort-order.enum';

export namespace Parameters {

var defaultValue: string = '';

export class ElementsParameter implements IQueryParameter {

Expand All @@ -13,16 +15,18 @@ export namespace Parameters {
constructor(
public elementCodenames: string[]
) {
if (!this.elementCodenames) {
throw Error(`'elementCodenames' are not set in 'ElementsParameter'`);
}

}

public GetParam(): string {
return 'elements';
}

public GetParamValue(): string {
if (!this.elementCodenames) {
return defaultValue;
}

return this.elementCodenames.map(m => {
if (!m) {
throw Error(`Codename of 'ElementsParameter' cannot be null or empty`);
Expand Down
8 changes: 0 additions & 8 deletions lib/query/element/base-element-query.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ export abstract class BaseElementQuery extends BaseQuery {
}

protected getElementQueryUrl(typeCodename: string, elementCodename: string): string {
if (!typeCodename) {
throw Error(`Type codename has to be set in order to fetch content type element`);
}

if (!elementCodename) {
throw Error(`Type element's codename has to be set in order to fetch content type element`);
}

var action = '/types/' + typeCodename + '/elements/' + elementCodename;

return this.getUrl(action, this._queryConfig, this.parameters);
Expand Down
3 changes: 0 additions & 3 deletions lib/query/item/multiple-item-query.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ export class MultipleItemQuery<TItem extends IContentItem> extends BaseItemQuery
* @param type Codename of type to get
*/
type(type: string): this {
if (!type) {
throw Error(`'type' cannot be null or empty`);
}
this._contentType = type;
return this;
}
Expand Down
11 changes: 9 additions & 2 deletions lib/services/field-map.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ export class FieldMapService {
* @param queryConfig Query configuration
*/
mapFields<TItem extends IContentItem>(item: IContentItem, modularContent: any, queryConfig: IItemQueryConfig): TItem{
if (item == null) {
if (!item) {
throw Error(`Cannot map fields because item is not defined`);
}

if (!item.elements) {
throw Error(`Cannot map elements of the item`);
}

if (!item.system) {
throw Error(`Cannot map system attributes of the item`);
}

var properties = Object.getOwnPropertyNames(item.elements);

// create typed item
Expand All @@ -64,7 +72,6 @@ export class FieldMapService {
if (!propertyName) {
propertyName = fieldName;
}

itemTyped[propertyName] = this.mapField(field, modularContent, itemTyped, queryConfig);
});

Expand Down
12 changes: 4 additions & 8 deletions lib/services/taxonomy-map.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export class TaxonomyMapService {
throw Error(`Cannot map taxonomy due to missing 'terms' property`);
}

if (!Array.isArray(taxonomyTerms)) {
throw Error(`Cannot map terms because no terms array was provided`);
}

var mappedSystemAttributes: TaxonomySystemAttributes = new TaxonomySystemAttributes(
taxonomySystem.id,
taxonomySystem.name,
Expand Down Expand Up @@ -56,14 +60,6 @@ export class TaxonomyMapService {
* @param termsArray Terms array to map
*/
private mapTaxonomyTerms(termsArray: ITaxonomyTerms[]): TaxonomyTerms[] {
if (!termsArray) {
throw Error(`Cannot map taxonomy terms due to invalid property data`);
}

if (!Array.isArray(termsArray)) {
throw Error(`Cannot map terms because no terms array was provided`);
}

if (termsArray.length == 0) {
return [];
}
Expand Down
54 changes: 27 additions & 27 deletions lib/services/type-map.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export class TypeMapService {

private mapType(type: IContentType): ContentType {
if (!type) {
throw Error(`Cannot map type: ` + type);
throw Error(`Cannot map type`);
}

if (!type.elements) {
throw Error(`Cannot map type elements`);
}

var typeSystem = new ContentTypeSystemAttributes(
Expand All @@ -27,39 +31,35 @@ export class TypeMapService {

var elements: Element[] = [];

if (type.elements) {
var elementNames = Object.getOwnPropertyNames(type.elements);
if (elementNames) {
elementNames.forEach(elementName => {
var typeElement = type.elements[elementName] as CloudTypeResponseInterfaces.IContentTypeElementCloudResponse;
var elementNames = Object.getOwnPropertyNames(type.elements);
elementNames.forEach(elementName => {
var typeElement = type.elements[elementName] as CloudTypeResponseInterfaces.IContentTypeElementCloudResponse;

if (!typeElement) {
throw Error(`Cannot find element '${elementName}' on type '${type}'`);
}

// use json property as a codename of the type element
var elementCodename = elementName;
if (!typeElement) {
throw Error(`Cannot find element '${elementName}' on type '${type}'`);
}

// extra properties for certain field types
var taxonomyGroup: string | undefined = typeElement.taxonomy_group;
var options: IElementOption[] = [];
// use json property as a codename of the type element
var elementCodename = elementName;

// some elements can contain options
var rawOptions = typeElement.options;
if (rawOptions){
if (!Array.isArray(rawOptions)){
throw Error(`Content type 'options' property has to be an array`);
}
// extra properties for certain field types
var taxonomyGroup: string | undefined = typeElement.taxonomy_group;
var options: IElementOption[] = [];

rawOptions.forEach(rawOption => {
options.push(new ElementOption(rawOption.name, rawOption.codename));
});
}
// some elements can contain options
var rawOptions = typeElement.options;
if (rawOptions) {
if (!Array.isArray(rawOptions)) {
throw Error(`Content type 'options' property has to be an array`);
}

elements.push(new Element(elementCodename, typeElement.type, typeElement.name, taxonomyGroup, options));
rawOptions.forEach(rawOption => {
options.push(new ElementOption(rawOption.name, rawOption.codename));
});
}
}

elements.push(new Element(elementCodename, typeElement.type, typeElement.name, taxonomyGroup, options));
});
return new ContentType(typeSystem, elements);
}

Expand Down
37 changes: 37 additions & 0 deletions test/isolated-tests/query/query-config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// url parser
import urlParser from 'url-parse';

// setup
import { setup, Context } from '../../setup';

import { QueryConfig } from '../../../lib/models/common/query.config';
import { ItemQueryConfig } from '../../../lib/models/item/item-query.config';

// tests
describe('Query configurations', () => {

var context = new Context();
setup(context);

it(`QueryConfig should set values properly`, () => {
var queryConfig = new QueryConfig({usePreviewMode: true, waitForLoadingNewContent: true});
expect(queryConfig.usePreviewMode).toEqual(true);
expect(queryConfig.waitForLoadingNewContent).toEqual(true);

var queryConfig = new QueryConfig({usePreviewMode: false, waitForLoadingNewContent: false});
expect(queryConfig.usePreviewMode).toEqual(false);
expect(queryConfig.waitForLoadingNewContent).toEqual(false);
});

it(`ItemQuerConfig should set values properly`, () => {
var queryConfig = new ItemQueryConfig({usePreviewMode: true, waitForLoadingNewContent: true});
expect(queryConfig.usePreviewMode).toEqual(true);
expect(queryConfig.waitForLoadingNewContent).toEqual(true);

var queryConfig = new ItemQueryConfig({usePreviewMode: false, waitForLoadingNewContent: false});
expect(queryConfig.usePreviewMode).toEqual(false);
expect(queryConfig.waitForLoadingNewContent).toEqual(false);
});

});

Loading

0 comments on commit 94f760f

Please sign in to comment.