|
import {assert, config} from "chai"; |
|
import {undoRedo} from "./solution"; |
|
config.truncateThreshold = 0; |
|
|
|
describe("sample tests", () => { |
|
it("should work on get/set tests", () => { |
|
const obj = { |
|
x: 1, |
|
y: 2 |
|
}; |
|
|
|
const unRe = undoRedo(obj); |
|
assert.strictEqual(unRe.get('x'), 1, 'The get method returns the value of a key'); |
|
unRe.set('x', 3); |
|
assert.strictEqual(unRe.get('x'), 3, 'The set method change the value of a key'); |
|
}); |
|
it("should work on simple undo", () => { |
|
const obj = { |
|
x: 1, |
|
y: 2 |
|
}; |
|
|
|
const unRe = undoRedo(obj); |
|
unRe.set('y', 10); |
|
assert.strictEqual(unRe.get('y'), 10, 'The get method returns the value of a key'); |
|
unRe.undo(); |
|
assert.strictEqual(unRe.get('y'), 2, 'The undo method restores the previous state'); |
|
assert.throws(() => unRe.undo()); |
|
assert.strictEqual(unRe.get('y'), 2); |
|
}); |
|
it("should work on simple redo", () => { |
|
const obj = { |
|
x: 1, |
|
y: 2 |
|
}; |
|
|
|
const unRe = undoRedo(obj); |
|
unRe.set('y', 10); |
|
assert.strictEqual(unRe.get('y'), 10, 'The get method returns the value of a key'); |
|
unRe.undo(); |
|
assert.strictEqual(unRe.get('y'), 2, 'The undo method restores the previous state'); |
|
unRe.redo(); |
|
assert.strictEqual(unRe.get('y'), 10, 'The undo method restores the previous state'); |
|
assert.throws(() => unRe.redo()); |
|
assert.strictEqual(unRe.get('y'), 10); |
|
}); |
|
it("should work on undo/redo", () => { |
|
const obj = { |
|
x: 1, |
|
y: 2 |
|
}; |
|
|
|
const unRe = undoRedo(obj); |
|
unRe.set('y', 10); |
|
unRe.set('y', 100); |
|
unRe.set('x', 150); |
|
unRe.set('x', 50); |
|
assert.strictEqual(unRe.get('y'), 100, 'The get method returns the value of a key'); |
|
assert.strictEqual(unRe.get('x'), 50, 'The get method returns the value of a key'); |
|
unRe.undo(); |
|
assert.strictEqual(unRe.get('x'), 150, 'The undo method restores the previous state'); |
|
assert.strictEqual(unRe.get('y'), 100, 'The y key stays the same'); |
|
unRe.redo(); |
|
assert.strictEqual(unRe.get('x'), 50, 'Undo the x value'); |
|
assert.strictEqual(unRe.get('y'), 100, 'The y key stays the same'); |
|
unRe.undo(); |
|
unRe.undo(); |
|
assert.strictEqual(unRe.get('x'), 1, 'Undo the x value'); |
|
assert.strictEqual(unRe.get('y'), 100, 'The y key stays the same'); |
|
unRe.undo(); |
|
unRe.undo(); |
|
assert.strictEqual(unRe.get('y'), 2, 'Undo the y value'); |
|
assert.strictEqual(unRe.get('x'), 1, 'The x key stays the same'); |
|
assert.throws(() => unRe.undo()); |
|
assert.strictEqual(unRe.get('y'), 2, 'There is nothing to undo'); |
|
unRe.redo(); |
|
unRe.redo(); |
|
unRe.redo(); |
|
unRe.redo(); |
|
assert.strictEqual(unRe.get('y'), 100, 'y key redo state'); |
|
assert.strictEqual(unRe.get('x'), 50, 'y key redo state'); |
|
assert.throws(() => unRe.redo()); |
|
assert.strictEqual(unRe.get('y'), 100, 'There is nothing to redo'); |
|
}); |
|
it('should work on a new key', () => { |
|
const obj = { |
|
x: 1, |
|
y: 2 |
|
}; |
|
|
|
const unRe = undoRedo(obj); |
|
unRe.set('z', 10); |
|
assert.strictEqual(unRe.get('z'), 10, 'A new key has been added'); |
|
unRe.undo(); |
|
assert.strictEqual(unRe.get('z'), undefined, 'The z key should not exist'); |
|
unRe.redo(); |
|
assert.strictEqual(unRe.get('z'), 10, 'A new key has been added'); |
|
}); |
|
it('should delete a key', () => { |
|
const obj = { |
|
x: 1, |
|
y: 2 |
|
}; |
|
|
|
const unRe = undoRedo(obj); |
|
unRe.del('x'); |
|
assert.strictEqual(unRe.get('x'), undefined, 'The x key should not exist'); |
|
assert.isFalse(obj.hasOwnProperty('x'), 'The x key should be deleted'); |
|
unRe.undo(); |
|
assert.strictEqual(unRe.get('x'), 1, 'A new key has been added'); |
|
unRe.redo(); |
|
assert.strictEqual(unRe.get('x'), undefined, 'The x key should not exist'); |
|
console.log("teste obj", obj) |
|
assert.isFalse(obj.hasOwnProperty('x'), 'The x key should be deleted'); |
|
}); |
|
it('should work on all mixed tests', () => { |
|
const obj = {}; |
|
|
|
const unRe = undoRedo(obj); |
|
|
|
assert.deepEqual(obj, {}, 'The obj object has no keys'); |
|
unRe.set('x', 5); |
|
assert.strictEqual(unRe.get('x'), 5, 'x key has been added'); |
|
unRe.set('y', 10); |
|
assert.strictEqual(unRe.get('y'), 10, 'y key has been added'); |
|
unRe.set('y', 8); |
|
assert.strictEqual(unRe.get('y'), 8, 'y key has change its value'); |
|
unRe.del('y'); |
|
assert.strictEqual(unRe.get('y'), undefined, 'y key should not exist'); |
|
unRe.undo(); |
|
assert.strictEqual(unRe.get('y'), 8, 'y key exists'); |
|
unRe.undo(); |
|
assert.strictEqual(unRe.get('y'), 10, 'y key has 10 value'); |
|
unRe.undo(); |
|
assert.strictEqual(unRe.get('y'), undefined, 'y key should not exist'); |
|
unRe.undo(); |
|
assert.strictEqual(unRe.get('x'), undefined, 'x key should not exist'); |
|
unRe.redo(); |
|
unRe.redo(); |
|
unRe.redo(); |
|
assert.deepEqual(obj, {x: 5, y: 8}, 'Redo all actions'); |
|
unRe.redo(); |
|
unRe.set('x', 55); |
|
assert.deepEqual(obj, {x: 55}, 'Redo three actions'); |
|
unRe.undo(); |
|
assert.deepEqual(obj, {x: 5}, 'Redo one actions'); |
|
}); |
|
}); |