Files
Umbraco-CMS/src/Umbraco.Web.UI.Client/libs/observable-api/deep-state.test.ts
2023-01-27 13:47:20 +01:00

61 lines
1.5 KiB
TypeScript

import { expect } from '@open-wc/testing';
import { DeepState } from './deep-state';
describe('DeepState', () => {
type ObjectType = {key: string, another: string};
let subject: DeepState<ObjectType>;
let initialData: ObjectType;
beforeEach(() => {
initialData = {key: 'some', another: 'myValue'};
subject = new DeepState(initialData);
});
it('getValue gives the initial data', () => {
expect(subject.value.another).to.be.equal(initialData.another);
});
it('update via next', () => {
subject.next({key: 'some', another: 'myNewValue'});
expect(subject.value.another).to.be.equal('myNewValue');
});
it('replays latests, no matter the amount of subscriptions.', (done) => {
const observer = subject.asObservable();
observer.subscribe((value) => {
expect(value).to.be.equal(initialData);
});
observer.subscribe((value) => {
expect(value).to.be.equal(initialData);
done();
});
});
it('use gObservablePart, updates on its specific change.', (done) => {
let amountOfCallbacks = 0;
const subObserver = subject.getObservablePart(data => data.another);
subObserver.subscribe((value) => {
amountOfCallbacks++;
if(amountOfCallbacks === 1) {
expect(value).to.be.equal('myValue');
}
if(amountOfCallbacks === 2) {
expect(value).to.be.equal('myNewValue');
done();
}
});
subject.next({key: 'change_this_first_should_not_trigger_update', another: 'myValue'});
subject.next({key: 'some', another: 'myNewValue'});
});
});