From 42bd3011030cbf060246051493db91a921a7874c Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 17 Feb 2017 19:44:03 +0100 Subject: [PATCH] Tests fixed --- .../framework/utils/immutable-array.spec.ts | 15 +++ .../shared/services/contents.service.spec.ts | 4 +- .../services/event-consumers.service.spec.ts | 106 ++++++++++++++++++ .../services/event-consumers.service.ts | 3 - .../app/shared/services/help.service.spec.ts | 73 ++++++++++++ .../app/shared/services/help.service.ts | 2 +- 6 files changed, 197 insertions(+), 6 deletions(-) create mode 100644 src/Squidex/app/shared/services/event-consumers.service.spec.ts create mode 100644 src/Squidex/app/shared/services/help.service.spec.ts diff --git a/src/Squidex/app/framework/utils/immutable-array.spec.ts b/src/Squidex/app/framework/utils/immutable-array.spec.ts index 9b7edabaf..b9e57db0c 100644 --- a/src/Squidex/app/framework/utils/immutable-array.spec.ts +++ b/src/Squidex/app/framework/utils/immutable-array.spec.ts @@ -47,6 +47,21 @@ describe('ImmutableArray', () => { expect(array_2).toBe(array_1); }); + it('should push front items', () => { + const array_1 = ImmutableArray.of([1, 2, 3]); + const array_2 = array_1.pushFront(4, 5); + + expect(array_2.length).toBe(5); + expect(array_2.values).toEqual([4, 5, 1, 2, 3]); + }); + + it('should return same array if pushing zero items to the front', () => { + const array_1 = ImmutableArray.of([1, 2, 3]); + const array_2 = array_1.pushFront(); + + expect(array_2).toBe(array_1); + }); + it('should remove item', () => { const array_1 = ImmutableArray.of([1, 2, 3]); const array_2 = array_1.remove(2); diff --git a/src/Squidex/app/shared/services/contents.service.spec.ts b/src/Squidex/app/shared/services/contents.service.spec.ts index 780bfa7f3..5521c3948 100644 --- a/src/Squidex/app/shared/services/contents.service.spec.ts +++ b/src/Squidex/app/shared/services/contents.service.spec.ts @@ -73,7 +73,7 @@ describe('ContentsService', () => { authService.verifyAll(); }); - it('should make append query to get request as search', () => { + it('should append query to get request as search', () => { authService.setup(x => x.authGet('http://service/p/api/content/my-app/my-schema?nonPublished=true&hidden=true&$search=my-query&$top=17&$skip=13')) .returns(() => Observable.of( new Response( @@ -95,7 +95,7 @@ describe('ContentsService', () => { authService.verifyAll(); }); - it('should make append query to get request as plain', () => { + it('should append query to get request as plain query string', () => { authService.setup(x => x.authGet('http://service/p/api/content/my-app/my-schema?nonPublished=true&hidden=true&$filter=my-filter&$top=17&$skip=13')) .returns(() => Observable.of( new Response( diff --git a/src/Squidex/app/shared/services/event-consumers.service.spec.ts b/src/Squidex/app/shared/services/event-consumers.service.spec.ts new file mode 100644 index 000000000..fad7b9ae9 --- /dev/null +++ b/src/Squidex/app/shared/services/event-consumers.service.spec.ts @@ -0,0 +1,106 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Sebastian Stehle. All rights reserved + */ + +import { Response, ResponseOptions } from '@angular/http'; +import { Observable } from 'rxjs'; +import { IMock, It, Mock, Times } from 'typemoq'; + +import { + ApiUrlConfig, + AuthService, + EventConsumerDto, + EventConsumersService +} from './../'; + +describe('EventConsumersService', () => { + let authService: IMock; + let eventConsumersService: EventConsumersService; + + beforeEach(() => { + authService = Mock.ofType(AuthService); + eventConsumersService = new EventConsumersService(authService.object, new ApiUrlConfig('http://service/p/')); + }); + + it('should make get request to get event consumers', () => { + authService.setup(x => x.authGet('http://service/p/api/event-consumers')) + .returns(() => Observable.of( + new Response( + new ResponseOptions({ + body: [{ + name: 'event-consumer1', + lastHandledEventNumber: 13, + isStopped: true, + isResetting: true, + error: 'an error 1' + }, { + name: 'event-consumer2', + lastHandledEventNumber: 29, + isStopped: true, + isResetting: true, + error: 'an error 2' + }] + }) + ) + )) + .verifiable(Times.once()); + + let eventConsumers: EventConsumerDto[] | null = null; + + eventConsumersService.getEventConsumers().subscribe(result => { + eventConsumers = result; + }).unsubscribe(); + + expect(eventConsumers).toEqual([ + new EventConsumerDto('event-consumer1', 13, true, true, 'an error 1'), + new EventConsumerDto('event-consumer2', 29, true, true, 'an error 2') + ]); + + authService.verifyAll(); + }); + + it('should make put request to start event consumer', () => { + authService.setup(x => x.authPut('http://service/p/api/event-consumers/event-consumer1/start', It.isAny())) + .returns(() => Observable.of( + new Response( + new ResponseOptions() + ) + )) + .verifiable(Times.once()); + + eventConsumersService.startEventConsumer('event-consumer1'); + + authService.verifyAll(); + }); + + it('should make put request to stop event consumer', () => { + authService.setup(x => x.authPut('http://service/p/api/event-consumers/event-consumer1/stop', It.isAny())) + .returns(() => Observable.of( + new Response( + new ResponseOptions() + ) + )) + .verifiable(Times.once()); + + eventConsumersService.stopEventConsumer('event-consumer1'); + + authService.verifyAll(); + }); + + it('should make put request to reset event consumer', () => { + authService.setup(x => x.authPut('http://service/p/api/event-consumers/event-consumer1/reset', It.isAny())) + .returns(() => Observable.of( + new Response( + new ResponseOptions() + ) + )) + .verifiable(Times.once()); + + eventConsumersService.resetEventConsumer('event-consumer1'); + + authService.verifyAll(); + }); +}); \ No newline at end of file diff --git a/src/Squidex/app/shared/services/event-consumers.service.ts b/src/Squidex/app/shared/services/event-consumers.service.ts index f89edcd0c..ddd506d22 100644 --- a/src/Squidex/app/shared/services/event-consumers.service.ts +++ b/src/Squidex/app/shared/services/event-consumers.service.ts @@ -56,7 +56,6 @@ export class EventConsumersService { const url = this.apiUrl.buildUrl(`api/event-consumers/${name}/start`); return this.authService.authPut(url, {}) - .map(response => response.json()) .catchError('Failed to start event consumer. Please reload.'); } @@ -64,7 +63,6 @@ export class EventConsumersService { const url = this.apiUrl.buildUrl(`api/event-consumers/${name}/stop`); return this.authService.authPut(url, {}) - .map(response => response.json()) .catchError('Failed to stop event consumer. Please reload.'); } @@ -72,7 +70,6 @@ export class EventConsumersService { const url = this.apiUrl.buildUrl(`api/event-consumers/${name}/reset`); return this.authService.authPut(url, {}) - .map(response => response.json()) .catchError('Failed to reset event consumer. Please reload.'); } } \ No newline at end of file diff --git a/src/Squidex/app/shared/services/help.service.spec.ts b/src/Squidex/app/shared/services/help.service.spec.ts new file mode 100644 index 000000000..c03c0db3c --- /dev/null +++ b/src/Squidex/app/shared/services/help.service.spec.ts @@ -0,0 +1,73 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Sebastian Stehle. All rights reserved + */ + +import { Http, Response, ResponseOptions } from '@angular/http'; +import { Observable } from 'rxjs'; +import { IMock, Mock, Times } from 'typemoq'; + +import { HelpService } from './../'; + +describe('AppClientsService', () => { + let helpService: HelpService; + let http: IMock; + + beforeEach(() => { + http = Mock.ofType(Http); + + helpService = new HelpService(http.object); + }); + + it('should make get request to get help sections', () => { + http.setup(x => x.get('https://api.gitbook.com/book/squidex/squidex/contents/01-chapter/02-article.json')) + .returns(() => Observable.of( + new Response( + new ResponseOptions({ + body: { + sections: [{ + content: 'A test content with' + }, { + content: 'A test content with a A Link' + }, { + content: 'A test content with a Glossary Link' + }] + } + }) + ) + )) + .verifiable(Times.once()); + + let helpSections: string[] | null = null; + + helpService.getHelp('01-chapter/02-article').subscribe(result => { + helpSections = result; + }); + + expect(helpSections).toEqual([ + 'A test content with', + 'A test content with a A Link', + 'A test content with a Glossary Link' + ]); + + http.verifyAll(); + }); + + it('should return empty sections if get request fails', () => { + http.setup(x => x.get('https://api.gitbook.com/book/squidex/squidex/contents/01-chapter/02-article.json')) + .returns(() => Observable.throw('An error')) + .verifiable(Times.once()); + + let helpSections: string[] | null = null; + + helpService.getHelp('01-chapter/02-article').subscribe(result => { + helpSections = result; + }); + + expect(helpSections).toEqual([]); + + http.verifyAll(); + }); +}); \ No newline at end of file diff --git a/src/Squidex/app/shared/services/help.service.ts b/src/Squidex/app/shared/services/help.service.ts index 1ca9e595a..53a8d8f00 100644 --- a/src/Squidex/app/shared/services/help.service.ts +++ b/src/Squidex/app/shared/services/help.service.ts @@ -32,6 +32,6 @@ export class HelpService { return result; }) - .catch(err => []); + .catch(err => Observable.of([])); } } \ No newline at end of file