add specs for IDBDatabase methods

This commit is contained in:
KtorZ 2017-06-23 14:28:19 +02:00
parent 7e71fe18ab
commit 43f28e480b
No known key found for this signature in database
GPG Key ID: 3F72E8BC2894C015

View File

@ -2,20 +2,21 @@ module Test.Main where
import Prelude
import Data.Time.Duration (Milliseconds(..))
import Control.Monad.Aff (Aff, launchAff, forkAff, delay)
import Control.Monad.Aff.AVar (AVAR, makeVar', modifyVar, peekVar, killVar)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Exception (EXCEPTION)
import Data.Maybe (Maybe(..))
import Test.Spec (describe, it, itOnly)
import Test.Spec.Assertions (shouldEqual)
import Test.Spec.Mocha (MOCHA, runMocha)
import Control.Monad.Aff (Aff, launchAff, forkAff, delay)
import Control.Monad.Aff.AVar (AVAR, makeVar', modifyVar, peekVar, killVar)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Exception (EXCEPTION)
import Data.Maybe (Maybe(..))
import Data.Time.Duration (Milliseconds(..))
import Test.Spec (describe, it)
import Test.Spec.Assertions (shouldEqual)
import Test.Spec.Mocha (MOCHA, runMocha)
import Database.IndexedDB.Core
import Database.IndexedDB.IDBDatabase as IDBDatabase
import Database.IndexedDB.IDBFactory as IDBFactory
import Database.IndexedDB.IDBDatabase as IDBDatabase
import Database.IndexedDB.IDBFactory as IDBFactory
import Database.IndexedDB.IDBObjectStore as IDBObjectStore
tearDown :: forall eff. String -> Int -> IDBDatabase -> Aff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit
@ -104,3 +105,133 @@ main = runMocha do
name <- peekVar var
name `shouldEqual` name
tearDown name version db02
describe "IDBDatabase" do
it "createObjectStore (keyPath: [], autoIncrement: true)" do
let dbName = "db-create-store-01"
dbVersion = 1
storeName = "store"
keyPath = []
autoIncrement = true
callback var db = do
store <- IDBDatabase.createObjectStore db storeName { keyPath, autoIncrement }
_ <- launchAff $ IDBObjectStore.name store `shouldEqual` storeName
_ <- launchAff $ IDBObjectStore.keyPath store `shouldEqual` keyPath
_ <- launchAff $ IDBObjectStore.autoIncrement store `shouldEqual` autoIncrement
_ <- launchAff $ IDBObjectStore.indexNames store `shouldEqual` []
_ <- launchAff $ modifyVar (const true) var
pure unit
var <- makeVar' false
db <- IDBFactory.open dbName Nothing
{ onUpgradeNeeded : Just (callback var)
, onBlocked : Nothing
}
hasBeenCalled <- peekVar var
hasBeenCalled `shouldEqual` true
tearDown dbName dbVersion db
it "createObjectStore (keyPath: [\"field\"], autoIncrement: true)" do
let dbName = "db-create-store-02"
dbVersion = 1
storeName = "store"
keyPath = ["field"]
autoIncrement = true
callback var db = do
store <- IDBDatabase.createObjectStore db storeName { keyPath, autoIncrement }
_ <- launchAff $ IDBObjectStore.name store `shouldEqual` storeName
_ <- launchAff $ IDBObjectStore.keyPath store `shouldEqual` keyPath
_ <- launchAff $ IDBObjectStore.autoIncrement store `shouldEqual` autoIncrement
_ <- launchAff $ IDBObjectStore.indexNames store `shouldEqual` []
_ <- launchAff $ modifyVar (const true) var
pure unit
var <- makeVar' false
db <- IDBFactory.open dbName Nothing
{ onUpgradeNeeded : Just (callback var)
, onBlocked : Nothing
}
hasBeenCalled <- peekVar var
hasBeenCalled `shouldEqual` true
tearDown dbName dbVersion db
it "createObjectStore (keyPath: [\"a\", \"b\"], autoIncrement: false)" do
let dbName = "db-create-store-03"
dbVersion = 1
storeName = "store"
keyPath = ["a", "b"]
autoIncrement = false
callback var db = do
store <- IDBDatabase.createObjectStore db storeName { keyPath, autoIncrement }
_ <- launchAff $ IDBObjectStore.name store `shouldEqual` storeName
_ <- launchAff $ IDBObjectStore.keyPath store `shouldEqual` keyPath
_ <- launchAff $ IDBObjectStore.autoIncrement store `shouldEqual` autoIncrement
_ <- launchAff $ IDBObjectStore.indexNames store `shouldEqual` []
_ <- launchAff $ modifyVar (const true) var
pure unit
var <- makeVar' false
db <- IDBFactory.open dbName Nothing
{ onUpgradeNeeded : Just (callback var)
, onBlocked : Nothing
}
hasBeenCalled <- peekVar var
hasBeenCalled `shouldEqual` true
tearDown dbName dbVersion db
it "createObjectStore (keyPath: [\"field\"], autoIncrement: false)" do
let dbName = "db-create-store-04"
dbVersion = 1
storeName = "store"
keyPath = ["field"]
autoIncrement = false
callback var db = do
store <- IDBDatabase.createObjectStore db storeName { keyPath, autoIncrement }
_ <- launchAff $ IDBObjectStore.name store `shouldEqual` storeName
_ <- launchAff $ IDBObjectStore.keyPath store `shouldEqual` keyPath
_ <- launchAff $ IDBObjectStore.autoIncrement store `shouldEqual` autoIncrement
_ <- launchAff $ IDBObjectStore.indexNames store `shouldEqual` []
_ <- launchAff $ modifyVar (const true) var
pure unit
var <- makeVar' false
db <- IDBFactory.open dbName Nothing
{ onUpgradeNeeded : Just (callback var)
, onBlocked : Nothing
}
hasBeenCalled <- peekVar var
hasBeenCalled `shouldEqual` true
tearDown dbName dbVersion db
it "deleteObjectStore" do
let dbName = "db-delete-store"
dbVersion = 2
storeName = "store"
keyPath = []
autoIncrement = true
callback db = do
_ <- IDBDatabase.createObjectStore db storeName { keyPath, autoIncrement }
pure unit
callback2 var db = do
_ <- IDBDatabase.deleteObjectStore db storeName
_ <- launchAff $ modifyVar (const true) var
pure unit
var <- makeVar' false
db <- IDBFactory.open dbName Nothing
{ onUpgradeNeeded : Just callback
, onBlocked : Nothing
}
liftEff $ IDBDatabase.close db
db <- IDBFactory.open dbName (Just dbVersion)
{ onUpgradeNeeded : Just (callback2 var)
, onBlocked : Nothing
}
hasBeenCalled <- peekVar var
hasBeenCalled `shouldEqual` true
tearDown dbName dbVersion db