Browse Source

More tests

pull/1/head
Sebastian 10 years ago
parent
commit
61abae3299
  1. 8
      src/Squidex/app/framework/services/title.service.spec.ts
  2. 4
      src/Squidex/app/framework/utils/date-time.spec.ts
  3. 14
      src/Squidex/app/framework/utils/modal-view.spec.ts
  4. 7
      src/Squidex/app/framework/utils/vec2.spec.ts
  5. 68
      src/Squidex/app/shared/guards/app-must-exist.guard.spec.ts
  6. 52
      src/Squidex/app/shared/guards/must-be-authenticated.guard.spec.ts
  7. 6
      src/Squidex/app/shared/guards/must-be-authenticated.guard.ts
  8. 52
      src/Squidex/app/shared/guards/must-be-not-authenticated.guard.spec.ts
  9. 6
      src/Squidex/app/shared/guards/must-be-not-authenticated.guard.ts
  10. 14
      src/Squidex/app/shared/guards/router-mockup.ts

8
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'

4
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');

14
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);

7
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);

68
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<AppsStoreService>;
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, <any>router);
guard.canActivate(<any> { 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<boolean>('error'));
const router = new RouterMockup();
const guard = new AppMustExistGuard(appsStore.object, <any>router);
guard.canActivate(<any> { 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, <any>router);
guard.canActivate(<any> { params: { appName: 'my-app' } }, null)
.then(result => {
expect(result).toBeTruthy();
expect(router.lastNavigation).toBeUndefined();
done();
});
});
});

52
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<AuthService>;
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, <any>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, <any>router);
guard.canActivate(null, null)
.then(result => {
expect(result).toBeTruthy();
expect(router.lastNavigation).toBeUndefined();
done();
});
});
});

6
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> | boolean {
public canActivate(route: Ng2Router.ActivatedRouteSnapshot, state: Ng2Router.RouterStateSnapshot): Promise<boolean> {
return this.auth.checkLogin().then(isAuthenticated => {
if (!isAuthenticated) {
this.router.navigate(['']);
return false;
}
return true;
return isAuthenticated;
});
}
}

52
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<AuthService>;
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, <any>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, <any>router);
guard.canActivate(null, null)
.then(result => {
expect(result).toBeTruthy();
expect(router.lastNavigation).toBeUndefined();
done();
});
});
});

6
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> | boolean {
public canActivate(route: Ng2Router.ActivatedRouteSnapshot, state: Ng2Router.RouterStateSnapshot): Promise<boolean> {
return this.auth.checkLogin().then(isAuthenticated => {
if (isAuthenticated) {
this.router.navigate(['app']);
return false;
}
return true;
return !isAuthenticated;
});
}
}

14
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;
}
}
Loading…
Cancel
Save