-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2402 from umbraco/v15.1/chore/state-manager-tests
Chore: Add unit tests for the state manager
- Loading branch information
Showing
1 changed file
with
141 additions
and
0 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
src/packages/core/utils/state-manager/state.manager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { UmbStateManager } from './state.manager.js'; | ||
import { expect } from '@open-wc/testing'; | ||
import { Observable } from '@umbraco-cms/backoffice/external/rxjs'; | ||
import { customElement } from '@umbraco-cms/backoffice/external/lit'; | ||
import { UmbControllerHostElementMixin } from '@umbraco-cms/backoffice/controller-api'; | ||
|
||
@customElement('test-my-controller-host') | ||
class UmbTestControllerHostElement extends UmbControllerHostElementMixin(HTMLElement) {} | ||
|
||
describe('UmbSelectionManager', () => { | ||
let manager: UmbStateManager; | ||
const state1 = { unique: '1', message: 'State 1' }; | ||
const state2 = { unique: '2', message: 'State 2' }; | ||
|
||
beforeEach(() => { | ||
const hostElement = new UmbTestControllerHostElement(); | ||
manager = new UmbStateManager(hostElement); | ||
}); | ||
|
||
describe('Public API', () => { | ||
describe('properties', () => { | ||
it('has a states property', () => { | ||
expect(manager).to.have.property('states').to.be.an.instanceOf(Observable); | ||
}); | ||
|
||
it('has a isOn property', () => { | ||
expect(manager).to.have.property('isOn').to.be.an.instanceOf(Observable); | ||
}); | ||
|
||
it('has a isOff property', () => { | ||
expect(manager).to.have.property('isOff').to.be.an.instanceOf(Observable); | ||
}); | ||
}); | ||
|
||
describe('methods', () => { | ||
it('has a addState method', () => { | ||
expect(manager).to.have.property('addState').that.is.a('function'); | ||
}); | ||
|
||
it('has a addStates method', () => { | ||
expect(manager).to.have.property('addStates').that.is.a('function'); | ||
}); | ||
|
||
it('has a removeState method', () => { | ||
expect(manager).to.have.property('removeState').that.is.a('function'); | ||
}); | ||
|
||
it('has a getStates method', () => { | ||
expect(manager).to.have.property('getStates').that.is.a('function'); | ||
}); | ||
|
||
it('has a clear method', () => { | ||
expect(manager).to.have.property('clear').that.is.a('function'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Add State', () => { | ||
it('throws an error if the state does not have a unique property', () => { | ||
// @ts-ignore - Testing invalid input | ||
expect(() => manager.addState({ message: 'State 1' })).to.throw(); | ||
}); | ||
|
||
it('adds a single state to the states array', () => { | ||
manager.addState(state1); | ||
expect(manager.getStates()).to.deep.equal([state1]); | ||
}); | ||
|
||
it('adds multiple states to the states array', () => { | ||
manager.addStates([state1, state2]); | ||
expect(manager.getStates()).to.deep.equal([state1, state2]); | ||
}); | ||
|
||
it('updates the observable', (done) => { | ||
manager.addState(state1); | ||
|
||
manager.states | ||
.subscribe((value) => { | ||
expect(value[0]).to.equal(state1); | ||
done(); | ||
}) | ||
.unsubscribe(); | ||
}); | ||
}); | ||
|
||
describe('Remove State', () => { | ||
beforeEach(() => { | ||
manager.addStates([state1, state2]); | ||
}); | ||
|
||
it('removes a single state from the states array', () => { | ||
manager.removeState('1'); | ||
expect(manager.getStates()).to.deep.equal([state2]); | ||
}); | ||
|
||
it('removes multiple states from the states array', () => { | ||
manager.removeStates(['1', '2']); | ||
expect(manager.getStates()).to.deep.equal([]); | ||
}); | ||
|
||
it('updates the observable', (done) => { | ||
manager.removeState('1'); | ||
|
||
manager.states | ||
.subscribe((value) => { | ||
expect(value).to.deep.equal([state2]); | ||
done(); | ||
}) | ||
.unsubscribe(); | ||
}); | ||
}); | ||
|
||
describe('Get States', () => { | ||
it('returns all states', () => { | ||
manager.addStates([state1, state2]); | ||
expect(manager.getStates()).to.deep.equal([state1, state2]); | ||
}); | ||
}); | ||
|
||
describe('Clear', () => { | ||
beforeEach(() => { | ||
manager.addStates([state1, state2]); | ||
}); | ||
|
||
it('clears all states', () => { | ||
manager.clear(); | ||
expect(manager.getStates()).to.deep.equal([]); | ||
}); | ||
|
||
it('updates the observable', (done) => { | ||
manager.clear(); | ||
|
||
manager.states | ||
.subscribe((value) => { | ||
expect(value).to.deep.equal([]); | ||
done(); | ||
}) | ||
.unsubscribe(); | ||
}); | ||
}); | ||
}); |