const chai = require('chai'),
      expect = chai.expect,
      yarn = require('../src/yarn.js');

describe('Dialogue', function() {
    it('emits a line event if it gets a line result', function() {
        var lineCallbackCalled = false;

        var dialogue = new yarn.Dialogue();

        dialogue.on('line', (result) => {
            lineCallbackCalled = true;
            expect(result).to.be.an.instanceof(yarn.LineResult);
        });

        dialogue.runner.run = () => [ new yarn.LineResult() ];

        return dialogue.start().then(() => {
            expect(lineCallbackCalled).to.be.true;
        });
    });

    it('emits an options event if it gets an options result', function() {
        var optionsCallbackCalled = false;

        var dialogue = new yarn.Dialogue();

        dialogue.on('options', (result, chooseCallback) => {
            optionsCallbackCalled = true;
            chooseCallback(true);

            expect(result).to.be.an.instanceof(yarn.OptionsResult);
            expect(chooseCallback).to.be.an.instanceof(Function);
        });

        dialogue.runner.run = () => [ new yarn.OptionsResult() ];

        return dialogue.start().then(() => {
            expect(optionsCallbackCalled).to.be.true;
        });
    });

    it('emits a nodecomplete event if it gets an node complete result', function() {
        var nodecompleteCallbackCalled = false;

        var dialogue = new yarn.Dialogue();

        dialogue.on('nodecomplete', (result) => {
            nodecompleteCallbackCalled = true;
            expect(result).to.be.an.instanceof(yarn.NodeCompleteResult);
        });

        dialogue.runner.run = () => [ new yarn.NodeCompleteResult() ];

        return dialogue.start().then(() => {
            expect(nodecompleteCallbackCalled).to.be.true;
        });
    });

    it('emits result events in order that they are given', function() {
        var resultsCalled = [];

        var dialogue = new yarn.Dialogue();

        dialogue.on('line', () => {
            resultsCalled.push('line');
        });
        dialogue.on('options', (results, chooseCallback) => {
            chooseCallback(true);
            resultsCalled.push('options');
        });
        dialogue.on('nodecomplete', () => {
            resultsCalled.push('nodecomplete');
        });

        dialogue.runner.run = () => [ new yarn.OptionsResult(),
                                      new yarn.NodeCompleteResult(),
                                      new yarn.LineResult() ];

        return dialogue.start().then(() => {
            expect(resultsCalled).to.deep.equal(['options', 'nodecomplete', 'line']);
        });
    });

    it('emits a start and finish result before/after other results', function() {
        var resultsCalled = [];

        var dialogue = new yarn.Dialogue();

        dialogue.on('start', () => {
            resultsCalled.push('start');
        });
        dialogue.on('finish', () => {
            resultsCalled.push('finish');
        });
        dialogue.on('line', () => {
            resultsCalled.push('line');
        });

        dialogue.runner.run = () => [ new yarn.LineResult() ];

        return dialogue.start().then(() => {
            expect(resultsCalled).to.deep.equal(['start', 'line', 'finish']);
        });
    });

    it('is able to load yarn data into the runner', function() {
        var inputData = [
            {
                'title': 'TestNode',
                'tags': 'tag',
                'body': 'This is a test line',
                'position': {
                    'x': 50,
                    'y': 50,
                },
                'colorID': 0
            }
        ]

        var outputData = {
            'TestNode': {
                'tags': 'tag',
                'body': 'This is a test line',
            }
        }

        var dialogue = new yarn.Dialogue();

        expect(dialogue.runner.nodes).to.deep.equal({});

        dialogue.load(inputData);

        expect(dialogue.runner.nodes).to.deep.equal(outputData);
    });
});

@@ -122,24 +122,120 @@
 unction() %7B%0A
+    var dialogue;%0A%0A    beforeEach(function() %7B%0A        dialogue = new yarn.Dialogue();%0A    %7D);%0A%0A
     it('emit
@@ -334,53 +334,8 @@
 e;%0A%0A
-        var dialogue = new yarn.Dialogue();%0A%0A
     
@@ -793,53 +793,8 @@
 e;%0A%0A
-        var dialogue = new yarn.Dialogue();%0A%0A
     
@@ -1399,53 +1399,8 @@
 e;%0A%0A
-        var dialogue = new yarn.Dialogue();%0A%0A
     
@@ -1884,53 +1884,8 @@
 %5D;%0A%0A
-        var dialogue = new yarn.Dialogue();%0A%0A
     
@@ -2692,53 +2692,8 @@
 %5D;%0A%0A
-        var dialogue = new yarn.Dialogue();%0A%0A
     
@@ -3708,53 +3708,8 @@
  %7D%0A%0A
-        var dialogue = new yarn.Dialogue();%0A%0A
     

Stop repeating the dialogue instantiation in the tests