mirror of https://github.com/Squidex/squidex.git
8 changed files with 30 additions and 85 deletions
@ -1,38 +0,0 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Sebastian Stehle. All rights reserved |
|||
*/ |
|||
|
|||
import { ImmutableObject } from './../'; |
|||
|
|||
class MockupObject extends ImmutableObject { |
|||
constructor( |
|||
public property1: string, |
|||
public property2: string |
|||
) { |
|||
super(); |
|||
} |
|||
|
|||
public changeProperty1(newValue: string): MockupObject { |
|||
return super.cloned<MockupObject>((x: MockupObject) => x.property1 = newValue); |
|||
} |
|||
|
|||
public clone(): ImmutableObject { |
|||
return new MockupObject(this.property1, this.property2); |
|||
} |
|||
} |
|||
|
|||
describe('ImmutableObject', () => { |
|||
it('should create new instance on update', () => { |
|||
const oldObj = new MockupObject('old1', 'old2'); |
|||
const newObj = oldObj.changeProperty1('new1'); |
|||
|
|||
expect(oldObj.property1).toBe('old1'); |
|||
expect(oldObj.property2).toBe('old2'); |
|||
|
|||
expect(newObj.property1).toBe('new1'); |
|||
expect(newObj.property2).toBe('old2'); |
|||
}); |
|||
}); |
|||
@ -1,24 +0,0 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Sebastian Stehle. All rights reserved |
|||
*/ |
|||
|
|||
/* tslint:disable:no-empty */ |
|||
|
|||
export abstract class ImmutableObject { |
|||
public abstract clone(): ImmutableObject; |
|||
|
|||
protected afterClone() { } |
|||
|
|||
protected cloned<T extends ImmutableObject>(updater: (instance: ImmutableObject) => void) { |
|||
const cloned = <T>this.clone(); |
|||
|
|||
updater(cloned); |
|||
|
|||
cloned.afterClone(); |
|||
|
|||
return cloned; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue