All Projects → day8 → Re Frame Test

day8 / Re Frame Test

Licence: mit
Cross platform (cljs and clj) utilities for testing re-frame applications

Programming Languages

clojure
4091 projects

Projects that are alternatives of or similar to Re Frame Test

Webandappuitesting
UI automated testing framework (and examples), suitable for Web, Android, iOS, H5, and supports Android performance testing. UI自动化测试框架(及示例),适用于Web、Android、iOS、H5,并支持android性能数据的收集
Stars: ✭ 62 (-25.3%)
Mutual labels:  test
Testing Video
Generator of test video files for testing your media playback devices and calibrate TV sets
Stars: ✭ 70 (-15.66%)
Mutual labels:  test
Kitab
Kitab is the ideal companion for Documentation-Driven Quality: Render and Test your documentation.
Stars: ✭ 79 (-4.82%)
Mutual labels:  test
Tib
Easy e2e browser testing in Node
Stars: ✭ 64 (-22.89%)
Mutual labels:  test
Elmyr
A utility to make Kotlin/Java tests random yet reproducible
Stars: ✭ 68 (-18.07%)
Mutual labels:  test
Gremlins.js
Monkey testing library for web apps and Node.js
Stars: ✭ 8,790 (+10490.36%)
Mutual labels:  test
Jenkinspipelineunit
Framework for unit testing Jenkins pipelines
Stars: ✭ 1,110 (+1237.35%)
Mutual labels:  test
Testcafe
A Node.js tool to automate end-to-end web testing.
Stars: ✭ 9,176 (+10955.42%)
Mutual labels:  test
Assert
A collection of convenient assertions for Swift testing
Stars: ✭ 69 (-16.87%)
Mutual labels:  test
Httpmock
HTTP mocking library for Rust.
Stars: ✭ 76 (-8.43%)
Mutual labels:  test
Fake server
FakeServer integrates with ExUnit to make external APIs testing simpler
Stars: ✭ 64 (-22.89%)
Mutual labels:  test
Katt
KATT (Klarna API Testing Tool) is an HTTP-based API testing tool for Erlang.
Stars: ✭ 67 (-19.28%)
Mutual labels:  test
Warriorjs
🏰 An exciting game of programming and Artificial Intelligence
Stars: ✭ 8,673 (+10349.4%)
Mutual labels:  test
Subgraph
Reactive graph database for re-frame
Stars: ✭ 63 (-24.1%)
Mutual labels:  re-frame
Keycloak Clojure
A Clojure library helping the integration of Keycloak with a Clojure Application + a sample SPA Client and API Server demonstrating the Keycloak integration
Stars: ✭ 81 (-2.41%)
Mutual labels:  re-frame
Re Navigate
Example of React Native Navigation with re-frame/re-natal
Stars: ✭ 61 (-26.51%)
Mutual labels:  re-frame
Gorilla Notebook
A clojure/clojurescript notebook application/-library based on Gorilla-REPL
Stars: ✭ 73 (-12.05%)
Mutual labels:  re-frame
Wstest
go websocket client for unit testing of a websocket handler
Stars: ✭ 83 (+0%)
Mutual labels:  test
Protractor
E2E test framework for Angular apps
Stars: ✭ 8,792 (+10492.77%)
Mutual labels:  test
Pystdf
Python module for working with STDF files
Stars: ✭ 74 (-10.84%)
Mutual labels:  test

CI CD GitHub tag (latest by date) Clojars Project GitHub issues GitHub pull requests License

re-frame-test

This library provides utilities for testing re-frame applications.

These utilities:

  • allow you to use subscribe in your tests
  • allow you to use dispatch in your tests
  • allow you to run tests on both the JVM and JS platforms
  • allow you to create "end to end" integration tests, involving backend servers

For context, please be sure to read the basic testing tutorial in the main re-frame docs before going any further.

This library primarily supports the testing of Event Handlers, but Subscription Handlers get to come along for the ride.

Quick Start Guide

Step 1. Add Dependency

Add the following project dependency:
Clojars Project

Requires re-frame >= "1.1.1"

Step 2. Registration And Use

In the namespace where you register your tests, perhaps called tests.cljs, you have 2 things to do.

First, add this require to the ns:

(ns app.tests
  (:require
    ...
    [day8.re-frame.test :as rf-test]   ;; <-- add this
    ...))

Second, Define or Modify some tests.

(deftest init
  (rf-test/run-test-sync               ;; <-- add this 
    ;; with the above macro this becomes a dispatch-sync 
    ;; and app-db is isolated between tests
    (rf/dispatch [:initialise-db])   
    ;; Define subscriptions to the app state
    (let [showing         (rf/subscribe [:showing])] 
      ;;Assert the initial state
      (is (= :all @showing)))))

How It Works

re-frame-test provides two macros which dovetail with cljs.test.

run-test-sync

Execute body as a test, where each dispatch call is executed synchronously (via dispatch-sync), and any subsequent dispatches which are caused by that dispatch are also fully handled/executed prior to control flow returning to your test.

Think of it as though every dispatch in your app had been magically turned into dispatch-sync, and re-frame had lifted the restriction that says you can't call dispatch-sync from within an event handler.

This macro is applicable for most events that do not run async behaviour within the event.

From the todomvc example:

(defn test-fixtures
  []
  ;; change this coeffect to make tests start with nothing
  (rf/reg-cofx
    :local-store-todos
    (fn [cofx _]
      "Read in todos from localstore, and process into a map we can merge into app-db."
      (assoc cofx :local-store-todos
                  (sorted-map)))))

Define some test-fixtures. In this case we have to ignore the localstore in the tests.

(deftest basic--sync
  (rf-test/run-test-sync
    (test-fixtures)
    (rf/dispatch [:initialise-db])

Use the run-test-sync macro to construct the tests and initialise the app state. Note that, the dispatch will be handled before the following code is executed, effectively turning it into a dispatch-sync. Also any changes to the database and registrations will be rolled back at the termination of the test, therefore our fixtures are run within the run-test-sync macro.

        ;; Define subscriptions to the app state
        (let [showing         (rf/subscribe [:showing])
              sorted-todos    (rf/subscribe [:sorted-todos])
              todos           (rf/subscribe [:todos])
              visible-todos   (rf/subscribe [:visible-todos])
              all-complete?   (rf/subscribe [:all-complete?])
              completed-count (rf/subscribe [:completed-count])
              footer-counts   (rf/subscribe [:footer-counts])] 
                 
          ;;Assert the initial state
          (is (= :all @showing))
          (is (empty? @sorted-todos))
          (is (empty? @todos))
          (is (empty? @visible-todos))
          (is (= false (boolean @all-complete?)))
          (is (= 0 @completed-count))
          (is (= [0 0] @footer-counts)) 
             
          ;;Dispatch the event that you want to test, remember that `re-frame-test` 
          ;;will process this event immediately.
          (rf/dispatch [:add-todo "write first test"])
              
          ;;Test that the dispatch has mutated the state in the way that we expect.
          (is (= 1 (count @todos) (count @visible-todos) (count @sorted-todos)))
          (is (= 0 @completed-count))
          (is (= [1 0] @footer-counts))
          (is (= {:id 1, :title "write first test", :done false}
                 (first @todos)))

run-test-async

This macro is applicable for events that do run some async behaviour (usually outside or re-frame, e.g. an http request) within the event.

Run body as an async re-frame test. The async nature means you'll need to use wait-for any time you want to make any assertions that should be true after an event has been handled. It's assumed that there will be at least one wait-for in the body of your test (otherwise you don't need this macro at all).

Note: unlike regular ClojureScript cljs.test/async tests, wait-for takes care of calling (done) for you: you don't need to do anything specific to handle the fact that your test is asynchronous, other than make sure that all your assertions happen with wait-for blocks.

From the todomvc example:

(deftest basic--async
  (rf-test/run-test-async
    (test-fixtures)
    (rf/dispatch-sync [:initialise-db])

Use the run-test-async macro to construct the tests and initialise the app state note that the dispatch-sync must be used as this macro does not run the dispatch immediately like run-test-sync. Also any changes to the database and registrations will be rolled back at the termination of the test, therefore our fixtures are run within the run-test-async macro.

    ;;Define subscriptions to the app state
    (let [showing         (rf/subscribe [:showing])
          sorted-todos    (rf/subscribe [:sorted-todos])
          todos           (rf/subscribe [:todos])
          visible-todos   (rf/subscribe [:visible-todos])
          all-complete?   (rf/subscribe [:all-complete?])
          completed-count (rf/subscribe [:completed-count])
          footer-counts   (rf/subscribe [:footer-counts])]
          
      ;;Assert the initial state
      (is (= :all @showing))
      (is (empty? @sorted-todos))
      (is (empty? @todos))
      (is (empty? @visible-todos))
      (is (= 0 @completed-count))
                    
      ;;Dispatch the event that you want to test, remember 
      ;;that re-frame will not process this event immediately, 
      ;;and need to use the `wait-for` macro to continue the tests.
      (rf/dispatch [:add-todo "write first test"])
          
          
      ;;Wait for the `:add-todo` event to be dispatched 
      ;;(note, in the use of this macro we would typically wait for 
      ;;an event that had been triggered by the successful return of 
      ;;the async event).        
      (rf-test/wait-for [:add-todo-finished]
          
        ;;Test that the dispatch has mutated the state in the way 
        ;;that we expect.    
        (is (= [{:id 1, :title "write first test", :done false}] @todos))

Here we have assumed that the :add-todo event will make some sort of async call which will in turn generate an add-todo-finished event when it has finished. This is not actually the case in the example code.

Running the CLJS tests with Karma

You will need npm, with:

$ npm install -g karma karma-cli karma-cljs-test karma-junit-reporter karma-chrome-launcher

And you will need Chrome.

License

Copyright (c) 2016 Mike Thompson

Distributed under the The MIT License (MIT).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].