module.exports = storeTest
var toValue = require('./utils/to-value')

function storeTest (test, api, server, debug) {
  test('hoodie.store', function (t) {
    t.beforeEach(function (done) {
      api.browser.url(server.info.uri).then(function () {
        done()
      })
    })

    t.afterEach(function (done) {
      api.browser.execute(function () {
        window.localStorage.clear()
      }).url('about:blank').then(function () {
        done()
      }).catch(function (error) {
        done(error)
      })
    })

    t.test('.hasLocalChanges() cleared after sign up', function (tt) {
      api.browser.executeAsync(function (done) {
        window.hoodie.store.removeAll().then(function () {
          return window.hoodie.store.hasLocalChanges()
        }).then(done, done)
      }).then(toValue).then(function (hasChanges) {
        tt.is(hasChanges, false)
      }).executeAsync(function (done) {
        window.hoodie.store.add({ foo: 'bar' }).then(function () {
          return window.hoodie.store.hasLocalChanges()
        }).then(done, done)
      }).then(toValue).then(function (hasChanges) {
        tt.is(hasChanges, true)
      })

      // sanity check
      .execute(function getUsername () {
        return window.hoodie.account.username
      }).then(toValue).then(function (username) {
        tt.is(username, null)
      }).executeAsync(function (done) {
        window.hoodie.account.signUp({
          username: 'storetest',
          password: 'secret'
        }).then(function () {
          return window.hoodie.account.signIn({
            username: 'storetest',
            password: 'secret'
          })
        }).then(done, done)
      }).waitUntil(function () {
        return this.execute(function storeHasNoLocalChanges (done) {
          return window.hoodie.store.hasLocalChanges() === false
        }).then(toValue)
      }, 10000, 'waiting for "hoodie.store.hasLocalChanges() === false"').then(function () {
        tt.end()
      }).catch(tt.error)
    })

    // https://github.com/hoodiehq/hoodie-client/issues/44
    t.test('.findAll() objects after signin', function (tt) {
      tt.plan(1)

      api.browser.executeAsync(function (done) {
        window.hoodie.account.signIn({
          username: 'storetest',
          password: 'secret'
        }).then(done, done)
      }).waitUntil(function () {
        return this.executeAsync(function findsObjects (done) {
          window.hoodie.store.findAll().then(done, done)
        }).then(toValue).then(function (objects) {
          return objects.length === 1
        })
      }, 10000).then(function () {
        tt.pass('finds objects after signin')
      }).catch(tt.error)
    })

    t.end()
  })
}

@@ -325,32 +325,37 @@
 .browser.execute
+Async
 (function () %7B%0A 
@@ -341,32 +341,36 @@
 Async(function (
+done
 ) %7B%0A        wind
@@ -376,27 +376,58 @@
 dow.
-localStorage.clear(
+hoodie.account.destroy()%0A%0A        .then(done, done
 )%0A  
@@ -565,42 +565,93 @@
     
-t.test('.hasLocalChanges() cleared
+// https://github.com/hoodiehq/hoodie-client/issues/44%0A    t.test('.findAll() objects
  aft
@@ -661,11 +661,10 @@
 sign
- up
+in
 ', f
@@ -674,24 +674,42 @@
 tion (tt) %7B%0A
+      tt.plan(4)%0A%0A
       api.br
@@ -705,32 +705,40 @@
      api.browser
+%0A%0A      
 .executeAsync(fu
@@ -785,215 +785,61 @@
 ore.
-removeAll().then(function () %7B%0A          return window.hoodie.store.hasLocalChanges()%0A        %7D).then(done, done)%0A      %7D).then(toValue).then(function (hasChanges) %7B%0A        tt.is(hasChanges, false)
+add(%7B foo: 'bar' %7D).then(done, done)%0A      %7D)%0A
 %0A      
-%7D)
 .exe
@@ -898,111 +898,16 @@
 ore.
-add(%7B foo: 'bar' %7D).then(function () %7B%0A          return window.hoodie.store.hasLocalChanges()%0A        %7D
+findAll(
 ).th
@@ -935,32 +935,40 @@
 %7D).then(toValue)
+%0A%0A      
 .then(function (
@@ -967,25 +967,19 @@
 nction (
-hasChange
+doc
 s) %7B%0A   
@@ -993,233 +993,139 @@
 .is(
-hasChanges, true)%0A      %7D)%0A%0A      // sanity check%0A      .execute(function getUsername () %7B%0A        return window.hoodie.account.username%0A      %7D).then(toValue).then(function (username) %7B%0A        tt.is(username, null)
+docs.length, 1, 'finds 1 doc after creating one')%0A        tt.is(docs%5B0%5D.foo, 'bar', 'doc has expected properties')%0A      %7D)%0A
 %0A      
-%7D)
 .exe
@@ -1450,60 +1450,24 @@
   %7D)
-.waitUntil(function () %7B%0A        return this
+%0A%0A      
 .execute
 (fun
@@ -1462,16 +1462,21 @@
 .execute
+Async
 (functio
@@ -1481,31 +1481,8 @@
 ion 
-storeHasNoLocalChanges 
 (don
@@ -1490,33 +1490,24 @@
 ) %7B%0A        
-  return 
 window.hoodi
@@ -1512,366 +1512,289 @@
 die.
-store.hasLocalChanges() === false%0A        %7D).then(toValue)%0A      %7D, 10000, 'waiting for %22hoodie.store.hasLocalChanges() === false%22').then(function () %7B%0A        tt.end(
+account.signOut().then(done, done)%0A      %7D)%0A%0A      .executeAsync(function (done) %7B%0A        window.hoodie.store.findAll().then(done, done)%0A      %7D).then(toValue
 )%0A
+%0A
       
-%7D).catch(tt.error)%0A    %7D)%0A%0A    // https://github.com/hoodiehq/hoodie-client/issues/44%0A    t.test('.findAll() objects after signin', function (tt) %7B%0A      tt.plan(1)%0A%0A      api.browser
+.then(function (docs) %7B%0A        tt.is(docs.length, 0, 'does not find created doc after signout')%0A      %7D)%0A%0A      
 .exe
@@ -1958,16 +1958,24 @@
       %7D)
+%0A%0A      
 .waitUnt

remove tests for hoodie.store.hasLocalChanges()