'use strict';

var rewire = require('rewire');
var api = require('./Dispatch.api');
var Dispatch = rewire('../../../core/Dispatch');
var PathUtilsStub = require('../path/Path.stub');
var NodeStub = require('../node/Node.stub');
var test = require('tape');

Dispatch.__set__('PathUtils', PathUtilsStub);

test('Dispatch singleton', function (t) {

    t.test('Dispatch object', function (t) {

        t.test('Dispatch should exist as a module', function (t) {
            t.equal(typeof Dispatch, 'object', 'Dispatch should be an object');
            t.end();
        });
        
        t.test('Dispatch should conform to its public api', function (t) {

            api.forEach(function (method) {
                t.ok(Dispatch[method], 'Dispatch should have a ' +
                                        method + ' method');

                t.equal(Dispatch[method].constructor, Function, 'Dispatch.'
                                                     + method + ' should be a function');
            });

            t.end();
        });

        t.end();
    });

    t.test('._setUpdater method', function (t) {

        var testUpdater = 'a';
        
        t.doesNotThrow(
            Dispatch._setUpdater.bind(Dispatch, testUpdater),
            '._setUpdater should be callable'
        );

        var stub = new NodeStub();

        Dispatch.mount('body', stub);

        t.ok(stub._setUpdater.getCall(0).calledWith(testUpdater), 'Nodes mounted with the Dispatch ' +
                                                                  'should have their updaters set to ' +
                                                                  'the dispatch\'s updater');

        var testUpdater2 = 'b';

        Dispatch._setUpdater(testUpdater2);

        var stub2 = new NodeStub();

        Dispatch.mount('body/0', stub2);

        t.notOk(stub2._setUpdater.getCall(0).calledWith(testUpdater), 'Nodes mounted with the Dispatch ' +
                                                                      'should have their updaters set to ' +
                                                                      'the dispatch\'s current updater and not a previous one');

        t.ok(stub2._setUpdater.getCall(0).calledWith(testUpdater2), 'Nodes mounted with the Dispatch ' +
                                                                    'should have their updaters set to ' +
                                                                    'the dispatch\'s current updater');

        t.ok(stub._setUpdater.getCall(1).calledWith(testUpdater2), 'Nodes mounted with the Dispatch ' +
                                                                   'should have their updaters set to the new ' +
                                                                   'updater when the dispatch has its updater changed');

        t.end();
    });

    t.test('.mount method', function (t) {
        t.end();
    });

    t.test('.dismount method', function (t) {
        t.end();
    });

    t.test('.getNode method', function (t) {
        t.end();
    });

    t.test('.show method', function (t) {
        t.end();
    });

    t.test('.hide method', function (t) {
        t.end();
    });

    t.test('.lookupNode method', function (t) {
        t.end();
    });

    t.test('.dispatch method', function (t) {
        t.end();
    });

    t.test('.dispatchUIEvents method', function (t) {
        t.end();
    });

    t.end();
});

@@ -12,40 +12,8 @@
 ';%0A%0A
-var rewire = require('rewire');%0A
 var 
@@ -62,17 +62,18 @@
 tch = re
-w
+qu
 ire('../
@@ -99,25 +99,20 @@
 ');%0Avar 
-PathUtils
+Node
 Stub = r
@@ -126,17 +126,17 @@
 '../
-path/Path
+node/Node
 .stu
@@ -140,36 +140,32 @@
 stub');%0Avar Node
-Stub
  = require('../n
@@ -159,38 +159,39 @@
 require('../
-nod
+../../cor
 e/Node
-.stub
 ');%0Avar test
@@ -215,55 +215,8 @@
 );%0A%0A
-Dispatch.__set__('PathUtils', PathUtilsStub);%0A%0A
 test
@@ -3239,32 +3239,1564 @@
  function (t) %7B%0A
+        var nodes = %7B%0A            'path': new Node(),%0A            'path/1': new Node(),%0A            'path/1/2': new Node(),%0A            'path/1/1': new Node(),%0A            'path/1/2/3': new Node(),%0A            'path/1/2/4': new Node()%0A        %7D;%0A    %0A        var received = %5B%5D;%0A    %0A        function onReceive(actualEvent, actualPayload) %7B%0A            t.equal(actualEvent, expectedEvent, 'Node should receive ');%0A            t.equal(actualPayload, expectedPayload);%0A            %0A            received.push(this.__path__);%0A        %7D%0A        %0A        nodes.path.addChild(nodes%5B'path/1'%5D);%0A        nodes%5B'path/1'%5D.addChild(nodes%5B'path/1/2'%5D);%0A        nodes%5B'path/1'%5D.addChild(nodes%5B'path/1/1'%5D);%0A        nodes%5B'path/1/2'%5D.addChild(nodes%5B'path/1/2/3'%5D);%0A        nodes%5B'path/1/2'%5D.addChild(nodes%5B'path/1/2/4'%5D);%0A    %0A        for (var path in nodes) %7B%0A            nodes%5Bpath%5D.__path__ = path;%0A            nodes%5Bpath%5D.onReceive = onReceive;%0A            Dispatch.mount(path, nodes%5Bpath%5D);%0A        %7D%0A        %0A        var expectedEvent = 'event';%0A        var expectedPayload = %7B some: 'payload' %7D;%0A    %0A        Dispatch.dispatch('path/1', expectedEvent, expectedPayload);%0A%0A        t.equal(%0A            received.indexOf('path/1'), -1,%0A            'path/1 should not receive event dispatched via Dispatch.dispatch(%22path/1%22)'%0A        );%0A        %0A        t.deepEqual(%0A            received, %5B'path/1/2', 'path/1/1', 'path/1/2/3', 'path/1/2/4'%5D,%0A            'Dispatch.dispatch should trigger event first on parent, then on children'%0A        );%0A    %0A
         t.end();

Test basic Dispatch.dispatch