From 61abae32990ebf2a4a84b1552e591f4f18e864a9 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 14 Nov 2016 22:15:32 +0100 Subject: [PATCH] More tests --- .../framework/services/title.service.spec.ts | 8 +++ .../app/framework/utils/date-time.spec.ts | 4 ++ .../app/framework/utils/modal-view.spec.ts | 14 ++++ src/Squidex/app/framework/utils/vec2.spec.ts | 7 ++ .../guards/app-must-exist.guard.spec.ts | 68 +++++++++++++++++++ .../must-be-authenticated.guard.spec.ts | 52 ++++++++++++++ .../guards/must-be-authenticated.guard.ts | 6 +- .../must-be-not-authenticated.guard.spec.ts | 52 ++++++++++++++ .../guards/must-be-not-authenticated.guard.ts | 6 +- .../app/shared/guards/router-mockup.ts | 14 ++++ 10 files changed, 223 insertions(+), 8 deletions(-) create mode 100644 src/Squidex/app/shared/guards/app-must-exist.guard.spec.ts create mode 100644 src/Squidex/app/shared/guards/must-be-authenticated.guard.spec.ts create mode 100644 src/Squidex/app/shared/guards/must-be-not-authenticated.guard.spec.ts create mode 100644 src/Squidex/app/shared/guards/router-mockup.ts diff --git a/src/Squidex/app/framework/services/title.service.spec.ts b/src/Squidex/app/framework/services/title.service.spec.ts index 2b8eaa4e7..76a9034e4 100644 --- a/src/Squidex/app/framework/services/title.service.spec.ts +++ b/src/Squidex/app/framework/services/title.service.spec.ts @@ -52,6 +52,14 @@ describe('TitleService', () => { expect(document.title).toBe('my-title - myapp'); }); + it('should do nothing if title is null', () => { + const titleService = new TitleService(new TitlesConfig({}, null, 'myapp')); + + titleService.setTitle(null, {}); + + expect(document.title).toBe(''); + }); + it('should set document title when title key is found in configuration', () => { const titles: { [key: string]: string } = { found: 'found-title' diff --git a/src/Squidex/app/framework/utils/date-time.spec.ts b/src/Squidex/app/framework/utils/date-time.spec.ts index eecf533be..794f44658 100644 --- a/src/Squidex/app/framework/utils/date-time.spec.ts +++ b/src/Squidex/app/framework/utils/date-time.spec.ts @@ -35,6 +35,10 @@ describe('DateTime', () => { expect(() => DateTime.parse('#', 'yyyy-MM-dd')).toThrow(); }); + it('should throw when utc date string to parse is invalid', () => { + expect(() => DateTime.parseUTC('#', 'yyyy-MM-dd')).toThrow(); + }); + it('should parse Microsoft date format', () => { const actual = DateTime.parseMSDate('/Date(1224043200000)/'); const expected = DateTime.parseISO('2008-10-15T04:00:00'); diff --git a/src/Squidex/app/framework/utils/modal-view.spec.ts b/src/Squidex/app/framework/utils/modal-view.spec.ts index 307379d4c..01dda2efe 100644 --- a/src/Squidex/app/framework/utils/modal-view.spec.ts +++ b/src/Squidex/app/framework/utils/modal-view.spec.ts @@ -8,6 +8,14 @@ import { ModalView } from './../'; describe('ModalView', () => { + it('should have default values', () => { + const dialog = new ModalView(); + + checkValue(dialog, false); + + expect(dialog.closeAlways).toBeFalsy(); + }); + it('should have initial true value', () => { const dialog = new ModalView(true); @@ -20,6 +28,12 @@ describe('ModalView', () => { checkValue(dialog, false); }); + it('should have close always set by constructor', () => { + const dialog = new ModalView(false, true); + + expect(dialog.closeAlways).toBeTruthy(); + }); + it('should become open after show', () => { const dialog = new ModalView(false); diff --git a/src/Squidex/app/framework/utils/vec2.spec.ts b/src/Squidex/app/framework/utils/vec2.spec.ts index 3d02e7ac3..071589756 100644 --- a/src/Squidex/app/framework/utils/vec2.spec.ts +++ b/src/Squidex/app/framework/utils/vec2.spec.ts @@ -27,6 +27,13 @@ describe('Vec2', () => { expect(new Vec2(10, 10).ne(new Vec2(10, 10))).toBeFalsy(); }); + it('should round with default value', () => { + const actual = new Vec2(1.3, 1.6).round(); + const expected = new Vec2(1, 2); + + expect(actual).toEqual(expected); + }); + it('should calculate multiple of 10', () => { const actual = new Vec2(13, 16).round(10); const expected = new Vec2(10, 20); diff --git a/src/Squidex/app/shared/guards/app-must-exist.guard.spec.ts b/src/Squidex/app/shared/guards/app-must-exist.guard.spec.ts new file mode 100644 index 000000000..deddcddfe --- /dev/null +++ b/src/Squidex/app/shared/guards/app-must-exist.guard.spec.ts @@ -0,0 +1,68 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Sebastian Stehle. All rights reserved + */ + +import * as TypeMoq from 'typemoq'; + +import { AppMustExistGuard } from './app-must-exist.guard'; +import { AppsStoreService } from 'shared'; +import { RouterMockup } from './router-mockup'; + +describe('AppMustExistGuard', () => { + let appsStore: TypeMoq.Mock; + + beforeEach(() => { + appsStore = TypeMoq.Mock.ofType(AppsStoreService); + }); + + it('should navigate to 404 page if app is not found', (done) => { + appsStore.setup(x => x.selectApp('my-app')) + .returns(() => Promise.resolve(false)); + + const router = new RouterMockup(); + const guard = new AppMustExistGuard(appsStore.object, router); + + guard.canActivate( { params: { appName: 'my-app' } }, null) + .then(result => { + expect(result).toBeFalsy(); + expect(router.lastNavigation).toEqual(['/404']); + + done(); + }); + }); + + it('should navigate to 404 page if app loading fails', (done) => { + appsStore.setup(x => x.selectApp('my-app')) + .returns(() => Promise.reject('error')); + + const router = new RouterMockup(); + const guard = new AppMustExistGuard(appsStore.object, router); + + guard.canActivate( { params: { appName: 'my-app' } }, null) + .then(result => { + expect(result).toBeFalsy(); + expect(router.lastNavigation).toEqual(['/404']); + + done(); + }); + }); + + it('should return true if app is found', (done) => { + appsStore.setup(x => x.selectApp('my-app')) + .returns(() => Promise.resolve(true)); + + const router = new RouterMockup(); + const guard = new AppMustExistGuard(appsStore.object, router); + + guard.canActivate( { params: { appName: 'my-app' } }, null) + .then(result => { + expect(result).toBeTruthy(); + expect(router.lastNavigation).toBeUndefined(); + + done(); + }); + }); +}); \ No newline at end of file diff --git a/src/Squidex/app/shared/guards/must-be-authenticated.guard.spec.ts b/src/Squidex/app/shared/guards/must-be-authenticated.guard.spec.ts new file mode 100644 index 000000000..b97601441 --- /dev/null +++ b/src/Squidex/app/shared/guards/must-be-authenticated.guard.spec.ts @@ -0,0 +1,52 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Sebastian Stehle. All rights reserved + */ + +import * as TypeMoq from 'typemoq'; + +import { MustBeAuthenticatedGuard } from './must-be-authenticated.guard'; +import { RouterMockup } from './router-mockup'; +import { AuthService } from 'shared'; + +describe('MustBeAuthenticatedGuard', () => { + let authService: TypeMoq.Mock; + + beforeEach(() => { + authService = TypeMoq.Mock.ofType(AuthService); + }); + + it('should navigate to default page if not authenticated', (done) => { + authService.setup(x => x.checkLogin()) + .returns(() => Promise.resolve(false)); + + const router = new RouterMockup(); + const guard = new MustBeAuthenticatedGuard(authService.object, router); + + guard.canActivate(null, null) + .then(result => { + expect(result).toBeFalsy(); + expect(router.lastNavigation).toEqual(['']); + + done(); + }); + }); + + it('should return true if authenticated', (done) => { + authService.setup(x => x.checkLogin()) + .returns(() => Promise.resolve(true)); + + const router = new RouterMockup(); + const guard = new MustBeAuthenticatedGuard(authService.object, router); + + guard.canActivate(null, null) + .then(result => { + expect(result).toBeTruthy(); + expect(router.lastNavigation).toBeUndefined(); + + done(); + }); + }); +}); \ No newline at end of file diff --git a/src/Squidex/app/shared/guards/must-be-authenticated.guard.ts b/src/Squidex/app/shared/guards/must-be-authenticated.guard.ts index f9cab69f5..8911ff7b2 100644 --- a/src/Squidex/app/shared/guards/must-be-authenticated.guard.ts +++ b/src/Squidex/app/shared/guards/must-be-authenticated.guard.ts @@ -18,14 +18,12 @@ export class MustBeAuthenticatedGuard implements Ng2Router.CanActivate { ) { } - public canActivate(route: Ng2Router.ActivatedRouteSnapshot, state: Ng2Router.RouterStateSnapshot): Promise | boolean { + public canActivate(route: Ng2Router.ActivatedRouteSnapshot, state: Ng2Router.RouterStateSnapshot): Promise { return this.auth.checkLogin().then(isAuthenticated => { if (!isAuthenticated) { this.router.navigate(['']); - - return false; } - return true; + return isAuthenticated; }); } } \ No newline at end of file diff --git a/src/Squidex/app/shared/guards/must-be-not-authenticated.guard.spec.ts b/src/Squidex/app/shared/guards/must-be-not-authenticated.guard.spec.ts new file mode 100644 index 000000000..ae549344e --- /dev/null +++ b/src/Squidex/app/shared/guards/must-be-not-authenticated.guard.spec.ts @@ -0,0 +1,52 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Sebastian Stehle. All rights reserved + */ + +import * as TypeMoq from 'typemoq'; + +import { MustBeNotAuthenticatedGuard } from './must-be-not-authenticated.guard'; +import { RouterMockup } from './router-mockup'; +import { AuthService } from 'shared'; + +describe('MustBeNotAuthenticatedGuard', () => { + let authService: TypeMoq.Mock; + + beforeEach(() => { + authService = TypeMoq.Mock.ofType(AuthService); + }); + + it('should navigate to app page if authenticated', (done) => { + authService.setup(x => x.checkLogin()) + .returns(() => Promise.resolve(true)); + + const router = new RouterMockup(); + const guard = new MustBeNotAuthenticatedGuard(authService.object, router); + + guard.canActivate(null, null) + .then(result => { + expect(result).toBeFalsy(); + expect(router.lastNavigation).toEqual(['app']); + + done(); + }); + }); + + it('should return true if not authenticated', (done) => { + authService.setup(x => x.checkLogin()) + .returns(() => Promise.resolve(false)); + + const router = new RouterMockup(); + const guard = new MustBeNotAuthenticatedGuard(authService.object, router); + + guard.canActivate(null, null) + .then(result => { + expect(result).toBeTruthy(); + expect(router.lastNavigation).toBeUndefined(); + + done(); + }); + }); +}); \ No newline at end of file diff --git a/src/Squidex/app/shared/guards/must-be-not-authenticated.guard.ts b/src/Squidex/app/shared/guards/must-be-not-authenticated.guard.ts index 7c01f3c50..0ef867cdb 100644 --- a/src/Squidex/app/shared/guards/must-be-not-authenticated.guard.ts +++ b/src/Squidex/app/shared/guards/must-be-not-authenticated.guard.ts @@ -18,14 +18,12 @@ export class MustBeNotAuthenticatedGuard implements Ng2Router.CanActivate { ) { } - public canActivate(route: Ng2Router.ActivatedRouteSnapshot, state: Ng2Router.RouterStateSnapshot): Promise | boolean { + public canActivate(route: Ng2Router.ActivatedRouteSnapshot, state: Ng2Router.RouterStateSnapshot): Promise { return this.auth.checkLogin().then(isAuthenticated => { if (isAuthenticated) { this.router.navigate(['app']); - - return false; } - return true; + return !isAuthenticated; }); } } \ No newline at end of file diff --git a/src/Squidex/app/shared/guards/router-mockup.ts b/src/Squidex/app/shared/guards/router-mockup.ts new file mode 100644 index 000000000..3f9851c27 --- /dev/null +++ b/src/Squidex/app/shared/guards/router-mockup.ts @@ -0,0 +1,14 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Sebastian Stehle. All rights reserved + */ + +export class RouterMockup { + public lastNavigation: any[]; + + public navigate(target: any[]) { + this.lastNavigation = target; + } +} \ No newline at end of file