Skip to content

Add tests #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ out
example-app/figwheel_server.log
example-app/resources/public/js/
example-app/target/
.cljsbuild
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
default: help

test:: ## Run tests
lein do clean, doo phantom test once

.PHONY: help

help:
@echo "usage: make target ..."
@echo "available targets:"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
| sort \
| awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# re-crud

[![Clojars Project](https://img.shields.io/clojars/v/org.omnyway/re-crud.svg)](https://clojars.org/org.omnyway/re-crud)
[![CircleCI](https://circleci.com/gh/omnypay/re-crud.svg?style=svg)](https://circleci.com/gh/omnypay/re-crud)

A re-frame library for developing CRUD applications.

Expand All @@ -13,24 +14,29 @@ swagger.json and perform HTTP calls to show, list, create and update resources.
## Installation
re-crud is (will be) on clojars: `[re-crud "0.1.0"]`

## Usage
## Example app
- There is a comprehensive example-app in this repo: [example-app](example-app/)
- You can find a running instance of this app here: [demo](https://omnypay.github.io/re-crud/)
- This uses a fairly minimal CRUD web service written in Rails, deployed here: [web app](https://re-crud-example.herokuapp.com/swagger/index.html)

The [example-app](example-app/) uses all features of `re-crud`.
## Usage

Initialize `re-crud` soon after you initialize your re-frame app db.
`re-crud.core/init` takes a map of `service-name`:`config` pairs.

```clojure
{"re-crud-example" {:service-host "https://my-service.host"
:swagger-url "https://my-service.host/swagger.json"
:dispatch-on-ready [:on-ready]}}
(require '[re-crud.core :as crud])
(crud/init
{"service-name" {:service-host "https://my-service.host"
:swagger-url "https://my-service.host/swagger.json"
:dispatch-on-ready [:on-ready]}})
```

The event `dispatch-on-ready` event from service config is dispatched once `re-crud` has parsed the swagger spec for that service.
The `dispatch-on-ready` event from service config is dispatched once `re-crud` has parsed the swagger spec for that service. Initialize your views after this event has been triggered.

Add [crud.css](css/crud.css) in your app for styling. `re-crud` comes with a skin that appies [MUI CSS](https://www.muicss.com/).

Use component-generators from `re-crud.components` to generate the view compnent and associated events.
Use component-generators from `re-crud.components` to generate the view compnent and associated events.

A simple component to retrieve and display a resource would look like this.

Expand All @@ -48,6 +54,8 @@ A simple component to retrieve and display a resource would look like this.
- `:fetch` describes how to fetch the resource to show
- `:view` configures UI details

TODO: add documentation around `:load-component`

Here's a slightly more involved example:

```clojure
Expand Down Expand Up @@ -83,6 +91,10 @@ Here's an example of what you''d get on creating a component
- `:events` are `id`s of the re-frame events you can dispatch
- `:events :refresh` needs to be implemented by the user

## Running tests

`make test` (requires phantomjs)

## License - Apache 2.0

Copyright 2017 Omnyway Inc.
Expand All @@ -98,4 +110,3 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

10 changes: 10 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
machine:
java:
version: oraclejdk8

test:
override:
- lein run -m re-crud.mock-http-server:
background: true
- for i in `seq 120`; do nc -v -w 1 localhost 8000 && break ; sleep 1; done
- make test
21 changes: 17 additions & 4 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@
[camel-snake-kebab "0.4.0"]
[cljsjs/reactable "0.14.1-0"]]

:plugins [[lein-cljsbuild "1.1.4"]]
:hooks [leiningen.cljsbuild]
:profiles {:dev {:dependencies [[http-kit "2.2.0"]
[bidi "2.0.13"]
[ring/ring-core "1.5.0"]
[ring/ring-devel "1.5.0"]
[ring/ring-json "0.4.0"]]}}
:plugins [[lein-cljsbuild "1.1.4"]
[lein-doo "0.1.7"]]

:min-lein-version "2.5.3"
:doo {:paths {:phantom "phantomjs --web-security=false"}}

:source-paths ["src/cljs"]
:source-paths ["test/clj"]

:clean-targets ^{:protect false} ["resources/public/js/compiled" "target"]

Expand All @@ -37,4 +44,10 @@
:output-to "public/re-crud.js"
:optimizations :whitespace
:pretty-print true
:closure-defines {goog.DEBUG false}}}]})
:closure-defines {goog.DEBUG false}}}
{:id "test"
:source-paths ["src/cljs" "test/cljs"]
:compiler {:output-dir ".cljsbuild/re-crud-test"
:output-to "re-crud-test.js"
:main re-crud.test-runner
:pretty-print true}}]})
41 changes: 41 additions & 0 deletions test/clj/re_crud/mock_http_server.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(ns re-crud.mock-http-server
(:require [org.httpkit.server :as http-server]
[bidi.ring :as bidi]
[ring.middleware.json :refer [wrap-json-body wrap-json-response]]
[ring.util.response :as response]
[re-crud.test-data :as data]))

(def server-instance (atom nil))

(defn swagger [_]
(response/response data/swagger))

(defn get-user [_]
(response/response data/user))

(defn update-user [_]
(response/response data/user))

(def handler
(bidi/make-handler
["/" [["swagger.json" swagger]
["users" [[true {:get get-user
:patch update-user}]]]]]))

(def server
(-> handler
(wrap-json-body {:keywords? true :bigdecimals? true})
(wrap-json-response)))

(defn start-server []
(when-not (some? @server-instance)
(reset! server-instance (http-server/run-server #'server {:port 8000}))
(println "Web server started at port" 8000)))

(defn stop-server []
(when-not (nil? @server-instance)
(@server-instance :timeout 100)
(reset! server-instance nil)))

(defn -main [& args]
(start-server))
94 changes: 94 additions & 0 deletions test/clj/re_crud/test_data.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
(ns re-crud.test-data
(:import [java.sql Timestamp]))

(defn timestamp []
(Timestamp. (System/currentTimeMillis)))

(def swagger
{"swagger" "2.0"
"info"
{"title" "re-crud-example API"
"description" "This is a regular CRUD app for demo purposes"
"version" "1.0.0"}
"host" "localhost:8000"
"schemes" ["https"]
"basePath" "/"
"produces" ["application/json"]
"paths"
{"/users"
{"get"
{"operationId" "listUsers"
"summary" "List all Users"
"responses"
{"200"
{"description" "An array of users"
"schema" {"type" "array"
"items" {"$ref" "#/definitions/User"}}}}}
"post"
{"summary" "Create a user"
"operationId" "createUser"
"responses" {"200" {"description" "Create A user"
"schema" {"$ref" "#/definitions/User"}}}
"parameters" [{"name" "body"
"in" "body"
"required" true
"schema" {"$ref" "#/definitions/UserParam"}}]}}
"/users/{user-id}"
{"get"
{"summary" "Get a user"
"operationId" "getUser"
"responses" {"200" {"description" "A user"
"schema" {"$ref" "#/definitions/User"}}}
"parameters" [{"name" "user-id"
"in" "path"
"required" true
"type" "number"
"format" "integer"}]}
"patch"
{"summary" "Update a user info"
"operationId" "updateUser"
"responses" {"200" {"description" "Update A user"
"schema" {"$ref" "#/definitions/User"}}}
"parameters"
[{"name" "user-id"
"in" "path"
"required" true
"type" "number"
"format" "integer"}
{"name" "UserParam"
"in" "body"
"required" true
"schema" {"$ref" "#/definitions/UserParam"}}]}
"delete"
{"summary" "Delete a user"
"operationId" "deleteUser"
"responses" {"204" {"description" "User deleted empty response"}}
"parameters" [{"name" "user-id"
"in" "path"
"required" true
"type" "number"
"format" "integer"}]}}}
"definitions"
{"User"
{"type" "object"
"properties"
{"id" {"type" "integer"}
"first_name" {"type" "string"}
"last_name" {"type" "string"}
"email" {"type" "string"}
"created_at" {"type" "string"}
"updated_at" {"type" "string"}}}
"UserParam"
{"type" "object"
"properties" {"first_name" {"type" "string"}
"last_name" {"type" "string"}
"email" {"type" "string"}}}}})

(def user
{:id 1
:first_name "Foo"
:last_name "Bar"
:email "[email protected]"
:created_at (timestamp)
:updated_at (timestamp)
:url "/user/1"})
67 changes: 67 additions & 0 deletions test/cljs/re_crud/core_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
(ns re-crud.core-test
(:require [cljs.test :refer-macros [deftest is testing run-tests async]]
[re-frame.registrar :as registrar]
[re-frame.core :as re-frame]
[re-crud.core :as crud]
[re-crud.components :as comp]
[re-crud.components.utils :as u]))

(defonce things-to-test
(atom #{:create-components
:fetch
:perform}))

(defn init []
(crud/init
{"test-service" {:service-host "http://localhost:8000"
:swagger-url "http://localhost:8000/swagger.json"
:dispatch-on-ready [:on-ready]}}))

(defn create-components [{:keys [db]} _]
(let [component (comp/update {:id :user.update
:fetch {:operation-id "getUser"
:after (u/create-fx (fn [_]
(re-frame/dispatch [:invoke-perform])
(re-frame/dispatch [:assert! :fetch])))}
:form {:operation-id "updateUser"}
:perform {:operation-id "updateUser"
:after (u/create-fx (fn [response]
(re-frame/dispatch [:assert! :perform response])))}
:config {:service-name "test-service"}})]
{:dispatch-n [[:crud-load-component component {:fetch {:user-id 1}
:form {:user-id 1}}]
[:assert! :create-components]]}))

(defn invoke-perform [{:keys [db]} _]
(let [data (-> (get-in db (u/resource-path :user.update))
(assoc :user-id 1))]
{:db (assoc-in db (u/user-input-path :user.update) data)
:dispatch [:crud-perform-user.update]}))

(defn create-components-assertions []
(is (some? (registrar/get-handler :event :crud-fetch-user.update)))
(is (some? (registrar/get-handler :event :crud-perform-user.update))))

(defn assert! [{:keys [db]} [_ thing-to-test]]
(swap! things-to-test disj thing-to-test)
(case thing-to-test
:create-components (create-components-assertions)
:fetch (is (= (set (keys (get-in db (u/resource-path :user.update))))
#{:id :first_name :last_name :email :created_at :updated_at :url}))
:perform (is (= (set (keys (get-in db (u/resource-path :user.update))))
#{:id :first_name :last_name :email :created_at :updated_at :url})))
(if (empty? @things-to-test)
{:done []}
{}))

(defn register-events [done]
(re-frame/reg-event-fx :assert! assert!)
(re-frame/reg-event-fx :invoke-perform invoke-perform)
(re-frame/reg-event-fx :create-components create-components)
(re-frame/reg-event-fx :on-ready (fn [_ _] {:dispatch [:create-components]}))
(re-frame/reg-fx :done (fn [_] (done))))

(deftest event-handlers
(async done
(register-events done)
(init)))
5 changes: 5 additions & 0 deletions test/cljs/re_crud/test_runner.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns re-crud.test-runner
(:require [doo.runner :refer-macros [doo-tests]]
[re-crud.core-test]))

(doo-tests 're-crud.core-test)