/**
 * Created by Phuc on 9/19/2015.
 */

'use strict';

describe('ngTimeMachine module', () => {

  beforeEach(angular.mock.module('ngTimeMachine', ($provide) => {
    $provide.service('setupTimeControls', function () {
      return {
        start: jasmine.createSpy()
      }
    });
  }));

  var $q, $http, setupTimeControls;

  beforeEach(angular.mock.inject((_$q_, _$http_, _setupTimeControls_) => {
    // The injector unwraps the underscores (_) from around the parameter names when matching
    $q = _$q_;
    $http = _$http_;
    setupTimeControls = _setupTimeControls_;
    //var tmStore = require('../src/tm-store');
  }));

  it('should setup correctly', () => {
    expect(setupTimeControls.start).toHaveBeenCalled();
  });

  describe('ngStore provider in angular', () => {
    var Store;
    beforeEach(() => {
      Store = require('../src/tm-store');
    });

    it('support create Actions', () => {

      var Actions = Store.makeActions(['a1', 'a2', 'a3']);

      expect(Object.keys(Actions).length).toEqual(3);

    });

    it('support create new Store Class', () => {

      var StoreClass = Store.define({});

      expect(StoreClass).toBeDefined();
      expect(StoreClass.createClass).toBeDefined();

    })

  });

  describe('ngStore', () => {
    var Actions, StoreClass;
    var Store;
    beforeEach(() => {
      Store = require('../src/tm-store');
    });

    beforeEach(() => {

      Actions = Store.makeActions(['foo', 'bar', 'baz', 'qux']);
      StoreClass = Store.define({
        onFoo: function () {
        },

        onBar: function () {
        },

        onBaz: function () {
        }
      });
    });

    it('create instance correctly', () => {
      var instance = new StoreClass(Actions, $q, $http);
      expect(instance.onFoo).toBeDefined();
      expect(instance.onBar).toBeDefined();
      expect(instance.onBaz).toBeDefined();
    });

  })

});

@@ -304,16 +304,24 @@
 , $http,
+ $scope,
  setupTi
@@ -401,16 +401,30 @@
 ontrols_
+, _$rootScope_
 ) =%3E %7B%0A 
@@ -552,16 +552,43 @@
 $http_;%0A
+    $scope = _$rootScope_;%0A
     setu
@@ -1320,24 +1320,31 @@
 =%3E %7B%0A    var
+ Store,
  Actions, St
@@ -1353,31 +1353,16 @@
 eClass;%0A
-    var Store;%0A
     befo
@@ -1565,16 +1565,34 @@
  onFoo: 
+jasmine.createSpy(
 function
@@ -1609,40 +1609,113 @@
     
-%7D,%0A%0A        onBar: function () %7B
+  this.trigger(this.state);%0A        %7D),%0A%0A        onBar: function () %7B%0A          this.trigger(this.state);
 %0A   
@@ -1744,32 +1744,68 @@
 : function () %7B%0A
+          this.trigger(this.state);%0A
         %7D%0A      
@@ -2059,16 +2059,1387 @@
    %7D);%0A%0A
+    it('allow $scope register', () =%3E %7B%0A      var storeInstance = new StoreClass(Actions, $q, $http);%0A      var selector = jasmine.createSpy();%0A      storeInstance.register($scope, 'result', selector);%0A      expect(selector).toHaveBeenCalled();%0A    %7D);%0A%0A    it('dispatch request correctly', () =%3E %7B%0A      var storeInstance = new StoreClass(Actions, $q, $http);%0A      storeInstance.dispatch('foo', 1, 2, 3);%0A      expect(storeInstance.onFoo).toHaveBeenCalled();%0A      expect(storeInstance.onFoo).toHaveBeenCalledWith(1, 2, 3);%0A%0A    %7D);%0A%0A    it('update the $scope correctly when state change', () =%3E %7B%0A      var i = 0;%0A      var storeInstance = new StoreClass(Actions, $q, $http);%0A      storeInstance.register($scope, 'result', function (state) %7B%0A        return ++i;%0A      %7D);%0A      expect($scope.result).toEqual(1);%0A%0A      storeInstance.dispatch('bar', 1, 2, 3);%0A      expect($scope.result).toEqual(2);%0A    %7D);%0A%0A    it('allow assign multiple fields on $scope register', () =%3E %7B%0A      var i = 0;%0A      var storeInstance = new StoreClass(Actions, $q, $http);%0A      storeInstance.register($scope, %7BresultField1: 'scopeField1', resultField2: 'scopeField2'%7D, function (state) %7B%0A        return %7B%0A          resultField1: ++i,%0A          resultField2: true%0A        %7D;%0A      %7D);%0A%0A      expect($scope.scopeField1).toEqual(1);%0A      expect($scope.scopeField2).toEqual(true);%0A    %7D)%0A%0A%0A
   %7D)%0A%0A%7D)

fix small issue