Skip to content

Commit 574a178

Browse files
authored
Merge pull request #2402 from umbraco/v15.1/chore/state-manager-tests
Chore: Add unit tests for the state manager
2 parents 4b63ac9 + 19d7cd1 commit 574a178

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { UmbStateManager } from './state.manager.js';
2+
import { expect } from '@open-wc/testing';
3+
import { Observable } from '@umbraco-cms/backoffice/external/rxjs';
4+
import { customElement } from '@umbraco-cms/backoffice/external/lit';
5+
import { UmbControllerHostElementMixin } from '@umbraco-cms/backoffice/controller-api';
6+
7+
@customElement('test-my-controller-host')
8+
class UmbTestControllerHostElement extends UmbControllerHostElementMixin(HTMLElement) {}
9+
10+
describe('UmbSelectionManager', () => {
11+
let manager: UmbStateManager;
12+
const state1 = { unique: '1', message: 'State 1' };
13+
const state2 = { unique: '2', message: 'State 2' };
14+
15+
beforeEach(() => {
16+
const hostElement = new UmbTestControllerHostElement();
17+
manager = new UmbStateManager(hostElement);
18+
});
19+
20+
describe('Public API', () => {
21+
describe('properties', () => {
22+
it('has a states property', () => {
23+
expect(manager).to.have.property('states').to.be.an.instanceOf(Observable);
24+
});
25+
26+
it('has a isOn property', () => {
27+
expect(manager).to.have.property('isOn').to.be.an.instanceOf(Observable);
28+
});
29+
30+
it('has a isOff property', () => {
31+
expect(manager).to.have.property('isOff').to.be.an.instanceOf(Observable);
32+
});
33+
});
34+
35+
describe('methods', () => {
36+
it('has a addState method', () => {
37+
expect(manager).to.have.property('addState').that.is.a('function');
38+
});
39+
40+
it('has a addStates method', () => {
41+
expect(manager).to.have.property('addStates').that.is.a('function');
42+
});
43+
44+
it('has a removeState method', () => {
45+
expect(manager).to.have.property('removeState').that.is.a('function');
46+
});
47+
48+
it('has a getStates method', () => {
49+
expect(manager).to.have.property('getStates').that.is.a('function');
50+
});
51+
52+
it('has a clear method', () => {
53+
expect(manager).to.have.property('clear').that.is.a('function');
54+
});
55+
});
56+
});
57+
58+
describe('Add State', () => {
59+
it('throws an error if the state does not have a unique property', () => {
60+
// @ts-ignore - Testing invalid input
61+
expect(() => manager.addState({ message: 'State 1' })).to.throw();
62+
});
63+
64+
it('adds a single state to the states array', () => {
65+
manager.addState(state1);
66+
expect(manager.getStates()).to.deep.equal([state1]);
67+
});
68+
69+
it('adds multiple states to the states array', () => {
70+
manager.addStates([state1, state2]);
71+
expect(manager.getStates()).to.deep.equal([state1, state2]);
72+
});
73+
74+
it('updates the observable', (done) => {
75+
manager.addState(state1);
76+
77+
manager.states
78+
.subscribe((value) => {
79+
expect(value[0]).to.equal(state1);
80+
done();
81+
})
82+
.unsubscribe();
83+
});
84+
});
85+
86+
describe('Remove State', () => {
87+
beforeEach(() => {
88+
manager.addStates([state1, state2]);
89+
});
90+
91+
it('removes a single state from the states array', () => {
92+
manager.removeState('1');
93+
expect(manager.getStates()).to.deep.equal([state2]);
94+
});
95+
96+
it('removes multiple states from the states array', () => {
97+
manager.removeStates(['1', '2']);
98+
expect(manager.getStates()).to.deep.equal([]);
99+
});
100+
101+
it('updates the observable', (done) => {
102+
manager.removeState('1');
103+
104+
manager.states
105+
.subscribe((value) => {
106+
expect(value).to.deep.equal([state2]);
107+
done();
108+
})
109+
.unsubscribe();
110+
});
111+
});
112+
113+
describe('Get States', () => {
114+
it('returns all states', () => {
115+
manager.addStates([state1, state2]);
116+
expect(manager.getStates()).to.deep.equal([state1, state2]);
117+
});
118+
});
119+
120+
describe('Clear', () => {
121+
beforeEach(() => {
122+
manager.addStates([state1, state2]);
123+
});
124+
125+
it('clears all states', () => {
126+
manager.clear();
127+
expect(manager.getStates()).to.deep.equal([]);
128+
});
129+
130+
it('updates the observable', (done) => {
131+
manager.clear();
132+
133+
manager.states
134+
.subscribe((value) => {
135+
expect(value).to.deep.equal([]);
136+
done();
137+
})
138+
.unsubscribe();
139+
});
140+
});
141+
});

0 commit comments

Comments
 (0)