Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Further work on converting js codebase to cljs. #54

Merged
merged 1 commit into from

1 participant

@zoldar
Collaborator

No description provided.

@zoldar zoldar merged commit 862b244 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 21, 2013
  1. @zoldar
This page is out of date. Refresh to see the latest.
View
1  resources/public/tests/index.html
@@ -9,6 +9,7 @@
<div id="qunit"></div>
<script src="/tests/js/qunit.js"></script>
<script src="//code.jquery.com/jquery-latest.js"></script>
+ <script src="/tests/js/jquery.mockjax.js"></script>
<script src="/tests/js/librarian-tests.js"></script>
</body>
</html>
View
570 resources/public/tests/js/jquery.mockjax.js
@@ -0,0 +1,570 @@
+/*!
+ * MockJax - jQuery Plugin to Mock Ajax requests
+ *
+ * Version: 1.5.1
+ * Released:
+ * Home: http://github.com/appendto/jquery-mockjax
+ * Author: Jonathan Sharp (http://jdsharp.com)
+ * License: MIT,GPL
+ *
+ * Copyright (c) 2011 appendTo LLC.
+ * Dual licensed under the MIT or GPL licenses.
+ * http://appendto.com/open-source-licenses
+ */
+(function($) {
+ var _ajax = $.ajax,
+ mockHandlers = [],
+ CALLBACK_REGEX = /=\?(&|$)/,
+ jsc = (new Date()).getTime();
+
+
+ // Parse the given XML string.
+ function parseXML(xml) {
+ if ( window['DOMParser'] == undefined && window.ActiveXObject ) {
+ DOMParser = function() { };
+ DOMParser.prototype.parseFromString = function( xmlString ) {
+ var doc = new ActiveXObject('Microsoft.XMLDOM');
+ doc.async = 'false';
+ doc.loadXML( xmlString );
+ return doc;
+ };
+ }
+
+ try {
+ var xmlDoc = ( new DOMParser() ).parseFromString( xml, 'text/xml' );
+ if ( $.isXMLDoc( xmlDoc ) ) {
+ var err = $('parsererror', xmlDoc);
+ if ( err.length == 1 ) {
+ throw('Error: ' + $(xmlDoc).text() );
+ }
+ } else {
+ throw('Unable to parse XML');
+ }
+ } catch( e ) {
+ var msg = ( e.name == undefined ? e : e.name + ': ' + e.message );
+ $(document).trigger('xmlParseError', [ msg ]);
+ return undefined;
+ }
+ return xmlDoc;
+ }
+
+ // Trigger a jQuery event
+ function trigger(s, type, args) {
+ (s.context ? $(s.context) : $.event).trigger(type, args);
+ }
+
+ // Check if the data field on the mock handler and the request match. This
+ // can be used to restrict a mock handler to being used only when a certain
+ // set of data is passed to it.
+ function isMockDataEqual( mock, live ) {
+ var identical = false;
+ // Test for situations where the data is a querystring (not an object)
+ if (typeof live === 'string') {
+ // Querystring may be a regex
+ return $.isFunction( mock.test ) ? mock.test(live) : mock == live;
+ }
+ $.each(mock, function(k, v) {
+ if ( live[k] === undefined ) {
+ identical = false;
+ return identical;
+ } else {
+ identical = true;
+ if ( typeof live[k] == 'object' ) {
+ return isMockDataEqual(mock[k], live[k]);
+ } else {
+ if ( $.isFunction( mock[k].test ) ) {
+ identical = mock[k].test(live[k]);
+ } else {
+ identical = ( mock[k] == live[k] );
+ }
+ return identical;
+ }
+ }
+ });
+
+ return identical;
+ }
+
+ // Check the given handler should mock the given request
+ function getMockForRequest( handler, requestSettings ) {
+ // If the mock was registered with a function, let the function decide if we
+ // want to mock this request
+ if ( $.isFunction(handler) ) {
+ return handler( requestSettings );
+ }
+
+ // Inspect the URL of the request and check if the mock handler's url
+ // matches the url for this ajax request
+ if ( $.isFunction(handler.url.test) ) {
+ // The user provided a regex for the url, test it
+ if ( !handler.url.test( requestSettings.url ) ) {
+ return null;
+ }
+ } else {
+ // Look for a simple wildcard '*' or a direct URL match
+ var star = handler.url.indexOf('*');
+ if (handler.url !== requestSettings.url && star === -1 ||
+ !new RegExp(handler.url.replace(/[-[\]{}()+?.,\\^$|#\s]/g, "\\$&").replace('*', '.+')).test(requestSettings.url)) {
+ return null;
+ }
+ }
+
+ // Inspect the data submitted in the request (either POST body or GET query string)
+ if ( handler.data && requestSettings.data ) {
+ if ( !isMockDataEqual(handler.data, requestSettings.data) ) {
+ // They're not identical, do not mock this request
+ return null;
+ }
+ }
+ // Inspect the request type
+ if ( handler && handler.type &&
+ handler.type.toLowerCase() != requestSettings.type.toLowerCase() ) {
+ // The request type doesn't match (GET vs. POST)
+ return null;
+ }
+
+ return handler;
+ }
+
+ // If logging is enabled, log the mock to the console
+ function logMock( mockHandler, requestSettings ) {
+ if ( window.console && console.log ) {
+ var message = 'MOCK ' + requestSettings.type.toUpperCase() + ': ' + requestSettings.url;
+ var request = $.extend({}, requestSettings);
+
+ if (typeof console.log === 'function') {
+ console.log(message, request);
+ } else {
+ try {
+ console.log( message + ' ' + JSON.stringify(request) );
+ } catch (e) {
+ console.log(message);
+ }
+ }
+ }
+ }
+
+ // Process the xhr objects send operation
+ function _xhrSend(mockHandler, requestSettings, origSettings) {
+
+ // This is a substitute for < 1.4 which lacks $.proxy
+ var process = (function(that) {
+ return function() {
+ return (function() {
+ // The request has returned
+ this.status = mockHandler.status;
+ this.statusText = mockHandler.statusText;
+ this.readyState = 4;
+
+ // We have an executable function, call it to give
+ // the mock handler a chance to update it's data
+ if ( $.isFunction(mockHandler.response) ) {
+ mockHandler.response(origSettings);
+ }
+ // Copy over our mock to our xhr object before passing control back to
+ // jQuery's onreadystatechange callback
+ if ( requestSettings.dataType == 'json' && ( typeof mockHandler.responseText == 'object' ) ) {
+ this.responseText = JSON.stringify(mockHandler.responseText);
+ } else if ( requestSettings.dataType == 'xml' ) {
+ if ( typeof mockHandler.responseXML == 'string' ) {
+ this.responseXML = parseXML(mockHandler.responseXML);
+ } else {
+ this.responseXML = mockHandler.responseXML;
+ }
+ } else {
+ this.responseText = mockHandler.responseText;
+ }
+ if( typeof mockHandler.status == 'number' || typeof mockHandler.status == 'string' ) {
+ this.status = mockHandler.status;
+ }
+ if( typeof mockHandler.statusText === "string") {
+ this.statusText = mockHandler.statusText;
+ }
+ // jQuery < 1.4 doesn't have onreadystate change for xhr
+ if ( $.isFunction(this.onreadystatechange) ) {
+ if( mockHandler.isTimeout) {
+ this.status = -1;
+ }
+ this.onreadystatechange( mockHandler.isTimeout ? 'timeout' : undefined );
+ } else if ( mockHandler.isTimeout ) {
+ // Fix for 1.3.2 timeout to keep success from firing.
+ this.status = -1;
+ }
+ }).apply(that);
+ };
+ })(this);
+
+ if ( mockHandler.proxy ) {
+ // We're proxying this request and loading in an external file instead
+ _ajax({
+ global: false,
+ url: mockHandler.proxy,
+ type: mockHandler.proxyType,
+ data: mockHandler.data,
+ dataType: requestSettings.dataType === "script" ? "text/plain" : requestSettings.dataType,
+ complete: function(xhr, txt) {
+ mockHandler.responseXML = xhr.responseXML;
+ mockHandler.responseText = xhr.responseText;
+ mockHandler.status = xhr.status;
+ mockHandler.statusText = xhr.statusText;
+ this.responseTimer = setTimeout(process, mockHandler.responseTime || 0);
+ }
+ });
+ } else {
+ // type == 'POST' || 'GET' || 'DELETE'
+ if ( requestSettings.async === false ) {
+ // TODO: Blocking delay
+ process();
+ } else {
+ this.responseTimer = setTimeout(process, mockHandler.responseTime || 50);
+ }
+ }
+ }
+
+ // Construct a mocked XHR Object
+ function xhr(mockHandler, requestSettings, origSettings, origHandler) {
+ // Extend with our default mockjax settings
+ mockHandler = $.extend(true, {}, $.mockjaxSettings, mockHandler);
+
+ if (typeof mockHandler.headers === 'undefined') {
+ mockHandler.headers = {};
+ }
+ if ( mockHandler.contentType ) {
+ mockHandler.headers['content-type'] = mockHandler.contentType;
+ }
+
+ return {
+ status: mockHandler.status,
+ statusText: mockHandler.statusText,
+ readyState: 1,
+ open: function() { },
+ send: function() {
+ origHandler.fired = true;
+ _xhrSend.call(this, mockHandler, requestSettings, origSettings);
+ },
+ abort: function() {
+ clearTimeout(this.responseTimer);
+ },
+ setRequestHeader: function(header, value) {
+ mockHandler.headers[header] = value;
+ },
+ getResponseHeader: function(header) {
+ // 'Last-modified', 'Etag', 'content-type' are all checked by jQuery
+ if ( mockHandler.headers && mockHandler.headers[header] ) {
+ // Return arbitrary headers
+ return mockHandler.headers[header];
+ } else if ( header.toLowerCase() == 'last-modified' ) {
+ return mockHandler.lastModified || (new Date()).toString();
+ } else if ( header.toLowerCase() == 'etag' ) {
+ return mockHandler.etag || '';
+ } else if ( header.toLowerCase() == 'content-type' ) {
+ return mockHandler.contentType || 'text/plain';
+ }
+ },
+ getAllResponseHeaders: function() {
+ var headers = '';
+ $.each(mockHandler.headers, function(k, v) {
+ headers += k + ': ' + v + "\n";
+ });
+ return headers;
+ }
+ };
+ }
+
+ // Process a JSONP mock request.
+ function processJsonpMock( requestSettings, mockHandler, origSettings ) {
+ // Handle JSONP Parameter Callbacks, we need to replicate some of the jQuery core here
+ // because there isn't an easy hook for the cross domain script tag of jsonp
+
+ processJsonpUrl( requestSettings );
+
+ requestSettings.dataType = "json";
+ if(requestSettings.data && CALLBACK_REGEX.test(requestSettings.data) || CALLBACK_REGEX.test(requestSettings.url)) {
+ createJsonpCallback(requestSettings, mockHandler);
+
+ // We need to make sure
+ // that a JSONP style response is executed properly
+
+ var rurl = /^(\w+:)?\/\/([^\/?#]+)/,
+ parts = rurl.exec( requestSettings.url ),
+ remote = parts && (parts[1] && parts[1] !== location.protocol || parts[2] !== location.host);
+
+ requestSettings.dataType = "script";
+ if(requestSettings.type.toUpperCase() === "GET" && remote ) {
+ var newMockReturn = processJsonpRequest( requestSettings, mockHandler, origSettings );
+
+ // Check if we are supposed to return a Deferred back to the mock call, or just
+ // signal success
+ if(newMockReturn) {
+ return newMockReturn;
+ } else {
+ return true;
+ }
+ }
+ }
+ return null;
+ }
+
+ // Append the required callback parameter to the end of the request URL, for a JSONP request
+ function processJsonpUrl( requestSettings ) {
+ if ( requestSettings.type.toUpperCase() === "GET" ) {
+ if ( !CALLBACK_REGEX.test( requestSettings.url ) ) {
+ requestSettings.url += (/\?/.test( requestSettings.url ) ? "&" : "?") +
+ (requestSettings.jsonp || "callback") + "=?";
+ }
+ } else if ( !requestSettings.data || !CALLBACK_REGEX.test(requestSettings.data) ) {
+ requestSettings.data = (requestSettings.data ? requestSettings.data + "&" : "") + (requestSettings.jsonp || "callback") + "=?";
+ }
+ }
+
+ // Process a JSONP request by evaluating the mocked response text
+ function processJsonpRequest( requestSettings, mockHandler, origSettings ) {
+ // Synthesize the mock request for adding a script tag
+ var callbackContext = origSettings && origSettings.context || requestSettings,
+ newMock = null;
+
+
+ // If the response handler on the moock is a function, call it
+ if ( mockHandler.response && $.isFunction(mockHandler.response) ) {
+ mockHandler.response(origSettings);
+ } else {
+
+ // Evaluate the responseText javascript in a global context
+ if( typeof mockHandler.responseText === 'object' ) {
+ $.globalEval( '(' + JSON.stringify( mockHandler.responseText ) + ')');
+ } else {
+ $.globalEval( '(' + mockHandler.responseText + ')');
+ }
+ }
+
+ // Successful response
+ jsonpSuccess( requestSettings, mockHandler );
+ jsonpComplete( requestSettings, mockHandler );
+
+ // If we are running under jQuery 1.5+, return a deferred object
+ if($.Deferred){
+ newMock = new $.Deferred();
+ if(typeof mockHandler.responseText == "object"){
+ newMock.resolveWith( callbackContext, [mockHandler.responseText] );
+ }
+ else{
+ newMock.resolveWith( callbackContext, [$.parseJSON( mockHandler.responseText )] );
+ }
+ }
+ return newMock;
+ }
+
+
+ // Create the required JSONP callback function for the request
+ function createJsonpCallback( requestSettings, mockHandler ) {
+ jsonp = requestSettings.jsonpCallback || ("jsonp" + jsc++);
+
+ // Replace the =? sequence both in the query string and the data
+ if ( requestSettings.data ) {
+ requestSettings.data = (requestSettings.data + "").replace(CALLBACK_REGEX, "=" + jsonp + "$1");
+ }
+
+ requestSettings.url = requestSettings.url.replace(CALLBACK_REGEX, "=" + jsonp + "$1");
+
+
+ // Handle JSONP-style loading
+ window[ jsonp ] = window[ jsonp ] || function( tmp ) {
+ data = tmp;
+ jsonpSuccess( requestSettings, mockHandler );
+ jsonpComplete( requestSettings, mockHandler );
+ // Garbage collect
+ window[ jsonp ] = undefined;
+
+ try {
+ delete window[ jsonp ];
+ } catch(e) {}
+
+ if ( head ) {
+ head.removeChild( script );
+ }
+ };
+ }
+
+ // The JSONP request was successful
+ function jsonpSuccess(requestSettings, mockHandler) {
+ // If a local callback was specified, fire it and pass it the data
+ if ( requestSettings.success ) {
+ requestSettings.success.call( callbackContext, ( mockHandler.response ? mockHandler.response.toString() : mockHandler.responseText || ''), status, {} );
+ }
+
+ // Fire the global callback
+ if ( requestSettings.global ) {
+ trigger(requestSettings, "ajaxSuccess", [{}, requestSettings] );
+ }
+ }
+
+ // The JSONP request was completed
+ function jsonpComplete(requestSettings, mockHandler) {
+ // Process result
+ if ( requestSettings.complete ) {
+ requestSettings.complete.call( callbackContext, {} , status );
+ }
+
+ // The request was completed
+ if ( requestSettings.global ) {
+ trigger( "ajaxComplete", [{}, requestSettings] );
+ }
+
+ // Handle the global AJAX counter
+ if ( requestSettings.global && ! --$.active ) {
+ $.event.trigger( "ajaxStop" );
+ }
+ }
+
+
+ // The core $.ajax replacement.
+ function handleAjax( url, origSettings ) {
+ var mockRequest, requestSettings, mockHandler;
+
+ // If url is an object, simulate pre-1.5 signature
+ if ( typeof url === "object" ) {
+ origSettings = url;
+ url = undefined;
+ } else {
+ // work around to support 1.5 signature
+ origSettings.url = url;
+ }
+
+ // Extend the original settings for the request
+ requestSettings = $.extend(true, {}, $.ajaxSettings, origSettings);
+
+ // Iterate over our mock handlers (in registration order) until we find
+ // one that is willing to intercept the request
+ for(var k = 0; k < mockHandlers.length; k++) {
+ if ( !mockHandlers[k] ) {
+ continue;
+ }
+
+ mockHandler = getMockForRequest( mockHandlers[k], requestSettings );
+ if(!mockHandler) {
+ // No valid mock found for this request
+ continue;
+ }
+
+ // Handle console logging
+ logMock( mockHandler, requestSettings );
+
+
+ if ( requestSettings.dataType === "jsonp" ) {
+ if ((mockRequest = processJsonpMock( requestSettings, mockHandler, origSettings ))) {
+ // This mock will handle the JSONP request
+ return mockRequest;
+ }
+ }
+
+
+ // Removed to fix #54 - keep the mocking data object intact
+ //mockHandler.data = requestSettings.data;
+
+ mockHandler.cache = requestSettings.cache;
+ mockHandler.timeout = requestSettings.timeout;
+ mockHandler.global = requestSettings.global;
+
+ copyUrlParameters(mockHandler, origSettings);
+
+ (function(mockHandler, requestSettings, origSettings, origHandler) {
+ mockRequest = _ajax.call($, $.extend(true, {}, origSettings, {
+ // Mock the XHR object
+ xhr: function() { return xhr( mockHandler, requestSettings, origSettings, origHandler ) }
+ }));
+ })(mockHandler, requestSettings, origSettings, mockHandlers[k]);
+
+ return mockRequest;
+ }
+
+ // We don't have a mock request, trigger a normal request
+ return _ajax.apply($, [origSettings]);
+ }
+
+ /**
+ * Copies URL parameter values if they were captured by a regular expression
+ * @param {Object} mockHandler
+ * @param {Object} origSettings
+ */
+ function copyUrlParameters(mockHandler, origSettings) {
+ //parameters aren't captured if the URL isn't a RegExp
+ if (!mockHandler.url instanceof RegExp) {
+ return;
+ }
+ //if no URL params were defined on the handler, don't attempt a capture
+ if (!mockHandler.hasOwnProperty('urlParams')) {
+ return;
+ }
+ var captures = mockHandler.url.exec(origSettings.url);
+ //the whole RegExp match is always the first value in the capture results
+ if (captures.length === 1) {
+ return;
+ }
+ captures.shift();
+ //use handler params as keys and capture resuts as values
+ var i = 0,
+ capturesLength = captures.length,
+ paramsLength = mockHandler.urlParams.length,
+ //in case the number of params specified is less than actual captures
+ maxIterations = Math.min(capturesLength, paramsLength),
+ paramValues = {};
+ for (i; i < maxIterations; i++) {
+ var key = mockHandler.urlParams[i];
+ paramValues[key] = captures[i];
+ }
+ origSettings.urlParams = paramValues;
+ }
+
+
+ // Public
+
+ $.extend({
+ ajax: handleAjax
+ });
+
+ $.mockjaxSettings = {
+ //url: null,
+ //type: 'GET',
+ log: function( msg ) {
+ if (window['console'] && window.console.log) {
+ var log = Function.prototype.bind.call(console.log, console);
+ log.apply(console, arguments);
+ }
+ },
+ status: 200,
+ statusText: "OK",
+ responseTime: 500,
+ isTimeout: false,
+ contentType: 'text/plain',
+ response: '',
+ responseText: '',
+ responseXML: '',
+ proxy: '',
+ proxyType: 'GET',
+
+ lastModified: null,
+ etag: '',
+ headers: {
+ etag: 'IJF@H#@923uf8023hFO@I#H#',
+ 'content-type' : 'text/plain'
+ }
+ };
+
+ $.mockjax = function(settings) {
+ var i = mockHandlers.length;
+ mockHandlers[i] = settings;
+ return i;
+ };
+ $.mockjaxClear = function(i) {
+ if ( arguments.length == 1 ) {
+ mockHandlers[i] = null;
+ } else {
+ mockHandlers = [];
+ }
+ };
+ $.mockjax.handler = function(i) {
+ if ( arguments.length == 1 ) {
+ return mockHandlers[i];
+ }
+ };
+})(jQuery);
View
143 src-cljs/librarian_clojure/frontend.cljs
@@ -4,9 +4,6 @@
(:require [jayq.core :as jq]
[clojure.string :as str]))
-(defn hello [x]
- (js/alert (str "Hello " x "!")))
-
(defn create-button
[& {:keys [button-class icon caption callback]}]
(let [button ($ "<button>")]
@@ -21,12 +18,150 @@
(jq/on button :click callback))
button))
+(defn book-id-from-row [row]
+ (jq/attr row "data-id"))
+
(defn existing-book?
[row]
- (jq/attr row "data-id"))
+ (book-id-from-row row))
(defn get-edited-book
[row]
{:_id (jq/attr row "data-id")
:author (-> row (jq/find "input[name=author]") jq/val str/trim)
:title (-> row (jq/find "input[name=title]") jq/val str/trim)})
+
+(defn delete-book
+ [row]
+ (let [book-id (jq/attr row "data-id")]
+ (jq/ajax (str "/books/" book-id)
+ {:type "DELETE"
+ :dataType :json
+ :success (fn [book]
+ (jq/remove row))})))
+
+(defn validate-book-with-handler [book handler-fn]
+ (let [error ""
+ error (if-not (-> book :author count pos?)
+ (str error "Book has not author\n")
+ error)
+ error (if-not (-> book :title count pos?)
+ (str error "Book has no title\n")
+ error)]
+ (handler-fn error)
+ (-> error count zero?)))
+
+(defn validate-book [book]
+ (validate-book-with-handler book #(when (-> % count pos?) (js/alert %))))
+
+(defn review-book
+ "FIXME: Implement me!"
+ [row])
+
+(defn delete-book [row]
+ (let [book-id (book-id-from-row row)]
+ (jq/ajax (str "/books/" book-id)
+ {:type "DELETE"
+ :dataType :json
+ :success (fn [book]
+ (jq/remove row))})))
+
+(declare append-book)
+(declare append-book-admin)
+
+(defn perform-save
+ [book row]
+ (jq/ajax "/books"
+ {:type "PUT"
+ :dataType :json
+ :success (fn [book]
+ (jq/empty row)
+ (append-book row book))}))
+
+(defn perform-update
+ [row]
+ (jq/ajax "/books"
+ {:type "PUT"
+ :dataType :json
+ :success (fn [book]
+ (jq/empty row)
+ (append-book row book))}))
+
+(defn update-book [row]
+ (let [book (get-edited-book row)]
+ (if (validate-book book)
+ (-> row (jq/find "input") (jq/attr "disabled" true))
+ (-> row (jq/find "button") (jq/attr "disabled" true))
+ (if (existing-book? row)
+ (perform-update book row)
+ (perform-save book row)))))
+
+(defn cancel-edit [row]
+ (if (existing-book? row)
+ (let [book {:_id (jq/attr row "data-id")
+ :author (jq/attr row "data-author")
+ :title (jq/attr row "data-title")}]
+ (jq/empty row)
+ (append-book-admin row book))))
+
+(defn edit-book [row]
+ (jq/empty row)
+ (if (existing-book? row)
+ (-> row
+ (jq/append (str "<td>" (jq/attr row "data-id") "</td>"))
+ (jq/append (str "<td><input type=\"text\" name=\"author\" value=\""
+ (jq/attr row "data-author") "\" /></td>"))
+ (jq/append (str "<td><input type=\"text\" name=\"title\" value=\"" (jq/attr row "data-title") "\" /></td>")))
+ (-> row
+ (jq/append "<td></td>")
+ (jq/append "<td><input type=\"text\" name=\"author\" placeholder=\"Author\" /></td>")
+ (jq/append "<td><input type=\"text\" name=\"title\" placeholder=\"Title\" /></td>")))
+ (let [save-button (create-button :button-class "btn btn-primary"
+ :caption "Save"
+ :icon "icon-ok icon-white"
+ :callback (fn [] (update-book row)))
+ cancel-button (create-button :button-class "btn"
+ :caption "Cancel"
+ :callback (fn [] (cancel-edit row)))
+ buttons (-> ($ "<td>")
+ (jq/append save-button)
+ (jq/append " ")
+ (jq/append cancel-button))]
+ (-> row (jq/append buttons) (jq/find "input:first") jq/focus)))
+
+(defn new-book-clicked []
+ (let [row ($ "<tr>")]
+ (jq/append ($ ".book-list") row)
+ (edit-book row)))
+
+(defn append-book [row {:keys [_id author title] :as book}]
+ (-> row
+ (jq/attr "data-id" _id)
+ (jq/attr "data-author" author)
+ (jq/attr "data-title" title)
+ (jq/append (str "<td>" _id "</td>"))
+ (jq/append (str "<td>" author "</td>"))
+ (jq/append (str "<td>" title "</td>"))))
+
+(defn append-book-admin [row book]
+ (append-book row book)
+ (let [edit-button (create-button :button-class "btn btn-primary"
+ :icon "icon-edit icon-white"
+ :caption "Edit"
+ :callback (fn [] (edit-book row)))
+ delete-button (create-button :button-class "btn btn-danger"
+ :icon "icon-trash icon-white"
+ :caption "Delete"
+ :callback (fn [] (delete-book row)))
+ review-button (create-button :button-class "btn"
+ :icon "icon-star"
+ :caption "Review"
+ :callback (fn [] (review-book row)))
+ buttons (-> ($ "<td>")
+ (jq/append edit-button)
+ (jq/append " ")
+ (jq/append review-button)
+ (jq/append " ")
+ (jq/append delete-button))]
+ (jq/append row buttons)))
+
View
29 test-cljs/librarian_clojure/test/frontend.cljs
@@ -1,19 +1,20 @@
(ns librarian-clojure.test.frontend
- (:use [jayq.core :only [$]]
- [librarian-clojure.test.test-helpers :only [jqdom jqdom= ok not-ok]])
+ (:use [jayq.core :only [$ remove]]
+ [librarian-clojure.test.test-helpers :only [jqdom jqdom= ok not-ok mockjax
+ mockjax-clear]])
(:require [librarian-clojure.frontend :as frontend]
[jayq.core :as jq])
- (:require-macros [librarian-clojure.test.macro-utils :as m]))
+ (:use-macros [librarian-clojure.test.macro-utils :only [deftest deftest-async]]))
-(m/deftest "(create-button) returns an empty button"
+(deftest "(create-button) returns an empty button"
(js/ok (jqdom= (frontend/create-button) ($ "<button>")), "Returns empty button"))
-(m/deftest "(create-button :button-class :foo) returns button with class foo"
+(deftest "(create-button :button-class :foo) returns button with class foo"
(let [button (jqdom (frontend/create-button :button-class :foo))]
(ok (== button/tagName "BUTTON") "This is a button!")
(ok (== button/className "foo") "Button does have class foo!")))
-(m/deftest "(create-button :button-class :foo :caption \"Foo\" :icon :icon-class)
+(deftest "(create-button :button-class :foo :caption \"Foo\" :icon :icon-class)
returns according button"
(let [jbutton (frontend/create-button :button-class :foo :caption "Foo" :icon :icon-class)
button (jqdom jbutton)]
@@ -22,21 +23,29 @@ returns according button"
(ok (-> jbutton (jq/children "i") first $ (jq/has-class "icon-class")) "There's an icon!")
(ok (->> jbutton jq/text (re-find #"Foo")) "There's a caption!")))
-(m/deftest-async "(create-button :callback ...) test" 1
+(deftest-async "(create-button :callback ...) test" 1
(let [button (frontend/create-button :callback
(fn [] (ok true "Called once!") (js/start)))]
(jq/trigger button :click)))
-(m/deftest "(existing-book? ...)"
+(deftest "(existing-book? ...)"
(ok (frontend/existing-book? (-> "<span>" $ (jq/attr "data-id" "foo")))
"True when has data-id")
(not-ok (frontend/existing-book? ($ "<span>"))
"False when not"))
-(m/deftest "(get-edited-book ...) test"
+(deftest "(get-edited-book ...) test"
(let [row ($ (str "<div data-id=\"foo\"><input name=\"author\" value=\"bar\">"
"<input name=\"title\" value=\"baz\"></div>"))
res (frontend/get-edited-book row)]
(ok (== (:_id res) "foo") "Has :id")
(ok (== (:author res) "bar") "Has :author")
- (ok (== (:title res) "baz")) "Has :title"))
+ (ok (== (:title res) "baz") "Has :title")))
+
+(deftest "(validate-book-with-handler book handler-fn) validation tests"
+ (not-ok (frontend/validate-book-with-handler {} #(ok (pos? (count %)) "Non-empty error."))
+ "Validation fails with an empty book")
+ (not-ok (frontend/validate-book-with-handler {:author "Deso"} #(ok (pos? (count %)) "Non-empty error."))
+ "Validation fails with partially complete book")
+ (ok (frontend/validate-book-with-handler {:author "Deso" :title "Caso"} #(ok (zero? (count %))))
+ "Validation succeeds with complete book"))
View
7 test-cljs/librarian_clojure/test/macro_utils.clj
@@ -5,3 +5,10 @@
(defmacro deftest-async
([name expected & body] `(js/asyncTest ~name ~expected (fn [] ~@body))))
+(defmacro redef-fn [fn-binding fn-val & body]
+ (let [old-binding (-> fn-binding name gensym)]
+ `(try
+ ~(list 'set! fn-binding fn-val)
+ ~@body
+ (finally
+ ~(list 'set! fn-binding old-binding)))))
View
7 test-cljs/librarian_clojure/test/test_helpers.cljs
@@ -1,4 +1,5 @@
(ns librarian-clojure.test.test-helpers
+ (:use [jayq.core :only [$ ->ajax-settings]])
(:require-macros [librarian-clojure.test.macro-utils :as m]))
(defn jqdom [jqnode]
@@ -10,3 +11,9 @@
(def ok js/ok)
(defn not-ok [assertion comment] (js/ok (not assertion) comment))
+
+(defn mockjax [& {:keys [url type dataType responseText] :as settings}]
+ (.mockjax js/jQuery (->ajax-settings settings)))
+
+(defn mockjax-clear []
+ (.mockjaxClear js/jQuery))
Something went wrong with that request. Please try again.