{"size": 2299, "ext": "js", "max_stars_repo_path": "test/mocha/partial/01-model/UndoRedoManager.spec.babel.js", "max_stars_repo_name": "zacharyShao/Myscript", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/mocha/partial/01-model/UndoRedoManager.spec.babel.js", "max_issues_repo_name": "zacharyShao/Myscript", "max_issues_repo_issues_event_min_datetime": "2020-07-19T09:00:17.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-11T01:24:24.000Z", "max_forks_repo_path": "test/mocha/partial/01-model/UndoRedoManager.spec.babel.js", "max_forks_repo_name": "zacharyShao/Myscript", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 43.3773584906, "max_line_length": 108, "alphanum_fraction": 0.6920400174}
import { describe, it } from 'mocha';
import { assert } from 'chai';
import * as InkModel from '../../../../src/model/InkModel';
import * as UndoRedoContext from '../../../../src/model/UndoRedoContext';
import * as UndoRedoManager from '../../../../src/model/UndoRedoManager';
import * as DefaultConfiguration from '../../../../src/configuration/DefaultConfiguration';

describe('Check undo/redo manager', () => {
  const configuration = DefaultConfiguration.overrideDefaultConfiguration();
  const undoRedoContext = UndoRedoContext.createUndoRedoContext(configuration);
  const maxSize = undoRedoContext.maxSize;

  it('Should be empty', () => {
    assert.lengthOf(undoRedoContext.stack, 0);
    assert.equal(undoRedoContext.currentPosition, -1);
    assert.equal(undoRedoContext.maxSize, configuration.undoRedoMaxStackSize);
  });

  const count = maxSize;
  it(`Should add ${count} models in stack`, (done) => {
    for (let i = 0; i < count; i++) {
      UndoRedoManager.updateModel(undoRedoContext, InkModel.createModel(configuration), (err, model) => {});
    }
    assert.lengthOf(undoRedoContext.stack, maxSize);
    assert.equal(undoRedoContext.currentPosition, maxSize - 1);
    UndoRedoManager.getModel(undoRedoContext, (err, model) => {
      assert.isTrue(undoRedoContext.canUndo, 'Wrong canUndo state');
      assert.isFalse(undoRedoContext.canRedo, 'Wrong canRedo state');
      done(err);
    });
  });

  it(`Should undo and update current index to ${maxSize - 2}`, (done) => {
    UndoRedoManager.undo(undoRedoContext, undefined, (err, model) => {
      assert.lengthOf(undoRedoContext.stack, maxSize);
      assert.equal(undoRedoContext.currentPosition, maxSize - 2);
      assert.isTrue(undoRedoContext.canUndo, 'Wrong canUndo state');
      assert.isTrue(undoRedoContext.canRedo, 'Wrong canRedo state');
      done(err);
    });
  });

  it(`Should redo and update current index to ${maxSize - 1}`, (done) => {
    UndoRedoManager.redo(undoRedoContext, undefined, (err, model) => {
      assert.lengthOf(undoRedoContext.stack, maxSize);
      assert.equal(undoRedoContext.currentPosition, maxSize - 1);
      assert.isTrue(undoRedoContext.canUndo, 'Wrong canUndo state');
      assert.isFalse(undoRedoContext.canRedo, 'Wrong canRedo state');
      done(err);
    });
  });
});
