state-synchronizers
Version:
Deterministically update state based on other state
65 lines (64 loc) • 2.59 kB
JavaScript
;
exports.__esModule = true;
var get_topological_sorting_1 = require("./get-topological-sorting");
describe('getTopologicalSorting', function () {
var topologicalSortingScenarios = [
{
description: 'simple 3 vertex graph',
edges: { a: ['b', 'c'], b: ['c'] },
expectedResult: ['a', 'b', 'c']
},
{
description: 'long chain',
edges: { a: ['b'], b: ['c'], c: ['d'], d: ['e'] },
expectedResult: ['a', 'b', 'c', 'd', 'e']
},
];
describe.each(topologicalSortingScenarios)('should return a topological sorting for', function (_a) {
var edges = _a.edges, expectedResult = _a.expectedResult, description = _a.description;
it("" + description, function () {
expect.hasAssertions();
var result = get_topological_sorting_1.getTopologicalSorting(edges);
expect(result).toStrictEqual(expectedResult);
});
});
it('should return a topological sorting for two connected components', function () {
expect.hasAssertions();
var edges = {
a: ['b'],
b: ['c'],
foo: ['bar'],
bar: ['baz']
};
var result = get_topological_sorting_1.getTopologicalSorting(edges);
expect(result.indexOf('a')).toBeLessThan(result.indexOf('b'));
expect(result.indexOf('b')).toBeLessThan(result.indexOf('c'));
expect(result.indexOf('foo')).toBeLessThan(result.indexOf('bar'));
expect(result.indexOf('bar')).toBeLessThan(result.indexOf('baz'));
});
var errorScenarios = [
{
description: 'a vertex connected to itself',
edges: {
a: ['a', 'b'],
b: ['c']
},
expectedErrorMessage: 'Cycle detected: a->a'
},
{
description: 'a vertex connected to itself',
edges: {
a: ['b'],
b: ['a']
},
expectedErrorMessage: 'Cycle detected: a->b->a'
},
];
describe.each(errorScenarios)('should throw an error when there is', function (_a) {
var description = _a.description, edges = _a.edges, expectedErrorMessage = _a.expectedErrorMessage;
it("" + description, function () {
expect.hasAssertions();
expect(function () { return get_topological_sorting_1.getTopologicalSorting(edges); }).toThrow(expectedErrorMessage);
});
});
});