123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- module JasmineTest {
- import ObservableStringDictionary = BABYLON.ObservableStringDictionary;
- import PropertyChangedBase = BABYLON.PropertyChangedBase;
- import DictionaryChanged = BABYLON.DictionaryChanged;
- class Customer extends PropertyChangedBase {
- constructor(firstName: string, lastName: string) {
- super();
- this._firstName = firstName;
- this._lastName = lastName;
- }
- public get firstName(): string {
- return this._firstName;
- }
- public set firstName(value: string) {
- if (this._firstName === value) {
- return;
- }
- let old = this._firstName;
- let oldDN = this.displayName;
- this._firstName = value;
- this.onPropertyChanged("firstName", old, value);
- this.onPropertyChanged("displayName", oldDN, this.displayName);
- }
- public get lastName(): string {
- return this._lastName;
- }
- public set lastName(value: string) {
- if (this._lastName === value) {
- return;
- }
- let old = this._lastName;
- let oldDN = this.displayName;
- this._lastName = value;
- this.onPropertyChanged("lastName", old, value);
- this.onPropertyChanged("displayName", oldDN, this.displayName);
- }
- public get displayName(): string {
- return this.firstName + " " + this.lastName;
- }
- private _firstName: string;
- private _lastName: string;
- }
- describe("Tools - ObservableDictionary",
- () => {
- it("Add",
- () => {
- let oa = new ObservableStringDictionary<Customer>(true);
- oa.add("cust0", new Customer("loic", "baumann"));
- oa.propertyChanged.add((e, c) => {
- expect(e.oldValue).toBe(1, "PropChanged length is bad");
- expect(e.newValue).toBe(2, "PropChanged length is bad");
- });
- oa.dictionaryChanged.add((e, c) => {
- expect(e.action).toEqual(DictionaryChanged.newItemAction);
- let item = e.newItem;
- expect(item.key).toEqual("cust1");
- expect(item.value.firstName).toEqual("david");
- expect(item.value.lastName).toEqual("catuhe");
- });
- oa.add("cust1", new Customer("david", "catuhe"));
- expect(oa.count).toBe(2);
- let cust = oa.get("cust1");
- expect(cust).toBeDefined();
- expect(cust.firstName).toEqual("david");
- expect(cust.lastName).toEqual("catuhe");
- }
- );
- it("Remove",
- () => {
- let oa = new ObservableStringDictionary<Customer>(true);
- let cust0 = new Customer("loic", "baumann");
- let cust1 = new Customer("david", "catuhe");
- oa.add("cust0", cust0);
- oa.add("cust1", cust1);
- oa.propertyChanged.add((e, c) => {
- expect(e.oldValue).toBe(2, "PropChanged length is bad");
- expect(e.newValue).toBe(1, "PropChanged length is bad");
- });
- oa.dictionaryChanged.add((e, c) => {
- expect(e.action).toEqual(DictionaryChanged.removedItemAction);
- let key = e.removedKey;
- expect(key).toEqual("cust0");
- });
- oa.watchedObjectChanged.add((e, c) => {
- fail("watchedObject shouldn't be called as only a removed object had a property changed");
- });
- expect(oa.count).toBe(2);
- let cust = oa.get("cust1");
- expect(cust).toBeDefined();
- expect(cust.firstName).toEqual("david");
- expect(cust.lastName).toEqual("catuhe");
- oa.remove("cust0");
- cust = oa.get("cust0");
- expect(cust).toBeUndefined();
- cust0.firstName = "nockawa";
- }
- );
- it("Watched Element",
- () => {
- let oa = new ObservableStringDictionary<Customer>(true);
- oa.add("cust0", new Customer("loic", "baumann"));
- oa.add("cust1", new Customer("david", "catuhe"));
- let triggerCounter = 0;
- oa.watchedObjectChanged.add((e, c) => {
- if (triggerCounter === 0) {
- expect(e.key).toBe("cust1");
- expect(e.propertyChanged.propertyName).toBe("firstName");
- expect(e.propertyChanged.oldValue).toBe("david");
- expect(e.propertyChanged.newValue).toBe("delta");
- ++triggerCounter;
- } else {
- expect(e.key).toBe("cust1");
- expect(e.propertyChanged.propertyName).toBe("displayName");
- expect(e.propertyChanged.oldValue).toBe("david catuhe");
- expect(e.propertyChanged.newValue).toBe("delta catuhe");
- ++triggerCounter;
- }
- });
- let cust = oa.get("cust1");
- cust.firstName = "delta";
- }
- );
- }
- );
- }
|