Skip to content

Commit

Permalink
Adds strongly typed support for 'options' & 'taxonomy_group' type ele…
Browse files Browse the repository at this point in the history
…ments (#16)
  • Loading branch information
Enngage committed Aug 28, 2017
1 parent fa4eea0 commit c75cfa0
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 8 deletions.
10 changes: 9 additions & 1 deletion lib/interfaces/type/cloud-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { IContentType } from './icontent-type.interface';
import { IContentTypeSystemAttributes } from './icontent-type-system-attributes.interface';
import { IContentTypeElement } from './icontent-type-element.interface';
import { IPagination } from '../common/ipagination.interface';
import { IContentTypeOption } from './icontent-type-option.interface';

export namespace CloudTypeResponseInterfaces {

export interface ICloudMultipleTypeResponse {
types: IContentType[];
pagination: IPagination;
Expand All @@ -14,4 +15,11 @@ export namespace CloudTypeResponseInterfaces {
system: IContentTypeSystemAttributes;
elements: IContentTypeElement[];
}

export interface IContentTypeElementCloudResponse {
type: string;
name: string;
taxonomy_group?: string;
options?: IContentTypeOption[];
}
}
7 changes: 6 additions & 1 deletion lib/interfaces/type/icontent-type-element.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export interface IContentTypeElement{
import { IContentTypeOption } from './icontent-type-option.interface';

export interface IContentTypeElement {
codename: string;
type: string;
name: string;
taxonomyGroup?: string;
options?: IContentTypeOption[];
}
4 changes: 4 additions & 0 deletions lib/interfaces/type/icontent-type-option.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IContentTypeOption{
name: string;
codename: string;
}
2 changes: 1 addition & 1 deletion lib/models/common/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export namespace Parameters {
throw Error(`Codename of 'ElementsParameter' cannot be null or empty`);
}
return m.trim()
}).join();
}).join(',');
}
}

Expand Down
10 changes: 7 additions & 3 deletions lib/models/type/content-type-element.class.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { IContentTypeElement } from '../../interfaces/type/icontent-type-element.interface';
import { IContentTypeOption } from '../../interfaces/type/icontent-type-option.interface';

export class ContentTypeElement implements IContentTypeElement {
export class ContentTypeElement implements IContentTypeElement{
constructor(
public codename: string,
public type: string,
public name: string
){}
public name: string,
public taxonomyGroup?: string,
public options?: IContentTypeOption[]
) { }
}
8 changes: 8 additions & 0 deletions lib/models/type/content-type-option.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IContentTypeOption } from '../../interfaces/type/icontent-type-option.interface';

export class ContentTypeOption implements IContentTypeOption{
constructor(
public name: string,
public codename: string
){}
}
26 changes: 24 additions & 2 deletions lib/services/type-map.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { IContentType } from '../interfaces/type/icontent-type.interface';
import { ContentType } from '../models/type/content-type.class';
import { ContentTypeSystemAttributes } from '../models/type/content-type-system-attributes.class';
import { ContentTypeElement } from '../models/type/content-type-element.class';
import { ContentTypeOption } from '../models/type/content-type-option.class';
import { IContentTypeElement } from '../interfaces/type/icontent-type-element.interface';
import { IContentTypeOption } from '../interfaces/type/icontent-type-option.interface';
import { DeliveryClientConfig } from '../config/delivery-client.config';

export class TypeMapService {
Expand Down Expand Up @@ -31,12 +33,32 @@ export class TypeMapService {
var elementNames = Object.getOwnPropertyNames(type.elements);
if (elementNames) {
elementNames.forEach(elementName => {
var typeElement = type.elements[elementName] as IContentTypeElement;
var typeElement = type.elements[elementName] as CloudTypeResponseInterfaces.IContentTypeElementCloudResponse;

if (!typeElement) {
throw Error(`Cannot find element '${elementName}' on type '${type}'`);
}
elements.push(new ContentTypeElement(typeElement.type, typeElement.name));

// use json property as a codename of the type element
var elementCodename = elementName;

// extra properties for certain field types
var taxonomyGroup: string | undefined = typeElement.taxonomy_group;
var options: IContentTypeOption[] = [];

// 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`);
}

rawOptions.forEach(rawOption => {
options.push(new ContentTypeOption(rawOption.name, rawOption.codename));
});
}

elements.push(new ContentTypeElement(elementCodename, typeElement.type, typeElement.name, taxonomyGroup, options));
});
}
}
Expand Down
64 changes: 64 additions & 0 deletions test/live-tests/types/live-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// setup
import { setup, Context, Actor, Movie } from '../../setup';

// models
import { ItemResponses, FieldModels, TypeResponses } from '../../../lib';

// tests
describe('Live type', () => {

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

var codename: string = 'movie';
var response: TypeResponses.DeliveryTypeResponse;

var multipleChoiceElement: string = 'category';
var taxonomyElement: string = 'releasecategory';

beforeAll((done) => {
context.deliveryClient.type(codename)
.get()
.subscribe(r => {
response = r as TypeResponses.DeliveryTypeResponse;
done();
});
});

it(`type should be defined`, () => {
console.log(response);
expect(response).toBeDefined();
});

it(`elements should be defined`, () => {
expect(response.type.elements).toBeDefined();
});

it(`system properties should be defined`, () => {
expect(response.type.system).toBeDefined();
});

it(`proper type should be returned based on test config`, () => {
expect(response.type.system.codename).toEqual(codename);
});

it(`Verifies taxonomy element - '${taxonomyElement}'`, () => {
var releaseCategoryElement = response.type.elements.find(m => m.codename === taxonomyElement);

expect(releaseCategoryElement).toBeDefined();
expect(releaseCategoryElement.taxonomyGroup).toBeDefined();
expect(releaseCategoryElement.taxonomyGroup).toEqual('releasecategory');
});

it(`Verifies multiple_choice element - '${multipleChoiceElement}'`, () => {
var categoryElement = response.type.elements.find(m => m.codename === multipleChoiceElement);

expect(categoryElement).toBeDefined();
expect(categoryElement.options).toBeDefined();
expect(categoryElement.options.length).toBeGreaterThan(0);
expect(categoryElement.options[0].codename).toBeDefined();
expect(categoryElement.options[0].name).toBeDefined();
});

});

44 changes: 44 additions & 0 deletions test/live-tests/types/live-types.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// setup
import { setup, Context, Actor, Movie } from '../../setup';

// models
import { ItemResponses, FieldModels, TypeResponses } from '../../../lib';

// tests
describe('Live types', () => {

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

var response: TypeResponses.DeliveryTypeListingResponse;

beforeAll((done) => {
context.deliveryClient.types()
.get()
.subscribe(r => {
response = r as TypeResponses.DeliveryTypeListingResponse;
done();
});
});

it(`types should be defined`, () => {
expect(response).toBeDefined();
});

it(`there should be at least 1 type`, () => {
expect(response.types.length).toBeGreaterThan(0);
});

it(`elements should be defined`, () => {
expect(response.types[0].elements).toBeDefined();
});

it(`system properties should be defined`, () => {
expect(response.types[0].system).toBeDefined();
});

it(`pagination should be defined`, () => {
expect(response.pagination).toBeDefined();
});
});

0 comments on commit c75cfa0

Please sign in to comment.