 (deftest pomodoro-inc-test
   "Test the time increment handler for running pomodoros. Expectation is that the :completed-time key
   is incremented on every call."
-  (let [current-state @(:state (store/initial-state-fn #()))
-        new-state (:new-state (store/update-local-fn {:current-state current-state :msg-payload test-entry}))
-        new-state1 (:new-state (store/pomodoro-inc-fn {:current-state new-state :msg-payload pomodoro-inc-msg}))
-        new-state2 (:new-state (store/pomodoro-inc-fn {:current-state new-state1 :msg-payload pomodoro-inc-msg}))]
-    (testing "pomodoro test entry in new-entries"
-      (is (= test-entry (get-in new-state [:new-entries 1465059173965]))))
-    (testing "time incremented"
-      (is (= 1 (get-in new-state1 [:new-entries 1465059173965 :completed-time]))))
-    (testing "time incremented"
-      (is (= 2 (get-in new-state2 [:new-entries 1465059173965 :completed-time]))))))
+  (with-redefs [store/new-entries-ls (atom {})]
+    (let [current-state @(:state (store/initial-state-fn #()))
+          new-state (:new-state (store/update-local-fn {:current-state current-state :msg-payload test-entry}))
+          new-state1 (:new-state (store/pomodoro-inc-fn {:current-state new-state :msg-payload pomodoro-inc-msg}))
+          new-state2 (:new-state (store/pomodoro-inc-fn {:current-state new-state1 :msg-payload pomodoro-inc-msg}))]
+      (testing "pomodoro test entry in new-entries"
+        (is (= test-entry (get-in new-state [:new-entries 1465059173965]))))
+      (testing "time incremented"
+        (is (= 1 (get-in new-state1 [:new-entries 1465059173965 :completed-time]))))
+      (testing "time incremented"
+        (is (= 2 (get-in new-state2 [:new-entries 1465059173965 :completed-time]))))
+      (testing "new entries atom properly updated - this would be backed by localstorage on client"
+        (is (= (:new-entries new-state2) @store/new-entries-ls))))))
+
+(deftest toggle-key-test
+  "toggle key messages flip boolean value"
+  (let [current-state @(:state (store/initial-state-fn #()))
+        new-state (:new-state (store/toggle-key-fn {:current-state current-state
+                                                    :msg-payload   {:path [:cfg :sort-by-upvotes]}}))
+        new-state2 (:new-state (store/toggle-key-fn {:current-state current-state
+                                                     :msg-payload   {:path [:some :crazy :long :path]}}))]
+    (testing "before receiving toggle-key msg, key is false, as per initial state"
+      (is (not (:sort-by-upvotes (:cfg current-state)))))
+    (testing "after receiving toggle-key msg, key is true"
+      (is (:sort-by-upvotes (:cfg new-state))))
+    (testing "previously unknown key is set to true. can be nested"
+      (is (get-in new-state2 [:some :crazy :long :path])))))
+
+(deftest toggle-set-test
+  "toggle key messages flip boolean value"
+  (let [test-ts 1465059139281
+        path [:cfg :show-maps-for]
+        current-state @(:state (store/initial-state-fn #()))
+        new-state (:new-state (store/toggle-set-fn {:current-state current-state
+                                                    :msg-payload   {:timestamp test-ts :path path}}))
+        new-state1 (:new-state (store/toggle-set-fn {:current-state new-state
+                                                     :msg-payload   {:timestamp test-ts :path path}}))]
+    (testing "cfg set is initially empty"
+      (is (empty? (:show-maps-for (:cfg current-state)))))
+    (testing "set contains timestamp after initial toggle"
+      (is (contains? (:show-maps-for (:cfg new-state)) test-ts)))
+    (testing "timestamp removed from set after subsequent toggle"
+      (is (not (contains? (:show-maps-for (:cfg new-state1)) 1465059139281))))))
(ns iwaswhere-web.client-store-test
  "Here, we test the handler functions of the server side store component."
  (:require [clojure.test :refer [deftest testing is]]
            [iwaswhere-web.client-store :as store]))

(def empty-query

   :timestamp      1465059173965
   :md             "Moving to #cljc"})

(def state-from-backend
  {:entries [test-entry st/some-test-entry]
   :linked-entries-list []
   :hashtags            #{"#drama" "#hashtag" "#blah"}
   :mentions            #{"@myself" "@me" "@I"}
   :stats               {:entry-count 4118 :node-count 5636 :edge-count 28726}
   :duration-ms         19})

(def entry-update
  {:timestamp 1465059173965
   :md        "Moving to #cljc. Edited entry."})

(def entry-geo-update
  {:mentions   #{}
   :tags       #{}
   :timezone   "Europe/Berlin"
   :new-entry  true
   :utc-offset -120
   :longitude  9.9
   :latitude   53.5
   :timestamp  1465059173965
   :md         ""})

(deftest new-state-test
  (let [current-state @(:state (store/initial-state-fn #()))
        new-state (:new-state (store/new-state-fn {:current-state current-state
                                                   :msg-payload   state-from-backend}))]
    (testing "entries are on new state"
      (is (= (:entries new-state) (:entries state-from-backend))))
    (testing "entries map is on new state"
      (is (= (:entries-map new-state)
             (into {} (map (fn [entry] [(:timestamp entry) entry]) (:entries state-from-backend))))))
    (testing "hashtags are on new state"
      (is (= (:hashtags (:cfg new-state)) (:hashtags state-from-backend))))
    (testing "mentions are on new state"
      (is (= (:mentions (:cfg new-state)) (:mentions state-from-backend))))
    (testing "stats are on new state"
      (is (= (:stats new-state) (:stats state-from-backend))))
    (testing "query duration is on new state"
      (is (= (:duration-ms new-state) (:duration-ms state-from-backend))))))

(deftest set-active-test
  "Test that active entry is updated properly in store component state"
  (let [current-state @(:state (store/initial-state-fn #()))
        new-state (:new-state (store/set-active-fn {:current-state current-state
                                                    :msg-payload   test-entry}))]
    (testing "active entry is set"
      (is (= test-entry (:active new-state))))))

(deftest update-query-test
  "Test that new query is updated properly in store component state"
