Browse Source

Started with tests for schema service.

pull/271/head
Sebastian Stehle 8 years ago
parent
commit
c67d3b9993
  1. 2
      src/Squidex/app/features/schemas/pages/schemas/schemas-page.component.ts
  2. 249
      src/Squidex/app/features/schemas/state/schemas.state.spec.ts
  3. 14
      src/Squidex/app/features/schemas/state/schemas.state.ts

2
src/Squidex/app/features/schemas/pages/schemas/schemas-page.component.ts

@ -73,7 +73,7 @@ export class SchemasPageComponent implements OnDestroy, OnInit {
} }
}); });
this.schemasState.load().subscribe(); this.schemasState.loadSchemas().subscribe();
} }
public createSchema(importing: any) { public createSchema(importing: any) {

249
src/Squidex/app/features/schemas/state/schemas.state.spec.ts

@ -0,0 +1,249 @@
/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/
import { Observable } from 'rxjs';
import { IMock, It, Mock, Times } from 'typemoq';
import { AuthService, DialogService } from '@app/shared';
import { SchemasState } from './schemas.state';
import {
AppsState,
DateTime,
SchemaDetailsDto,
SchemaDto,
SchemasService,
Version
} from '@app/shared';
describe('SchemasState', () => {
const app = 'my-app';
const yesterday = DateTime.today().addDays(-1);
const oldSchemas = [
new SchemaDto('id1', 'name1', {}, true, 'me', 'me', yesterday, yesterday, new Version('1')),
new SchemaDto('id2', 'name2', {}, true, 'me', 'me', yesterday, yesterday, new Version('2'))
];
const schema = new SchemaDetailsDto('id2', 'name2', {}, true, 'me', 'me', yesterday, yesterday, new Version('2'), []);
let dialogs: IMock<DialogService>;
let appsState: IMock<AppsState>;
let authService: IMock<AuthService>;
let schemasService: IMock<SchemasService>;
let schemasState: SchemasState;
beforeEach(() => {
dialogs = Mock.ofType<DialogService>();
authService = Mock.ofType<AuthService>();
authService.setup(x => x.user)
.returns(() => <any>{ id: 'id2' });
appsState = Mock.ofType<AppsState>();
appsState.setup(x => x.appName)
.returns(() => app);
schemasService = Mock.ofType<SchemasService>();
schemasService.setup(x => x.getSchemas(app))
.returns(() => Observable.of(oldSchemas));
schemasService.setup(x => x.getSchema(app, 'name2'))
.returns(() => Observable.of(schema));
schemasState = new SchemasState(appsState.object, authService.object, dialogs.object, schemasService.object);
schemasState.loadSchemas().subscribe();
});
it('should load schemas', () => {
expect(schemasState.snapshot.schemas.values).toEqual(oldSchemas);
schemasService.verifyAll();
});
it('should not load schema when already loaded', () => {
schemasState.selectSchema('name2').subscribe();
schemasState.selectSchema('name2').subscribe();
schemasService.verify(x => x.getSchema(app, 'name2'), Times.once());
});
it('should load selected schema when not loaded', () => {
let selectedSchema: SchemaDetailsDto;
schemasState.selectSchema('name2').subscribe(x => {
selectedSchema = x!;
});
expect(selectedSchema!).toBe(schema);
expect(schemasState.snapshot.selectedSchema).toBe(schema);
expect(schemasState.snapshot.selectedSchema).toBe(schemasState.snapshot.schemas.at(1));
});
it('should return null when unselecting schema', () => {
let selectedSchema: SchemaDetailsDto;
schemasState.selectSchema(null).subscribe(x => {
selectedSchema = x!;
});
expect(selectedSchema!).toBeNull();
expect(schemasState.snapshot.selectedSchema).toBeNull();
schemasService.verify(x => x.getSchema(app, It.isAnyString()), Times.never());
});
/*
it('should mark as current user when selected user equals to profile', () => {
usersState.selectUser('id2').subscribe();
expect(usersState.snapshot.isCurrentUser).toBeTruthy();
});
it('should load user when not loaded', () => {
usersService.setup(x => x.getUser('id3'))
.returns(() => Observable.of(newUser));
let selectedUser: UserDto;
usersState.selectUser('id3').subscribe(x => {
selectedUser = x!;
});
expect(selectedUser!).toEqual(newUser);
expect(usersState.snapshot.selectedUser).toBe(newUser);
});
it('should return null when user to select is not found', () => {
usersService.setup(x => x.getUser('unknown'))
.returns(() => Observable.throw({}));
let selectedUser: UserDto;
usersState.selectUser('unknown').subscribe(x => {
selectedUser = x!;
}).unsubscribe();
expect(selectedUser!).toBeNull();
expect(usersState.snapshot.selectedUser).toBeNull();
});
it('should mark user as locked', () => {
usersService.setup(x => x.lockUser('id1'))
.returns(() => Observable.of({}));
usersState.selectUser('id1').subscribe();
usersState.lockUser(oldUsers[0]).subscribe();
expect(usersState.snapshot.users.at(0).isLocked).toBeTruthy();
expect(usersState.snapshot.selectedUser).toBe(usersState.snapshot.users.at(0));
});
it('should raise notification when locking failed', () => {
usersService.setup(x => x.lockUser('id1'))
.returns(() => Observable.throw({}));
usersState.lockUser(oldUsers[0]).onErrorResumeNext().subscribe();
dialogs.verify(x => x.notifyError(It.isAny()), Times.once());
});
it('should unmark user as locked', () => {
usersService.setup(x => x.unlockUser('id2'))
.returns(() => Observable.of({}));
usersState.selectUser('id2').subscribe();
usersState.unlockUser(oldUsers[1]).subscribe();
expect(usersState.snapshot.users.at(1).isLocked).toBeFalsy();
expect(usersState.snapshot.selectedUser).toBe(usersState.snapshot.users.at(1));
});
it('should raise notification when unlocking failed', () => {
usersService.setup(x => x.unlockUser('id2'))
.returns(() => Observable.throw({}));
usersState.unlockUser(oldUsers[1]).onErrorResumeNext().subscribe();
dialogs.verify(x => x.notifyError(It.isAny()), Times.once());
});
it('should update user on update', () => {
const request = new UpdateUserDto('new@mail.com', 'New');
usersService.setup(x => x.putUser('id1', request))
.returns(() => Observable.of({}));
usersState.selectUser('id1').subscribe();
usersState.updateUser(oldUsers[0], request).subscribe();
expect(usersState.snapshot.users.at(0).email).toEqual('new@mail.com');
expect(usersState.snapshot.users.at(0).displayName).toEqual('New');
expect(usersState.snapshot.selectedUser).toBe(usersState.snapshot.users.at(0));
});
it('should not raise notification when updating failed', () => {
const request = new UpdateUserDto('new@mail.com', 'New');
usersService.setup(x => x.putUser('id1', request))
.returns(() => Observable.throw({}));
usersState.updateUser(oldUsers[0], request).onErrorResumeNext().subscribe();
dialogs.verify(x => x.notifyError(It.isAny()), Times.never());
});
it('should add user to state when created', () => {
const request = new CreateUserDto(newUser.email, newUser.displayName, 'password');
usersService.setup(x => x.postUser(request))
.returns(() => Observable.of(newUser));
usersState.createUser(request).subscribe();
expect(usersState.snapshot.users.at(0)).toBe(newUser);
expect(usersState.snapshot.usersPager.numberOfItems).toBe(201);
});
it('should not raise notification when creating failed', () => {
const request = new CreateUserDto(newUser.email, newUser.displayName, 'password');
usersService.setup(x => x.postUser(request))
.returns(() => Observable.throw({}));
usersState.createUser(request).onErrorResumeNext().subscribe();
dialogs.verify(x => x.notifyError(It.isAny()), Times.never());
});
it('should load next page and prev page when paging', () => {
usersService.setup(x => x.getUsers(10, 10, undefined))
.returns(() => Observable.of(new UsersDto(200, [])));
usersState.goNext().subscribe();
usersState.goPrev().subscribe();
usersService.verify(x => x.getUsers(10, 10, undefined), Times.once());
usersService.verify(x => x.getUsers(10, 0, undefined), Times.exactly(2));
});
it('should load with query when searching', () => {
usersService.setup(x => x.getUsers(10, 0, 'my-query'))
.returns(() => Observable.of(new UsersDto(0, [])));
usersState.search('my-query').subscribe();
expect(usersState.snapshot.usersQuery).toEqual('my-query');
usersService.verify(x => x.getUsers(10, 0, 'my-query'), Times.once());
});*/
});

14
src/Squidex/app/features/schemas/state/schemas.state.ts

@ -165,8 +165,8 @@ export class SchemasState extends State<Snapshot> {
super({ schemas: ImmutableArray.of() }); super({ schemas: ImmutableArray.of() });
} }
public selectSchema(id: string | null): Observable<SchemaDetailsDto | null> { public selectSchema(idOrName: string | null): Observable<SchemaDetailsDto | null> {
return this.loadSchema(id) return this.loadSchema(idOrName)
.do(schema => { .do(schema => {
this.next(s => { this.next(s => {
const schemas = schema ? s.schemas.replaceBy('id', schema) : s.schemas; const schemas = schema ? s.schemas.replaceBy('id', schema) : s.schemas;
@ -176,20 +176,20 @@ export class SchemasState extends State<Snapshot> {
}); });
} }
private loadSchema(id: string | null) { private loadSchema(idOrName: string | null) {
return !id ? return !idOrName ?
Observable.of(null) : Observable.of(null) :
Observable.of(<SchemaDetailsDto>this.snapshot.schemas.find(x => x.id === id && x instanceof SchemaDetailsDto)) Observable.of(<SchemaDetailsDto>this.snapshot.schemas.find(x => (x.name === idOrName || x.id === idOrName) && x instanceof SchemaDetailsDto))
.switchMap(schema => { .switchMap(schema => {
if (!schema) { if (!schema) {
return this.schemasService.getSchema(this.appName, id).catch(() => Observable.of(null)); return this.schemasService.getSchema(this.appName, idOrName).catch(() => Observable.of(null));
} else { } else {
return Observable.of(schema); return Observable.of(schema);
} }
}); });
} }
public load(): Observable<any> { public loadSchemas(): Observable<any> {
return this.schemasService.getSchemas(this.appName) return this.schemasService.getSchemas(this.appName)
.do(dtos => { .do(dtos => {
return this.next(s => { return this.next(s => {

Loading…
Cancel
Save