const assert = require('assert');
const Space = require('../index');

describe('Space', function() {
  beforeEach(function() {
    this.space = new Space({ initialState: 'here', count: 1, child: {} });
  });

  it('returns Space instance', function() {
    assert(new Space() instanceof Space);
  });

  it('has initial state', function() {
    assert.deepEqual(this.space.state, { initialState: 'here', count: 1, child: {} });
  });

  it('updates the state', function() {
    const oldState = this.space.state;
    this.space.doAction(({ state }) => ({ count: state.count + 1 }))();
    assert.deepEqual(this.space.state, { initialState: 'here', count: 2, child: {} });
    assert.notEqual(this.space.state, oldState);
  });

  it('registers subscribers', function() {
    const subscriber = function() { return { success: true }; };
    this.space.subscribe(subscriber);
    assert.equal(this.space.subscribers[0], subscriber);
  });

  describe('adding child spaces', function() {
    beforeEach(function() {
      this.subSpaceByName = this.space.subSpace('child');
      this.subSpaceByAction = this.space.doAction(({ subSpace }) => ({
        actionChild: subSpace({ value: 'present' })
      }))();
    });

    it('propagates changes upwards', function() {
      this.subSpaceByName.doAction(() => ({ value: 'is there' }))();
      assert.equal(this.space.state.child.value, 'is there');
    });

    it('uses existing sub space', function() {
      assert.equal(this.space.subSpace('child'), this.subSpaceByName);
    });

    it('supports adding subspaces by action', function() {
      assert(this.space.children.actionChild);
      assert.equal(this.space.state.actionChild.value, 'present');
    });

    it('supports subSpaces in lists', function() {
      this.space.doAction(({ subSpace }) => ({
        list: [subSpace({ value: 'present', id: 'abc12-3' })]
      }))();

      assert.deepEqual(this.space.state, {
        initialState: 'here',
        count: 1,
        child: {},
        actionChild: { value: 'present' },
        list: [{value: 'present', id: 'abc12-3' }]
      });
    });

    it('throws list subSpaces missing id', function() {
      assert.throws(() => {
        this.space.doAction(({ subSpace }) => ({
          list: [subSpace({ value: 'present' })]
        }))();
      });
    });
  });
});

@@ -713,32 +713,231 @@
 dState);%0A  %7D);%0A%0A
+  it('actions pass in event', function() %7B%0A    let called = false;%0A    this.space.doAction((space, event) =%3E %7B called = event.called; %7D)(%7B called: 'once' %7D);%0A    assert.equal(called, 'once');%0A  %7D);%0A%0A
   it('registers 
@@ -1142,23 +1142,16 @@
 scribe('
-adding 
 child sp
@@ -1908,24 +1908,27 @@
     it('
-supports
+throws list
  subSpac
@@ -1934,16 +1934,18 @@
 ces 
-in lists
+missing id
 ', f
@@ -1948,32 +1948,62 @@
 ', function() %7B%0A
+      assert.throws(() =%3E %7B%0A  
       this.space
@@ -2033,32 +2033,34 @@
 ) =%3E (%7B%0A        
+  
 list: %5BsubSpace(
@@ -2081,28 +2081,15 @@
 ent'
-, id: 'abc12-3'
  %7D)%5D%0A
+  
     
@@ -2101,99 +2101,130 @@
 ();%0A
-%0A      assert.deepEqual(this.space.state, %7B%0A        initialState: 'here',%0A        count: 1,
+      %7D);%0A    %7D);%0A%0A    describe('child spaces in lists', function() %7B%0A      it('supports subSpaces in lists', function() %7B
 %0A   
@@ -2232,62 +2232,51 @@
     
-child: %7B%7D,%0A        actionChild: %7B value: 'present' %7D,%0A
+this.space.doAction((%7B subSpace %7D) =%3E (%7B%0A  
     
@@ -2286,17 +2286,27 @@
  list: %5B
-%7B
+subSpace(%7B 
 value: '
@@ -2330,24 +2330,25 @@
 c12-3' %7D
+)
 %5D%0A      
 %7D);%0A    
@@ -2343,153 +2343,180 @@
     
-%7D);%0A    %7D);%0A%0A    it('throws list subSpaces missing id', function() %7B%0A      assert.throws(() =%3E %7B%0A        this.space.doAction((%7B subSpace %7D) =%3E (%7B
+  %7D))();%0A%0A        assert.deepEqual(this.space.state, %7B%0A          initialState: 'here',%0A          count: 1,%0A          child: %7B%7D,%0A          actionChild: %7B value: 'present' %7D,
 %0A   
@@ -2525,35 +2525,25 @@
      list: %5B
-subSpace(%7B 
+%7B
 value: 'pres
@@ -2538,35 +2538,49 @@
 value: 'present'
+, id: 'abc12-3'
  %7D
-)
 %5D%0A        %7D))();
@@ -2567,35 +2567,32 @@
 3' %7D%5D%0A        %7D)
-)()
 ;%0A      %7D);%0A    
@@ -2591,22 +2591,24 @@
 %7D);%0A    %7D);%0A
+%0A%0A
   %7D);%0A%7D);%0A

Move some tests around