 (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

      (is (= :state/get (first emit-msg)))
      (is (= updated-query (second emit-msg))))))

(deftest new-entry-test
  "Test that local entry is properly set in state."
  (with-redefs [store/new-entries-ls (atom {})]
    (let [current-state @(:state (store/initial-state-fn #()))
          new-state (:new-state (store/new-entry-fn {:current-state current-state :msg-payload test-entry}))]
      (testing "pomodoro test entry in new-entries"
        (is (= test-entry (get-in new-state [:new-entries 1465059173965]))))
      (testing "new entries atom properly updated - this would be backed by localstorage on client"
        (is (= (:new-entries new-state) @store/new-entries-ls))))))

(deftest update-local-test
  "Test that local entry is properly attached to state."
  (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-state2 (:new-state (store/update-local-fn {:current-state new-state :msg-payload entry-update}))]
      (testing "pomodoro test entry in new-entries"
        (is (= test-entry (get-in new-state [:new-entries 1465059173965]))))
      (testing "entry update is merged with previous entry, thus allows omitting keys"
        (is (= (merge test-entry entry-update) (get-in new-state2 [:new-entries 1465059173965]))))
      (testing "new entries atom properly updated - this would be backed by localstorage on client"
        (is (= (:new-entries new-state2) @store/new-entries-ls))))))

(deftest geo-enrich-test
  "Test that local entry is properly attached to state."
  (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-state2 (:new-state (store/geo-enrich-fn {:current-state new-state :msg-payload entry-geo-update}))]
      (testing "pomodoro test entry in new-entries"
        (is (= test-entry (get-in new-state [:new-entries 1465059173965]))))
      (testing "entry update is merged with previous entry, thus allows omitting keys"
        (is (= (merge entry-geo-update test-entry) (get-in new-state2 [:new-entries 1465059173965]))))
      (testing "new entries atom properly updated - this would be backed by localstorage on client"
        (is (= (:new-entries new-state2) @store/new-entries-ls))))))

(deftest remove-local-test
  "Test that local entry is properly removed from state after delete message."
  (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-state2 (:new-state (store/remove-local-fn {:current-state new-state :msg-payload entry-update}))]
      (testing "pomodoro test entry in new-entries"
        (is (= test-entry (get-in new-state [:new-entries 1465059173965]))))
      (testing "entry update is removed from component state"
        (is (not (get-in new-state2 [:new-entries 1465059173965]))))
      (testing "new entries atom properly updated - this would be backed by localstorage on client"
        (is (= (:new-entries new-state2) @store/new-entries-ls))))))

(deftest entry-saved-test
  "New entry removed after backend confirms save."
  (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-state2 (:new-state (store/entry-saved-fn {:current-state new-state :msg-payload entry-update}))]
      (testing "test entry in new-entries"
        (is (= test-entry (get-in new-state [:new-entries 1465059173965]))))
      (testing "entry update is removed from component state after receiving save confirmation"
        (is (not (get-in new-state2 [:new-entries 1465059173965]))))
      (testing "new entries atom properly updated - this would be backed by localstorage on client"
        (is (= (:new-entries new-state2) @store/new-entries-ls))))))

(def pomodoro-inc-msg
  {:timestamp 1465059173965})
