Files
Umbraco-CMS/src/Umbraco.Web.UI.Client/libs/observable-api/array-state.test.ts

150 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-01-20 14:54:06 +01:00
import { expect } from '@open-wc/testing';
2023-01-24 13:40:25 +01:00
import { ArrayState } from './array-state';
2023-01-20 14:54:06 +01:00
2023-01-24 13:40:25 +01:00
describe('ArrayState', () => {
2023-01-20 14:54:06 +01:00
type ObjectType = {key: string, another: string};
type ArrayType = ObjectType[];
2023-01-24 13:40:25 +01:00
let subject: ArrayState<ObjectType>;
2023-01-20 14:54:06 +01:00
let initialData: ArrayType;
beforeEach(() => {
initialData = [
{key: '1', another: 'myValue1'},
{key: '2', another: 'myValue2'},
{key: '3', another: 'myValue3'}
];
2023-01-24 13:40:25 +01:00
subject = new ArrayState(initialData, x => x.key);
2023-01-20 14:54:06 +01:00
});
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();
});
});
2023-01-25 09:59:24 +01:00
it('remove method, removes the one with the key', (done) => {
const expectedData = [initialData[0], initialData[2]];
subject.remove(['2']);
const observer = subject.asObservable();
observer.subscribe((value) => {
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');
const observer = subject.asObservable();
observer.subscribe((value) => {
expect(JSON.stringify(value)).to.be.equal(JSON.stringify(expectedData));
done();
});
});
2023-01-20 14:54:06 +01:00
it('add new item via appendOne method.', (done) => {
const newItem = {key: '4', another: 'myValue4'};
subject.appendOne(newItem);
2023-01-25 09:59:24 +01:00
const expectedData = [...initialData, newItem];
2023-01-20 14:54:06 +01:00
const observer = subject.asObservable();
observer.subscribe((value) => {
expect(value.length).to.be.equal(expectedData.length);
expect(value[3].another).to.be.equal(expectedData[3].another);
done();
});
});
2023-01-27 13:47:20 +01:00
it('getObservablePart for a specific entry of array', (done) => {
2023-01-20 14:54:06 +01:00
2023-01-27 13:47:20 +01:00
const subObserver = subject.getObservablePart(data => data.find(x => x.key === '2'));
2023-01-20 14:54:06 +01:00
subObserver.subscribe((entry) => {
if(entry) {
expect(entry.another).to.be.equal(initialData[1].another);
done();
}
});
});
2023-01-27 13:47:20 +01:00
it('getObservablePart returns undefined if item does not exist', (done) => {
2023-01-20 14:54:06 +01:00
let amountOfCallbacks = 0;
const newItem = {key: '4', another: 'myValue4'};
2023-01-27 13:47:20 +01:00
const subObserver = subject.getObservablePart(data => data.find(x => x.key === newItem.key));
2023-01-20 14:54:06 +01:00
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 === 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();
}
}
});
subject.appendOne(newItem);
});
it('asObservable returns the replaced item', (done) => {
const newItem = {key: '2', another: 'myValue4'};
subject.appendOne(newItem);
const expectedData = [initialData[0], newItem, initialData[2]];
const observer = subject.asObservable();
observer.subscribe((value) => {
expect(value.length).to.be.equal(expectedData.length);
expect(value[1].another).to.be.equal(newItem.another);
done();
});
});
2023-01-27 13:47:20 +01:00
it('getObservablePart returns the replaced item', (done) => {
2023-01-20 14:54:06 +01:00
const newItem = {key: '2', another: 'myValue4'};
subject.appendOne(newItem);
2023-01-27 13:47:20 +01:00
const subObserver = subject.getObservablePart(data => data.find(x => x.key === newItem.key));
2023-01-20 14:54:06 +01:00
subObserver.subscribe((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();
}
});
});
});