Merge remote-tracking branch 'origin/main' into feature/manifest-conditions

# Conflicts:
#	src/backoffice/shared/components/section/section-views/section-views.element.ts
This commit is contained in:
Niels Lyngsø
2023-03-16 10:23:45 +01:00
13 changed files with 115 additions and 88 deletions

View File

@@ -2,8 +2,7 @@ import { expect } from '@open-wc/testing';
import { ArrayState } from './array-state';
describe('ArrayState', () => {
type ObjectType = {key: string, another: string};
type ObjectType = { key: string; another: string };
type ArrayType = ObjectType[];
let subject: ArrayState<ObjectType>;
@@ -11,29 +10,30 @@ describe('ArrayState', () => {
beforeEach(() => {
initialData = [
{key: '1', another: 'myValue1'},
{key: '2', another: 'myValue2'},
{key: '3', another: 'myValue3'}
{ key: '1', another: 'myValue1' },
{ key: '2', another: 'myValue2' },
{ key: '3', another: 'myValue3' },
];
subject = new ArrayState(initialData, x => x.key);
subject = new ArrayState(initialData, (x) => x.key);
});
it('replays latests, no matter the amount of subscriptions.', (done) => {
let amountOfCallbacks = 0;
const observer = subject.asObservable();
observer.subscribe((value) => {
amountOfCallbacks++;
expect(value).to.be.equal(initialData);
});
observer.subscribe((value) => {
amountOfCallbacks++;
expect(value).to.be.equal(initialData);
done();
if (amountOfCallbacks === 2) {
done();
}
});
});
it('remove method, removes the one with the key', (done) => {
const expectedData = [initialData[0], initialData[2]];
subject.remove(['2']);
@@ -42,25 +42,21 @@ describe('ArrayState', () => {
expect(JSON.stringify(value)).to.be.equal(JSON.stringify(expectedData));
done();
});
});
it('filter method, removes anything that is not true of the given predicate method', (done) => {
const expectedData = [initialData[0], initialData[2]];
subject.filter(x => x.key !== '2');
subject.filter((x) => x.key !== '2');
const observer = subject.asObservable();
observer.subscribe((value) => {
expect(JSON.stringify(value)).to.be.equal(JSON.stringify(expectedData));
done();
});
});
it('add new item via appendOne method.', (done) => {
const newItem = {key: '4', another: 'myValue4'};
const newItem = { key: '4', another: 'myValue4' };
subject.appendOne(newItem);
const expectedData = [...initialData, newItem];
@@ -71,37 +67,31 @@ describe('ArrayState', () => {
expect(value[3].another).to.be.equal(expectedData[3].another);
done();
});
});
it('getObservablePart for a specific entry of array', (done) => {
const subObserver = subject.getObservablePart(data => data.find(x => x.key === '2'));
const subObserver = subject.getObservablePart((data) => data.find((x) => x.key === '2'));
subObserver.subscribe((entry) => {
if(entry) {
if (entry) {
expect(entry.another).to.be.equal(initialData[1].another);
done();
}
});
});
it('getObservablePart returns undefined if item does not exist', (done) => {
let amountOfCallbacks = 0;
const newItem = {key: '4', another: 'myValue4'};
const newItem = { key: '4', another: 'myValue4' };
const subObserver = subject.getObservablePart(data => data.find(x => x.key === newItem.key));
const subObserver = subject.getObservablePart((data) => data.find((x) => x.key === newItem.key));
subObserver.subscribe((entry) => {
amountOfCallbacks++;
if(amountOfCallbacks === 1) {
expect(entry).to.be.equal(undefined);// First callback should give null, cause we didn't have this entry when the subscription was made.
if (amountOfCallbacks === 1) {
expect(entry).to.be.equal(undefined); // First callback should give null, cause we didn't have this entry when the subscription was made.
}
if(amountOfCallbacks === 2) {
expect(entry).to.be.equal(newItem);// Second callback should give us the right data:
if(entry) {
if (amountOfCallbacks === 2) {
expect(entry).to.be.equal(newItem); // Second callback should give us the right data:
if (entry) {
expect(entry.another).to.be.equal(newItem.another);
done();
}
@@ -109,13 +99,10 @@ describe('ArrayState', () => {
});
subject.appendOne(newItem);
});
it('asObservable returns the replaced item', (done) => {
const newItem = {key: '2', another: 'myValue4'};
const newItem = { key: '2', another: 'myValue4' };
subject.appendOne(newItem);
const expectedData = [initialData[0], newItem, initialData[2]];
@@ -126,24 +113,61 @@ describe('ArrayState', () => {
expect(value[1].another).to.be.equal(newItem.another);
done();
});
});
it('getObservablePart returns the replaced item', (done) => {
const newItem = {key: '2', another: 'myValue4'};
const newItem = { key: '2', another: 'myValue4' };
subject.appendOne(newItem);
const subObserver = subject.getObservablePart(data => data.find(x => x.key === newItem.key));
const subObserver = subject.getObservablePart((data) => data.find((x) => x.key === newItem.key));
subObserver.subscribe((entry) => {
expect(entry).to.be.equal(newItem);// Second callback should give us the right data:
if(entry) {
expect(entry).to.be.equal(newItem); // Second callback should give us the right data:
if (entry) {
expect(entry.another).to.be.equal(newItem.another);
done();
}
});
});
it('getObservablePart replays existing data to any amount of subscribers.', (done) => {
let amountOfCallbacks = 0;
const subObserver = subject.getObservablePart((data) => data.find((x) => x.key === '2'));
subObserver.subscribe((entry) => {
if (entry) {
amountOfCallbacks++;
expect(entry.another).to.be.equal(initialData[1].another);
}
});
subObserver.subscribe((entry) => {
if (entry) {
amountOfCallbacks++;
expect(entry.another).to.be.equal(initialData[1].another);
if (amountOfCallbacks === 2) {
done();
}
}
});
});
it('getObservablePart replays existing data to any amount of subscribers.', (done) => {
let amountOfCallbacks = 0;
const subObserver = subject.getObservablePart((data) => data.find((x) => x.key === '2'));
subObserver.subscribe((entry) => {
if (entry) {
amountOfCallbacks++;
expect(entry.another).to.be.equal(initialData[1].another);
}
});
subObserver.subscribe((entry) => {
if (entry) {
amountOfCallbacks++;
expect(entry.another).to.be.equal(initialData[1].another);
if (amountOfCallbacks === 2) {
done();
}
}
});
});
});

View File

@@ -13,6 +13,7 @@ export class UmbObserverController<T = unknown> extends UmbObserver<T> implement
this._alias = alias;
// Lets check if controller is already here:
// No we don't want this, as multiple different controllers might be looking at the same source.
/*
if (this._subscriptions.has(source)) {
const subscription = this._subscriptions.get(source);
@@ -22,8 +23,4 @@ export class UmbObserverController<T = unknown> extends UmbObserver<T> implement
host.addController(this);
}
hostConnected() {
return;
}
}

View File

@@ -1,15 +1,20 @@
import { Observable, Subscription } from 'rxjs';
export class UmbObserver<T> {
#source!: Observable<T>;
#callback!: (_value: T) => void;
#subscription!: Subscription;
constructor(source: Observable<T>, callback: (_value: T) => void) {
this.#subscription = source.subscribe((value) => {
callback(value);
});
this.#source = source;
this.#subscription = source.subscribe(callback);
}
// Notice controller class implements empty hostConnected().
hostConnected() {
if (this.#subscription.closed) {
this.#subscription = this.#source.subscribe(this.#callback);
}
}
hostDisconnected() {
this.#subscription.unsubscribe();