mirror of https://github.com/Squidex/squidex.git
14 changed files with 531 additions and 32 deletions
@ -0,0 +1,76 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import { PanelService, PanelServiceFactory } from './../'; |
||||
|
|
||||
|
interface Styling { element: any; property: string; value: string; } |
||||
|
|
||||
|
describe('PanelService', () => { |
||||
|
it('should instantiate from factory', () => { |
||||
|
const panelService = PanelServiceFactory(); |
||||
|
|
||||
|
expect(panelService).toBeDefined(); |
||||
|
}); |
||||
|
|
||||
|
it('should instantiate', () => { |
||||
|
const panelService = new PanelService(); |
||||
|
|
||||
|
expect(panelService).toBeDefined(); |
||||
|
}); |
||||
|
|
||||
|
it('should update elements with renderer service', () => { |
||||
|
let styles: Styling[] = []; |
||||
|
|
||||
|
const renderer = { |
||||
|
setElementStyle: (element: any, property: string, value: string) => { |
||||
|
styles.push({element, property, value}); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
const panelService = new PanelService(); |
||||
|
|
||||
|
const element1 = { |
||||
|
getBoundingClientRect: () => { |
||||
|
return { width: 100 }; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
const element2 = { |
||||
|
getBoundingClientRect: () => { |
||||
|
return { width: 200 }; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
const element3 = { |
||||
|
getBoundingClientRect: () => { |
||||
|
return { width: 300 }; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
let numPublished = 0; |
||||
|
panelService.changed.subscribe(() => { |
||||
|
numPublished++; |
||||
|
}); |
||||
|
|
||||
|
panelService.push(element1, <any>renderer); |
||||
|
panelService.push(element2, <any>renderer); |
||||
|
panelService.push(element3, <any>renderer); |
||||
|
|
||||
|
styles = []; |
||||
|
|
||||
|
panelService.pop(element3, <any>renderer); |
||||
|
|
||||
|
expect(styles).toEqual([ |
||||
|
{ element: element1, property: 'left', value: '0px' }, |
||||
|
{ element: element1, property: 'z-index', value: '20' }, |
||||
|
{ element: element2, property: 'left', value: '100px' }, |
||||
|
{ element: element2, property: 'z-index', value: '10' } |
||||
|
]); |
||||
|
|
||||
|
expect(numPublished).toBe(4); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,100 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import { IMock, Mock } from 'typemoq'; |
||||
|
import { Observable } from 'rxjs'; |
||||
|
|
||||
|
import { SchemasService } from 'shared'; |
||||
|
|
||||
|
import { ResolvePublishedSchemaGuard } from './resolve-published-schema.guard'; |
||||
|
import { RouterMockup } from './router-mockup'; |
||||
|
|
||||
|
describe('ResolveSchemaGuard', () => { |
||||
|
const route = { |
||||
|
params: { |
||||
|
appName: 'my-app' |
||||
|
}, |
||||
|
parent: { |
||||
|
params: { |
||||
|
schemaName: 'my-schema' |
||||
|
} |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
let appsStore: IMock<SchemasService>; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
appsStore = Mock.ofType(SchemasService); |
||||
|
}); |
||||
|
|
||||
|
it('should navigate to 404 page if schema is not found', (done) => { |
||||
|
appsStore.setup(x => x.getSchema('my-app', 'my-schema')) |
||||
|
.returns(() => Observable.of(null)); |
||||
|
const router = new RouterMockup(); |
||||
|
|
||||
|
const guard = new ResolvePublishedSchemaGuard(appsStore.object, <any>router); |
||||
|
|
||||
|
guard.resolve(<any>route, null) |
||||
|
.then(result => { |
||||
|
expect(result).toBeFalsy(); |
||||
|
expect(router.lastNavigation).toEqual(['/404']); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
it('should navigate to 404 page if schema loading fails', (done) => { |
||||
|
appsStore.setup(x => x.getSchema('my-app', 'my-schema')) |
||||
|
.returns(() => Observable.throw(null)); |
||||
|
const router = new RouterMockup(); |
||||
|
|
||||
|
const guard = new ResolvePublishedSchemaGuard(appsStore.object, <any>router); |
||||
|
|
||||
|
guard.resolve(<any>route, null) |
||||
|
.then(result => { |
||||
|
expect(result).toBeFalsy(); |
||||
|
expect(router.lastNavigation).toEqual(['/404']); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
it('should navigate to 404 page if schema not published', (done) => { |
||||
|
const schema = { isPublished: false }; |
||||
|
|
||||
|
appsStore.setup(x => x.getSchema('my-app', 'my-schema')) |
||||
|
.returns(() => Observable.of(schema)); |
||||
|
const router = new RouterMockup(); |
||||
|
|
||||
|
const guard = new ResolvePublishedSchemaGuard(appsStore.object, <any>router); |
||||
|
|
||||
|
guard.resolve(<any>route, null) |
||||
|
.then(result => { |
||||
|
expect(result).toBeFalsy(); |
||||
|
expect(router.lastNavigation).toEqual(['/404']); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
it('should return schema if loading succeeded', (done) => { |
||||
|
const schema = { isPublished: true }; |
||||
|
|
||||
|
appsStore.setup(x => x.getSchema('my-app', 'my-schema')) |
||||
|
.returns(() => Observable.of(schema)); |
||||
|
const router = new RouterMockup(); |
||||
|
|
||||
|
const guard = new ResolvePublishedSchemaGuard(appsStore.object, <any>router); |
||||
|
|
||||
|
guard.resolve(<any>route, null) |
||||
|
.then(result => { |
||||
|
expect(result).toBe(schema); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,82 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import { IMock, Mock } from 'typemoq'; |
||||
|
import { Observable } from 'rxjs'; |
||||
|
|
||||
|
import { SchemasService } from 'shared'; |
||||
|
|
||||
|
import { ResolveSchemaGuard } from './resolve-schema.guard'; |
||||
|
import { RouterMockup } from './router-mockup'; |
||||
|
|
||||
|
describe('ResolveSchemaGuard', () => { |
||||
|
const route = { |
||||
|
params: { |
||||
|
appName: 'my-app' |
||||
|
}, |
||||
|
parent: { |
||||
|
params: { |
||||
|
schemaName: 'my-schema' |
||||
|
} |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
let appsStore: IMock<SchemasService>; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
appsStore = Mock.ofType(SchemasService); |
||||
|
}); |
||||
|
|
||||
|
it('should navigate to 404 page if schema is not found', (done) => { |
||||
|
appsStore.setup(x => x.getSchema('my-app', 'my-schema')) |
||||
|
.returns(() => Observable.of(null)); |
||||
|
const router = new RouterMockup(); |
||||
|
|
||||
|
const guard = new ResolveSchemaGuard(appsStore.object, <any>router); |
||||
|
|
||||
|
guard.resolve(<any>route, null) |
||||
|
.then(result => { |
||||
|
expect(result).toBeFalsy(); |
||||
|
expect(router.lastNavigation).toEqual(['/404']); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
it('should navigate to 404 page if schema loading fails', (done) => { |
||||
|
appsStore.setup(x => x.getSchema('my-app', 'my-schema')) |
||||
|
.returns(() => Observable.throw(null)); |
||||
|
const router = new RouterMockup(); |
||||
|
|
||||
|
const guard = new ResolveSchemaGuard(appsStore.object, <any>router); |
||||
|
|
||||
|
guard.resolve(<any>route, null) |
||||
|
.then(result => { |
||||
|
expect(result).toBeFalsy(); |
||||
|
expect(router.lastNavigation).toEqual(['/404']); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
it('should return schema if loading succeeded', (done) => { |
||||
|
const schema = {}; |
||||
|
|
||||
|
appsStore.setup(x => x.getSchema('my-app', 'my-schema')) |
||||
|
.returns(() => Observable.of(schema)); |
||||
|
const router = new RouterMockup(); |
||||
|
|
||||
|
const guard = new ResolveSchemaGuard(appsStore.object, <any>router); |
||||
|
|
||||
|
guard.resolve(<any>route, null) |
||||
|
.then(result => { |
||||
|
expect(result).toBe(schema); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,158 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import { Response, ResponseOptions } from '@angular/http'; |
||||
|
import { Observable } from 'rxjs'; |
||||
|
import { It, IMock, Mock, Times } from 'typemoq'; |
||||
|
|
||||
|
import { |
||||
|
ApiUrlConfig, |
||||
|
AuthService, |
||||
|
EntityCreatedDto, |
||||
|
ContentDto, |
||||
|
ContentsService, |
||||
|
DateTime |
||||
|
} from './../'; |
||||
|
|
||||
|
describe('ContentsService', () => { |
||||
|
let authService: IMock<AuthService>; |
||||
|
let contentsService: ContentsService; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
authService = Mock.ofType(AuthService); |
||||
|
contentsService = new ContentsService(authService.object, new ApiUrlConfig('http://service/p/')); |
||||
|
}); |
||||
|
|
||||
|
it('should make get request to get content1', () => { |
||||
|
authService.setup(x => x.authGet('http://service/p/api/content/my-app/my-schema?take=17&skip=13&query=my-query&nonPublished=true')) |
||||
|
.returns(() => Observable.of( |
||||
|
new Response( |
||||
|
new ResponseOptions({ |
||||
|
body: [{ |
||||
|
id: 'id1', |
||||
|
isPublished: true, |
||||
|
created: '2016-12-12T10:10', |
||||
|
createdBy: 'Created1', |
||||
|
lastModified: '2017-12-12T10:10', |
||||
|
lastModifiedBy: 'LastModifiedBy1', |
||||
|
data: {} |
||||
|
}, { |
||||
|
id: 'id2', |
||||
|
isPublished: true, |
||||
|
created: '2016-10-12T10:10', |
||||
|
createdBy: 'Created2', |
||||
|
lastModified: '2017-10-12T10:10', |
||||
|
lastModifiedBy: 'LastModifiedBy2', |
||||
|
data: {} |
||||
|
}] |
||||
|
}) |
||||
|
) |
||||
|
)) |
||||
|
.verifiable(Times.once()); |
||||
|
|
||||
|
let contents: ContentDto[] = null; |
||||
|
|
||||
|
contentsService.getContents('my-app', 'my-schema', 17, 13, 'my-query').subscribe(result => { |
||||
|
contents = result; |
||||
|
}).unsubscribe(); |
||||
|
|
||||
|
expect(contents).toEqual([ |
||||
|
new ContentDto('id1', true, 'Created1', 'LastModifiedBy1', DateTime.parseISO_UTC('2016-12-12T10:10'), DateTime.parseISO_UTC('2017-12-12T10:10'), {}), |
||||
|
new ContentDto('id2', true, 'Created2', 'LastModifiedBy2', DateTime.parseISO_UTC('2016-10-12T10:10'), DateTime.parseISO_UTC('2017-10-12T10:10'), {}) |
||||
|
]); |
||||
|
|
||||
|
authService.verifyAll(); |
||||
|
}); |
||||
|
|
||||
|
it('should make get request to get content', () => { |
||||
|
authService.setup(x => x.authGet('http://service/p/api/content/my-app/my-schema/content1')) |
||||
|
.returns(() => Observable.of( |
||||
|
new Response( |
||||
|
new ResponseOptions({ |
||||
|
body: { |
||||
|
id: 'id1', |
||||
|
isPublished: true, |
||||
|
created: '2016-12-12T10:10', |
||||
|
createdBy: 'Created1', |
||||
|
lastModified: '2017-12-12T10:10', |
||||
|
lastModifiedBy: 'LastModifiedBy1', |
||||
|
data: {} |
||||
|
} |
||||
|
}) |
||||
|
) |
||||
|
)) |
||||
|
.verifiable(Times.once()); |
||||
|
|
||||
|
let content: ContentDto = null; |
||||
|
|
||||
|
contentsService.getContent('my-app', 'my-schema', 'content1').subscribe(result => { |
||||
|
content = result; |
||||
|
}).unsubscribe(); |
||||
|
|
||||
|
expect(content).toEqual( |
||||
|
new ContentDto('id1', true, 'Created1', 'LastModifiedBy1', DateTime.parseISO_UTC('2016-12-12T10:10'), DateTime.parseISO_UTC('2017-12-12T10:10'), {})); |
||||
|
|
||||
|
authService.verifyAll(); |
||||
|
}); |
||||
|
|
||||
|
it('should make post request to create content', () => { |
||||
|
const dto = {}; |
||||
|
|
||||
|
authService.setup(x => x.authPost('http://service/p/api/content/my-app/my-schema', It.isValue(dto))) |
||||
|
.returns(() => Observable.of( |
||||
|
new Response( |
||||
|
new ResponseOptions({ |
||||
|
body: { |
||||
|
id: 'content1' |
||||
|
} |
||||
|
}) |
||||
|
) |
||||
|
)) |
||||
|
.verifiable(Times.once()); |
||||
|
|
||||
|
let created: EntityCreatedDto = null; |
||||
|
|
||||
|
contentsService.postContent('my-app', 'my-schema', dto).subscribe(result => { |
||||
|
created = result; |
||||
|
}); |
||||
|
|
||||
|
expect(created).toEqual( |
||||
|
new EntityCreatedDto('content1')); |
||||
|
|
||||
|
authService.verifyAll(); |
||||
|
}); |
||||
|
|
||||
|
it('should make put request to update content', () => { |
||||
|
const dto = {}; |
||||
|
|
||||
|
authService.setup(x => x.authPut('http://service/p/api/content/my-app/my-schema/content1', It.isValue(dto))) |
||||
|
.returns(() => Observable.of( |
||||
|
new Response( |
||||
|
new ResponseOptions() |
||||
|
) |
||||
|
)) |
||||
|
.verifiable(Times.once()); |
||||
|
|
||||
|
contentsService.putContent('my-app', 'my-schema', 'content1', dto); |
||||
|
|
||||
|
authService.verifyAll(); |
||||
|
}); |
||||
|
|
||||
|
it('should make delete request to delete content', () => { |
||||
|
authService.setup(x => x.authDelete('http://service/p/api/content/my-app/my-schema/content1')) |
||||
|
.returns(() => Observable.of( |
||||
|
new Response( |
||||
|
new ResponseOptions() |
||||
|
) |
||||
|
)) |
||||
|
.verifiable(Times.once()); |
||||
|
|
||||
|
contentsService.deleteContent('my-app', 'my-schema', 'content1'); |
||||
|
|
||||
|
authService.verifyAll(); |
||||
|
}); |
||||
|
}); |
||||
Loading…
Reference in new issue