Improve tests for UserSelection
- Refactor for more logic reuse - Adds more assertments for events
This commit is contained in:
@@ -1,265 +1,283 @@
|
|||||||
import 'mocha';
|
import 'mocha';
|
||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import { IScript } from '@/domain/IScript';
|
|
||||||
import { SelectedScript } from '@/application/Context/State/Selection/SelectedScript';
|
import { SelectedScript } from '@/application/Context/State/Selection/SelectedScript';
|
||||||
import { UserSelection } from '@/application/Context/State/Selection/UserSelection';
|
import { UserSelection } from '@/application/Context/State/Selection/UserSelection';
|
||||||
import { CategoryStub } from '@tests/unit/stubs/CategoryStub';
|
import { CategoryStub } from '@tests/unit/stubs/CategoryStub';
|
||||||
import { CategoryCollectionStub } from '@tests/unit/stubs/CategoryCollectionStub';
|
import { CategoryCollectionStub } from '@tests/unit/stubs/CategoryCollectionStub';
|
||||||
import { SelectedScriptStub } from '@tests/unit/stubs/SelectedScriptStub';
|
import { SelectedScriptStub } from '@tests/unit/stubs/SelectedScriptStub';
|
||||||
import { ScriptStub } from '@tests/unit/stubs/ScriptStub';
|
import { ScriptStub } from '@tests/unit/stubs/ScriptStub';
|
||||||
|
import { UserSelectionTestRunner } from './UserSelectionTestRunner';
|
||||||
|
|
||||||
describe('UserSelection', () => {
|
describe('UserSelection', () => {
|
||||||
describe('ctor', () => {
|
describe('ctor', () => {
|
||||||
it('has nothing with no initial selection', () => {
|
describe('has nothing with no initial selection', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const collection = new CategoryCollectionStub().withAction(new CategoryStub(1).withScriptIds('s1'));
|
const allScripts = [
|
||||||
const selection = [];
|
new SelectedScriptStub('s1', false),
|
||||||
|
];
|
||||||
|
new UserSelectionTestRunner()
|
||||||
|
.withSelectedScripts([])
|
||||||
|
.withCategory(1, allScripts.map((s) => s.script))
|
||||||
// act
|
// act
|
||||||
const sut = new UserSelection(collection, selection);
|
.run()
|
||||||
// assert
|
// assert
|
||||||
expect(sut.selectedScripts).to.have.lengthOf(0);
|
.expectFinalScripts([]);
|
||||||
});
|
});
|
||||||
it('has initial selection', () => {
|
describe('has initial selection', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const firstScript = new ScriptStub('1');
|
const scripts = [
|
||||||
const secondScript = new ScriptStub('2');
|
new SelectedScriptStub('s1', false),
|
||||||
const collection = new CategoryCollectionStub()
|
new SelectedScriptStub('s2', false),
|
||||||
.withAction(new CategoryStub(1).withScript(firstScript).withScripts(secondScript));
|
];
|
||||||
const expected = [ new SelectedScript(firstScript, false), new SelectedScript(secondScript, true) ];
|
new UserSelectionTestRunner()
|
||||||
|
.withSelectedScripts(scripts)
|
||||||
|
.withCategory(1, scripts.map((s) => s.script))
|
||||||
// act
|
// act
|
||||||
const sut = new UserSelection(collection, expected);
|
.run()
|
||||||
// assert
|
// assert
|
||||||
expect(sut.selectedScripts).to.deep.include(expected[0]);
|
.expectFinalScripts(scripts);
|
||||||
expect(sut.selectedScripts).to.deep.include(expected[1]);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('deselectAll removes all items', () => {
|
describe('deselectAll removes all items', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const events: Array<readonly SelectedScript[]> = [];
|
const allScripts = [
|
||||||
const collection = new CategoryCollectionStub()
|
new SelectedScriptStub('s1', false),
|
||||||
.withAction(new CategoryStub(1)
|
new SelectedScriptStub('s2', false),
|
||||||
.withScriptIds('s1', 's2', 's3', 's4'));
|
new SelectedScriptStub('s3', false),
|
||||||
const selectedScripts = [
|
new SelectedScriptStub('s4', false),
|
||||||
new SelectedScriptStub('s1'), new SelectedScriptStub('s2'), new SelectedScriptStub('s3'),
|
|
||||||
];
|
];
|
||||||
const sut = new UserSelection(collection, selectedScripts);
|
const selectedScripts = allScripts.filter(
|
||||||
sut.changed.on((newScripts) => events.push(newScripts));
|
(s) => ['s1', 's2', 's3'].includes(s.id));
|
||||||
|
new UserSelectionTestRunner()
|
||||||
|
.withSelectedScripts(selectedScripts)
|
||||||
|
.withCategory(1, allScripts.map((s) => s.script))
|
||||||
// act
|
// act
|
||||||
sut.deselectAll();
|
.run((sut) => {
|
||||||
|
sut.deselectAll();
|
||||||
|
})
|
||||||
// assert
|
// assert
|
||||||
expect(sut.selectedScripts).to.have.length(0);
|
.expectTotalFiredEvents(1)
|
||||||
expect(events).to.have.lengthOf(1);
|
.expectFinalScripts([])
|
||||||
expect(events[0]).to.have.length(0);
|
.expectFinalScriptsInEvent(0, []);
|
||||||
});
|
});
|
||||||
it('selectOnly selects expected', () => {
|
describe('selectOnly selects expected', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const events: Array<readonly SelectedScript[]> = [];
|
const allScripts = [
|
||||||
const collection = new CategoryCollectionStub()
|
new SelectedScriptStub('s1', false),
|
||||||
.withAction(new CategoryStub(1)
|
new SelectedScriptStub('s2', false),
|
||||||
.withScriptIds('s1', 's2', 's3', 's4'));
|
new SelectedScriptStub('s3', false),
|
||||||
const selectedScripts = [
|
new SelectedScriptStub('s4', false),
|
||||||
new SelectedScriptStub('s1'), new SelectedScriptStub('s2'), new SelectedScriptStub('s3'),
|
|
||||||
];
|
];
|
||||||
const sut = new UserSelection(collection, selectedScripts);
|
const selectedScripts = allScripts.filter(
|
||||||
sut.changed.on((newScripts) => events.push(newScripts));
|
(s) => ['s1', 's2', 's3'].includes(s.id));
|
||||||
const scripts = [new ScriptStub('s2'), new ScriptStub('s3'), new ScriptStub('s4')];
|
const scriptsToSelect = allScripts.filter(
|
||||||
const expected = [ new SelectedScriptStub('s2'), new SelectedScriptStub('s3'),
|
(s) => ['s2', 's3', 's4'].includes(s.id));
|
||||||
new SelectedScript(scripts[2], false)];
|
new UserSelectionTestRunner()
|
||||||
|
.withSelectedScripts(selectedScripts)
|
||||||
|
.withCategory(1, allScripts.map((s) => s.script))
|
||||||
// act
|
// act
|
||||||
sut.selectOnly(scripts);
|
.run((sut) => {
|
||||||
|
sut.selectOnly(scriptsToSelect.map((s) => s.script));
|
||||||
|
})
|
||||||
// assert
|
// assert
|
||||||
expect(sut.selectedScripts).to.have.deep.members(expected,
|
.expectTotalFiredEvents(1)
|
||||||
`Expected: ${JSON.stringify(sut.selectedScripts)}\n` +
|
.expectFinalScripts(scriptsToSelect)
|
||||||
`Actual: ${JSON.stringify(expected)}`);
|
.expectFinalScriptsInEvent(0, scriptsToSelect);
|
||||||
expect(events).to.have.lengthOf(1);
|
|
||||||
expect(events[0]).to.deep.equal(expected);
|
|
||||||
});
|
});
|
||||||
it('selectAll selects as expected', () => {
|
describe('selectAll selects as expected', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const events: Array<readonly SelectedScript[]> = [];
|
const expected = [
|
||||||
const scripts: IScript[] = [new ScriptStub('s1'), new ScriptStub('s2'), new ScriptStub('s3'), new ScriptStub('s4')];
|
new SelectedScriptStub('s1', false),
|
||||||
const collection = new CategoryCollectionStub()
|
new SelectedScriptStub('s2', false),
|
||||||
.withAction(new CategoryStub(1)
|
];
|
||||||
.withScripts(...scripts));
|
new UserSelectionTestRunner()
|
||||||
const sut = new UserSelection(collection, []);
|
.withSelectedScripts([])
|
||||||
sut.changed.on((newScripts) => events.push(newScripts));
|
.withCategory(1, expected.map((s) => s.script))
|
||||||
const expected = scripts.map((script) => new SelectedScript(script, false));
|
|
||||||
// act
|
// act
|
||||||
sut.selectAll();
|
.run((sut) => {
|
||||||
|
sut.selectAll();
|
||||||
|
})
|
||||||
// assert
|
// assert
|
||||||
expect(sut.selectedScripts).to.deep.equal(expected);
|
.expectTotalFiredEvents(1)
|
||||||
expect(events).to.have.lengthOf(1);
|
.expectFinalScripts(expected)
|
||||||
expect(events[0]).to.deep.equal(expected);
|
.expectFinalScriptsInEvent(0, expected);
|
||||||
});
|
});
|
||||||
describe('addOrUpdateSelectedScript', () => {
|
describe('addOrUpdateSelectedScript', () => {
|
||||||
it('adds when item does not exist', () => {
|
describe('adds when item does not exist', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const events: Array<readonly SelectedScript[]> = [];
|
const scripts = [new ScriptStub('s1'), new ScriptStub('s2')];
|
||||||
const collection = new CategoryCollectionStub()
|
const expected = [ new SelectedScript(scripts[0], false) ];
|
||||||
.withAction(new CategoryStub(1)
|
new UserSelectionTestRunner()
|
||||||
.withScripts(new ScriptStub('s1'), new ScriptStub('s2')));
|
.withSelectedScripts([])
|
||||||
const sut = new UserSelection(collection, []);
|
.withCategory(1, scripts)
|
||||||
sut.changed.on((scripts) => events.push(scripts));
|
|
||||||
const expected = [ new SelectedScript(new ScriptStub('s1'), false) ];
|
|
||||||
// act
|
// act
|
||||||
sut.addOrUpdateSelectedScript('s1', false);
|
.run((sut) => {
|
||||||
|
sut.addOrUpdateSelectedScript(scripts[0].id, false);
|
||||||
|
})
|
||||||
// assert
|
// assert
|
||||||
expect(sut.selectedScripts).to.deep.equal(expected);
|
.expectTotalFiredEvents(1)
|
||||||
expect(events).to.have.lengthOf(1);
|
.expectFinalScripts(expected)
|
||||||
expect(events[0]).to.deep.equal(expected);
|
.expectFinalScriptsInEvent(0, expected);
|
||||||
});
|
});
|
||||||
it('updates when item exists', () => {
|
describe('updates when item exists', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const events: Array<readonly SelectedScript[]> = [];
|
const scripts = [new ScriptStub('s1'), new ScriptStub('s2')];
|
||||||
const collection = new CategoryCollectionStub()
|
const existing = new SelectedScript(scripts[0], false);
|
||||||
.withAction(new CategoryStub(1)
|
const expected = new SelectedScript(scripts[0], true);
|
||||||
.withScripts(new ScriptStub('s1'), new ScriptStub('s2')));
|
new UserSelectionTestRunner()
|
||||||
const sut = new UserSelection(collection, []);
|
.withSelectedScripts([existing])
|
||||||
sut.changed.on((scripts) => events.push(scripts));
|
.withCategory(1, scripts)
|
||||||
const expected = [ new SelectedScript(new ScriptStub('s1'), true) ];
|
|
||||||
// act
|
// act
|
||||||
sut.addOrUpdateSelectedScript('s1', true);
|
.run((sut) => {
|
||||||
|
sut.addOrUpdateSelectedScript(expected.id, expected.revert);
|
||||||
|
})
|
||||||
// assert
|
// assert
|
||||||
expect(sut.selectedScripts).to.deep.equal(expected);
|
.expectTotalFiredEvents(1)
|
||||||
expect(events).to.have.lengthOf(1);
|
.expectFinalScripts([ expected ])
|
||||||
expect(events[0]).to.deep.equal(expected);
|
.expectFinalScriptsInEvent(0, [ expected ]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('removeAllInCategory', () => {
|
describe('removeAllInCategory', () => {
|
||||||
it('does nothing when nothing exists', () => {
|
describe('does nothing when nothing exists', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const events: Array<readonly SelectedScript[]> = [];
|
const categoryId = 99;
|
||||||
const categoryId = 1;
|
const scripts = [new ScriptStub('s1'), new ScriptStub('s2')];
|
||||||
const collection = new CategoryCollectionStub()
|
new UserSelectionTestRunner()
|
||||||
.withAction(new CategoryStub(categoryId)
|
.withSelectedScripts([])
|
||||||
.withScripts(new ScriptStub('s1'), new ScriptStub('s2')));
|
.withCategory(categoryId, scripts)
|
||||||
const sut = new UserSelection(collection, []);
|
|
||||||
sut.changed.on((s) => events.push(s));
|
|
||||||
// act
|
// act
|
||||||
sut.removeAllInCategory(categoryId);
|
.run((sut) => {
|
||||||
|
sut.removeAllInCategory(categoryId);
|
||||||
|
})
|
||||||
// assert
|
// assert
|
||||||
expect(events).to.have.lengthOf(0);
|
.expectTotalFiredEvents(0)
|
||||||
expect(sut.selectedScripts).to.have.lengthOf(0);
|
.expectFinalScripts([]);
|
||||||
});
|
});
|
||||||
it('removes all when all exists', () => {
|
describe('removes all when all exists', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const categoryId = 1;
|
const categoryId = 34;
|
||||||
const scripts = [new SelectedScriptStub('s1'), new SelectedScriptStub('s2')];
|
const scripts = [new SelectedScriptStub('s1'), new SelectedScriptStub('s2')];
|
||||||
const collection = new CategoryCollectionStub()
|
new UserSelectionTestRunner()
|
||||||
.withAction(new CategoryStub(categoryId)
|
.withSelectedScripts(scripts)
|
||||||
.withScripts(...scripts.map((script) => script.script)));
|
.withCategory(categoryId, scripts.map((s) => s.script))
|
||||||
const sut = new UserSelection(collection, scripts);
|
|
||||||
// act
|
// act
|
||||||
sut.removeAllInCategory(categoryId);
|
.run((sut) => {
|
||||||
|
sut.removeAllInCategory(categoryId);
|
||||||
|
})
|
||||||
// assert
|
// assert
|
||||||
expect(sut.selectedScripts.length).to.equal(0);
|
.expectTotalFiredEvents(1)
|
||||||
|
.expectFinalScripts([]);
|
||||||
});
|
});
|
||||||
it('removes existing some exists', () => {
|
describe('removes existing when some exists', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const categoryId = 1;
|
const categoryId = 55;
|
||||||
const existing = [new ScriptStub('s1'), new ScriptStub('s2')];
|
const existing = [new ScriptStub('s1'), new ScriptStub('s2')];
|
||||||
const notExisting = [new ScriptStub('s3'), new ScriptStub('s4')];
|
const notExisting = [new ScriptStub('s3'), new ScriptStub('s4')];
|
||||||
const collection = new CategoryCollectionStub()
|
new UserSelectionTestRunner()
|
||||||
.withAction(new CategoryStub(categoryId)
|
.withSelectedScripts(existing.map((script) => new SelectedScript(script, false)))
|
||||||
.withScripts(...existing, ...notExisting));
|
.withCategory(categoryId, [ ...existing, ...notExisting ])
|
||||||
const sut = new UserSelection(collection, existing.map((script) => new SelectedScript(script, false)));
|
|
||||||
// act
|
// act
|
||||||
sut.removeAllInCategory(categoryId);
|
.run((sut) => {
|
||||||
|
sut.removeAllInCategory(categoryId);
|
||||||
|
})
|
||||||
// assert
|
// assert
|
||||||
expect(sut.selectedScripts.length).to.equal(0);
|
.expectTotalFiredEvents(1)
|
||||||
|
.expectFinalScripts([]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('addOrUpdateAllInCategory', () => {
|
describe('addOrUpdateAllInCategory', () => {
|
||||||
it('does nothing when all already exists', () => {
|
describe('when all already exists', () => {
|
||||||
// arrange
|
describe('does nothing if nothing is changed', () => {
|
||||||
const events: Array<readonly SelectedScript[]> = [];
|
// arrange
|
||||||
const categoryId = 1;
|
const categoryId = 55;
|
||||||
const scripts = [new ScriptStub('s1'), new ScriptStub('s2')];
|
const existingScripts = [
|
||||||
const collection = new CategoryCollectionStub()
|
new SelectedScriptStub('s1', false),
|
||||||
.withAction(new CategoryStub(categoryId)
|
new SelectedScriptStub('s2', false),
|
||||||
.withScripts(...scripts));
|
];
|
||||||
const sut = new UserSelection(collection, scripts.map((script) => new SelectedScript(script, false)));
|
new UserSelectionTestRunner()
|
||||||
sut.changed.on((s) => events.push(s));
|
.withSelectedScripts(existingScripts)
|
||||||
// act
|
.withCategory(categoryId, existingScripts.map((s) => s.script))
|
||||||
sut.addOrUpdateAllInCategory(categoryId);
|
// act
|
||||||
// assert
|
.run((sut) => {
|
||||||
expect(events).to.have.lengthOf(0);
|
sut.addOrUpdateAllInCategory(categoryId);
|
||||||
expect(sut.selectedScripts.map((script) => script.id))
|
})
|
||||||
.to.have.deep.members(scripts.map((script) => script.id));
|
// assert
|
||||||
|
.expectTotalFiredEvents(0)
|
||||||
|
.expectFinalScripts(existingScripts);
|
||||||
|
});
|
||||||
|
describe('changes revert status of all', () => {
|
||||||
|
// arrange
|
||||||
|
const newStatus = false;
|
||||||
|
const scripts = [
|
||||||
|
new SelectedScriptStub('e1', !newStatus),
|
||||||
|
new SelectedScriptStub('e2', !newStatus),
|
||||||
|
new SelectedScriptStub('e3', newStatus),
|
||||||
|
];
|
||||||
|
const expectedScripts = scripts.map((s) => new SelectedScript(s.script, newStatus));
|
||||||
|
const categoryId = 31;
|
||||||
|
new UserSelectionTestRunner()
|
||||||
|
.withSelectedScripts(scripts)
|
||||||
|
.withCategory(categoryId, scripts.map((s) => s.script))
|
||||||
|
// act
|
||||||
|
.run((sut) => {
|
||||||
|
sut.addOrUpdateAllInCategory(categoryId, newStatus);
|
||||||
|
})
|
||||||
|
// assert
|
||||||
|
.expectTotalFiredEvents(1)
|
||||||
|
.expectFinalScripts(expectedScripts)
|
||||||
|
.expectFinalScriptsInEvent(0, expectedScripts);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
it('adds all when nothing exists', () => {
|
describe('when nothing exists; adds all with given revert status', () => {
|
||||||
// arrange
|
const revertStatuses = [ true, false ];
|
||||||
const categoryId = 1;
|
for (const revertStatus of revertStatuses) {
|
||||||
const expected = [new ScriptStub('s1'), new ScriptStub('s2')];
|
describe(`when revert status is ${revertStatus}`, () => {
|
||||||
const collection = new CategoryCollectionStub()
|
// arrange
|
||||||
.withAction(new CategoryStub(categoryId)
|
const categoryId = 1;
|
||||||
.withScripts(...expected));
|
const scripts = [
|
||||||
const sut = new UserSelection(collection, []);
|
new SelectedScriptStub('s1', !revertStatus),
|
||||||
// act
|
new SelectedScriptStub('s2', !revertStatus),
|
||||||
sut.addOrUpdateAllInCategory(categoryId);
|
];
|
||||||
// assert
|
const expected = scripts.map((s) => new SelectedScript(s.script, revertStatus));
|
||||||
expect(sut.selectedScripts.map((script) => script.id))
|
new UserSelectionTestRunner()
|
||||||
.to.have.deep.members(expected.map((script) => script.id));
|
.withSelectedScripts([])
|
||||||
|
.withCategory(categoryId, scripts.map((s) => s.script))
|
||||||
|
// act
|
||||||
|
.run((sut) => {
|
||||||
|
sut.addOrUpdateAllInCategory(categoryId, revertStatus);
|
||||||
|
})
|
||||||
|
// assert
|
||||||
|
.expectTotalFiredEvents(1)
|
||||||
|
.expectFinalScripts(expected)
|
||||||
|
.expectFinalScriptsInEvent(0, expected);
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
it('adds all with given revert status when nothing exists', () => {
|
describe('when some exists; changes revert status of all', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const categoryId = 1;
|
const newStatus = true;
|
||||||
const expected = [new ScriptStub('s1'), new ScriptStub('s2')];
|
const existing = [
|
||||||
const collection = new CategoryCollectionStub()
|
new SelectedScriptStub('e1', true),
|
||||||
.withAction(new CategoryStub(categoryId)
|
new SelectedScriptStub('e2', false),
|
||||||
.withScripts(...expected));
|
];
|
||||||
const sut = new UserSelection(collection, []);
|
const notExisting = [
|
||||||
// act
|
new SelectedScriptStub('n3', true),
|
||||||
sut.addOrUpdateAllInCategory(categoryId, true);
|
new SelectedScriptStub('n4', false),
|
||||||
// assert
|
];
|
||||||
expect(sut.selectedScripts.every((script) => script.revert))
|
|
||||||
.to.equal(true);
|
|
||||||
});
|
|
||||||
it('changes revert status of all when some exists', () => {
|
|
||||||
// arrange
|
|
||||||
const categoryId = 1;
|
|
||||||
const notExisting = [ new ScriptStub('notExisting1'), new ScriptStub('notExisting2') ];
|
|
||||||
const existing = [ new ScriptStub('existing1'), new ScriptStub('existing2') ];
|
|
||||||
const allScripts = [ ...existing, ...notExisting ];
|
const allScripts = [ ...existing, ...notExisting ];
|
||||||
const collection = new CategoryCollectionStub()
|
const expectedScripts = allScripts.map((s) => new SelectedScript(s.script, newStatus));
|
||||||
.withAction(new CategoryStub(categoryId)
|
const categoryId = 77;
|
||||||
.withScripts(...allScripts));
|
new UserSelectionTestRunner()
|
||||||
const sut = new UserSelection(collection, existing.map((script) => new SelectedScript(script, false)));
|
.withSelectedScripts(existing)
|
||||||
|
.withCategory(categoryId, allScripts.map((s) => s.script))
|
||||||
// act
|
// act
|
||||||
sut.addOrUpdateAllInCategory(categoryId, true);
|
.run((sut) => {
|
||||||
|
sut.addOrUpdateAllInCategory(categoryId, newStatus);
|
||||||
|
})
|
||||||
// assert
|
// assert
|
||||||
expect(sut.selectedScripts.every((script) => script.revert))
|
.expectTotalFiredEvents(1)
|
||||||
.to.equal(true);
|
.expectFinalScripts(expectedScripts)
|
||||||
});
|
.expectFinalScriptsInEvent(0, expectedScripts);
|
||||||
it('changes revert status of all when some exists', () => {
|
|
||||||
// arrange
|
|
||||||
const categoryId = 1;
|
|
||||||
const notExisting = [ new ScriptStub('notExisting1'), new ScriptStub('notExisting2') ];
|
|
||||||
const existing = [ new ScriptStub('existing1'), new ScriptStub('existing2') ];
|
|
||||||
const allScripts = [ ...existing, ...notExisting ];
|
|
||||||
const collection = new CategoryCollectionStub()
|
|
||||||
.withAction(new CategoryStub(categoryId)
|
|
||||||
.withScripts(...allScripts));
|
|
||||||
const sut = new UserSelection(collection, existing.map((script) => new SelectedScript(script, false)));
|
|
||||||
// act
|
|
||||||
sut.addOrUpdateAllInCategory(categoryId, true);
|
|
||||||
// assert
|
|
||||||
expect(sut.selectedScripts.every((script) => script.revert))
|
|
||||||
.to.equal(true);
|
|
||||||
});
|
|
||||||
it('changes revert status of all when all already exists', () => {
|
|
||||||
// arrange
|
|
||||||
const categoryId = 1;
|
|
||||||
const scripts = [ new ScriptStub('existing1'), new ScriptStub('existing2') ];
|
|
||||||
const collection = new CategoryCollectionStub()
|
|
||||||
.withAction(new CategoryStub(categoryId)
|
|
||||||
.withScripts(...scripts));
|
|
||||||
const sut = new UserSelection(collection, scripts.map((script) => new SelectedScript(script, false)));
|
|
||||||
// act
|
|
||||||
sut.addOrUpdateAllInCategory(categoryId, true);
|
|
||||||
// assert
|
|
||||||
expect(sut.selectedScripts.every((script) => script.revert))
|
|
||||||
.to.equal(true);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('isSelected', () => {
|
describe('isSelected', () => {
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
import { expect } from 'chai';
|
||||||
|
import 'mocha';
|
||||||
|
import { SelectedScript } from '@/application/Context/State/Selection/SelectedScript';
|
||||||
|
import { CategoryCollectionStub } from '@tests/unit/stubs/CategoryCollectionStub';
|
||||||
|
import { CategoryStub } from '@tests/unit/stubs/CategoryStub';
|
||||||
|
import { UserSelection } from '@/application/Context/State/Selection/UserSelection';
|
||||||
|
import { IScript } from '@/domain/IScript';
|
||||||
|
|
||||||
|
export class UserSelectionTestRunner {
|
||||||
|
private readonly collection = new CategoryCollectionStub();
|
||||||
|
private existingScripts: readonly SelectedScript[] = [];
|
||||||
|
private events: Array<readonly SelectedScript[]> = [];
|
||||||
|
private sut: UserSelection;
|
||||||
|
|
||||||
|
public withCategory(categoryId: number, scripts: readonly IScript[]) {
|
||||||
|
const category = new CategoryStub(categoryId)
|
||||||
|
.withScripts(...scripts);
|
||||||
|
this.collection
|
||||||
|
.withAction(category);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public withSelectedScripts(existingScripts: readonly SelectedScript[]) {
|
||||||
|
this.existingScripts = existingScripts;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public run(runner?: (sut: UserSelection) => void) {
|
||||||
|
this.sut = this.createSut();
|
||||||
|
if (runner) {
|
||||||
|
runner(this.sut);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public expectTotalFiredEvents(amount: number) {
|
||||||
|
const testName = amount === 0 ? 'does not fire changed event' : `fires changed event ${amount} times`;
|
||||||
|
it(testName, () => {
|
||||||
|
expect(this.events).to.have.lengthOf(amount);
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public expectFinalScripts(finalScripts: readonly SelectedScript[]) {
|
||||||
|
expectSameScripts(finalScripts, this.sut.selectedScripts);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public expectFinalScriptsInEvent(eventIndex: number, finalScripts: readonly SelectedScript[]) {
|
||||||
|
expectSameScripts(this.events[eventIndex], finalScripts);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
private createSut(): UserSelection {
|
||||||
|
const sut = new UserSelection(this.collection, this.existingScripts);
|
||||||
|
sut.changed.on((s) => this.events.push(s));
|
||||||
|
return sut;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function expectSameScripts(actual: readonly SelectedScript[], expected: readonly SelectedScript[]) {
|
||||||
|
it('has same expected scripts', () => {
|
||||||
|
const existingScriptIds = expected.map((script) => script.id).sort();
|
||||||
|
const expectedScriptIds = actual.map((script) => script.id).sort();
|
||||||
|
expect(existingScriptIds).to.deep.equal(expectedScriptIds);
|
||||||
|
});
|
||||||
|
it('has expected revert state', () => {
|
||||||
|
const scriptsWithDifferentStatus = actual
|
||||||
|
.filter((script) => {
|
||||||
|
const other = expected.find((existing) => existing.id === script.id);
|
||||||
|
if (!other) {
|
||||||
|
throw new Error(`Script "${script.id}" does not exist in expected scripts: ${JSON.stringify(expected, null, '\t')}`);
|
||||||
|
}
|
||||||
|
return script.revert !== other.revert;
|
||||||
|
});
|
||||||
|
expect(scriptsWithDifferentStatus).to.have
|
||||||
|
.lengthOf(0, 'Scripts with different statuses:\n' + scriptsWithDifferentStatus
|
||||||
|
.map((s) =>
|
||||||
|
`[id: ${s.id}, actual status: ${s.revert}, ` +
|
||||||
|
`expected status: ${expected.find((existing) => existing.id === s.id).revert}]`)
|
||||||
|
.join(' , '),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user