describe('The on_finish trial level event handler', function(){
  test('should get an object of data generated by the trial', function(){

    require('../jspsych.js');
    require('../plugins/jspsych-text.js');

    return (new Promise(function(resolve, reject){

      var key_data = null;

      var trial = {
        type: 'text',
        text: 'hello',
        on_finish: function(data){
          key_data = data.key_press;
        }
      }

      jsPsych.init({
        timeline: [trial],
        on_finish: function() {
          resolve({key_data});
        }
      });

      document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32}));
      document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32}));

      //resolve();
    })).then(function(data) { expect(data.key_data).toBe(32) });
  });

  test('data should be writeable', function(){

    require('../jspsych.js');
    require('../plugins/jspsych-text.js');

    return (new Promise(function(resolve, reject){

      var promise_data = {};

      var trial = {
        type: 'text',
        text: 'hello',
        on_finish: function(data){
          data.key_press = 1;
        }
      }

      jsPsych.init({
        timeline: [trial],
        on_finish: function() {
          promise_data.final_key_press = jsPsych.data.getData().values()[0].key_press;
          resolve(promise_data);
        }
      });

      document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32}));
      document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32}));

      //resolve();
    })).then(function(pd) {
      expect(pd.final_key_press).toBe(1);
    });
  });
});

describe('The on_trial_finish handler for trials set at the experiment level', function(){
  test('should get an object containing the trial data', function(){
    require('../jspsych.js');
    require('../plugins/jspsych-text.js');

    return (new Promise(function(resolve, reject){

      var promise_data = {};

      var trial = {
        type: 'text',
        text: 'hello'
      }

      jsPsych.init({
        timeline: [trial],
        on_trial_finish: function(data){
          promise_data.key = data.key_press;
        },
        on_finish: function(){
          resolve(promise_data);
        }
      });

      document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32}));
      document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32}));

      //resolve();
    })).then(function(pd) {
      expect(pd.key).toBe(32);
    });
  });
  test('should allow writing to the data object', function(){
    require('../jspsych.js');
    require('../plugins/jspsych-text.js');

    return (new Promise(function(resolve, reject){

      var promise_data = {};

      var trial = {
        type: 'text',
        text: 'hello'
      }

      jsPsych.init({
        timeline: [trial],
        on_trial_finish: function(data){
          data.write = true;
        },
        on_finish: function(data){
          promise_data.write = data.values()[0].write;
          resolve(promise_data);
        }
      });

      document.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 32}));
      document.dispatchEvent(new KeyboardEvent('keyup', {keyCode: 32}));

      //resolve();
    })).then(function(pd) {
      expect(pd.write).toBe(true);
    });
  });
});
