Browse Source

Tests for schemas.

pull/271/head
Sebastian Stehle 8 years ago
parent
commit
5458aa9d93
  1. 52
      src/Squidex/app/features/administration/state/users.state.spec.ts
  2. 323
      src/Squidex/app/features/schemas/state/schemas.state.spec.ts
  3. 112
      src/Squidex/app/features/schemas/state/schemas.state.ts
  4. 47
      src/Squidex/app/shared/services/schemas.fields.spec.ts
  5. 159
      src/Squidex/app/shared/services/schemas.service.spec.ts

52
src/Squidex/app/features/administration/state/users.state.spec.ts

@ -79,6 +79,12 @@ describe('UsersState', () => {
dialogs.verify(x => x.notifyInfo(It.isAnyString()), Times.once());
});
it('should mark as current user when selected user equals to profile', () => {
usersState.selectUser('id2').subscribe();
expect(usersState.snapshot.isCurrentUser).toBeTruthy();
});
it('should not load user when already loaded', () => {
let selectedUser: UserDto;
@ -92,12 +98,6 @@ describe('UsersState', () => {
usersService.verify(x => x.getUser(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));
@ -150,15 +150,6 @@ describe('UsersState', () => {
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({}));
@ -170,15 +161,6 @@ describe('UsersState', () => {
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');
@ -193,17 +175,6 @@ describe('UsersState', () => {
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');
@ -216,17 +187,6 @@ describe('UsersState', () => {
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, [])));

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

@ -8,30 +8,46 @@
import { Observable } from 'rxjs';
import { IMock, It, Mock, Times } from 'typemoq';
import { AuthService, DialogService } from '@app/shared';
import { AuthService, DialogService, Versioned, AddFieldDto, CreateSchemaDto, UpdateFieldDto } from '@app/shared';
import { SchemasState } from './schemas.state';
import {
AppsState,
createProperties,
DateTime,
FieldDto,
SchemaDetailsDto,
SchemaDto,
SchemasService,
UpdateSchemaDto,
UpdateSchemaScriptsDto,
Version
} from '@app/shared';
describe('SchemasState', () => {
const app = 'my-app';
const yesterday = DateTime.today().addDays(-1);
const creation = DateTime.today();
const creator = 'not-me';
const modified = DateTime.now();
const modifier = 'me';
const version = new Version('1');
const newVersion = new Version('2');
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'))
new SchemaDto('id1', 'name1', {}, false, creator, creator, creation, creation, version),
new SchemaDto('id2', 'name2', {}, true , creator, creator, creation, creation, version)
];
const schema = new SchemaDetailsDto('id2', 'name2', {}, true, 'me', 'me', yesterday, yesterday, new Version('2'), []);
const field1 = new FieldDto(1, '1', false, false, false, 'l', createProperties('String'));
const field2 = new FieldDto(2, '2', true, true, true, 'l', createProperties('Number'));
const schema =
new SchemaDetailsDto('id2', 'name2', {}, true,
creator, creator,
creation, creation,
version,
[field1, field2]);
let dialogs: IMock<DialogService>;
let appsState: IMock<AppsState>;
@ -45,7 +61,7 @@ describe('SchemasState', () => {
authService = Mock.ofType<AuthService>();
authService.setup(x => x.user)
.returns(() => <any>{ id: 'id2' });
.returns(() => <any>{ id: '1', token: modifier });
appsState = Mock.ofType<AppsState>();
@ -57,7 +73,10 @@ describe('SchemasState', () => {
schemasService.setup(x => x.getSchemas(app))
.returns(() => Observable.of(oldSchemas));
schemasService.setup(x => x.getSchema(app, 'name2'))
schemasService.setup(x => x.getSchema(app, schema.name))
.returns(() => Observable.of(schema));
schemasService.setup(x => x.getSchema(app, schema.name))
.returns(() => Observable.of(schema));
schemasState = new SchemasState(appsState.object, authService.object, dialogs.object, schemasService.object);
@ -70,11 +89,11 @@ describe('SchemasState', () => {
schemasService.verifyAll();
});
it('should not load schema when already loaded', () => {
it('should reload schema when already loaded', () => {
schemasState.selectSchema('name2').subscribe();
schemasState.selectSchema('name2').subscribe();
schemasService.verify(x => x.getSchema(app, 'name2'), Times.once());
schemasService.verify(x => x.getSchema(app, 'name2'), Times.exactly(2));
});
it('should load selected schema when not loaded', () => {
@ -89,6 +108,20 @@ describe('SchemasState', () => {
expect(schemasState.snapshot.selectedSchema).toBe(schemasState.snapshot.schemas.at(1));
});
it('should return null when loading failed', () => {
schemasService.setup(x => x.getSchema(app, 'failed'))
.returns(() => Observable.throw({}));
let selectedSchema: SchemaDetailsDto;
schemasState.selectSchema('failed').subscribe(x => {
selectedSchema = x!;
});
expect(selectedSchema!).toBeNull();
expect(schemasState.snapshot.selectedSchema).toBeNull();
});
it('should return null when unselecting schema', () => {
let selectedSchema: SchemaDetailsDto;
@ -102,148 +135,222 @@ describe('SchemasState', () => {
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();
it('should update isPublished property and user info when publishing', () => {
schemasService.setup(x => x.publishSchema(app, oldSchemas[0].name, version))
.returns(() => Observable.of(new Versioned<any>(newVersion, {})));
expect(usersState.snapshot.isCurrentUser).toBeTruthy();
schemasState.publish(oldSchemas[0], modified).subscribe();
const schema_1 = schemasState.snapshot.schemas.at(0);
expect(schema_1.isPublished).toBeTruthy();
expectToBeModified(schema_1);
});
it('should load user when not loaded', () => {
usersService.setup(x => x.getUser('id3'))
.returns(() => Observable.of(newUser));
it('should update isPublished property and user info when unpublishing', () => {
schemasService.setup(x => x.unpublishSchema(app, oldSchemas[1].name, version))
.returns(() => Observable.of(new Versioned<any>(newVersion, {})));
let selectedUser: UserDto;
schemasState.unpublish(oldSchemas[1], modified).subscribe();
usersState.selectUser('id3').subscribe(x => {
selectedUser = x!;
});
const schema_1 = schemasState.snapshot.schemas.at(1);
expect(selectedUser!).toEqual(newUser);
expect(usersState.snapshot.selectedUser).toBe(newUser);
expect(schema_1.isPublished).toBeFalsy();
expectToBeModified(schema_1);
});
it('should return null when user to select is not found', () => {
usersService.setup(x => x.getUser('unknown'))
.returns(() => Observable.throw({}));
describe('without selection', () => {
beforeEach(() => {
schemasState.selectSchema(schema.name).subscribe();
});
let selectedUser: UserDto;
it('should update isPublished property and user info when publishing selected schema', () => {
schemasService.setup(x => x.publishSchema(app, schema.name, version))
.returns(() => Observable.of(new Versioned<any>(newVersion, {})));
usersState.selectUser('unknown').subscribe(x => {
selectedUser = x!;
}).unsubscribe();
schemasState.publish(schema, modified).subscribe();
expect(selectedUser!).toBeNull();
expect(usersState.snapshot.selectedUser).toBeNull();
});
const schema_1 = <SchemaDetailsDto>schemasState.snapshot.schemas.at(1);
it('should mark user as locked', () => {
usersService.setup(x => x.lockUser('id1'))
.returns(() => Observable.of({}));
expect(schema_1.isPublished).toBeTruthy();
expectToBeModified(schema_1);
});
usersState.selectUser('id1').subscribe();
usersState.lockUser(oldUsers[0]).subscribe();
it('should update properties property and user info when updating', () => {
const request = new UpdateSchemaDto('name2_label', 'name2_hints');
expect(usersState.snapshot.users.at(0).isLocked).toBeTruthy();
expect(usersState.snapshot.selectedUser).toBe(usersState.snapshot.users.at(0));
});
schemasService.setup(x => x.putSchema(app, schema.name, It.isAny(), version))
.returns(() => Observable.of(new Versioned<any>(newVersion, {})));
it('should raise notification when locking failed', () => {
usersService.setup(x => x.lockUser('id1'))
.returns(() => Observable.throw({}));
schemasState.update(schema, request, modified).subscribe();
usersState.lockUser(oldUsers[0]).onErrorResumeNext().subscribe();
const schema_1 = <SchemaDetailsDto>schemasState.snapshot.schemas.at(1);
dialogs.verify(x => x.notifyError(It.isAny()), Times.once());
});
expect(schema_1.properties.label).toEqual('name2_label');
expect(schema_1.properties.hints).toEqual('name2_hints');
expectToBeModified(schema_1);
});
it('should unmark user as locked', () => {
usersService.setup(x => x.unlockUser('id2'))
.returns(() => Observable.of({}));
it('should update script properties and user info when updating', () => {
const request = new UpdateSchemaScriptsDto('query', 'create', 'update', 'delete', 'change');
usersState.selectUser('id2').subscribe();
usersState.unlockUser(oldUsers[1]).subscribe();
schemasService.setup(x => x.putSchemaScripts(app, schema.name, It.isAny(), version))
.returns(() => Observable.of(new Versioned<any>(newVersion, {})));
expect(usersState.snapshot.users.at(1).isLocked).toBeFalsy();
expect(usersState.snapshot.selectedUser).toBe(usersState.snapshot.users.at(1));
});
schemasState.configureScripts(schema, request, modified).subscribe();
it('should raise notification when unlocking failed', () => {
usersService.setup(x => x.unlockUser('id2'))
.returns(() => Observable.throw({}));
const schema_1 = <SchemaDetailsDto>schemasState.snapshot.schemas.at(1);
usersState.unlockUser(oldUsers[1]).onErrorResumeNext().subscribe();
expect(schema_1.scriptQuery).toEqual('query');
expect(schema_1.scriptCreate).toEqual('create');
expect(schema_1.scriptUpdate).toEqual('update');
expect(schema_1.scriptDelete).toEqual('delete');
expect(schema_1.scriptChange).toEqual('change');
expectToBeModified(schema_1);
});
dialogs.verify(x => x.notifyError(It.isAny()), Times.once());
});
it('should add schema when created', () => {
const request = new CreateSchemaDto('newName');
it('should update user on update', () => {
const request = new UpdateUserDto('new@mail.com', 'New');
const result = new SchemaDetailsDto('id4', 'newName', {}, false, modifier, modifier, modified, modified, version, []);
usersService.setup(x => x.putUser('id1', request))
.returns(() => Observable.of({}));
schemasService.setup(x => x.postSchema(app, request, modifier, modified))
.returns(() => Observable.of(result));
usersState.selectUser('id1').subscribe();
usersState.updateUser(oldUsers[0], request).subscribe();
schemasState.create(request, modified).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));
});
expect(schemasState.snapshot.schemas.values.length).toBe(3);
expect(schemasState.snapshot.schemas.at(2)).toBe(result);
});
it('should not raise notification when updating failed', () => {
const request = new UpdateUserDto('new@mail.com', 'New');
it('should remove schema when deleted', () => {
schemasService.setup(x => x.deleteSchema(app, schema.name, version))
.returns(() => Observable.of(new Versioned<any>(newVersion, {})));
usersService.setup(x => x.putUser('id1', request))
.returns(() => Observable.throw({}));
schemasState.delete(schema).subscribe();
usersState.updateUser(oldUsers[0], request).onErrorResumeNext().subscribe();
expect(schemasState.snapshot.schemas.values.length).toBe(1);
expect(schemasState.snapshot.selectedSchema).toBeNull();
});
dialogs.verify(x => x.notifyError(It.isAny()), Times.never());
});
it('should update fields property and user info when adding field', () => {
const request = new AddFieldDto(field1.name, field1.partitioning, field1.properties);
it('should add user to state when created', () => {
const request = new CreateUserDto(newUser.email, newUser.displayName, 'password');
const newField = new FieldDto(3, '3', false, false, false, 'l', createProperties('String'));
usersService.setup(x => x.postUser(request))
.returns(() => Observable.of(newUser));
schemasService.setup(x => x.postField(app, schema.name, It.isAny(), version))
.returns(() => Observable.of(new Versioned<FieldDto>(newVersion, newField)));
usersState.createUser(request).subscribe();
schemasState.addField(schema, request, modified).subscribe();
expect(usersState.snapshot.users.at(0)).toBe(newUser);
expect(usersState.snapshot.usersPager.numberOfItems).toBe(201);
});
const schema_1 = <SchemaDetailsDto>schemasState.snapshot.schemas.at(1);
it('should not raise notification when creating failed', () => {
const request = new CreateUserDto(newUser.email, newUser.displayName, 'password');
expect(schema_1.fields).toEqual([field1, field2, newField]);
expectToBeModified(schema_1);
});
usersService.setup(x => x.postUser(request))
.returns(() => Observable.throw({}));
it('should update fields property and user info when removing field', () => {
schemasService.setup(x => x.deleteField(app, schema.name, field1.fieldId, version))
.returns(() => Observable.of(new Versioned<any>(newVersion, {})));
usersState.createUser(request).onErrorResumeNext().subscribe();
schemasState.deleteField(schema, field1, modified).subscribe();
dialogs.verify(x => x.notifyError(It.isAny()), Times.never());
});
const schema_1 = <SchemaDetailsDto>schemasState.snapshot.schemas.at(1);
it('should load next page and prev page when paging', () => {
usersService.setup(x => x.getUsers(10, 10, undefined))
.returns(() => Observable.of(new UsersDto(200, [])));
expect(schema_1.fields).toEqual([field2]);
expectToBeModified(schema_1);
});
usersState.goNext().subscribe();
usersState.goPrev().subscribe();
it('should update fields property and user info when sorting fields', () => {
schemasService.setup(x => x.putFieldOrdering(app, schema.name, [field2.fieldId, field1.fieldId], version))
.returns(() => Observable.of(new Versioned<any>(newVersion, {})));
usersService.verify(x => x.getUsers(10, 10, undefined), Times.once());
usersService.verify(x => x.getUsers(10, 0, undefined), Times.exactly(2));
});
schemasState.sortFields(schema, [field2, field1], modified).subscribe();
const schema_1 = <SchemaDetailsDto>schemasState.snapshot.schemas.at(1);
expect(schema_1.fields).toEqual([field2, field1]);
expectToBeModified(schema_1);
});
it('should update fields property and user info when updating field', () => {
const request = new UpdateFieldDto(createProperties('String'));
schemasService.setup(x => x.putField(app, schema.name, field1.fieldId, request, version))
.returns(() => Observable.of(new Versioned<any>(newVersion, {})));
schemasState.updateField(schema, field1, request, modified).subscribe();
const schema_1 = <SchemaDetailsDto>schemasState.snapshot.schemas.at(1);
expect(schema_1.fields[0].properties).toBe(request.properties);
expectToBeModified(schema_1);
});
it('should update fields property and user info when hiding field', () => {
schemasService.setup(x => x.hideField(app, schema.name, field1.fieldId, version))
.returns(() => Observable.of(new Versioned<any>(newVersion, {})));
schemasState.hideField(schema, field1, modified).subscribe();
const schema_1 = <SchemaDetailsDto>schemasState.snapshot.schemas.at(1);
expect(schema_1.fields[0].isHidden).toBeTruthy();
expectToBeModified(schema_1);
});
it('should load with query when searching', () => {
usersService.setup(x => x.getUsers(10, 0, 'my-query'))
.returns(() => Observable.of(new UsersDto(0, [])));
it('should update fields property and user info when disabling field', () => {
schemasService.setup(x => x.disableField(app, schema.name, field1.fieldId, version))
.returns(() => Observable.of(new Versioned<any>(newVersion, {})));
usersState.search('my-query').subscribe();
schemasState.disableField(schema, field1, modified).subscribe();
expect(usersState.snapshot.usersQuery).toEqual('my-query');
const schema_1 = <SchemaDetailsDto>schemasState.snapshot.schemas.at(1);
expect(schema_1.fields[0].isDisabled).toBeTruthy();
expectToBeModified(schema_1);
});
it('should update fields property and user info when locking field', () => {
schemasService.setup(x => x.lockField(app, schema.name, field1.fieldId, version))
.returns(() => Observable.of(new Versioned<any>(newVersion, {})));
schemasState.lockField(schema, field1, modified).subscribe();
const schema_1 = <SchemaDetailsDto>schemasState.snapshot.schemas.at(1);
expect(schema_1.fields[0].isLocked).toBeTruthy();
expectToBeModified(schema_1);
});
it('should update fields property and user info when showing field', () => {
schemasService.setup(x => x.showField(app, schema.name, field2.fieldId, version))
.returns(() => Observable.of(new Versioned<any>(newVersion, {})));
schemasState.showField(schema, field2, modified).subscribe();
const schema_1 = <SchemaDetailsDto>schemasState.snapshot.schemas.at(1);
expect(schema_1.fields[1].isHidden).toBeFalsy();
expectToBeModified(schema_1);
});
it('should update fields property and user info when enabling field', () => {
schemasService.setup(x => x.enableField(app, schema.name, field2.fieldId, version))
.returns(() => Observable.of(new Versioned<any>(newVersion, {})));
schemasState.enableField(schema, field2, modified).subscribe();
const schema_1 = <SchemaDetailsDto>schemasState.snapshot.schemas.at(1);
expect(schema_1.fields[1].isDisabled).toBeFalsy();
expectToBeModified(schema_1);
});
});
usersService.verify(x => x.getUsers(10, 0, 'my-query'), Times.once());
});*/
function expectToBeModified(schema_1: SchemaDto) {
expect(schema_1.lastModified).toEqual(modified);
expect(schema_1.lastModifiedBy).toEqual(modifier);
expect(schema_1.version).toEqual(newVersion);
}
});

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

@ -177,16 +177,9 @@ export class SchemasState extends State<Snapshot> {
}
private loadSchema(idOrName: string | null) {
return !idOrName ?
Observable.of(null) :
Observable.of(<SchemaDetailsDto>this.snapshot.schemas.find(x => (x.name === idOrName || x.id === idOrName) && x instanceof SchemaDetailsDto))
.switchMap(schema => {
if (!schema) {
return this.schemasService.getSchema(this.appName, idOrName).catch(() => Observable.of(null));
} else {
return Observable.of(schema);
}
});
return !idOrName ? Observable.of(null) :
this.schemasService.getSchema(this.appName, idOrName)
.catch(() => Observable.of(null));
}
public loadSchemas(): Observable<any> {
@ -198,133 +191,134 @@ export class SchemasState extends State<Snapshot> {
return { ...s, schemas };
});
})
.catch(error => this.dialogs.notifyError(error));
.notify(this.dialogs);
}
public delete(schema: SchemaDto): Observable<any> {
return this.schemasService.deleteSchema(this.appName, schema.name, schema.version)
public create(request: CreateSchemaDto, now?: DateTime) {
return this.schemasService.postSchema(this.appName, request, this.user, now || DateTime.now())
.do(dto => {
return this.next(s => {
const schemas = s.schemas.filter(x => x.id !== schema.id);
const schemas = s.schemas.push(dto).sortByStringAsc(x => x.displayName);
return { ...s, schemas };
});
})
.catch(error => this.dialogs.notifyError(error));
});
}
public create(request: CreateSchemaDto) {
return this.schemasService.postSchema(this.appName, request, this.user, DateTime.now())
public delete(schema: SchemaDto): Observable<any> {
return this.schemasService.deleteSchema(this.appName, schema.name, schema.version)
.do(dto => {
return this.next(s => {
const schemas = s.schemas.push(dto).sortByStringAsc(x => x.displayName);
const schemas = s.schemas.filter(x => x.id !== schema.id);
const selectedSchema = s.selectedSchema && s.selectedSchema.id === schema.id ? null : s.selectedSchema;
return { ...s, schemas };
return { ...s, schemas, selectedSchema };
});
});
})
.notify(this.dialogs);
}
public addField(schema: SchemaDetailsDto, request: AddFieldDto): Observable<FieldDto> {
public addField(schema: SchemaDetailsDto, request: AddFieldDto, now?: DateTime): Observable<FieldDto> {
return this.schemasService.postField(this.appName, schema.name, request, schema.version)
.do(dto => {
this.replaceSchema(addField(schema, dto.payload, this.user, dto.version));
this.replaceSchema(addField(schema, dto.payload, this.user, dto.version, now));
}).map(d => d.payload);
}
public publish(schema: SchemaDto): Observable<any> {
public publish(schema: SchemaDto, now?: DateTime): Observable<any> {
return this.schemasService.publishSchema(this.appName, schema.name, schema.version)
.do(dto => {
this.replaceSchema(setPublished(schema, true, this.user, dto.version));
this.replaceSchema(setPublished(schema, true, this.user, dto.version, now));
})
.catch(error => this.dialogs.notifyError(error));
.notify(this.dialogs);
}
public unpublish(schema: SchemaDto): Observable<any> {
public unpublish(schema: SchemaDto, now?: DateTime): Observable<any> {
return this.schemasService.unpublishSchema(this.appName, schema.name, schema.version)
.do(dto => {
this.replaceSchema(setPublished(schema, false, this.user, dto.version));
this.replaceSchema(setPublished(schema, false, this.user, dto.version, now));
})
.catch(error => this.dialogs.notifyError(error));
.notify(this.dialogs);
}
public enableField(schema: SchemaDetailsDto, field: FieldDto): Observable<any> {
public enableField(schema: SchemaDetailsDto, field: FieldDto, now?: DateTime): Observable<any> {
return this.schemasService.enableField(this.appName, schema.name, field.fieldId, schema.version)
.do(dto => {
this.replaceSchema(updateField(schema, setDisabled(field, false), this.user, dto.version));
this.replaceSchema(updateField(schema, setDisabled(field, false), this.user, dto.version, now));
})
.catch(error => this.dialogs.notifyError(error));
.notify(this.dialogs);
}
public disableField(schema: SchemaDetailsDto, field: FieldDto): Observable<any> {
public disableField(schema: SchemaDetailsDto, field: FieldDto, now?: DateTime): Observable<any> {
return this.schemasService.disableField(this.appName, schema.name, field.fieldId, schema.version)
.do(dto => {
this.replaceSchema(updateField(schema, setDisabled(field, true), this.user, dto.version));
this.replaceSchema(updateField(schema, setDisabled(field, true), this.user, dto.version, now));
})
.catch(error => this.dialogs.notifyError(error));
.notify(this.dialogs);
}
public lockField(schema: SchemaDetailsDto, field: FieldDto): Observable<any> {
public lockField(schema: SchemaDetailsDto, field: FieldDto, now?: DateTime): Observable<any> {
return this.schemasService.lockField(this.appName, schema.name, field.fieldId, schema.version)
.do(dto => {
this.replaceSchema(updateField(schema, setLocked(field, true), this.user, dto.version));
this.replaceSchema(updateField(schema, setLocked(field, true), this.user, dto.version, now));
})
.catch(error => this.dialogs.notifyError(error));
.notify(this.dialogs);
}
public showField(schema: SchemaDetailsDto, field: FieldDto): Observable<any> {
public showField(schema: SchemaDetailsDto, field: FieldDto, now?: DateTime): Observable<any> {
return this.schemasService.showField(this.appName, schema.name, field.fieldId, schema.version)
.do(dto => {
this.replaceSchema(updateField(schema, setHidden(field, false), this.user, dto.version));
this.replaceSchema(updateField(schema, setHidden(field, false), this.user, dto.version, now));
})
.catch(error => this.dialogs.notifyError(error));
.notify(this.dialogs);
}
public hideField(schema: SchemaDetailsDto, field: FieldDto): Observable<any> {
public hideField(schema: SchemaDetailsDto, field: FieldDto, now?: DateTime): Observable<any> {
return this.schemasService.hideField(this.appName, schema.name, field.fieldId, schema.version)
.do(dto => {
this.replaceSchema(updateField(schema, setHidden(field, true), this.user, dto.version));
this.replaceSchema(updateField(schema, setHidden(field, true), this.user, dto.version, now));
})
.catch(error => this.dialogs.notifyError(error));
.notify(this.dialogs);
}
public deleteField(schema: SchemaDetailsDto, field: FieldDto): Observable<any> {
public deleteField(schema: SchemaDetailsDto, field: FieldDto, now?: DateTime): Observable<any> {
return this.schemasService.deleteField(this.appName, schema.name, field.fieldId, schema.version)
.do(dto => {
this.replaceSchema(removeField(schema, field, this.user, dto.version));
this.replaceSchema(removeField(schema, field, this.user, dto.version, now));
})
.catch(error => this.dialogs.notifyError(error));
.notify(this.dialogs);
}
public sortFields(schema: SchemaDetailsDto, fields: FieldDto[]): Observable<any> {
public sortFields(schema: SchemaDetailsDto, fields: FieldDto[], now?: DateTime): Observable<any> {
return this.schemasService.putFieldOrdering(this.appName, schema.name, fields.map(t => t.fieldId), schema.version)
.do(dto => {
this.replaceSchema(replaceFields(schema, fields, this.user, dto.version));
this.replaceSchema(replaceFields(schema, fields, this.user, dto.version, now));
})
.catch(error => this.dialogs.notifyError(error));
.notify(this.dialogs);
}
public updateField(schema: SchemaDetailsDto, field: FieldDto, request: UpdateFieldDto): Observable<any> {
public updateField(schema: SchemaDetailsDto, field: FieldDto, request: UpdateFieldDto, now?: DateTime): Observable<any> {
return this.schemasService.putField(this.appName, schema.name, field.fieldId, request, schema.version)
.do(dto => {
this.replaceSchema(updateField(schema, update(field, request.properties), this.user, dto.version));
this.replaceSchema(updateField(schema, update(field, request.properties), this.user, dto.version, now));
})
.catch(error => this.dialogs.notifyError(error));
.notify(this.dialogs);
}
public configureScripts(schema: SchemaDetailsDto, request: UpdateSchemaScriptsDto): Observable<any> {
public configureScripts(schema: SchemaDetailsDto, request: UpdateSchemaScriptsDto, now?: DateTime): Observable<any> {
return this.schemasService.putSchemaScripts(this.appName, schema.name, request, schema.version)
.do(dto => {
this.replaceSchema(configureScripts(schema, request, this.user, dto.version));
this.replaceSchema(configureScripts(schema, request, this.user, dto.version, now));
})
.catch(error => this.dialogs.notifyError(error));
.notify(this.dialogs);
}
public update(schema: SchemaDetailsDto, request: UpdateSchemaDto): Observable<any> {
public update(schema: SchemaDetailsDto, request: UpdateSchemaDto, now?: DateTime): Observable<any> {
return this.schemasService.putSchema(this.appName, schema.name, request, schema.version)
.do(dto => {
this.replaceSchema(updateProperties(schema, request, this.user, dto.version));
this.replaceSchema(updateProperties(schema, request, this.user, dto.version, now));
})
.catch(error => this.dialogs.notifyError(error));
.notify(this.dialogs);
}
private replaceSchema(schema: SchemaDto) {

47
src/Squidex/app/shared/services/schemas.fields.spec.ts

@ -10,7 +10,6 @@ import { DateTime } from '@app/framework';
import {
AssetsFieldPropertiesDto,
BooleanFieldPropertiesDto,
createProperties,
DateTimeFieldPropertiesDto,
FieldDto,
FieldPropertiesDto,
@ -22,52 +21,6 @@ import {
TagsFieldPropertiesDto
} from './../';
describe('FieldDto', () => {
it('should update isLocked property when locking', () => {
const field_1 = createField(createProperties('String'));
const field_2 = field_1.lock();
expect(field_2.isLocked).toBeTruthy();
});
it('should update isHidden property when hiding', () => {
const field_1 = createField(createProperties('String'));
const field_2 = field_1.hide();
expect(field_2.isHidden).toBeTruthy();
});
it('should update isHidden property when showing', () => {
const field_1 = createField(createProperties('String')).hide();
const field_2 = field_1.show();
expect(field_2.isHidden).toBeFalsy();
});
it('should update isDisabled property when disabling', () => {
const field_1 = createField(createProperties('String'));
const field_2 = field_1.disable();
expect(field_2.isDisabled).toBeTruthy();
});
it('should update isDisabled property when enabling', () => {
const field_1 = createField(createProperties('String')).disable();
const field_2 = field_1.enable();
expect(field_2.isDisabled).toBeFalsy();
});
it('should update properties property when updating', () => {
const newProperty = createProperties('Number');
const field_1 = createField(createProperties('String'));
const field_2 = field_1.update(newProperty);
expect(field_2.properties).toEqual(newProperty);
});
});
describe('AssetsField', () => {
const field = createField(new AssetsFieldPropertiesDto(null, null, null, true, false, 1, 1));

159
src/Squidex/app/shared/services/schemas.service.spec.ts

@ -26,165 +26,6 @@ import {
Version
} from './../';
describe('SchemaDto', () => {
const properties = new SchemaPropertiesDto('Name');
const creation = DateTime.today();
const creator = 'not-me';
const modified = DateTime.now();
const modifier = 'me';
const version = new Version('1');
const newVersion = new Version('2');
it('should update isPublished property and user info when publishing', () => {
const schema_1 = new SchemaDto('1', 'name', properties, false, creator, creator, creation, creation, version);
const schema_2 = schema_1.publish(modifier, newVersion, modified);
expect(schema_2.isPublished).toBeTruthy();
expect(schema_2.lastModified).toEqual(modified);
expect(schema_2.lastModifiedBy).toEqual(modifier);
expect(schema_2.version).toEqual(newVersion);
});
it('should update isPublished property and user info when unpublishing', () => {
const schema_1 = new SchemaDto('1', 'name', properties, false, creator, creator, creation, creation, version);
const schema_2 = schema_1.unpublish(modifier, newVersion, modified);
expect(schema_2.isPublished).toBeFalsy();
expect(schema_2.lastModified).toEqual(modified);
expect(schema_2.lastModifiedBy).toEqual(modifier);
expect(schema_2.version).toEqual(newVersion);
});
it('should update properties property and user info when updating', () => {
const newProperties = new SchemaPropertiesDto('New Name');
const schema_1 = new SchemaDto('1', 'name', properties, false, creator, creator, creation, creation, version);
const schema_2 = schema_1.update(newProperties, modifier, newVersion, modified);
expect(schema_2.properties).toEqual(newProperties);
expect(schema_2.lastModified).toEqual(modified);
expect(schema_2.lastModifiedBy).toEqual(modifier);
expect(schema_2.version).toEqual(newVersion);
});
it('should update scripts properties and user info when configure scripts', () => {
const newScripts =
new UpdateSchemaScriptsDto(
'<script-query>',
'<script-create>',
'<script-update>',
'<script-delete>',
'<script-change>');
const schema_1 = new SchemaDetailsDto('1', 'name', properties, false, creator, creator, creation, creation, version, []);
const schema_2 = schema_1.configureScripts(newScripts, modifier, newVersion, modified);
expect(schema_2.scriptQuery).toEqual('<script-query>');
expect(schema_2.scriptCreate).toEqual('<script-create>');
expect(schema_2.scriptUpdate).toEqual('<script-update>');
expect(schema_2.scriptDelete).toEqual('<script-delete>');
expect(schema_2.scriptChange).toEqual('<script-change>');
expect(schema_2.lastModified).toEqual(modified);
expect(schema_2.lastModifiedBy).toEqual(modifier);
expect(schema_2.version).toEqual(newVersion);
});
});
describe('SchemaDetailsDto', () => {
const properties = new SchemaPropertiesDto('Name');
const creation = DateTime.today();
const creator = 'not-me';
const modified = DateTime.now();
const modifier = 'me';
const version = new Version('1');
const newVersion = new Version('2');
it('should update isPublished property and user info when publishing', () => {
const schema_1 = new SchemaDetailsDto('1', 'name', properties, false, creator, creator, creation, creation, version, []);
const schema_2 = schema_1.publish(modifier, newVersion, modified);
expect(schema_2.isPublished).toBeTruthy();
expect(schema_2.lastModified).toEqual(modified);
expect(schema_2.lastModifiedBy).toEqual(modifier);
expect(schema_2.version).toEqual(newVersion);
});
it('should update isPublished property and user info when unpublishing', () => {
const schema_1 = new SchemaDetailsDto('1', 'name', properties, true, creator, creator, creation, creation, version, []);
const schema_2 = schema_1.unpublish(modifier, newVersion, modified);
expect(schema_2.isPublished).toBeFalsy();
expect(schema_2.lastModified).toEqual(modified);
expect(schema_2.lastModifiedBy).toEqual(modifier);
expect(schema_2.version).toEqual(newVersion);
});
it('should update properties property and user info when updating', () => {
const newProperties = new SchemaPropertiesDto('New Name');
const schema_1 = new SchemaDetailsDto('1', 'name', properties, false, creator, creator, creation, creation, version, []);
const schema_2 = schema_1.update(newProperties, modifier, newVersion, modified);
expect(schema_2.properties).toEqual(newProperties);
expect(schema_2.lastModified).toEqual(modified);
expect(schema_2.lastModifiedBy).toEqual(modifier);
expect(schema_2.version).toEqual(newVersion);
});
it('should update fields property and user info when adding field', () => {
const field1 = new FieldDto(1, '1', false, false, false, 'l', createProperties('String'));
const field2 = new FieldDto(2, '2', false, false, false, 'l', createProperties('Number'));
const schema_1 = new SchemaDetailsDto('1', 'name', properties, false, creator, creator, creation, creation, version, [field1]);
const schema_2 = schema_1.addField(field2, modifier, newVersion, modified);
expect(schema_2.fields).toEqual([field1, field2]);
expect(schema_2.lastModified).toEqual(modified);
expect(schema_2.lastModifiedBy).toEqual(modifier);
expect(schema_2.version).toEqual(newVersion);
});
it('should update fields property and user info when removing field', () => {
const field1 = new FieldDto(1, '1', false, false, false, 'l', createProperties('String'));
const field2 = new FieldDto(2, '2', false, false, false, 'l', createProperties('Number'));
const schema_1 = new SchemaDetailsDto('1', 'name', properties, false, creator, creator, creation, creation, version, [field1, field2]);
const schema_2 = schema_1.removeField(field1, modifier, newVersion, modified);
expect(schema_2.fields).toEqual([field2]);
expect(schema_2.lastModified).toEqual(modified);
expect(schema_2.lastModifiedBy).toEqual(modifier);
expect(schema_2.version).toEqual(newVersion);
});
it('should update fields property and user info when replacing fields', () => {
const field1 = new FieldDto(1, '1', false, false, false, 'l', createProperties('String'));
const field2 = new FieldDto(2, '2', false, false, false, 'l', createProperties('Number'));
const schema_1 = new SchemaDetailsDto('1', 'name', properties, false, creator, creator, creation, creation, version, [field1, field2]);
const schema_2 = schema_1.replaceFields([field2, field1], modifier, newVersion, modified);
expect(schema_2.fields).toEqual([field2, field1]);
expect(schema_2.lastModified).toEqual(modified);
expect(schema_2.lastModifiedBy).toEqual(modifier);
expect(schema_2.version).toEqual(newVersion);
});
it('should update fields property and user info when updating field', () => {
const field1_0 = new FieldDto(1, '1', false, false, false, 'l', createProperties('String'));
const field2_1 = new FieldDto(2, '2', false, false, false, 'l', createProperties('Number'));
const field2_2 = new FieldDto(2, '2', false, false, false, 'l', createProperties('Boolean'));
const schema_1 = new SchemaDetailsDto('1', 'name', properties, false, creator, creator, creation, creation, version, [field1_0, field2_1]);
const schema_2 = schema_1.updateField(field2_2, modifier, newVersion, modified);
expect(schema_2.fields).toEqual([field1_0, field2_2]);
expect(schema_2.lastModified).toEqual(modified);
expect(schema_2.lastModifiedBy).toEqual(modifier);
expect(schema_2.version).toEqual(newVersion);
});
});
describe('SchemasService', () => {
const now = DateTime.now();
const user = 'me';

Loading…
Cancel
Save